Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Profit Locking, exit for individual instrument in a account, and exit for...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Profit Locking, exit for individual instrument in a account, and exit for...

    Hi there:

    I am testing incorporating 3 things in my exits (and reverse for profit targets)...was wondering if you could help...

    #1 I am wanting to put in the following "profit lock" exits...that is just what I call it:

    ***On a short trade,Looking for if the current bar's price rises above the open of the 3 bars back, then I am looking for it to exit the short position and vice versa on long positions (then, maybe the reverse for target or something similar). Do you see a better way to do this or anything wrong with this?

    Code:
    // TESTING Lock in profits if current trend down changes...
                else if (GetCurrentBid() > Open[3])
                    {
                        ExitShort(); // OFF Need to test and see wicks etc 
                    }
    
                        // TESTING Lock in profits if current trend down changes...
                else if (GetCurrentAsk() < Open[3])
                    {
                        ExitLong();  // OFF Need to test and see wicks etc 
                    }
    #2 I am wanting to include a emergency exit for individual instruments (which happen to be using the same strategies NOT account wide just one instrument) in case a stop loss is skipped, removed, canceled, or otherwise for some reason I have a naked position (I could then do the reverse to add a total profit target to exit?):

    Code:
    // TESTING Unrealized PnL Emergency Exit Long or Short for individual instruments (not account wide) in case stop loss gets skipped, removed, or missed.
                else if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) < -300)
                    {
                        ExitLong("UnP Emr Exit Long", "");
                        ExitShort("UnP Emr Exit Short", "");
                        Disable(); // OFF 
                    }
    #3 On this one, I don't have code yet, but looking to see if you could show me an idea...What I would like to do is to have it look at Total Unrealized profit/loss ACCOUNT WIDE and Total Realized profit/loss ACCOUNT WIDE and the difference real time current Total profit/loss account-wide. I would like it to monitor and then if the total goes below "x" amount, say, -$500 then exit ALL positions in this particular account and also disable ALL strategies associated with this particular account (then, would like to do the revers of it to add a total profit target to exit and disable as well)?
    Last edited by birdog; 02-01-2013, 08:34 AM.

    #2
    Hello Greg,
    Thanks for your note.

    For 1 & 2 I will also suggest to check for the current market position.
    Code:
    if (Position.MarketPosition == MarketPosition.Long && GetCurrentBid() > Open[3])
    {
       ExitShort(); 
    }
    3. Unfortunately there are no supported codes to do it. I will however leave this thread open for your forum members who can provide their valuable inputs.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      If anyone has any ideas, you are welcome to post...

      Comment


        #4
        Joy:

        What would be the difference in using:

        && Position.MarketPosition == MarketPosition.Long

        and

        && Position.MarketPosition != MarketPosition.Long

        "==" vs "!=" here?

        Comment


          #5
          Originally posted by birdog View Post
          What would be the difference in using:

          && Position.MarketPosition == MarketPosition.Long

          and

          && Position.MarketPosition != MarketPosition.Long

          "==" vs "!=" here?
          != is a comparison check that checks to see if they are Not Equal
          == is a comparison check that checks to see if they are Equal

          Let me know if I can be of further assistance.
          LanceNinjaTrader Customer Service

          Comment


            #6
            Exit Positions on a Long or Short

            Hi Anyone:

            ***Still can not quite get it to function on Market Replay or Live.

            I have used Market Replay to test this to exit out of a short or long position using both "==" and "!=" below. Here is what I am trying to do: If it is in a Short position, it should exit the Short if the current Bid is greater than the Open of the bar that is 3 bars back. If it is in a Long position, it should exit the Long if the current Ask is less than the Open of the bar that is 3 bars back.

            Am I missing a piece of the puzzle here?
            Should I use "!=" or "=="?
            Is using "Bid" or "Ask" a issue? If so, should I just use the last price? If so, what command rather than "Bid" or "Ask" should I use?
            If there is a better method, I would appreciate input (I have so far tried testing it with "!=" and "=" in the below example but when price reaches the point where it is suppose to exit, it does not exit.

            else if (Position.MarketPosition == MarketPosition.Long
            and
            else if (Position.MarketPosition != MarketPosition.Long

            Example using "==" trying to get out of a long and short position:

            Code:
                        else if (Position.MarketPosition == MarketPosition.Long 
                                && (GetCurrentBid() > Open[3]))
                            {
                                ExitShort(); 
                            }
            
                                
                        else if (Position.MarketPosition == MarketPosition.Short 
                                && (GetCurrentAsk() < Open[3]))
                            {
                                ExitLong();
            Thanks a bunch folks!



            Greg
            Last edited by birdog; 02-03-2013, 11:19 AM.

            Comment


              #7
              Open the output window. Tools->Output Window

              Add these lines before your if statement:

              Print ("Ask=" + GetCurrentAsk() + " Bid=" + GetCurrentBid() + " Open[3]=" + Open[3] );


              Originally posted by birdog View Post
              Hi Anyone:

              ***Still can not quite get it to function on Market Replay or Live.

              I have used Market Replay to test this to exit out of a short or long position using both "==" and "!=" below. Here is what I am trying to do: If it is in a Short position, it should exit the Short if the current Bid is greater than the Open of the bar that is 3 bars back. If it is in a Long position, it should exit the Long if the current Ask is less than the Open of the bar that is 3 bars back.

              Am I missing a piece of the puzzle here?
              Should I use "!=" or "=="?
              Is using "Bid" or "Ask" a issue? If so, should I just use the last price? If so, what command rather than "Bid" or "Ask" should I use?
              If there is a better method, I would appreciate input (I have so far tried testing it with "!=" and "=" in the below example but when price reaches the point where it is suppose to exit, it does not exit.

              else if (Position.MarketPosition == MarketPosition.Long
              and
              else if (Position.MarketPosition != MarketPosition.Long

              Example using "==" trying to get out of a long and short position:

              Code:
                          else if (Position.MarketPosition == MarketPosition.Long 
                                  && (GetCurrentBid() > Open[3]))
                              {
                                  ExitShort(); 
                              }
              
                                  
                          else if (Position.MarketPosition == MarketPosition.Short 
                                  && (GetCurrentAsk() < Open[3]))
                              {
                                  ExitLong();
              Thanks a bunch folks!



              Greg

              Comment


                #8
                Lance: I will try it.

                What would I use for just getting the "Last" price instead of the bid or ask instead?

                Would it just be GetCurrent? just to get the last traded price? If I did this would I have to Print? Trying to save on system resources as much as possible if it...but, am trying what you said now...just what to accomplish it with the simplest code possible...smile.

                Comment


                  #9
                  Hello,

                  Close[0] will get the last traded price


                  Printing will allow you to debug your code and see if there are errors in the values you were expecting.

                  Please let me know if I can assist further.
                  LanceNinjaTrader Customer Service

                  Comment


                    #10
                    Lance:

                    Ok, so, Close[0] is not the close of the bar, but the actual real time last traded price (or both)?

                    So, will adding the print actually potentially make it work with the Bid and Ask or is it just used for diagnosing (or both)?

                    Comment


                      #11
                      Originally posted by birdog View Post
                      Lance:

                      Ok, so, Close[0] is not the close of the bar, but the actual real time last traded price (or both)?
                      It is both.

                      In backtesting this will always be the Close of the bar because COBC always = true (which is also that bar's last traded price)

                      In real-time trading if COBC = false then it will be the last traded price updated each tick

                      So, will adding the print actually potentially make it work with the Bid and Ask or is it just used for diagnosing (or both)?
                      Just for diagnosing
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        Lance, Ok...sounds good...BUT when I use Market Replay it should behave just like real time live trading right? I am not doing a traditional backtesting...Market Replay only to test this?

                        Also, I am doing a Market Replay and the Ask, Bid, and Open etc print perfectly in the Output Window.

                        Why, do you think, it will not exit the long and short positions?

                        ...in the meantime, I will test it with just using Close[0] as well and report back to ya on that...smile...thanks for your help Lance...Greg

                        Originally posted by NinjaTrader_Lance View Post
                        It is both.

                        In backtesting this will always be the Close of the bar because COBC always = true (which is also that bar's last traded price)

                        In real-time trading if COBC = false then it will be the last traded price updated each tick



                        Just for diagnosing

                        Comment


                          #13
                          Originally posted by birdog View Post
                          Lance, Ok...sounds good...BUT when I use Market Replay it should behave just like real time live trading right? I am not doing a traditional backtesting...Market Replay only to test this?

                          Also, I am doing a Market Replay and the Ask, Bid, and Open etc print perfectly in the Output Window.

                          Why, do you think, it will not exit the long and short positions?

                          ...in the meantime, I will test it with just using Close[0] as well and report back to ya on that...smile...thanks for your help Lance...Greg
                          Let's see if you are even getting in there:


                          Code:
                          else if (Position.MarketPosition == MarketPosition.Long 
                                              && (GetCurrentBid() > Open[3]))
                                          {
                          [I]                   Print ("Exiting Short");
                          [/I]                    ExitShort(); 
                                          }
                          
                                              
                                      else if (Position.MarketPosition == MarketPosition.Short 
                                              && (GetCurrentAsk() < Open[3]))
                                          {
                          [I]                   Print ("Exiting Long");
                          [/I]                    ExitLong();
                                          }

                          Comment


                            #14
                            Sledge...thanks for helping out...!

                            Ok...should it be "!=" instead of "==" where "!=" is saying effectively if the current market position is NOT Long then exit Short? I was thinking "==" would be saying if the current market position IS Long then it would be Exit Long (as opposed to Exit Short)? Just making sure I am thinking right on it...so, should it be "!=" instead? Or, is "==" what we need just as you have it?

                            Originally posted by sledge View Post
                            Let's see if you are even getting in there:


                            Code:
                            else if (Position.MarketPosition == MarketPosition.Long 
                                                && (GetCurrentBid() > Open[3]))
                                            {
                            [I]                   Print ("Exiting Short");
                            [/I]                    ExitShort(); 
                                            }
                            
                                                
                                        else if (Position.MarketPosition == MarketPosition.Short 
                                                && (GetCurrentAsk() < Open[3]))
                                            {
                            [I]                   Print ("Exiting Long");
                            [/I]                    ExitLong();
                                            }

                            Comment


                              #15
                              Originally posted by birdog View Post
                              Sledge...thanks for helping out...!

                              Ok...should it be "!=" instead of "==" where "!=" is saying effectively if the current market position is NOT Long then exit Short? I was thinking "==" would be saying if the current market position IS Long then it would be Exit Long (as opposed to Exit Short)? Just making sure I am thinking right on it...so, should it be "!=" instead? Or, is "==" what we need just as you have it?
                              OK - I think my caffeine is kicking in.

                              You are right. It makes no sense to issue an ExitShort when your Position is Long??



                              This might work better for you:



                              Code:
                              else if (Position.MarketPosition == MarketPosition.Long 
                                                  && (GetCurrentBid() > Open[3]))
                                              {
                                                 Print ("Exiting Long");
                                                  ExitLong(); 
                                              }
                              
                                                  
                                          else if (Position.MarketPosition == MarketPosition.Short 
                                                  && (GetCurrentAsk() < Open[3]))
                                              {
                                                 Print ("Exiting Short");
                                                  ExitShort();
                                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              59 views
                              0 likes
                              Last Post DynamicTest  
                              Started by ScottWalsh, Today, 06:52 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by olisav57, Today, 07:39 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post olisav57  
                              Started by trilliantrader, Today, 03:01 PM
                              2 responses
                              21 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X