My criteria is as such -
Long entry:
- 20SMA is above the 200SMA
- Price is above the 20SMA
- A red bar opens above the 20SMA, within a certain distance from the 20SMA in ticks
- The current bar passes the immediately preceding red bar to the upside, triggering the entry. In other words, a green bar overtakes a red bar above the 20SMA.
- 20SMA is below the 200SMA
- Price is below the 20SMA
- A green bar closes below the 20SMA, within a certain distance from the 20SMA in ticks
- The current bar passes the immediately preceding green bar to the downside, triggering the entry. In other words, a red bar overtakes a green bar below the 20SMA.
#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion namespace NinjaTrader.NinjaScript.Strategies { // Define the Johnstrat strategy class public class Johnstrat : Strategy { private SMA fastSma; private SMA slowSma; [ Range(1, int.MaxValue), NinjaScriptProperty ] public int FastPeriod { get; set; } [ Range(1, int.MaxValue), NinjaScriptProperty ] public int SlowPeriod { get; set; } [ Range(1, int.MaxValue), NinjaScriptProperty ] public int TicksProximity { get; set; } [ Range(1, int.MaxValue), NinjaScriptProperty ] public int StopLossTicks { get; set; } [ Range(1, int.MaxValue), NinjaScriptProperty ] public int ProfitTargetTicks { get; set; } protected override void OnStateChange() { if (State == State.SetDefaults) { Description = "Johnstrat"; Name = "Johnstrat"; Calculate = Calculate.OnEachTick; // Strategy calculation on each tick // for real-time decision making // Setting default values FastPeriod = 20; SlowPeriod = 200; TicksProximity = 100; StopLossTicks = 40; // 10 points below entry for stop loss ProfitTargetTicks = 80; // 20 points for profit target } else if (State == State.Configure) { // Add Fast and Slow SMA indicators fastSma = SMA(FastPeriod); slowSma = SMA(SlowPeriod); fastSma.Plots[0].Brush = Brushes.DodgerBlue; slowSma.Plots[0].Brush = Brushes.Orchid; AddChartIndicator(fastSma); AddChartIndicator(slowSma); } } protected override void OnBarUpdate() { if (CurrentBar < SlowPeriod) return; // Checking Long and Short Entry Criteria if (IsLongEntry()) EnterLong("LongEntry"); // Use a signal name for the entry else if (IsShortEntry()) EnterShort("ShortEntry"); // Use a signal name for the entry } // Long Entry Criteria private bool IsLongEntry() { double currentPrice = Instrument.MarketData.Ask.Price; // Real-time ask price for long entry return fastSma[0] > slowSma[0] && Math.Abs(currentPrice - fastSma[0]) <= TicksProximity && Close[1] < Open[1] && // Previous bar is red High[1] > fastSma[1] && // High of previous bar is above Fast SMA Close[1] >= fastSma[1] && currentPrice > High[1]; // Real-time price is above the high of the // previous red bar } private bool IsShortEntry() { double currentPrice = Instrument.MarketData.Bid.Price; // Real-time bid price for short entry return fastSma[0] < slowSma[0] && Math.Abs(currentPrice - fastSma[0]) <= TicksProximity && Close[1] > Open[1] && // Previous bar is green Low[1] < fastSma[1] && // Low of previous bar is below Fast SMA Close[1] <= fastSma[1] && currentPrice < Low[1]; // Real-time price is below the low of the // previous green bar } protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment) { if (order.Name == "LongEntry" && order.OrderState == OrderState.Filled) { // Set the stop loss and profit target for long positions SetStopLoss("LongEntry", CalculationMode.Ticks, StopLossTicks, false); SetProfitTarget("LongEntry", CalculationMode.Ticks, ProfitTargetTicks); } else if (order.Name == "ShortEntry" && order.OrderState == OrderState.Filled) { // Set the stop loss and profit target for short positions SetStopLoss("ShortEntry", CalculationMode.Ticks, StopLossTicks, false); SetProfitTarget("ShortEntry", CalculationMode.Ticks, ProfitTargetTicks); } } } }
Comment