Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto-breakeven in strategy

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

    Auto-breakeven in strategy

    Is there a way to use SetStopLoss() in a strategy to set a default stoploss in the Initialize() tag and then reset that stoploss later in OnBarUpdate() to breakeven at some particular profit level? I tried it and it didn't work -- it just immediately set a breakeven stoploss as soon as the order was filled. I'm looking for an example of it working somewhere. I don't want to manage my profit target and stoploss manually in the OnOrderUpdate() tag.

    Thanks!
    Bryan

    #2
    Sure, just call the method with the new price.

    Here is a reference sample - http://www.ninjatrader-support.com/v...ead.php?t=3222
    RayNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Ray View Post
      Sure, just call the method with the new price.

      Here is a reference sample - http://www.ninjatrader-support.com/v...ead.php?t=3222
      I already tried that in OnBarUpdate() like this:

      if (Position.MarketPosition == MarketPosition.Long && GetCurrentBid() >= (Position.AvgPrice + (TickSize * breakeven)))
      {
      SetStopLoss("MyEntry", CalculationMode.Ticks, 0, false)
      }

      It just immediately set a breakeven stoploss as soon as the order was filled.

      Comment


        #4
        Please review the reference sample again and pay attention to the first SetStopLoss() call in OnBarUpdate() which is a reset step.
        RayNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ray View Post
          Please review the reference sample again and pay attention to the first SetStopLoss() call in OnBarUpdate() which is a reset step.
          D'oh. Oh yeah... forgot about the reset step, thank you.

          Comment


            #6
            Question about that sample code...

            // Once the price is greater than entry price+50 ticks, set stop loss to breakeven

            So once you do reset the stoploss to breakeven, how do you prevent it from (re)setting the stoploss to breakeven on every OnBarUpdate() call? Seems like that might slow down the processing. I guess I could set a local variable to signal when the breakeven was set and check that, if that's the only way.

            Also, a seconary question... if I want to set my breakeven using the last price instead of Close[0], what is the method for that? I can't seem to find it -- I found GetCurrentAsk() and GetCurrentBid(), but not GetLastPrice() (or something like that).

            Thanks, Ray!

            Comment


              #7
              NT is smart enough to ignore method calls that don't change anything.

              Close[0] is last price.
              RayNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Ray View Post
                NT is smart enough to ignore method calls that don't change anything.

                Close[0] is last price.
                Good job on the core code then! :-)

                And yes... I should have guessed that Close[0] will always be the latest price for a bar. Thanks!

                Comment


                  #9
                  I'm sorry, but it still doesn't seem to work. I copied the code from the sample and modified it a bit. This is what I'm trying to execute:

                  Code:
                              else if (Position.MarketPosition == MarketPosition.Long)
                              {
                                  double breakevenprice = (Position.AvgPrice + (breakeven * TickSize));
                                  Print("Last price = " + Close[0] + ", PositionAvg = " + Position.AvgPrice + ", breakeven = " + breakevenprice);
                                  // Once the price is greater than entry price+breakeven ticks, set stop loss to entry price
                                  if (Close[0] > Position.AvgPrice)
                                  {
                                      Print("Setting Stoploss to " + Position.AvgPrice);
                                      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                  }
                              }
                  And this is what I get in the Output window when it runs, and it never does actually change the stoploss price in the SuperDOM.

                  Last price = 731.7, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6
                  Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
                  Setting Stoploss to 731.6

                  Comment


                    #10
                    Sorry, you will have to debug your code to figure out what is happening.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Ray View Post
                      Sorry, you will have to debug your code to figure out what is happening.
                      I'm trying to... but when the SetStopLoss() call executes, it's not changing the stoploss for the working order. Here, I turned on TraceOrder and also put a line in OnOrderUpdate() to Print(order.ToString()) as well. It looks like it's calling your internal SetStopTarget() method over and over, but the actual stop order is not being changed to 731.5, nor is OnOrderUpdate() being called. Is this a bug in the core code?

                      [...]
                      Last price = 731.6, PositionAvg = 731.5, breakeven = 731.7
                      Last price = 731.6, PositionAvg = 731.5, breakeven = 731.7
                      Last price = 731.7, PositionAvg = 731.5, breakeven = 731.7
                      Setting Stoploss to 731.5
                      5/7/2008 9:50:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=731.5 Currency=0 Simulated=False
                      Last price = 731.7, PositionAvg = 731.5, breakeven = 731.7
                      Setting Stoploss to 731.5
                      5/7/2008 9:50:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=731.5 Currency=0 Simulated=False
                      Last price = 731.7, PositionAvg = 731.5, breakeven = 731.7
                      Setting Stoploss to 731.5
                      5/7/2008 9:50:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=731.5 Currency=0 Simulated=False

                      Comment


                        #12
                        Not sure what you are seeing. How are you determining that your stop loss is not set to breakeven? If you run this you should be able to watch the order in the Control Center and watch it move up to breakeven.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Brian this code works, just put in your strategy code in the appropriate spots, or run it as is to see it work.

                          Also I have coded a 3 step trail stop strategy if you want something that allows more complex stop movement strategies, like what ATM can provide. Hope it helps.
                          Attached Files

                          Comment


                            #14
                            Originally posted by Josh View Post
                            Not sure what you are seeing. How are you determining that your stop loss is not set to breakeven? If you run this you should be able to watch the order in the Control Center and watch it move up to breakeven.
                            Hi Josh. I do watch the order in SuperDOM and the original stoploss at 10 ticks stays set... it does not move to breakeven when SetStopLoss() is called. I can't debug the core NT code, so I'm not sure how to proceed to figure this out. I guess what I could do is create a simple strategy with just this method and see if I can reproduce it for you and send you the strategy code.
                            Last edited by cassb; 05-08-2008, 07:33 AM.

                            Comment


                              #15
                              Originally posted by pdawg View Post
                              Brian this code works, just put in your strategy code in the appropriate spots, or run it as is to see it work.

                              Also I have coded a 3 step trail stop strategy if you want something that allows more complex stop movement strategies, like what ATM can provide. Hope it helps.
                              Hey, thanks pdawg! That's kind of you. I'll load it and take a look.

                              Bryan

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, Yesterday, 10:06 AM
                              0 responses
                              17 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              16 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              14 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              36 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X