Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto-breakeven in strategy

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

    #16
    Thanks pdawg!
    RayNinjaTrader Customer Service

    Comment


      #17
      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.

      What am I doing wrong here? It's still not working and I cannot see why it wouldn't. Pdawg, you and I seem to write the same coding style... mine was almost exactly like your example. And yours is working OK? Hm, I'm not sure what's wrong with mine then.

      Code:
                  if (Position.MarketPosition == MarketPosition.Flat && resetStoploss)
                  {
                      SetStopLoss(CalculationMode.Ticks, Stoploss);
                      resetStoploss = false;       // Don't reset the stoploss value again until another position changes it
                  }
                  // If a long position is open, allow for stop loss modification to breakeven
                  else if (Position.MarketPosition == MarketPosition.Long)
                  {
                      // Once the price is greater than entry price+breakeven ticks, set stop loss to breakeven
                      if (Close[0] >= Position.AvgPrice + breakeven * TickSize)                {
                          Print("Setting Stoploss to " + Position.AvgPrice);
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          resetStoploss = true;       // Reset the stoploss value once this position is closed
                      }
                  }
                  // If a short position is open, allow for stop loss modification to breakeven
                  else if (Position.MarketPosition == MarketPosition.Short)
                  {
                      // Once the price is less than entry price-breakeven ticks, set stop loss to breakeven
                      if (Close[0] <= Position.AvgPrice - breakeven * TickSize)
                      {
                          Print("Setting Stoploss to " + Position.AvgPrice);
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          resetStoploss = true;       // Reset the stoploss value once this position is closed
                      }
                  }
      This statement just executes over and over and over, but the stop order never changes to the breakeven price as I watch the trade in the SuperDOM:

      Code:
                      if (Close[0] >= Position.AvgPrice + breakeven * TickSize)                {
                          Print("Setting Stoploss to " + Position.AvgPrice);
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      The output window shows:

      Code:
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:26 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:27 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:27 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:27 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715.1, PositionAvg = 715.3, breakeven = 715.1
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:27 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:27 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:28 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:28 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      Last price = 715, PositionAvg = 715.3, breakeven = 715.1
      Setting Stoploss to 715.3
      5/8/2008 10:02:28 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=715.3 Currency=0 Simulated=False
      I attached my SuperDOM showing that the stop was not reset even through the price is lower than the entry price - breakeven (2).


      Once thing I noticed that's different between pdawg and my strategy is that I have CalculateOnBarClose = false. So, does the SetStopLoss() not work unless I have this set to true?
      Attached Files
      Last edited by cassb; 05-08-2008, 08:40 AM.

      Comment


        #18
        hehe... sorry guys for maybe overloading you with information. Or maybe I stumped the panel.

        I think I'll set up a simple strategy (or just use pdawg's) and run it against a Market Replay. If it still fails to reset the stoploss, would you like me to send the strategy in for you to examine?

        Comment


          #19
          No since its not our strategy. Use the reference sample I provided in an earlier post. If this does not work as expected then we would look into it.
          RayNinjaTrader Customer Service

          Comment


            #20
            OK, I figured it out finally. Want to know what it was?

            In the Initialize() tag, you set the default Target and Stoploss:

            Code:
                    protected override void Initialize()
                    {                
                        SetProfitTarget("", CalculationMode.Ticks, Target);
                        SetStopLoss("", CalculationMode.Ticks, Stoploss, false);
                     }
            But, I guess the amended SetStopLoss call from within OnBarUpdate() has to be the same overloaded call as what's in Initialize(). Well, I had copied the sample code into my strategy but did not change my Initialize() tag to match the OnBarUpdate() call:

            Code:
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice);
            I changed my Initialize() tag and voila, now it works. It was driving me nuts because it looked like the code was trying to change the stoploss... but it wasn't changing. Sheesh, I've expended too many brain cells on this strategy... it better make me some money, darn it!
            Last edited by cassb; 05-08-2008, 06:42 PM.

            Comment


              #21
              LOL- I can sympathize, I've spent hours debugging code only to find something small I overlooked being the culprit. That's the thing about code, it just does what it's told!

              Comment


                #22
                I wish my kids obeyed like my code does! lol

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                672 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                379 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                111 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                577 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                582 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X