NexusFi: Find Your Edge


Home Menu

 





Trailing stops and backtesting


Discussion in NinjaTrader

Updated
    1. trending_up 3,301 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
ValutaTrader's Avatar
 ValutaTrader 
Oslo,Norway
 
Experience: Intermediate
Platform: NinjaTrader
Trading: EUR/USD
Posts: 66 since Apr 2011
Thanks Given: 4
Thanks Received: 52

I am calling my trailing stop function on each bar. It only moves the stop level if my logic tells it to (i.e. a minimum profit level has been reached). My issue with NinjaTrader on this is that on back test the trailing stops do not seem to kick in. Any suggestions on whether this is by design or something I can change to make my logic work in backtest mode as well?

protected override void OnMarketData(MarketDataEventArgs e){
if (e.MarketDataType == MarketDataType.Last){
lastPrice = e.Price;
}
}


public void adjustStopLoss(){

if ( stopOrder != null )
CancelOrder(stopOrder);

double pointsProfit=Position.GetProfitLoss(lastPrice, PerformanceUnit.Points);
double tickProfit=pointsProfit/TickSize;

if ((tickProfit-TicksMinProfit)<=0)
return; // No need to set trailing stop if level not triggered. Default stop from order entry still remains.

if (Position.MarketPosition == MarketPosition.Short){
double stop = lastPrice+ (TicksTrailingSL*TickSize);
stopOrder=SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, DefaultQuantity, stop, stop, "Exit", "ShortTrailStop");
}
if (Position.MarketPosition == MarketPosition.Long){
double stop = lastPrice - (TicksTrailingSL*TickSize);
stopOrder=SubmitOrder(0, OrderAction.Sell, OrderType.Stop, DefaultQuantity, stop, stop, "Exit", "LongTrailStop");
}
}


Follow me on X Started this thread Reply With Quote

 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #3 (permalink)
 
ValutaTrader's Avatar
 ValutaTrader 
Oslo,Norway
 
Experience: Intermediate
Platform: NinjaTrader
Trading: EUR/USD
Posts: 66 since Apr 2011
Thanks Given: 4
Thanks Received: 52


I may have figured this out myself. It seems to be the event

protected override void OnMarketData(MarketDataEventArgs e){
if (e.MarketDataType == MarketDataType.Last){
lastPrice = e.Price;
}
}
that is not triggered in back test, so lastPrice was not set. The strange thing was that for sell orders it seemed to work. Using latest Close does give the expected result however.


Follow me on X Started this thread Reply With Quote
Thanked by:
  #4 (permalink)
 Laserdan 
North Carolina
 
Experience: Beginner
Platform: Ninja Trader
Broker: Ninja Trader Broker / Continuum
Trading: ES
Posts: 57 since Jun 2009
Thanks Given: 4
Thanks Received: 57

You are correct. The OnMarketData event does not fire in backtest, only on live data.


Laserdan
Follow me on X Reply With Quote




Last Updated on February 29, 2012


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts