I'm trying to test some trading ideas and I solved most of the problems I found...
But I cannot code a system with 2 entry orders: for example in this code, I'd like that will be active both entry orders, but in the report short trades are significally lower than long trades and sometimes it seems an open long position is not reverted into a short position by a short entry order...
In trying to convert some system from Multicharts to NinjaTrader and apart from this problem with different entry orders, everything else seems to be very fine in NinjaTrader 8...
I read somewhere there's the possibility to use another approach to manage orders but it seems too much complicated, then if you can help me I'd edit this file for the nexts strategies!
Thanks for your help!
Have a nice day!
#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 Test2Orders : Strategy { private double MyAtr; private bool OkLong; private bool OkShort; private double highd1; private double lowd1; private double closed1; private double opend1; private double highd2; private double lowd2; private double closed2; private double opend2; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Test to place 2 opposite entry orders: 1 long on High of Yesterday, 1 short on Low of Yesterday"; Name = "Test2Orders"; Calculate = Calculate.OnEachTick; EntriesPerDirection = 1; EntryHandling = EntryHandling.UniqueEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = true; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlatSynchronizeAccount; TimeInForce = TimeInForce.Day; TraceOrders = true; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.ByStrategyPosition; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; OffsetBrk = 0; MyContracts = 1; StopATR = 1; BegTime = DateTime.Parse("08:00"); EndTime = DateTime.Parse("16:00"); CloseTime = DateTime.Parse("16:15"); MyStopTicks = 120; OkLong = true; OkShort = true; } else if (State == State.Configure) { AddDataSeries(BarsPeriodType.Minute, 15); // Added to calculate ATR on 15 min chart AddDataSeries(BarsPeriodType.Minute, 1440); // Added to calculate previous days OHLC values SetStopLoss(CalculationMode.Ticks, MyStopTicks); } } protected override void OnBarUpdate() { if (CurrentBars[0] < 1 || CurrentBars[2] < 2) return; if (ToDay(Times[0][0]) != ToDay(Times[0][1])) // Do something when changes Day... { highd1 = Highs[2][0]; // Calculate previous days OHLC values from intraday data (1440 mins) => Data Index [2] lowd1 = Lows[2][0]; closed1 = Closes[2][0]; opend1 = Opens[2][0]; highd2 = Highs[2][1]; lowd2 = Lows[2][1]; closed2 = Closes[2][1]; opend2 = Opens[2][1]; OkLong = true; // 1 long trade per day OkShort = true; // 1 short trade per day } MyAtr = ATR(BarsArray[1], 20)[0]; //ATR based on 15 mins chart => Data Index [1] if (Times[0][0].TimeOfDay >= BegTime.TimeOfDay && Times[0][0].TimeOfDay < EndTime.TimeOfDay) { if (OkLong) //Send order only if it would be the first long trade of the day { EnterLongStopMarket(MyContracts, highd1+OffsetBrk, @"LE"); } if (OkShort) //Send order only if it would be the first short trade of the day { EnterShortStopMarket(MyContracts, lowd1-OffsetBrk, @"SE"); } } if (Position.MarketPosition == MarketPosition.Long) { OkLong = false; // No more Long trades for today! if (StopATR>0) { ExitLongStopMarket(Position.AveragePrice - StopATR*MyAtr, @"LE", @"LXx"); } if (Times[0][0].TimeOfDay >= CloseTime.TimeOfDay) // Time to close open long position { ExitLong(@"LX", @"LE"); } } if (Position.MarketPosition == MarketPosition.Short) { OkShort = false; // No more Short trades for today! if (StopATR>0) { ExitShortStopMarket(Position.AveragePrice + StopATR*MyAtr, @"SE", @"SXx"); } if (Times[0][0].TimeOfDay >= CloseTime.TimeOfDay) // Time to close open short position { ExitShort(@"SX", @"SE"); } } } #region Properties [NinjaScriptProperty] [Range(-100, double.MaxValue)] [Display(ResourceType = typeof(Custom.Resource), Name="OffsetBrk", Description="Offset Breakout Levels: <0 anticipate >0 posticipate by X points", Order=1, GroupName="NinjaScriptStrategyParameters")] public double OffsetBrk { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(ResourceType = typeof(Custom.Resource), Name="MyContracts", Description="Contracts", Order=2, GroupName="NinjaScriptStrategyParameters")] public int MyContracts { get; set; } [NinjaScriptProperty] [Range(1, double.MaxValue)] [Display(ResourceType = typeof(Custom.Resource), Name="StopATR", Description="ATR Multiplier", Order=3, GroupName="NinjaScriptStrategyParameters")] public double StopATR { get; set; } [NinjaScriptProperty] [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")] [Display(ResourceType = typeof(Custom.Resource), Name="BegTime", Description="Begin Time: when start placing orders", Order=4, GroupName="NinjaScriptStrategyParameters")] public DateTime BegTime { get; set; } [NinjaScriptProperty] [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")] [Display(ResourceType = typeof(Custom.Resource), Name="EndTime", Description="End Time: when stop entering the market", Order=5, GroupName="NinjaScriptStrategyParameters")] public DateTime EndTime { get; set; } [NinjaScriptProperty] [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")] [Display(ResourceType = typeof(Custom.Resource), Name="CloseTime", Description="Close Time: When to Flat Positions", Order=6, GroupName="NinjaScriptStrategyParameters")] public DateTime CloseTime { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(ResourceType = typeof(Custom.Resource), Name="MyStopTicks", Description="StopLoss Ticks: Maximum amount of ticks for stoploss value", Order=7, GroupName="NinjaScriptStrategyParameters")] public int MyStopTicks { get; set; } #endregion } }
Comment