College Point, NY (Queens)
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20
|
How I got NinjaTrader Market Replay to work live it's live ...
The Problem ... Market replay sets the state of NT based on a number of historical bars it sees prior to the start of the replay (i.e. real time bar). It will run the Initialization method, etc at the first historical bar. So to make replay work like live, we've got to get NT to ignore all the bars prior to the first real time bar (just like a live connection).
Here's how I did it ... note: assume there are several boolean states as follows ...
iAmNoPosition, iAmLong, iAmShort.
To see the difference with how market replay normally runs, I added the following code to my strategy ...
private bool iAmLong = false;
private bool iAmShort = false;
private bool iAmNoPosition = true;
private int once = 1; // Note, not static !
private int tickCnt = 1;
private int prevBar;
...
private void ClearPositions()
{
iAmLong = false;
iAmShort = false;
iAmNoPosition = true;
}
...
protected override void Initialize()
{
...
prevBar = -1;
CalculateOnBarClose = false;
}
protected override void OnBarUpdate()
{
if ( prevBar != CurrentBar ) // Historical bars and 1st tick of a real time bar.
{
prevBar = CurrentBar;
tickCnt = 1;
}
else if ( prevBar == CurrentBar ) // Real time bars only.
{
tickCnt++;
}
// Testing only - because market replay sets state prior to the
// real time date, I want to ignore that setting.
//
if ( true == IsMarketReplay && once == 1 && tickCnt > 1 )
{
ClearPositions();
Print("\n\nCleared all.\n\n");
once = 0;
}
That's it !!!!!
To see the difference between how market replay normally runs and this work-around,
change this line ...
if ( true == IsMarketReplay && once == 1 && tickCnt > 1 )
to this ...
if ( true == IsMarketReplay && once == 1 )
Cheers
|