Can I get some assistance on this strat I'm trying to build? Basic MA crossover strategy: For Long, enter when 9ma crosses above the 20ma but I also want to enter when price crosses back over the 9ma but only while the 9ma is above the 20ma, and vice versa for shorts. And for exits on both long and short, when price closes below and above the 9ma respectively.
Thank you,
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class MyMA : Strategy
{
private SMA SMA1;
private SMA SMA2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyMA";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
Long = 1;
Short = 1;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
SMA1 = SMA(Close, 9);
SMA2 = SMA(Close, 20);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((CrossAbove(SMA1, SMA2, 1))
&& (Close[0] > SMA1[0]))
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"");
}
// Set 2
if ((CrossBelow(SMA1, SMA2, 1))
&& (Close[0] < SMA1[0]))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}
// Set 3
if (CrossBelow(Close, SMA1, 1))
{
ExitLong(Convert.ToInt32(DefaultQuantity), "", @"");
}
// Set 4
if (CrossAbove(Close, SMA1, 1))
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Long", Order=1, GroupName="Parameters")]
public int Long
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Short", Order=2, GroupName="Parameters")]
public int Short
{ get; set; }
#endregion
}
}
region Wizard settings, neither change nor remove
/*@
Comment