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

Saving pivots as variables

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

    Saving pivots as variables

    Click image for larger version

Name:	image.png
Views:	139
Size:	32.5 KB
ID:	1227052
    I am trying to save previous signals as pivots for positioning cues. I would like to save the previous 3 down arrows and previous 3 up arrows as variables ​​for reference in a strategy.

    When I save the most recent signal as the close price
    > H0 = Default input[0]

    I then want to save the most recent H0 as H1, but when I enter
    > H1 = H0

    It creates a feedback loop equivalent of
    > Default input[0] = H0 = H1 = Default input[0]

    How can I save the recent price of a signal without creating this infinite loop that saves all the signals as the previous close?

    The picture above is supposed to show the end goal. The logic being
    > H0 = Close of new sell signal
    > H1 = Close of previous sell signal (This is where I am stuck because of the feedback loop of changing H0 to the close price of Default input[0])
    > H2 = Close of 2nd to last sell signal

    If H0 < H1, it's assigned a 2.
    If H0 < H1 < H2, it's assigned a 3.
    If H0 > H1, it's assigned a 1.


    #2
    Hello RallySquirrel,

    Thanks for your post.

    A custom Series<double> variable could be used to save the Close price to the Series<double> variable and you could reference previous values held in that custom Series by specifying a BarsAgo index value. For example, if you have a custom Series<double> variable called 'H', you could assign the Close price to that variable. You could reference the current value by calling H[0] and the previous value by calling H[1].

    Please review this help guide page for information about how to create custom Series<double> variables in NinjaScript and assign values to them: https://ninjatrader.com/support/help...t8/seriest.htm

    You could also set up a custom Series variable in the 'Custom Series' section of the Additional Data screen of the Strategy Builder and click the 'View code' button to see the generated syntax.

    See this help guide page for more information: https://ninjatrader.com/support/help...onalDataScreen

    Let me know if I may further assist.
    Last edited by NinjaTrader_BrandonH; 12-11-2022, 05:48 PM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Click image for larger version

Name:	image.png
Views:	95
Size:	16.5 KB
ID:	1227173Click image for larger version

