NinjaTrader strategy: backtest vs live

462 views
Skip to first unread message

Mark Griffin

unread,
Dec 17, 2014, 7:23:05 PM12/17/14
to adaptrad...@googlegroups.com
Hi Mike,

I created a NinjaTrader strategy with Adaptrade Builder version 1.7.2.1.

When running live, I found that it would exit very quickly and didn't match the backtest results.

After further investigation by debugging the strategy in Visual Studio, I found a bug in the Ninjascript code generated by AB.

It relates to the "BTAdjust" variable. Initially it is set to 1. If the chart runs on historical data, it is set to 0.

This is ok except that a strategy is usually added to a chart initially containing only historical data. So BTAdjust is set to 0 and when realtime data is encountered it stays on 0. It should change to 1.

In order to overcome this, I added the following code to the OnBarUpdate()

                // Set adjustment for bar references for historical versus real-time
                if (Historical)
                    BTAdjust = 0;
                else
                    BTAdjust = 1;


Live results then more closely matched backtest results.

I suggest you correct the Ninjascript code generated by AB similar to my fix (you are of course welcome to use my code).

Regards, Mark

 

Mark Griffin

unread,
Dec 23, 2014, 12:29:44 AM12/23/14
to adaptrad...@googlegroups.com
Hi Mike,

Another unusual thing I found in the NinjaTrader strategy created with Adaptrade Builder version 1.7.2.1 was that it was structured to open new trades before closing existing ones.

It's usually best to close existing trades before opening new ones.

I suggest you change the strategy structure so that the position exit logic is before the open trade logic. (i.e. put the open trade logic at the end of the OnBarUpdate() method).

Regards, Mark

Mark Griffin

unread,
Jan 12, 2015, 8:44:15 PM1/12/15
to adaptrad...@googlegroups.com
Hi Mike,

Another thing I found in the NinjaTrader strategy created with Adaptrade Builder version 1.7.2.1 was that if the "Exit End-of-Day" option is not considered/included, then the strategy would still exit at the end of the day as this is the default behaviour in Ninjatrader 7.

I suggest you change the strategy template to include ExitOnClose = false; in the Initialize() method when the "Exit End-of-Day" option is not considered/included.

Regards, Mark

DT&Swing

unread,
Jan 19, 2015, 10:35:14 PM1/19/15
to adaptrad...@googlegroups.com
Mark

I have Ninja strategies and ExitonClose =True/False works very well
use ExitonClose=True for DayTrades
use ExitonClose=False for swing trades, so it can stay Open for weeks
and don know about the code in AB, as I am brand new to try this Builder

Mark Griffin

unread,
Mar 13, 2015, 2:32:23 PM3/13/15
to adaptrad...@googlegroups.com
Hi Mike,
Another thing I found in the NinjaTrader strategy created with Adaptrade Builder version 1.7.2.1 is that "Max bars back" is not set in the Ninjascript code.
I suggest you change the strategy template to include BarsRequired = nnn; in the Initialize() method (nnn=Max bars back). I understand that the AmiBroker code does this.
FYC. Regards, Mark

Michael R. Bryant

unread,
Mar 13, 2015, 2:34:53 PM3/13/15
to adaptrad...@googlegroups.com

Currently, you would set Min Bars Required manually in the back-test window, but, yes, if you set BarsRequired in Initialize, it will fill this value in for you.

 

Mike Bryant

Mark G

unread,
Apr 14, 2015, 1:01:25 PM4/14/15
to adaptrad...@googlegroups.com
Hi Mike,
Another thing I found in the NinjaTrader strategy created with Adaptrade Builder version 1.7.2.1 is that when running live, the Long Stop (or Short Stop) is not set correctly in the Ninjascript code.
After debugging in Visual Studio, I found that "BarsSinceEntry()" is never zero when running live (only in backtesting). This is a NinjaTrader quirk.
So, the code:
if (BarsSinceEntry() == 0)
{
   
LStop = Position.AvgPrice - MMStopSz/Instrument.MasterInstrument.PointValue;
}
is never executed.

The fix is to change the first line to:
if (BarsSinceEntry() == BTAdjust)

Regards, Mark


Reply all
Reply to author
Forward
0 new messages