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

How to get previous Swing high/low values?

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

    #16
    Originally posted by NinjaTrader_ChelseaB View Post
    Hello vpatanka,

    First, may I confirm you followed the video I provided exactly, with exactly the same code, and the behavior was different?


    Your script calls AddDataSeries(Data.BarsPeriodType.Minute, 4);

    OnBarUpdate() will update for every series you have added.

    You are seeing multiple prints because OnBarUpdate() is being updated for multiple series at that time.

    Below is a link to the help guide on BarsInProgress.
    https://ninjatrader.com/support/help...inprogress.htm


    Your script also now splits the setting of mySwingHighBarsAgo and the print, which is not in the code you have suggested.
    Try the code you suggested in post Infractions and follow along with the video I provided you.

    If you were actually intending to change when the mySwingHighBarsAgo is assigned a value and not have this in the same condition action block, what are trying to achieve?
    Oh ok. I thought AddDataSeries was mandatory for the timeframe I am on.
    I just removed it and Print is showing up once correctly. Thanks. Sorry I am still new to this.

    Here is what I have now for OnBarUpdate() and is printing it once but it's still showing swing high from 20 bars ago. Let me go back to your video once again and see what I am missing now. I think I am very close

    protected override void OnBarUpdate()
    {
    //Long Entry with close below 8ema
    if (Times[0][0].TimeOfDay > new TimeSpan(8, 11, 30)
    && Times[0][0].TimeOfDay < new TimeSpan(8, 12, 30))
    {
    //EnterLong(lotSize,"Lot1");
    if (mySwing.SwingHigh[0] > 0)
    {
    if (mySwing.SwingHighBar(0,1,CurrentBar -1 ) > -1)
    {
    mySwingHighBarsAgo = mySwing.SwingHighBar(0, 1, CurrentBar -1);
    }
    }
    Print(Time[0]+" Instance Home "+mySwingHighBarsAgo+" High was "+High[mySwingHighBarsAgo]);
    //isReadyForLong = false;
    }
    }​

    Comment


      #17
      Originally posted by vpatanka View Post

      Oh ok. I thought AddDataSeries was mandatory for the timeframe I am on.
      I just removed it and Print is showing up once correctly. Thanks. Sorry I am still new to this.

      Here is what I have now for OnBarUpdate() and is printing it once but it's still showing swing high from 20 bars ago. Let me go back to your video once again and see what I am missing now. I think I am very close

      protected override void OnBarUpdate()
      {
      //Long Entry with close below 8ema
      if (Times[0][0].TimeOfDay > new TimeSpan(8, 11, 30)
      && Times[0][0].TimeOfDay < new TimeSpan(8, 12, 30))
      {
      //EnterLong(lotSize,"Lot1");
      if (mySwing.SwingHigh[0] > 0)
      {
      if (mySwing.SwingHighBar(0,1,CurrentBar -1 ) > -1)
      {
      mySwingHighBarsAgo = mySwing.SwingHighBar(0, 1, CurrentBar -1);
      }
      }
      Print(Time[0]+" Instance Home "+mySwingHighBarsAgo+" High was "+High[mySwingHighBarsAgo]);
      //isReadyForLong = false;
      }
      }​
      I looked at the code again you mentioned in the video and the only difference I see is indicator vs. strategy but the contents are the same.
      I don't know how to convert the existing strategy code to an indicator where I can add it to the chart so what I am doing is executing "Strategy Analzyer" for my strategy code for 1/31/2023 but ideally it should be the same. Is that correct?
      I am attaching the updated Strategy code for your reference. When I run that in the strategy analyzer for 1/31/2023 with 4 min for ES I see the following on the console output

      1/31/2023 8:12:00 AM Instance NinjaTrader 8 High was 4049.5
      1/31/2023 8:16:00 AM Instance Historical Beta Archive High was 4049.5
      1/31/2023 8:20:00 AM Instance General Development High was 4049.5
      1/31/2023 8:24:00 AM Instance Uncategorized Groups High was 4061


      Looking at the code I am only getting the 1st instance of the last swing high from the current bar so mySwing.SwingHighBar(0,1,CurrentBar -1) should ideally return 4061 for bar at 8:12 right?

      UPDATE - You can ignore "NinjaTrader 8', 'Historical Beta Archive', 'General Development' etc. above. It's supposed to '#' symbol followed by the number which is getting updated in the forum
      Attached Files
      Last edited by vpatanka; 02-01-2023, 06:46 PM.

      Comment


        #18
        Originally posted by vpatanka View Post

        I looked at the code again you mentioned in the video and the only difference I see is indicator vs. strategy but the contents are the same.
        I don't know how to convert the existing strategy code to an indicator where I can add it to the chart so what I am doing is executing "Strategy Analzyer" for my strategy code for 1/31/2023 but ideally it should be the same. Is that correct?
        I am attaching the updated Strategy code for your reference. When I run that in the strategy analyzer for 1/31/2023 with 4 min for ES I see the following on the console output

        1/31/2023 8:12:00 AM Instance NinjaTrader 8 High was 4049.5
        1/31/2023 8:16:00 AM Instance Historical Beta Archive High was 4049.5
        1/31/2023 8:20:00 AM Instance General Development High was 4049.5
        1/31/2023 8:24:00 AM Instance Uncategorized Groups High was 4061


        Looking at the code I am only getting the 1st instance of the last swing high from the current bar so mySwing.SwingHighBar(0,1,CurrentBar -1) should ideally return 4061 for bar at 8:12 right?

        UPDATE - You can ignore "NinjaTrader 8', 'Historical Beta Archive', 'General Development' etc. above. It's supposed to '#' symbol followed by the number which is getting updated in the forum
        I looked at the documentation for Swing Indicator on NT website and in there it shows the following code
        // Prints the high price of the most recent swing high
        Print("The high of the swing bar is "+High[Math.Max(0,Swing(5).SwingHighBar(0,1,10))]);

        I tried putting that in my OnBarUpdate and for 8:12am bar (4min chart) I don't see 4049.5 but instead I see 4057 which is the high of the current Bar at 8:12
        1/31/2023 8:12:00 AM The high of the swing bar is 4057.75
        1/31/2023 8:16:00 AM The high of the swing bar is 4057.25
        1/31/2023 8:20:00 AM The high of the swing bar is 4057.25
        1/31/2023 8:24:00 AM The high of the swing bar is 4061​

        Not quite sure how the third argument for SwingHighBar() operates

        Comment


          #19
          Hello vpatanka,

          I don't know how to convert the existing strategy code to an indicator
          Open the NinjaScript Editor -> in the Explorer pane on the right, right-click 'Indicators' -> select New Indicator -> give the new indicator a unique name -> add any Input properties needed -> click Generate.
          Copy the private and public variables from the original script to the new script. Copy the code of OnBarUpdate() from the original script to the new script. Remove any order method calls or uses of strategy only properties (like Position or SystemPerformance).

          Below is a link to a forum post with helpful resources on getting started with C# and NinjaScript. The 'NinjaScript Editor 401' training video, linked in this post, demonstrates copying code from one script to another. Please watch this training video.



          what I am doing is executing "Strategy Analzyer" for my strategy code for 1/31/2023 but ideally it should be the same. Is that correct?
          Yes, it would be exactly the same. Would not matter if this is an indicator or a strategy on a chart or in the Strategy Analyzer.


          I've made a new video showing your script tested in the Strategy Analyzer.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello vpatanka,


            Open the NinjaScript Editor -> in the Explorer pane on the right, right-click 'Indicators' -> select New Indicator -> give the new indicator a unique name -> add any Input properties needed -> click Generate.
            Copy the private and public variables from the original script to the new script. Copy the code of OnBarUpdate() from the original script to the new script. Remove any order method calls or uses of strategy only properties (like Position or SystemPerformance).

            Below is a link to a forum post with helpful resources on getting started with C# and NinjaScript. The 'NinjaScript Editor 401' training video, linked in this post, demonstrates copying code from one script to another. Please watch this training video.





            Yes, it would be exactly the same. Would not matter if this is an indicator or a strategy on a chart or in the Strategy Analyzer.


            I've made a new video showing your script tested in the Strategy Analyzer.
            https://drive.google.com/file/d/1MhN...w?usp=drivesdk
            Hi Chelsea,

            I added ChartIndicator and ran the exact same thing you showed in the video in the strategy analyzer for 4 min and have captured a screenshot.
            The vertical blue line on the chart is the 8:12 bar. As you can see the console output for the strategy code is picking up last swing high from 20bars ago at 4049.5 vs. 4061 on the chart which was 2 bars ago. Please take a look at the attached screenshot and let me know if you have any questions or if I am missing anything. Click image for larger version

