Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 cre8able, 02-11-2023, 05:43 PM
      3 responses
      236 views
      0 likes
      Last Post rhubear
      by rhubear
       
      Started by frslvr, 04-11-2024, 07:26 AM
      8 responses
      114 views
      1 like
      Last Post NinjaTrader_BrandonH  
      Started by stafe, 04-15-2024, 08:34 PM
      10 responses
      47 views
      0 likes
      Last Post stafe
      by stafe
       
      Started by rocketman7, Today, 09:41 AM
      3 responses
      11 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by traderqz, Today, 09:44 AM
      2 responses
      10 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X