![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Dec 2007
Posts: 365
Thanks: 36
Thanked 28 times in 24 posts
|
I'm trying to understand why a stop loss order (set via SetStopLoss) is getting reset twice on the same bar (I'm printing times out to the output.txt file). Strategy is calc on bar close. The only place in which the stop is modified is in this block of code:
Code:
if(Position.MarketPosition == MarketPosition.Short)
{
if (Position.MarketPosition == MarketPosition.Short && Low[0] <= theoEntry - oneDone*TickSize && stopControl)
{
Print("stop updated at One & Done tick increment in the money"+ Time[0].ToString());
stopPrice = theoEntry + entryOffset*TickSize;
SetStopLoss(CalculationMode.Price,stopPrice);// one & done stop move
stopControl = false; //this can only be reset to true if flat at end of bar on primary series
}
// MomDot stop loss adjustment
if (Position.MarketPosition == MarketPosition.Short && Close[0] > rhMomDot().Plot0[0] && High[0] + entryOffset*TickSize < stopPrice && Close[0] > Open[0])
{
Print("stop updated at momdot"+ Time[0].ToString());
SetStopLoss(CalculationMode.Price,High[0] + entryOffset*TickSize);//momdot stop move
//stopPrice = High[0] + entryOffset*TickSize;
}
else
{
SetStopLoss(CalculationMode.Price,stopPrice);//reset stop loss price to One&Done or Initial Stop
Print("stop RESET to stopPrice after momdot exit not elected"+ Time[0].ToString());
}
}
I'm totally baffled. |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
|
coolmoss, the strategy runs on historical and realtime data, correct?
Do you reset the stoploss then to a tick default setting for example when you're flat with the strategy? This would be needed as otherwise an historically used / set stop value could be reused for a subsequent entry.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Dec 2007
Posts: 365
Thanks: 36
Thanked 28 times in 24 posts
|
I do reset stopPrice to a new value when flat AND a new entry signal appears. And that works okay.
What is happening in this instance, is I'm changing the stop loss (via SetStopLoss) to a new value, and output.txt shows that new value in amended order. But then, on the same bar, with the position unaltered since beginning of bar, and calc'd on bar close, the stop gets changed again. It looks like this: stop updated at momdot3/27/2012 9:14:55 AM 3/27/2012 9:14:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.28 Currency=0 Simulated=False 3/27/2012 9:14:55 AM Amended stop order: Order='NT-00177/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00116-746' Filled=0 Fill price=0 Token='a7b113f0dc2946b280b9f1e7facdca85' Gtd='12/1/2099 12:00:00 AM' 3/27/2012 9:14:55 AM Amended stop order: Order='NT-00179/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00117-746' Filled=0 Fill price=0 Token='1bc5252517c04ccdad658c4fb2a0bb8e' Gtd='12/1/2099 12:00:00 AM' 3/27/2012 9:14:55 AM Amended stop order: Order='NT-00181/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00118-746' Filled=0 Fill price=0 Token='a4f4756b46784ab295915bc7ea69acc2' Gtd='12/1/2099 12:00:00 AM' 3/27/2012 9:14:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.34 Currency=0 Simulated=False 3/27/2012 9:14:55 AM Amended stop order: Order='NT-00177/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00116-746' Filled=0 Fill price=0 Token='a7b113f0dc2946b280b9f1e7facdca85' Gtd='12/1/2099 12:00:00 AM' 3/27/2012 9:14:55 AM Amended stop order: Order='NT-00179/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00117-746' Filled=0 Fill price=0 Token='1bc5252517c04ccdad658c4fb2a0bb8e' Gtd='12/1/2099 12:00:00 AM' 3/27/2012 9:14:55 AM Amended stop order: Order='NT-00181/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00118-746' Filled=0 Fill price=0 Token='a4f4756b46784ab295915bc7ea69acc2' Gtd='12/1/2099 12:00:00 AM' 3/27/2012 9:16:23 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.34 Currency=0 Simulated=False You can see at first the price is changed to 22.28, then in the same bar it's changed back to 22.34 (what it had just been). No where else in the code is there is SetStopLoss statement except where a condition exists that position is flat. This is only running on historical data, I'm not using this strategy for real time. |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
|
coolmoss, looks like you have 2 conditions for the bar update that would be called sequentially allowing for a SetStopLoss modification - the first one leasing to 22.28 as stoploss and second one to the 22.34.
I would suggest debugging your code to ensure only the intended condition is hitting and allowing the stop loss to be moved, this could be achieved for example via bool flags to streamline the code / flow control as you desire.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Dec 2007
Posts: 365
Thanks: 36
Thanked 28 times in 24 posts
|
Bertrand,
I thought that's what I was doing with the Print statements. If you note the section with an IF and an Else, they both have Print statements, and in the output.txt window, only one of those statements prints for each bar. I thought that meant only one or the other of the IF/Else was executing. I have Print statements at every location in the code where I have a SetStopLoss, yet not one of those Print statements executes at the portion of the output.txt window where the stop loss is getting reset incorrectly. I guess I would have this question then: if the stop loss is not being reset by one of my SetStopLoss, what else could be resetting it? Thanks much for your assistance. |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
|
coolmoss, I would recommend you remove code sections until you experience first of all a state that aligns with your expectations when running the script, only then include more complex logic. The stop loss would only be moved again if you call this in your OnBarUpdate() code, there's no way around. In the print you set please also incluce a timestamp and CurrentBar, so you can easily align the prints triggered with the corresponding trades.
Bertrand
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Accessing SetStop value | coolmoss | General Programming | 1 | 03-09-2012 05:17 AM |
| Resetting Defaults | kenb2004 | Strategy Development | 3 | 01-05-2011 11:15 AM |
| Resetting Variables | richp6 | Strategy Development | 4 | 12-30-2010 09:10 AM |
| Resetting Sim101 | cgeorgan | Version 7 Beta General Questions & Bug Reports | 1 | 08-11-2010 09:10 AM |
| Resetting a variable | trader413 | Strategy Development | 8 | 09-14-2009 10:12 AM |