• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 abfefmu, Today, 05:33 AM
      0 responses
      1 view
      0 likes
      Last Post abfefmu
      by abfefmu
       
      Started by bbdjad, Today, 04:36 AM
      0 responses
      4 views
      0 likes
      Last Post bbdjad
      by bbdjad
       
      Started by mittie3, Today, 03:33 AM
      0 responses
      7 views
      0 likes
      Last Post mittie3
      by mittie3
       
      Started by Againmain1, Today, 03:04 AM
      0 responses
      8 views
      0 likes
      Last Post Againmain1  
      Started by tonytimpa, Today, 01:33 AM
      0 responses
      5 views
      0 likes
      Last Post tonytimpa  
      Working...
      X