I am trading from a 5 min or 10 min barchart with CalculateOnBarClose=true.
In order to be able to play around with stops and profits during the trade I changed the same strategy to CalculateOnBarClose=false. This is working after some difficulties, but it is not possible to backtest this strategy.
So I was thinking of using 2 timeframes in the strategy – with CalculateOnBarClose=true. A 5 min and an 1 min timeframe. The entry signal is taken from the 5 min and the close/high/etc of the 1 min bars is used for playing with stops and profits.
According to the documentation orders can only be taken at the barclose of the primary timeframe, in Ninjascript the condition if(BarsInProgress==0). So the 1 min timeframe has to be the primary timeframe.
I developed a very small test script
It is a sma crossover with only an entry and some kind of trailing.
In the script I count the 1 min bars and at bar 5 the entry signal of the 5 min timeframe is checked.
In order to check the progress of the script the value of some variables is written to the output window.
- I have observed the following
- The strategy does not take any entry trade
- The exit is working but as no entry trade is taken there is no exit trade
- The boolean Long changes from false to true the next 1 min bar after it has become false, independently of the value of Barcount.
The last item I really do not understand as Long can only change to true when eg the condition if(Barcount==5) is met.
I really should appreciate if any one could could shed some light on this problem.
I have attached a part of a screenshot of the output window.
(The strategy is running on the Dax with european times)
The script follows below.
SCRIPT
Variables
double Max=0;
bool Long=false;
int Barcount=0;
if(BarsInProgress==0)
{
Barcount=Barcount+1;
if(Barcount==5
&& CrossAbove(Closes[1][0], SMA(BarsArray[1],5), 1)
&& Long==false)
{
EnterLong("Test");
Long=true;
}
//*******************Trailing
if(Long=true)
{
if(Close[0]>Max)
{
Max=Close[0];
}
if(Close[0]<=(Max-10*TickSize))
{
ExitLong("Test");
Long=false;
Max=0;
}
}
if(Barcount==5) // reset barcount to 0
Barcount=0;
Print(Time.ToString()+" "+ Close[0].ToString()+" "+Max.ToString()+" "+Barcount.ToString()+" "+Long.ToString());
}
if(BarsInProgress==1)
Print(Time.ToString()+" "+ Close[0].ToString()+" "+Max.ToString()+" "+Barcount.ToString()+" "+Long.ToString()+" "+"5min");

Comment