Name:	La232U2svE.png
Views:	164
Size:	181.9 KB
ID:	1233870

            Thanks in advance

            Comment


              #21
              Hello vpatanka,

              The swing indicator changes its values when a swing is made on line 148, 154, 162, 190, 196, 204, 226, 227, 234, 245, and 246.

              I am counting from the last bar on the chart intentionally as any bars before this could have had the swing change.

              Most likely there was no swing 2 bars ago when the bar at 8:12 was processed and the swing was detected later and the swing plot adjusted.

              Have you used prints to understand the behavior?

              When bar 8:12 was processing, what was the swing high?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello vpatanka,

                Most likely there was no swing 2 bars ago when the bar at 8:12 was processed and the swing was detected later and the swing plot adjusted.

                Have you used prints to understand the behavior?

                When bar 8:12 was processing, what was the swing high?
                Oh okay. That would explain the discrepancy between the two. I have not checked SwingHigh when bar 8:12 was processing and I assumed that it'd be the same as whats shown by Swing indicator on the chart but it sounds like when OnBarUpdate() was being executed for 8:12 bar the last swing high value wasn't set to 4061 (2 bars ago) and it was still at 4049.5 as shown on the console output which would be a problem because I was going to rely on that and use that as the target (say if I am going long at bar 8:12) but if that value isn't guaranteed to refresh at the time of processing bar 8:12 then I can't use it.
                Is there anyway around it where I can wait until the last swing high for bar 8:12 is updated to 4061 (2 bars ago) before going long and then using that as the target?

                Comment


                  #23
                  Hello vpatanka,

                  At 8:12 whatever the swing is providing was the swing values at 8:12. It may change after this because the swing indicator rewrites its values (on lines I specified in post # 21).

                  Try using the playback and stop the playback at 8:12. What do you see on the chart?

                  if that value isn't guaranteed to refresh at the time of processing bar 8:12 then I can't use it.
                  I don't know quite follow what this means.

                  On the 8:12 bar, the swing values were what they were. After 8:12 those values may have changed.

                  Is there anyway around it where I can wait until the last swing high for bar 8:12 is updated to 4061
                  Yes, wait until the last bar on the chart, like I have done in the videos I have provided you.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello vpatanka,

                    At 8:12 whatever the swing is providing was the swing values at 8:12. It may change after this because the swing indicator rewrites its values (on lines I specified in post # 21).

                    Try using the playback and stop the playback at 8:12. What do you see on the chart?



                    I don't know quite follow what this means.

                    On the 8:12 bar, the swing values were what they were. After 8:12 those values may have changed.



                    Yes, wait until the last bar on the chart, like I have done in the videos I have provided you.
                    I ran the strategy code in playback and that showed what you mentioned with Swing indicator updating the levels which is now making sense. It looks like when bar 8:12 was being processed the last swing high from Swing indicator was still at 4049.5 and was updated to 4061 after a few more bars.

                    In my existing strategy that I run live in my account I go long at bar 8:12 (on bar close) and currently I set the profit target manually by looking at the chart to last swing high i.e. 4061 (which was 2 bars ago) and was trying to get that set automatically through the strategy code. However, based on what we just discovered the last swing high value may not be updated when I go long at the bar close 8:12
                    What I will have to do is go long at 8:12 and then on subsequent OnBarUpdate() it'll check if the last SwingHigh is > Position.Average_Price and if yes it'll add SetProfitTarget to SwingHigh + one tick

                    Does that make sense to you? I am attaching the screenshot where you can see the last swing high from Swing indicator was still at 4049 when bar 8:12 was being processed.
                    Finally a closure. Thank you very much Click image for larger version

Name:	CWfmzL40Cv.png
Views:	172
Size:	211.6 KB
ID:	1233896

                    Comment


                      #25
                      Originally posted by vpatanka View Post

                      I ran the strategy code in playback and that showed what you mentioned with Swing indicator updating the levels which is now making sense. It looks like when bar 8:12 was being processed the last swing high from Swing indicator was still at 4049.5 and was updated to 4061 after a few more bars.

                      In my existing strategy that I run live in my account I go long at bar 8:12 (on bar close) and currently I set the profit target manually by looking at the chart to last swing high i.e. 4061 (which was 2 bars ago) and was trying to get that set automatically through the strategy code. However, based on what we just discovered the last swing high value may not be updated when I go long at the bar close 8:12
                      What I will have to do is go long at 8:12 and then on subsequent OnBarUpdate() it'll check if the last SwingHigh is > Position.Average_Price and if yes it'll add SetProfitTarget to SwingHigh + one tick

                      Does that make sense to you? I am attaching the screenshot where you can see the last swing high from Swing indicator was still at 4049 when bar 8:12 was being processed.
                      Finally a closure. Thank you very much Click image for larger version

Name:	CWfmzL40Cv.png
Views:	172
Size:	211.6 KB
ID:	1233896
                      just FYI - I added the code as mentioned in my post Strategy Development and it seems to be functional. Thanks for your help

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by cre8able, Today, 03:20 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post cre8able  
                      Started by Fran888, 02-16-2024, 10:48 AM
                      3 responses
                      47 views
                      0 likes
                      Last Post Sam2515
                      by Sam2515
                       
                      Started by martin70, 03-24-2023, 04:58 AM
                      15 responses
                      114 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by The_Sec, Today, 02:29 PM
                      1 response
                      7 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      2 responses
                      31 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Working...
                      X