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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      566 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      330 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      547 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      548 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X