Name:	image.png
Views:	84
Size:	7.5 KB
ID:	1227174
      I have created the variables for the pivots, but I'm having trouble creating the data series for the variables that I can call upon in the actions section. ​

      Comment


        #4
        Hello RallySquirrel,

        Thanks for your note.

        You must create a custom Series<double> variable in the Additional Data screen of the Strategy Builder as mentioned in post # 2, not regular double variables in the Inputs and Variables screen of the Strategy Builder. For example, you could create a single custom Series<double> variable called 'mySeriesDouble'.

        Custom Series variables allow you to reference previous values that have been assigned to them by referencing a BarsAgo value. See the help guide page linked in my previous post (Post # 2) for information about how to create custom Series<double> variables in the Additional Data screen of the Strategy Builder.

        After creating the Series<double> variable, you could assign the Close price to that variable (Misc folder > Set 'mySeriesDouble') in the Actions section of the Conditions and Actions screen of the Strategy Builder.

        Then, that variable could be used for Conditions or Actions later in the script.

        I also see that you created a condition that checks if State == <your double variable>. This would not be the correct use of State. State will check what state the data you are processing is. For example, you could create a condition that compares if the Current State (Misc folder > Current state) Equals State.Realtime (Misc folder > State > Realtime). This would check if the strategy is processing realtime data.

        Let me know if I may further assist.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_BrandonH View Post
          Hello RallySquirrel,

          Thanks for your note.

          You must create a custom Series<double> variable in the Additional Data screen of the Strategy Builder as mentioned in post # 2, not regular double variables in the Inputs and Variables screen of the Strategy Builder. For example, you could create a single custom Series<double> variable called 'mySeriesDouble'.

          Custom Series variables allow you to reference previous values that have been assigned to them by referencing a BarsAgo value. See the help guide page linked in my previous post (Post # 2) for information about how to create custom Series<double> variables in the Additional Data screen of the Strategy Builder.

          After creating the Series<double> variable, you could assign the Close price to that variable (Misc folder > Set 'mySeriesDouble') in the Actions section of the Conditions and Actions screen of the Strategy Builder.

          Then, that variable could be used for Conditions or Actions later in the script.

          I also see that you created a condition that checks if State == <your double variable>. This would not be the correct use of State. State will check what state the data you are processing is. For example, you could create a condition that compares if the Current State (Misc folder > Current state) Equals State.Realtime (Misc folder > State > Realtime). This would check if the strategy is processing realtime data.

          Let me know if I may further assist.
          Thanks, Brandon. I am still struggling to get the correct output using this double variable. I have got the variables working to some extent, but I'm not getting the desired outcome on the chart.

          The red triangles are working as I intended. They print every time we close down 2 or more points from the highest close of the last rotation.

          I set all the variables to the previous state using
          H[0] = Close[0]
          H1[0] = H[1]
          H2[0] = H1[1]

          I am trying to create a sell signal on the chart when
          H[0] < H1[0] < H2[0]
          and I've designated the red down arrow for that. As you can see they are showing up in what appear to be random places on the chart.

          Am I doing something wrong with the variables or the logic for the drawing?

          * The blue circles on the chart show where I want the red down arrow to print.

          Click image for larger version  Name:	image.png Views:	0 Size:	5.5 KB ID:	1227243Click image for larger version  Name:	image.png Views:	0 Size:	21.8 KB ID:	1227244Click image for larger version  Name:	image.png Views:	0 Size:	15.0 KB ID:	1227245Click image for larger version  Name:	image.png Views:	0 Size:	44.7 KB ID:	1227246​​​​
          Last edited by RallySquirrel; 12-12-2022, 04:19 PM.

          Comment


            #6
            Hello RallySquirrel,

            Thanks for your note.

            You would only need to create one custom Series<double> variable and assign the Close[0] price to that variable.

            Previous values assigned to that variable could then be accessed by providing a BarsAgo value when calling the custom Series<double> variable.

            For example, see the attached example script. In the attached script we create one custom Series<double> variable named 'myCustomSeries' and assign the Close[0] price to it.

            We can then access the current value of the variable by calling myCustomSeries[0]. Note we use a BarsAgo value of 0 to access the current value. To access the previous value we call myCustomSeries[1]. Note a BarsAgo value of 1 is used to access the previous value assigned to that variable. We pass in a BarsAgo value of 2 when calling myCustomSeries[2] to get the value assigned to the variable 2 values ago.

            The script then checks if myCustomSeries[0] > myCustomSeries[1] and checks if myCustomSeries[1] > myCustomSeries[2] and draws a dot on the chart where this condition becomes true. We also print out these values to see exactly how they evaluate. Prints will appear in a New > NinjaScript Output window.

            If the strategy is not behaving as expected, you must add debugging prints to the strategy (outside the condition) that prints out each value being used in the condition that is not behaving as you expect.

            Below is a link to a forum post that demonstrates how to use prints to understand behavior.

            https://ninjatrader.com/support/foru...121#post791121

            Please let me know if I may assist further.
            Attached Files
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_BrandonH View Post
              Hello RallySquirrel,

              Thanks for your note.

              You would only need to create one custom Series<double> variable and assign the Close[0] price to that variable.

              Previous values assigned to that variable could then be accessed by providing a BarsAgo value when calling the custom Series<double> variable.

              For example, see the attached example script. In the attached script we create one custom Series<double> variable named 'myCustomSeries' and assign the Close[0] price to it.

              We can then access the current value of the variable by calling myCustomSeries[0]. Note we use a BarsAgo value of 0 to access the current value. To access the previous value we call myCustomSeries[1]. Note a BarsAgo value of 1 is used to access the previous value assigned to that variable. We pass in a BarsAgo value of 2 when calling myCustomSeries[2] to get the value assigned to the variable 2 values ago.

              The script then checks if myCustomSeries[0] > myCustomSeries[1] and checks if myCustomSeries[1] > myCustomSeries[2] and draws a dot on the chart where this condition becomes true. We also print out these values to see exactly how they evaluate. Prints will appear in a New > NinjaScript Output window.

              If the strategy is not behaving as expected, you must add debugging prints to the strategy (outside the condition) that prints out each value being used in the condition that is not behaving as you expect.

              Below is a link to a forum post that demonstrates how to use prints to understand behavior.

              https://ninjatrader.com/support/foru...121#post791121

              Please let me know if I may assist further.
              ​​I've recreated the custom series how you described, and I highlighted those parts of the set in the screenshot. I added prints to try to debug, but I can't figure out what the script is doing. I am using the same conditions to set the variable C0 as I am for the custom series of pivots H.

              At 9:53, the script assigns the close price to C0, or Pivot in the script, of 4069. It also assigns the close price the the variable H[0], represented by my "Current High" label. Then at 9:54, the custom series assigns a current high of 4055.5 and a previous high of 4069. The Current High should remain 4069 until the conditions in set 1 are met again, but it appears to be changing the Current High or H[0] custom series variable on every bar update. I am stuck because I have no idea where 4055.5 comes from at 9:54 considering that price did not trade during the candle and the candle closed at 4071.25.​​Click image for larger version

Name:	image.png
Views:	83
Size:	37.1 KB
ID:	1227555Click image for larger version

Name:	image.png
Views:	79
Size:	75.4 KB
ID:	1227554​​ Click image for larger version

Name:	image.png
Views:	114
Size:	184.5 KB
ID:	1227553​​

              Comment


                #8
                Hello RallySquirrel,

                Thanks for your note.

                I cannot see the Prints you created in the screenshot you shared of your code so I am not certain I understand which print is for which variable. It is best practice to print out the variable name in the print to differentiate what each print is for.

                To clarify, the current value assigned to the custom Series<double> variable 'H' (H[0]) is the 'Current High:' print in the Output window, and the user-defined input variable 'C0' is the 'Pivot' print in the Output window. Is that correct?

                I am assuming the 'Previous High:' print in the Output window reflects the previous value assigned to the Series<double> H variable (H[1]). Is that correct?

                I see in the code you shared that the Close[0] price is being assigned to both C0 and H[0] in the same condition. Each time this condition becomes true, the current Close price will be assigned to both of these variables.

                I also see that you are assigning the Close[0] price to C0 in a different condition as well but you are not assigning the Close[0] price to H[0] in this condition. This is likely why the C0 value (Pivots print) is not matching the H[0] ('Current High' print in the Output window.

                Are you assigning a value to H[0] anywhere else in the script?

                According to the Output window results you shared, it seems the value being assigned to H[0] and the previous value, H[1], are printing out correctly. We see that when a new 'Current High:' value prints to the Output window, the 'Previous High:' value always reflects the 'Current High:' price printed on the previous line.

                For example, at 9:52 we see a print for 'Current High:' at a value of 0, and the 'Previous High:' value is 4059.25.

                At 9:53 the 'Current High:' print is 4069 and the 'Previous High:' print is 0 (the 'Current High:' value at 9:52).

                At 9:54 the 'Current High:' print is 4055.5 and the 'Previous High:' print is 4069 (the 'Current High:' value at 9:53), and so on.

                " but it appears to be changing the Current High or H[0] custom series variable on every bar update"

                If you are assigning a value to H[0] in any conditions and one of those conditions becomes true when OnBarUpdate() logic is processed, that new value will be assigned to H[0] as the logic in your script dictates.

                Please let me know if I may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_BrandonH View Post
                  Hello RallySquirrel,

                  Thanks for your note.

                  I cannot see the Prints you created in the screenshot you shared of your code so I am not certain I understand which print is for which variable. It is best practice to print out the variable name in the print to differentiate what each print is for.

                  To clarify, the current value assigned to the custom Series<double> variable 'H' (H[0]) is the 'Current High:' print in the Output window, and the user-defined input variable 'C0' is the 'Pivot' print in the Output window. Is that correct?

                  I am assuming the 'Previous High:' print in the Output window reflects the previous value assigned to the Series<double> H variable (H[1]). Is that correct?

                  I see in the code you shared that the Close[0] price is being assigned to both C0 and H[0] in the same condition. Each time this condition becomes true, the current Close price will be assigned to both of these variables.

                  I also see that you are assigning the Close[0] price to C0 in a different condition as well but you are not assigning the Close[0] price to H[0] in this condition. This is likely why the C0 value (Pivots print) is not matching the H[0] ('Current High' print in the Output window.

                  Are you assigning a value to H[0] anywhere else in the script?

                  According to the Output window results you shared, it seems the value being assigned to H[0] and the previous value, H[1], are printing out correctly. We see that when a new 'Current High:' value prints to the Output window, the 'Previous High:' value always reflects the 'Current High:' price printed on the previous line.

                  For example, at 9:52 we see a print for 'Current High:' at a value of 0, and the 'Previous High:' value is 4059.25.

                  At 9:53 the 'Current High:' print is 4069 and the 'Previous High:' print is 0 (the 'Current High:' value at 9:52).

                  At 9:54 the 'Current High:' print is 4055.5 and the 'Previous High:' print is 4069 (the 'Current High:' value at 9:53), and so on.

                  " but it appears to be changing the Current High or H[0] custom series variable on every bar update"

                  If you are assigning a value to H[0] in any conditions and one of those conditions becomes true when OnBarUpdate() logic is processed, that new value will be assigned to H[0] as the logic in your script dictates.

                  Please let me know if I may assist further.
                  Thanks for corresponding with me so frequently over the last few days.

                  You are correct in all your assumptions.

                  H[0] is Current High in the print, H[1] is the Previous High, and C0 is the Pivot value.

                  I am not assigning a new value to H[0] using any other conditions besides set 1. That's why I'm lost when the value changes on the Current High when it should stay the same.

                  The 9:53 to 9:54 transition is where I see it happen. At 9:53, the current high and pivot are both set to 4069. That step is correct. At 9:54, the Current High changes to 4055.5 even though the conditions for assigning another value for H[0] are not met. The value of H[0] keeps getting assigned 0 throughout the script as well, and I am not sure why that is happening. I will attach the script I've got compiled in case that helps.
                  Attached Files

                  Comment


                    #10
                    Hello RallySquirrel,

                    Thanks for your note.

                    After further research and consulting with my colleagues, I see that when running my example script there is a bug that occurs which led me to believe that custom Series<double> variables hold previous values until a new value is assigned to them. However, that is not the case.

                    When running the example script in post # 6, we can see that the myCustomSeries[0] print always contains a value. This should not be the case.

                    For each new bar that forms, the custom Series<double> variable should start at 0 and the print output for myCustomSeries[0] in the example script should be 0 if a value is not assigned to it on that bar. Similar to how you see a 0 print output for H[0] when a value is not assigned to it on that bar. Your script is behaving as expected whereas my example script is not.

                    This behavior with my example script is unexpected and led me to believe Series<double> variables worked differently than they should. This behavior has been reported to the Development team to look into further.

                    To accomplish your overall goal, regular double variables would need to be used instead of a custom Series<double> variable. You could create three double variables in the Inputs and Variables screen of the Builder as you were doing in post # 3. You would then assign the Close[0] price to the H variable. Then, assign the H variable to the H1 variable and assign the H1 variable to the H2 variable.

                    Next, a condition could be created that checks if H > H1 and another condition in the same Set that checks if H1 > H2. Then in the Actions section of that Set, you would call your Draw.TriangleDown() method to draw the down triangle on the chart when H > H1 and H1 > H2.

                    I apologize for the confusion. Please let me know if you have further questions.


                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_BrandonH View Post
                      Hello RallySquirrel,

                      Thanks for your note.

                      After further research and consulting with my colleagues, I see that when running my example script there is a bug that occurs which led me to believe that custom Series<double> variables hold previous values until a new value is assigned to them. However, that is not the case.

                      When running the example script in post # 6, we can see that the myCustomSeries[0] print always contains a value. This should not be the case.

                      For each new bar that forms, the custom Series<double> variable should start at 0 and the print output for myCustomSeries[0] in the example script should be 0 if a value is not assigned to it on that bar. Similar to how you see a 0 print output for H[0] when a value is not assigned to it on that bar. Your script is behaving as expected whereas my example script is not.

                      This behavior with my example script is unexpected and led me to believe Series<double> variables worked differently than they should. This behavior has been reported to the Development team to look into further.

                      To accomplish your overall goal, regular double variables would need to be used instead of a custom Series<double> variable. You could create three double variables in the Inputs and Variables screen of the Builder as you were doing in post # 3. You would then assign the Close[0] price to the H variable. Then, assign the H variable to the H1 variable and assign the H1 variable to the H2 variable.

                      Next, a condition could be created that checks if H > H1 and another condition in the same Set that checks if H1 > H2. Then in the Actions section of that Set, you would call your Draw.TriangleDown() method to draw the down triangle on the chart when H > H1 and H1 > H2.

                      I apologize for the confusion. Please let me know if you have further questions.

                      For any traders reading this forum with the same problem, I figured out the solution.

                      In the original post, I was not sure how to create the logic to save pivots without saving them all as the recent close price.

                      When the actions section calls:

                      > Set H = Close[0]
                      > Set H1 = H
                      > Set H2 = H1

                      The first thing the script does is change the variable H, and this leads to H1 and H2 saving as the same value.

                      If you reverse the order of the logic:

                      > Set H2 = H1
                      > Set H1 = H
                      > Set H = Close[0]

                      The script saves the previous pivots first and then updates the most recent one. This solved the problem I originally had.

                      Thanks again, Brandon.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by StockTrader88, 03-06-2021, 08:58 AM
                      44 responses
                      3,965 views
                      3 likes
                      Last Post jhudas88  
                      Started by rbeckmann05, Today, 06:48 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post rbeckmann05  
                      Started by rhyminkevin, Today, 04:58 PM
                      4 responses
                      52 views
                      0 likes
                      Last Post dp8282
                      by dp8282
                       
                      Started by iceman2018, Today, 05:07 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post iceman2018  
                      Started by lightsun47, Today, 03:51 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post lightsun47  
                      Working...
                      X