Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse every bar with SL & Profit Target attached

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Reverse every bar with SL & Profit Target attached

    I tried writing a simple strategy that goes long if it's short and short if it's long. When I comment out the method AssignSLTP, the strategy runs fine.

    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?

    Code:
        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); 
            } 
        }

    #2
    A clarifying note on the code

    The idea is to apply the strategy to a 60 minute chart. I added a second 1 minute chart to help check whether the stop or limit gets hit first.

    Code:
      
        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) 
                {
                    AddDataSeries(Instrument.MasterInstrument.Name,BarsPeriodType.Minute,1); 
                } 
            } 
     
            protected override void OnBarUpdate() 
            {         
                if( CurrentBar < BarsRequiredToTrade || BarsInProgress == 1 ) 
                    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); 
            } 
        }
    Last edited by texasnomad; 10-18-2016, 02:26 PM.

    Comment


      #3
      Problem solved

      I was able to resolve this issue by referencing the sample strategy SampleOnOrderUpdate.
      Attached Files

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CaptainJack, 05-29-2026, 05:09 AM
      0 responses
      388 views
      0 likes
      Last Post CaptainJack  
      Started by CaptainJack, 05-29-2026, 12:02 AM
      0 responses
      260 views
      0 likes
      Last Post CaptainJack  
      Started by charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      218 views
      1 like
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      302 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      268 views
      0 likes
      Last Post CarlTrading  
      Working...
      X