Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Auto-breakeven in strategy
Collapse
X
-
Originally posted by pdawg View PostBrian 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.
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 (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 } }
The output window shows:Code:if (Close[0] >= Position.AvgPrice + breakeven * TickSize) { Print("Setting Stoploss to " + Position.AvgPrice); SetStopLoss(CalculationMode.Price, Position.AvgPrice);
I attached my SuperDOM showing that the stop was not reset even through the price is lower than the entry price - breakeven (2).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
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?Last edited by cassb; 05-08-2008, 08:40 AM.
Comment
-
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
-
OK, I figured it out finally. Want to know what it was?
In the Initialize() tag, you set the default Target and Stoploss:
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:protected override void Initialize() { SetProfitTarget("", CalculationMode.Ticks, Target); SetStopLoss("", CalculationMode.Ticks, Stoploss, false); }
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!Code:SetStopLoss(CalculationMode.Price, Position.AvgPrice);
Last edited by cassb; 05-08-2008, 06:42 PM.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
666 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
377 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
110 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
575 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
580 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment