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

How to move a StopLoss to BreakEven?

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

    How to move a StopLoss to BreakEven?

    Hi,

    When I go Long/Short in a strategy, I open 2 positions, both of them with the same StopLoss, but position 1 have ProfitTarget1 and position 2 have ProfitTarget2.
    What I need to do is, When ProfitTarget1 is achieved change the stoploss of the position 2 to BreakEven.
    Can you help me please?

    #2
    Hello ervin,

    Thanks for your post and welcome to the NinjaTrader forum!

    Can you clarify if you are using the strategy builder or coding directly in Ninjascript?

    Can you clarify what means/methods you are using for your stoploss? (IE: SetStopLoss())

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi,
      I'm coding direcly in ninjascript.
      For my stop loss I'm using SetStopLoss, but if it is necesary I can change if you say me how.

      Comment


        #4
        Use a boolean variable called shiftstop and an int or double variable called breakeven, depending on if your stop is tick or price based.

        default shiftstop = false

        if shiftstop is false, stop loss is per your strategy

        if (conditions) are true, set shiftstop = true, and set stop loss to breakeven

        if position flattens, set shiftstop = false

        you will have to use the entry string names to designate which stop loss to move, in your case.

        Comment


          #5
          This logic I must to implement in OnBarUpdate rigth?
          Is there some event like "OnTargetReachedExecuted" (just for examole) or something like that where I can put the code to set stoploss to breakeven? If exist I just put the code there.

          Comment


            #6
            Also I have to save the price to put the breakeven into a vatiable rigth? Where is the price of the open position?

            Comment


              #7
              This logic I must to implement in OnBarUpdate rigth?
              Is there some event like "OnTargetReachedExecuted" (just for examole) or something like that where I can put the code to set stoploss to breakeven? If exist I just put the code there.
              The simplest way is probably put it in OnBarUpdate and use the same exit conditions for Position 1, in your case.

              Originally posted by ervin View Post
              Also I have to save the price to put the breakeven into a vatiable rigth? Where is the price of the open position?
              Position.AveragePrice

              Comment


                #8
                Hello ervin,

                Thanks for your reply.

                In the OnBarUpdate(), once your first order is closed, you can simply change the SetStopLoss() to the breakeven price. Please note that it is critical that you reset the SetStopLoss() to your original values before the next entry is made as the method will retain the last price used.

                You can use some script logic to set a bool when your Position.Quantity equals two. Then when the bool is true and Position.Quantity now is equal one to then set the stop at the breakeven value. You would also need to reset the bool when flat.

                Reference: https://ninjatrader.com/support/help...n_quantity.htm
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you, I'll try this later.
                  Sorry but let me insist, there are or there are not an event like "target achieved" or "on position close" or something like that?

                  Comment


                    #10
                    Originally posted by ervin View Post
                    Thank you, I'll try this later.
                    Sorry but let me insist, there are or there are not an event like "target achieved" or "on position close" or something like that?
                    No there are not.

                    Comment


                      #11
                      Hello ervin,

                      Thanks for your reply.

                      As member liquid150 advised there are no such methods. There are more advanced means of observing and controlling orders. I will link a couple of examples below:




                      Here is a link to all of the strategy example scripts: https://ninjatrader.com/support/help...?strategy2.htm
                      Paul H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by judysamnt7, 03-13-2023, 09:11 AM
                      4 responses
                      57 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
                      19 views
                      0 likes
                      Last Post helpwanted  
                      Started by cre8able, Today, 07:24 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post cre8able  
                      Working...
                      X