What do I need to change in order to make the stop loss and profit targets run properly? The goal is to either hit the SL or PT on every bar. If that doesn't happen, then switch directon on the next bar. Is there a way to do this without resorting to unmanaged orders?
public class SwitchEveryBar : Strategy { protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Switch direction every bar with silly SLTP values"; Name = "SwitchEveryBar"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = false; 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 = 1; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { } } protected override void OnBarUpdate() { if( CurrentBar < BarsRequiredToTrade ) return; if( Position.MarketPosition == MarketPosition.Short || Position.MarketPosition == MarketPosition.Flat) { EnterLong(10000); AssignSLTP(Low[0],High[0]); } if( Position.MarketPosition == MarketPosition.Long ) { EnterShort( 10000 ); AssignSLTP(High[0],Low[0]); } } void AssignSLTP(double sl, double tp) { SetStopLoss(CalculationMode.Price,sl); SetProfitTarget(CalculationMode.Price,tp); } }
Comment