I'm taking an indicator that I've been writing recently and trying to convert it into a strategy to backtest.
I think most of it is working, but I cannot seem to get my orders correct. What I'm trying to achieve is that if my conditions are true to enter a short order and set stop loss at 1 x ATR above the high of the candle that met conditions and for profit target to be simply 30 pips below the low of the candle that met conditions.
Below is my code, would appreciate if someone could help point me in the right direction!
Many thanks
Tim
#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.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class FTBwOrders : Strategy { private Double dStoreHigherOpenOrClose; // variable to store whether price close or open is higher value private Double dOpenClosePct; // variable to store % of bar open/close happens private int x = 0; private Double s1, s2, s3; private ATR ATR1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "FTBwOrders"; 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; dStoreHigherOpenOrClose =0; dOpenClosePct =0; } else if (State == State.Configure) { } else if (State == State.DataLoaded) { ATR1 = ATR(Close, 7); SetStopLoss("", CalculationMode.Price, (((High[0] + (ATR1[0])) * (TickSize * 10))) , false); SetProfitTarget("", CalculationMode.Price, (((Low[0] - 30) * (TickSize * 10))) ); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 25) return; dStoreHigherOpenOrClose = Close[0]; if (Open[0] > Close[0]) { dStoreHigherOpenOrClose = Open[0]; } // (higher of open/close less low) / (high less low) dOpenClosePct = ((dStoreHigherOpenOrClose - Low[0]) / (High[0] - Low[0])); // Set 1 if ((High[1] > High[2]) && (dOpenClosePct <= 0.5) && (High[0] > High[1]) && (Times[0][0].TimeOfDay >= new TimeSpan(6, 0, 0)) && (Times[0][0].TimeOfDay <= new TimeSpan(10, 0, 0)) && (Close[1] > Close[2])) { x = 0; for(int i = 1; i < 24; i++) { if(Close[i] > Close[i + 1]) { x = x + 1; } else { break; } } Print ("x " + x.ToString()); Print ("CurrentBars[0] " + CurrentBars[0].ToString()); Print ("dOpenClosePct " + dOpenClosePct.ToString()); Print ("Open[0] " + Open[0].ToString()); Print ("Close[0] " + Close[0].ToString()); Print ("Low[0] " + Low [0].ToString()); Print ("High[0] " + High[0].ToString()); Print ("ATR1[0] " + ATR1[0].ToString()); Print ("High + ATR[0] " + (High[0] + (ATR1[0])).ToString()); Print ("s1 " + s1.ToString()); Print ("s2 " + s2.ToString()); Print ("s3 " + s3.ToString()); Draw.ArrowDown(this, @"FTB0 " + CurrentBars[0].ToString(), true, 0, (High[0] + (1 * (TickSize * 10))) , Brushes.Orange); //draw an orange down arrow over the candle that could be a potential FTB setup string myText = string.Format("{0,0:N2}", (dOpenClosePct + (5 * (TickSize * 10)))); Draw.Text(this, CurrentBars[0].ToString() + @" Text_1a",myText , 0, Low[0] - 30 * TickSize); string myText2 = (x + " bullish"); Draw.Text(this, CurrentBars[0].ToString() + @" Text_1b",myText2 , 0, (High[0] + (10 * (TickSize * 10)))); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_1", @"5 pips", 9, (High[0] + (5 * (TickSize * 10))) ); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_1", true, 0, (High[0] + (5 * (TickSize * 10))) , 8, (High[0] + (5 * (TickSize * 10))) , Brushes.Red, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_1a", @"20 pips", 9, (High[0] + (20 * (TickSize * 10))) ); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_1a", true, 0, (High[0] + (20 * (TickSize * 10))) , 8, (High[0] + (20 * (TickSize * 10))) , Brushes.Red, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_3a", @"1 ATR", 9, (High[0] + ATR1[0])); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_3a", true, 0, (High[0] + (ATR1[0])) , 8, (High[0] + (ATR1[0])) , Brushes.Red, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_4a", @"1.5 ATR", 9, (High[0] + (1.5 * ATR1[0]))); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_4a", true, 0, (High[0] + (1.5 * (ATR1[0]))) , 8, (High[0] + (1.5 * (ATR1[0]))) , Brushes.Red, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_2", @"5 pips", 9, (Low[0] - (6 * (TickSize * 10)))); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_2", true, 0, (Low[0] - (6 * (TickSize * 10))) , 8, (Low[0] - (6 * (TickSize * 10))) , Brushes.Green, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_2a", @"20 pips", 9, (Low[0] - (21 * (TickSize * 10))) ); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_2a", true, 0, (Low[0] - (21 * (TickSize * 10))) , 8, (Low[0] - (21 * (TickSize * 10))) , Brushes.Green, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_6a", @"30 pips", 9, (Low[0] - (31 * (TickSize * 10))) ); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_6a", true, 0, (Low[0] - (31 * (TickSize * 10))) , 8, (Low[0] - (31 * (TickSize * 10))) , Brushes.Green, DashStyleHelper.Dash, 1); Draw.Text(this, CurrentBars[0].ToString() + @"FTB3 Text_5a", @"1 ATR", 9, (Low[0] - ATR1[0] - 1 * (TickSize * 10))); Draw.Line(this, CurrentBars[0].ToString() + @"FTB3 Line_5a", true, 0, (Low[0] - ATR1[0] - 1 * (TickSize * 10)) , 8, (Low[0] - ATR1[0] - 1 * (TickSize * 10)) , Brushes.Green, DashStyleHelper.Dash, 1); EnterShort(Convert.ToInt32(DefaultQuantity), @"FTB"); } } } }
Comment