NexusFi: Find Your Edge


Home Menu

 





Trailing stops and backtesting


Discussion in NinjaTrader

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




 
Search this Thread

Trailing stops and backtesting

  #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 Twitter Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
How to apply profiles
Traders Hideout
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
  #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 Twitter 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 Twitter Reply With Quote




Last Updated on February 29, 2012


© 2024 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 - Privacy Policy - Downloads - Top
no new posts