Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't get previous Swing instances after second

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

    Can't get previous Swing instances after second

    I am trying to create a strategy using previous instances of swing Highs that are evaluated on each Bar, ie: on each bar previous Swing Highs should be recalculated.

    High1 is the last swingHigh Occurrence
    High2 is the previous
    etc.

    On each new swing high, High1 will be new; High2 will get the value of previous High1, High3 will get the value of previous High2, etc.

    I was trying to be sure I was getting the right Highs by printing every value but when I compare to the chart, the value doesn't seem to be right. First two instances seem to be ok, but the rest won't calculate properly.

    This is my code so far:

    Code:
    if (
    Swing(StrengthLarge).SwingHighBar(0,1,0) != -1
    )
    {
    
    double High1 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 1, 2000))];
    double High2 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 2, 2000))];
    double High3 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 3, 2000))];
    double High4 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 4, 2000))];
    double High5 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 5, 2000))];
    double High6 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 6, 2000))];
    
    Print (Time[0] + " * HIGHS * " +
    + High1 + " / "
    + High2 + " / "
    + High3 + " / "
    + High4 + " / "
    + High5 + " / "
    + High6);
    
    }
    I am particularly confused with the lookbackperiod I should be using. It could be the source of my problems here

    Thanks in advance for your help.

    #2
    Hello Danilod,

    Thanks for your post.

    I don't see anything incorrect with your coding. The swing indicator will only look back as far as it can or the 2000 bars.

    What bar type and bar size are you testing on?

    What is the value of StrengthLarge?

    Are you applying the swing indicator with the same strength setting to the chart when you compare the prints?

    I've attached an example of what I see here on a 1 minute NQ 09-21 chart, using swing with a setting of 5. Looking at the last few lines of prints to the chart, you can see they match up.

    Click image for larger version

Name:	Danilod-1.PNG
Views:	243
Size:	145.3 KB
ID:	1169267

    Comment


      #3
      Hello Danilod,

      My last reply was incomplete.

      The swing values match up and to be clear the Times shown are 5 bars after the swing point and this is because I used 5 for the StrengthLarge value. The swing indicator does not confirm a swing point until the Strength number of bars (5 in my case) has passed the swing point.

      Comment


        #4
        Thanks Paul, I've tried using Strength = 5 as you did and it worked perfectly. But when I change strength to 50 (the one I intend to use), I am still having wrong values compared to the graph. Take a look:
        Starting from 8am values in output are:

        Click image for larger version

Name:	Screenshot 2021-08-27 124633.png
Views:	254
Size:	39.6 KB
ID:	1169307

        But in the graph, while 1, 2 and 3 worked match, occurrences number 4, 5, and 6 are giving me wrong values. (in white approx. values as they appear on the chart)

        Click image for larger version

Name:	Screenshot 2021-08-27 124320.png
Views:	211
Size:	843.3 KB
ID:	1169308

        I have this code at OnBarupdate

        Code:
        if (State == State.SetDefaults)
        {
        Description = @"Try to find good trends";
        Name = "Dan4";
        Calculate = Calculate.OnBarClose;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = false;
        ExitOnSessionCloseSeconds = 30;
        IsFillLimitOnTouch = true;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.Standard;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade = 1000;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
        StrengthLarge                = 50;
        
        protected override void OnBarUpdate()
        {
        if (CurrentBar < BarsRequiredToTrade)
        return;
        
        if (BarsInProgress != 0)
        return;
        
        
        if (
        Swing(StrengthLarge).SwingHighBar(0,1,0) != -1
        )
        {
        
        double High1 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 1, 2000))];
        double High2 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 2, 2000))];
        double High3 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 3, 2000))];
        double High4 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 4, 2000))];
        double High5 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 5, 2000))];
        double High6 = High[Math.Max(0, Swing(StrengthLarge).SwingHighBar(0, 6, 2000))];
        
        Print (Time[0] + " * HIGHS * " +
        + High1 + " / "
        + High2 + " / "
        + High3 + " / "
        + High4 + " / "
        + High5 + " / "
        + High6);
        
        }
        and in properties:


        Code:
        [NinjaScriptProperty]
        [Range(1, int.MaxValue)]
        [Display(Name="StrengthLarge", Order=8, GroupName="Parameters")]
        public int StrengthLarge
        { get; set; }
        Could be the change of day that is causing problems?

        Thanks again,

        Comment


          #5
          Hello Danilod,

          Thanks for your reply.

          When you apply your indicator to the chart, please set the parameter "Maximum bars lookback" to Infinite. Otherwise, by default, it is limited to 256 bars look back.

          You can also set this in your script: https://ninjatrader.com/support/help...rslookback.htm

          Comment


            #6
            Originally posted by NinjaTrader_PaulH View Post
            Hello Danilod,

            Thanks for your reply.

            When you apply your indicator to the chart, please set the parameter "Maximum bars lookback" to Infinite. Otherwise, by default, it is limited to 256 bars look back.

            You can also set this in your script: https://ninjatrader.com/support/help...rslookback.htm
            OMG that was it, thank you very much!

            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