Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Parabolic SAR lag?

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

    Parabolic SAR lag?

    Hello!

    Maybe you can help me out on this one. I'd like to drop a market order on the Parabolic SAR and refresh that order on each bar. This little bit of code below will do that BUT it lags one SAR behind. If you look at the image attached it would seem like I'm setting the order on ParabolicSAR1[1] but I'm setting on ParabolicSAR1[0].

    My hunch is that the "current" SAR is being calculated after my code runs so I'm not sure how to bump that order up one SAR--the most recent

    Thank you

    Coop



    Code:
    if ((Position.MarketPosition == MarketPosition.Flat)
    
    && (ParabolicSAR1[0] < Close[0])
    
    ) {
    
    SetProfitTarget("scalp", CalculationMode.Ticks, MyTarget);
    SetStopLoss("scalp", CalculationMode.Ticks, MyStop, false);
    EnterShortStopMarket(1, ParabolicSAR1[0], [USER="88330"]scalp[/USER]);
    
    
    }
    Attached Files

    #2
    Hello coopgrafik,

    I can't tell from the image what Calculate setting is being used, if you are using OnPriceChange like the indicator defaults to you may need to execute your condition from the IsFirstTickOfBar event to make sure the code is executed after the indicator has evaluated.

    Comment


      #3
      Jesse,

      Thanks you for your response. I gave it a shot but I'm still lagging behind one SAR. My defaults are below. Any idea what to try next?

      When I stick a Print inside the OnBarUpdate it does not Print on every price change so I feel like I'm doing something wrong on that. The docs, which I always have a hard time linking as they are in frames (what's the trick to that) say that I just need to Calculate.OnPriceChange to my OnStateChange as I have below so I'm not sure what I'm doing wrong. What piece am I missing to get the OnBarUpdate to fire off on every price change? I feel like I can do that and it's sort itself out.

      Coop



      Code:
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "CoopSar";
      Calculate = Calculate.OnPriceChange;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      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 = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      MyTarget = 40;
      MyStop = 120;
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {
      ParabolicSAR1 = ParabolicSAR(Close, 0.02, 0.2, 0.02);
      ADX1 = ADX(Close, 14);
      
      }
      Last edited by coopgrafik; 02-15-2022, 07:25 PM.

      Comment


        #4
        Any ideas on this?

        Comment


          #5
          Hello coopgrafik,
          When I stick a Print inside the OnBarUpdate it does not Print on every price change so I feel like I'm doing something wrong on that.
          I can't see what you tried in OnBarUpdate to provide any context on that. If you have the code within a condition its possible that is the cause.

          I would suggest to upload the whole script so that we can see where your print is and where everything is situated, that may help to further explain what is happening.

          Comment


            #6
            Jesse,

            Here's the whole script. Again the fundamental question I have is, why does the order lag one SAR. I don't want to get distracted by why something isn't printing or not. Why does the order appear on ParabolicSAR1[1] vs ParabolicSAR1[0] where it is actually coded t be?

            Thank you,

            Coop






            Code:
            #region Using declarations
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.ComponentModel.DataAnnotations;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Gui;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Gui.SuperDom;
            using NinjaTrader.Gui.Tools;
            using NinjaTrader.Data;
            using NinjaTrader.NinjaScript;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.NinjaScript.Indicators;
            using NinjaTrader.NinjaScript.DrawingTools;
            #endregion
            
            //This namespace holds Strategies in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Strategies
            {
            public class CoopSar : Strategy
            {
            private ParabolicSAR ParabolicSAR1;
            
            
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Strategy here.";
            Name = "CoopSar";
            //Calculate = Calculate.OnBarClose;
            Calculate = Calculate.OnPriceChange;
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.AllEntries;
            IsExitOnSessionCloseStrategy = true;
            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 = 20;
            // Disable this property for performance gains in Strategy Analyzer optimizations
            // See the Help Guide for additional information
            IsInstantiatedOnEachOptimizationIteration = true;
            MyTarget = 40;
            MyStop = 120;
            }
            else if (State == State.Configure)
            {
            }
            else if (State == State.DataLoaded)
            {
            ParabolicSAR1 = ParabolicSAR(Close, 0.02, 0.2, 0.02);
            
            
            }
            }
            
            protected override void OnBarUpdate()
            {
            if (BarsInProgress != 0)
            return;
            
            if (CurrentBars[0] &lt; 5)
            return;
            
            
            Print("--&gt; OnPriceChange");
            
            
            if ((Position.MarketPosition == MarketPosition.Flat)
            
            &amp;&amp; (ParabolicSAR1[0] &lt; Close[0])
            
            
            ) {
            
            
            SetProfitTarget("scalp", CalculationMode.Ticks, MyTarget);
            SetStopLoss("scalp", CalculationMode.Ticks, MyStop, false);
            EnterShortStopMarket(1, ParabolicSAR1[0], [USER="88330"]scalp[/USER]);
            
            
            }
            
            
            
            if ((Position.MarketPosition == MarketPosition.Flat)
            
            &amp;&amp; (ParabolicSAR1[0] &gt; Close[0])
            
            
            ) {
            
            
            SetProfitTarget("scalp", CalculationMode.Ticks, MyTarget);
            SetStopLoss("scalp", CalculationMode.Ticks, MyStop, false);
            EnterLongStopMarket(1, ParabolicSAR1[0], [USER="88330"]scalp[/USER]);
            
            
            }
            
            
            
            }
            
            
            #region Properties
            
            
            [NinjaScriptProperty]
            [Range(0, int.MaxValue)]
            [Display(Name="MyTarget", Order=1, GroupName="Parameters")]
            public int MyTarget
            { get; set; }
            
            
            [NinjaScriptProperty]
            [Range(0, int.MaxValue)]
            [Display(Name="MyStop", Order=2, GroupName="Parameters")]
            public int MyStop
            { get; set; }
            
            #endregion
            
            
            }
            }

            Comment


              #7
              Hello coopgrafik,

              It looks like when you pasted the code some characters were replaced, please try attaching the .cs file instead so that I can run the script. You can find the file in the folder Documents\NinjaTrader 8\bin\Custom\Strategy

              Comment


                #8
                Here's the script. Hopefully you can figure out the lag issue
                Attached Files

                Comment


                  #9
                  Hello coopgrafik,

                  I tried the script and see it using the current SAR values. One item that I noted is that you don't use the strategy to add the SAR indicator which means there could be differences between the manually applied indicator and the one the strategy uses. I would suggest to use AddChartIndicator in the strategy to visualize the indicator that the strategy is using and remove any manually applied indicators.



                  Code:
                  ParabolicSAR1 = ParabolicSAR(Close, 0.02, 0.2, 0.02);
                  AddChartIndicator(ParabolicSAR1);

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  62 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  134 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  75 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  50 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X