I want to develop a momentum trading strategy.
Here is my code:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class MyMomentumStrategy2 : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
double maxMomentum = 0;
// Series of best performing instruments
IntSeries InstrumentIndexes;
// This DateTimeSeries is for test and debug purpose only
DateTimeSeries MomentumChangeDates;
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
// I add additional instruments
Add("ABC",PeriodType.Day,1);
Add("ABT",PeriodType.Day,1);
//
InstrumentIndexes = new IntSeries(this, MaximumBarsLookBack.Infinite);
MomentumChangeDates = new DateTimeSeries(this, MaximumBarsLookBack.Infinite);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
// Check if there are enough bars
if(CurrentBar<14)return;
if(BarsInProgress==0)
{
// First instrument
maxMomentum=Momentum(14)[0];
InstrumentIndexes.Set(BarsInProgress);
MomentumChangeDates.Set(Time[0]);
}
else if (maxMomentum<Momentum(14)[0])
{
// This is for additional instruments
maxMomentum=Momentum(14)[0];
InstrumentIndexes.Set(BarsInProgress);
MomentumChangeDates.Set(Time[0]);
}
if(BarsInProgress==2)
{
// For last instrument only:
// The Print statement is for test and debug purpose only
Print(MomentumChangeDates[0] + " " + InstrumentIndexes[0] + " " + maxMomentum);
// Exit previous Long
ExitLong(InstrumentIndexes[1]);
// Enter new Long
EnterLong(InstrumentIndexes[0], DefaultQuantity, "EnterLongSignalName");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
13.2.2012 23:00 0 1,24
14.2.2012 23:00 2 -0,0800000000000018
15.2.2012 23:00 0 0,980000000000004
16.2.2012 23:00 0 1,15
17.2.2012 23:00 2 0,940000000000001
21.2.2012 23:00 2 1
I expect to ExitLong with Instrument_0 on 14.2.2012 23:00 and to EnterLong with Instrument_2. Then ExitLong with Instrument_2 on 15.2.2012 23:00 and EnterLong with Instrument_0, and so on.
But the strategy executes only one entry on 27.01.2012 (the first day) and only one exit on 25.01.2013 (the last day).
What is wrong with my strategy?

Comment