Basic logic
Bullish = EMA(20) across above the current close and previous close > current close ==> Buy
I used the strategy builder and the codes look like this
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"EMA";
Name = "WizEMA";
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;
Loss = 24;
Profit = 48;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(Close, 20);
EMA1.Plots[0].Brush = Brushes.Lime;
AddChartIndicator(EMA1);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 2)
return;
// Set 1
if ((CrossBelow(EMA1, Close, 1))
&& (Close[1] > Close[0]))
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"EMA");
}
// Set 2
if ((CrossAbove(EMA1, Close, 1))
&& (Close[1] < Close[0]))
{
EnterShort(Convert.ToInt32(DefaultQuantity), @"EMA");
}
}
Comment