namespace NinjaTrader.NinjaScript.Strategies
{
public class MACDTradeAuto : Strategy
{
private MACD MACD1;
private RSI RSI1;
private EMA EMA1;
private EMA EMA2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MACDTradeAuto";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 1;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
ProfitTarget = 20;
StopLoss = 10;
TradeSize = 1;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
MACD1 = MACD(Close, 12, 26, 9);
RSI1 = RSI(Close, 14, 3);
EMA1 = EMA(Close, 9);
EMA2 = EMA(Close, 9);
MACD1.Plots[0].Brush = Brushes.DarkCyan;
MACD1.Plots[1].Brush = Brushes.Crimson;
MACD1.Plots[2].Brush = Brushes.DodgerBlue;
RSI1.Plots[0].Brush = Brushes.DodgerBlue;
RSI1.Plots[1].Brush = Brushes.Goldenrod;
EMA1.Plots[0].Brush = Brushes.Goldenrod;
EMA2.Plots[0].Brush = Brushes.Gold;
AddChartIndicator(MACD1);
AddChartIndicator(RSI1);
AddChartIndicator(EMA1);
AddChartIndicator(EMA2);
SetProfitTarget(@"", CalculationMode.Ticks, ProfitTarget);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((MACD1.Diff[0] > 0)
&& (RSI1.Default[0] > 40)
&& (Close[0] > EMA1[0])
&& (MACD1.Default[0] > MACD1.Diff[0])
&& (MACD1.Avg[0] > MACD1.Diff[0]))
{
EnterLong(Convert.ToInt32(TradeSize), @"ENTERLong");
}
// Set 2
if ((BarsSinceEntryExecution(0, "", 0) >= 15)
|| (CrossBelow(Low, EMA2, 1)))
{
ExitLong(Convert.ToInt32(TradeSize), @"EXITLong", "");
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="ProfitTarget", Description="Number of ticks up from entry", Order=1, GroupName="Parameters")]
public int ProfitTarget
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="StopLoss", Description="Number of ticks down from entry ", Order=2, GroupName="Parameters")]
public int StopLoss
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="TradeSize", Order=3, GroupName="Parameters")]
public int TradeSize
{ get; set; }
#endregion
}
}
Comment