For various reasons (mainly since I eventually plan to send Stop Loss orders to different BarsArrays) I have been trying to build a way to do Stop Loss orders without using your SetStopLoss() method.
Using your Strategy Wizard, I built a strategy that is supposed to exit a Long trade once it has a loss of 10 ticks.
For some reason, when I test it out in the Strategy Analyzer, it immediately exits a trade after the order is filled, rather then waiting for a 10 tick loss.
Any idea why this is?
The relevant part of the strategy's code is below. The attached ExitExample.zip contains the full strategy.
Thanks!
public class ExitExample : Strategy { #region Variables // Wizard generated variables private int stopLoss1 = -10; // Default setting for StopLoss1 // User defined variables (add any user defined variables below) #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() { Add(MACD(12, 26, 9)); Add(MACD(12, 26, 9)); CalculateOnBarClose = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Condition set 1 if (Position.MarketPosition == MarketPosition.Flat && CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9), 1)) { EnterLong(DefaultQuantity, ""); } // Condition set 2 if (Position.MarketPosition == MarketPosition.Long && Open[0] + StopLoss1 * TickSize <= Position.AvgPrice) { ExitLong("", ""); } ]
Comment