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

Increment or Counter in the Strategy Builder?

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

    Increment or Counter in the Strategy Builder?

    Is there a way to increment an int in the strategy builder? When I go to set an int equal to itself there's no way to add 1 to it as well. Just wondering if this is possible in the wizard.

    #2
    Hello Plzwork123,

    Thanks for your post.

    No, there is no means to increment a variable in the Strategy builder.

    You could accomplish this by using a custom series. A custom series will provide a "slot" for every bar. You can assign values to the custom series. To work as a counter you would have to have one set that assigns the previous bar value of the series to the current bar value of the series to keep the latest count available.

    I've attached an example "Event counter" that counts events as they occur. In this case the event counted is when a fast moving average crosses above a slow.

    Set#1 - Is only used once, when the first bar of the data series is loaded, set the EventCounter to a value of 0 (zero)
    Set#2 - On every OnBarUpdate, assign the previous(bar) value of the counter to the current (bar) counter (NOTE: this need to happen before the event set(s) occur)
    Set#3 - Is the example crossabove. The key here is that the current counter is assigned the value of the previous counter + 1
    Set#4 - Is an example of printing out the counter to New>Ninjascript output window.

    The strategy will put the moving averages on the chart, will highlight the background of a cross and will display text to show the event number. See example:

    EventCounterExample.zip
    Attached Files
    Last edited by NinjaTrader_ChelseaB; 07-13-2022, 07:35 AM.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul thanks for the reply.

      That is very close to what I had in mind but it looks like you are not able to access custom series variables in a condition. I need to be able to read the value of a counter in a condition to check if a cross event has happened in 26 bars or more. The normal CrossBelow/Above function checks 26 bars or less. I am not sure how I can do this in the strategy builder (or if its even possible).

      Comment


        #4
        Hello Plzwork123,

        Thanks for your reply.

        In the case of a custom series, in the condition builder you would access via Misc>Custom series. If you had more than one series it will be selectable in the drop down parameters.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Oh i see it now. Thank you very much for the help!

          Comment


            #6
            Originally posted by NinjaTrader_PaulH View Post
            Hello Plzwork123,

            Thanks for your post.

            No, there is no means to increment a variable in the Strategy builder.

            You could accomplish this by using a custom series. A custom series will provide a "slot" for every bar. You can assign values to the custom series. To work as a counter you would have to have one set that assigns the previous bar value of the series to the current bar value of the series to keep the latest count available.

            I've attached an example "Event counter" that counts events as they occur. In this case the event counted is when a fast moving average crosses above a slow.

            Set#1 - Is only used once, when the first bar of the data series is loaded, set the EventCounter to a value of 0 (zero)
            Set#2 - On every OnBarUpdate, assign the previous(bar) value of the counter to the current (bar) counter (NOTE: this need to happen before the event set(s) occur)
            Set#3 - Is the example crossabove. The key here is that the current counter is assigned the value of the previous counter + 1
            Set#4 - Is an example of printing out the counter to New>Ninjascript output window.

            The strategy will put the moving averages on the chart, will highlight the background of a cross and will display text to show the event number. See example:





            [ATTACH]n1099188[/ATTACH]
            Hi NinjaTrader_PaulH - how can this adapt this counter to count the number of profit targets that my strategy has taken? My strategy was built using the NT8 StrategyBuilder and works well when directionally aligned and I would like to keep a count of how many times the profit target has been reached hence the gross PnL thus may want to exit/stop for the day. I believe OnExecutionUpdate() would be the place to keep this counter? Could you share an example so I can try with unlocked code?

            Comment


              #7
              Hello WattMan, you can try this:
              protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
              {
              if (position.MarketPosition == MarketPosition.Flat)
              {
              if (SystemPerformance.AllTrades.Count >= 1)
              {
              Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1] ;
              double res = lastRTTrade.ProfitCurrency * lastRTTrade.Quantity;
              if(res < 0)
              stop += 1;
              if(res > 0)
              target += 1;
              }
              }
              }
              mcosta72
              NinjaTrader Ecosystem Vendor - Quant-Wise

              Comment


                #8
                Originally posted by mcosta72 View Post
                Hello WattMan, you can try this:
                protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
                {
                if (position.MarketPosition == MarketPosition.Flat)
                {
                if (SystemPerformance.AllTrades.Count >= 1)
                {
                Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1] ;
                double res = lastRTTrade.ProfitCurrency * lastRTTrade.Quantity;
                if(res < 0)
                stop += 1;
                if(res > 0)
                target += 1;
                }
                }
                }
                Thanks mcosta72. 'll give it a whirl and share my results

                Comment


                  #9
                  Hello WattMan,

                  Thanks for your post.

                  To be clear you would not be able to access system performance while using the Strategy Builder and would have to unlock the script. As this thread is about "Increment or Counter in the Strategy Builder?" it would be best if you have further questions to create a new thread, thanks for your understanding and help to keep the forums organized.

                  As member mcosta72 pointed out you can access SystemPerformance values.

                  These could be simply accessed in OnBarUpdate(). Here are links to the various sections of information that you can access:




                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_PaulH View Post
                    Hello Plzwork123,

                    Thanks for your post.

                    No, there is no means to increment a variable in the Strategy builder.

                    You could accomplish this by using a custom series. A custom series will provide a "slot" for every bar. You can assign values to the custom series. To work as a counter you would have to have one set that assigns the previous bar value of the series to the current bar value of the series to keep the latest count available.

                    I've attached an example "Event counter" that counts events as they occur. In this case the event counted is when a fast moving average crosses above a slow.

                    Set#1 - Is only used once, when the first bar of the data series is loaded, set the EventCounter to a value of 0 (zero)
                    Set#2 - On every OnBarUpdate, assign the previous(bar) value of the counter to the current (bar) counter (NOTE: this need to happen before the event set(s) occur)
                    Set#3 - Is the example crossabove. The key here is that the current counter is assigned the value of the previous counter + 1
                    Set#4 - Is an example of printing out the counter to New>Ninjascript output window.

                    The strategy will put the moving averages on the chart, will highlight the background of a cross and will display text to show the event number. See example:





                    [ATTACH]n1099188[/ATTACH]


                    Hey Paul. It seems the link for the example is missing.
                    Thanks.

                    Comment


                      #11
                      Hello Trader17,

                      Thanks for your post.

                      Please check again to see if you can download. You may need to use a different browser.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_PaulH View Post
                        Hello Trader17,

                        Thanks for your post.

                        Please check again to see if you can download. You may need to use a different browser.
                        Am getting Invalid Page URL. Could you email it to me if possible?
                        Thanks.

                        Comment


                          #13
                          Hello Trader17,

                          Thanks for your reply.

                          Yes, it is possible to e-mail but you really need to be able to download the file. There are now two links in post#2. We use google chrome but you should be able to use any browser to download the files.

                          I would encourage you to try again on both links in post #2 and use different a browser.

                          If after that you cannot download, please write into ScriptingSupport[at]NinjaTrader[dot]com (Replace [at] with @ and [dot] with .)
                          Mark the E-mail Atten Paul, Ticket # 2550336 and include a link to this thread in the e-mail.
                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Forum,

                            The posted EventCounterExample works great. I've created a specific counter for my needs however I need a way to limit the print statement to report only when the counter value updates instead of printing on every bar. Could someone please post the syntax. I can't find a reference to it anywhere. Tried creating a new Custom Series and recording the bar's Date/Time to it, however the data format does not allow it. Same result when trying to create a Date/Time Variable to record the bar date/time when the counter updates.

                            The only issue is the massive Print Statement. Again, only the timestamp for the bar on which the counter increases by one (1) is required.

                            For the example let's put "EventCounter" on a 15sec chart & start counting from 8:31:00 AM on Jan 3, 2023. You can see the counter increment increasing by one (1) when an event occurs. The counter remains static until the next event.
                            1/3/2023 8:31:00 AM 0
                            1/3/2023 8:31:15 AM 1
                            1/3/2023 8:31:30 AM 2
                            1/3/2023 8:31:45 AM 3
                            1/3/2023 8:32:00 AM 3
                            1/3/2023 8:32:15 AM 3
                            1/3/2023 8:32:30 AM 3
                            prints a line representing each 15 sec. bar
                            ~~~
                            ...and we're still at a count of "3" after 24 hours and ~21,600 output lines later.....
                            1/4/2023 8:30:45 AM 3
                            1/4/2023 8:31:00 AM 3
                            1/4/2023 8:31:15 AM 4
                            1/4/2023 8:31:30 AM 4
                            1/4/2023 8:31:45 AM 5
                            1/4/2023 8:32:00 AM 5
                            1/4/2023 8:32:15 AM 5
                            ~~~

                            This is an example of the desired NinjaScript Output.
                            Count increases by one and only prints the bar date/time where the conditions were true.
                            1/3/2023 8:31:00 AM 0
                            1/3/2023 8:31:15 AM 1
                            1/3/2023 8:31:30 AM 2
                            1/3/2023 8:31:45 AM 3
                            1/4/2023 8:31:15 AM 4
                            1/4/2023 8:31:45 AM 5


                            There is a help guide Methods discussion which includes "Method With No Return Type" (pasted below) however this non-programmer can't get the strategy to compile after adding it to the NinjaScript.

                            // This method prints out the data and time
                            private void PrintDateTime()
                            {
                            Print(DateTime.Now.ToString());
                            }


                            Thanks in advance.​

                            Comment


                              #15
                              Hello culpepper,

                              To have the print only happen when the increment happens you can move the print statement into set 3 where the increment happens. The print is currently in a set with no conditions so it will happen for each bar.

                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,603 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              12 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X