I am new to Ninjatrader and trying to program a strategy.
It is a multi instrument that needs 2 instruments which are stocks- SPY and SSO and works on daily bars.
The buy and sell signals for both the instruments will be based on SMA crossover of primary instrument(SPY) only.
The logic is as follows:-
1) If primary instrument(SPY) SMA20 crosses above primary instrument(SPY) SMA50, then Exit short position of 2nd instrument(SSO)(if market position of 2nd instrument is short) and Enter long position of primary instrument(SPY).
2) If primary instrument(SPY) SMA20 crosses above primary instrument(SPY) SMA200,then Exit long position of primary instrumen(SPY)(if market position of primary instrument is long) and Enter long position of 2nd instrument(SSO).
3) If primary instrument(SPY) SMA20 crosses below primary instrument(SPY) SMA50, then Exit long position of 2nd instrument(SSO)(if market position of 2nd instrument is long) and Enter Short primary instrument(SPY).
4) If primary instrument(SPY) SMA20 crosses below primary instrument(SPY) SMA200,then Exit short position of primary instrument(SPY)(if market position of primary instrument is short) and Enter short position of 2nd instrument(SSO).
Below is the code:-
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Spy and SSO strategy
/// </summary>
[Description("Spy and SSO strategy")]
public class SPYSSO : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
private int Fast = 20;
private int Medium = 50;
private int Slow = 200;
#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()
{
// Add SSO 1 day Bars object to the strategy
Add("SSO", PeriodType.Day, 1);
SMA(Fast).Plots[0].Pen.Color = Color.Orange;
SMA(Medium).Plots[0].Pen.Color = Color.Green;
SMA(Slow).Plots[0].Pen.Color = Color.Red;
Add(SMA(Fast));
Add(SMA(Medium));
Add(SMA(Slow));
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
// We only want to process events on our primary Bars object (main instrument) (index = 0) which
// is set when adding the strategy to a chart
//
if (BarsInProgress == 1)
return;
// If SPY SMA20 crosses above SMA50, then go long SPY and exit short position of SSO.
if (Positions[1].MarketPosition == MarketPosition.Short
&& CrossAbove(SMA(Fast), SMA(Medium), 1))
{
ExitShort(1);
EnterLong();
}
// If SPY SMA20 crosses above SMA200,and long position of SPY exists then exit long position of SPY
// and go long SSO.
if (Positions[0].MarketPosition == MarketPosition.Long
&& CrossAbove(SMA(Fast), SMA(Slow), 1))
{
ExitLong();
EnterLong(1,100,"buy sso");
}
// If long SSO and SPY SMA20 crosses below SMA50, then exit long SSO and go short SPY
if (Positions[1].MarketPosition == MarketPosition.Long
&& CrossBelow(SMA(Fast), SMA(Medium), 1))
{
ExitLong(1);
EnterShort();
}
// If SPY SMA20 crosses below SMA200,and a short position of SPY exists then exit short position of SPY and
// go short SSO.
if (CrossBelow(SMA(Fast), SMA(Slow), 1))
{
if (Positions[0].MarketPosition == MarketPosition.Short)
ExitShort();
EnterShort(1,100,"short sso");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
I have 2 questions.
1) How do i place buy/sell and short/buy-to-cover orders on the 2nd instrument(SSO) correctly when the context is of the primary instrument(SPY)(BarsInProgress==0)? Do i have to use an IOrder or is there another way?
Do i have to use something like this:- "ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)". I have tried to do "ExitLong(1);" where 1 is barsInProgressIndex for the 2nd instrument, but i dont think thats correct.
2) When i run backtest with the strategy, i only see entry and exit signals on the chart of primary instrument(SPY). How can i see the chart of 2nd instrument after backtesting?
Thanks,
Subrat
Comment