I am struggling to get the code properly written to send orders at the specific prices. The exit prices were off the scale. Also, I got error message saying the order price is above/below the market price and caused my strategy to be disabled. I am using MACrossover to help me to understand how those exits work. What did I miss something?
Thanks for your help,
-traderjh
// // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>. // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. // #region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Strategy; #endregion namespace NinjaTrader.Strategy { public class myMACrossOver : Strategy { #region Variables private int fast = 10; private int slow = 25; private double targetPips = 0.00150; private double lossPips = 0.00100; #endregion /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { BarsRequired = 0; SMA(Fast).Plots[0].Pen.Color = Color.Yellow; SMA(Slow).Plots[0].Pen.Color = Color.Red; Add(SMA(Fast)); Add(SMA(Slow)); CalculateOnBarClose = false; } protected override void OnBarUpdate() { double fastPrice = (SMA(Fast)[0]); double slowPrice = (SMA(Slow)[0]); if (Position.MarketPosition == MarketPosition.Flat) { if (CrossAbove(SMA(Fast), SMA(Slow), 10)) EnterLong(); else if (CrossBelow(SMA(Fast), SMA(Slow), 10)) EnterShort(); } if (Position.MarketPosition == MarketPosition.Long) { if ((Close[0] <= Position.AvgPrice + targetPips) && (High[0] > (Position.AvgPrice + targetPips))) { ExitLongStop((Position.AvgPrice + targetPips)); } else if (Close[0] <= Position.AvgPrice + lossPips) { ExitLongStop((Position.AvgPrice - lossPips)); } } if (Position.MarketPosition == MarketPosition.Short) { if ((Close[0] >= (Position.AvgPrice - targetPips)) && (Low[0] < (Position.AvgPrice - targetPips))) { ExitShortStop((Position.AvgPrice - targetPips)); } else if (Close[0] >= Position.AvgPrice + lossPips) { ExitShortStop((Position.AvgPrice + lossPips)); } } } #region Properties public int Fast { get { return fast; } set { fast = Math.Max(1, value); } } public int Slow { get { return slow; } set { slow = Math.Max(1, value); } } public double TargetPips { get { return targetPips; } set { targetPips = Math.Max(0.0, value); } } public double LossPips { get { return lossPips; } set { lossPips = Math.Max(0.0, value); } } #endregion } }
Comment