Here is the code:
namespace NinjaTrader.Strategy
{
public class testv1 : Strategy
{
#region Variables
private DataSeries movAvgVal;
private DataSeries movAvgVal2;
private DataSeries tmp_avg;
private int avgLength = 40;
private int atrLength = 40;
private double upBand = 0;
private double dnBand = 0;
#endregion
protected override void Initialize()
{
CalculateOnBarClose = true;
movAvgVal = new DataSeries(this);
movAvgVal2 = new DataSeries(this);
tmp_avg = new DataSeries(this);
}
protected override void OnBarUpdate()
{
tmp_avg[0] = (High[0] + Low[0] + Close[0])/3;
if(CurrentBar < 41) return;
movAvgVal[0] = SMA(tmp_avg,40)[0];
movAvgVal2[0] = SMA(tmp_avg,40)[0];
upBand = movAvgVal[0] + ATR(atrLength)[0];
dnBand = movAvgVal[0] - ATR(atrLength)[0];
if ( movAvgVal[0] > movAvgVal[1] )
{
EnterLongStop(upBand, "Long Buy");
}
else if ( movAvgVal[0] < movAvgVal[1] )
{
EnterShortStop(dnBand, "Short Buy");
}
if ((Position.MarketPosition == MarketPosition.Long))
{
ExitLongStop(movAvgVal2[0], "Long Exit");
}
else if ((Position.MarketPosition == MarketPosition.Short))
{
ExitShortStop(movAvgVal2[0], "Buy to Cover");
}
}
Comment