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

Create Mathematical Formula for Indicator Within Strategy

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

    Create Mathematical Formula for Indicator Within Strategy

    I have a very simple equation that I want to integrate as an entry condition to my strategy.
    The equation is:

    [ (Upper Bollinger Band - Lower Bollinger Band) / (Upper Bollinger Band) ] * 100 >= 20

    I do not want any trades to be taken unless the above is true
    This is the code I tried to implement (unsuccessfully):

    ((((Bollinger(2, 20).Upper[1]) - (Bollinger(2, 20).Lower[1])) / (Bollinger(2, 20).Upper[1])) * 100 >= 20)


    I have multiple entries on the strategy, and when I add this condition, the entry does not open positions.

    Please let me know what I need to modify to get the condition to work

    #2
    Also, do I change the "1" to "idx" to get the most recent bar?

    Thanks

    Comment


      #3
      Originally posted by thereyoflite View Post
      Also, do I change the "1" to "idx" to get the most recent bar?
      Welcome to the forums!

      No, change the '1' to '0'.

      Dude, we can't see your code.
      What is 'idx'?

      Comment


        #4
        Hello thereyoflite,

        Welcome to the NinjaTrader forums!

        Perhaps the condition is never true.

        Print the values to see if the condition is evaluating as true or false. Use TraceOrders to see if orders are being submitted or cancelled.

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


        Print the time of the bar and all values in the condition, along with labels to note what the value is and how it is being compared.

        As an example print:
        Code:
        Print(string.Format("{0} | ((((Bollinger(2, 20).Upper[1]: {1}) - (Bollinger(2, 20).Lower[1]: {2})) / (Bollinger(2, 20).Upper[1]: {1})) * 100: {3} >= 20)", Time[0], Bollinger(2, 20).Upper[1], Bollinger(2, 20).Lower[1], (((Bollinger(2, 20).Upper[1]) - (Bollinger(2, 20).Lower[1])) / (Bollinger(2, 20).Upper[1])) * 100));
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          For some reason the print was not working for me, but I got it to work. It was most likely a syntax issue. Thank you both!

          Comment


            #6
            Hi again,

            So the Bollinger band equation is functioning correctly. However, I am trying to change the last value at the end of the equation to a variable, so that I can optimize it within the strategy analyzer tool, and have been having some trouble.

            As I have it set up now, there is a parameter, and it allows me "optimize" it, but nothing actually happens. Somehow it is not interpreting the variable changing.
            This is the code I have:

            This is the function that is working and calculating correctly (backtests produce various results when I change the value), I am trying to optimize the value ".2" at the end of the equation
            ((((Bollinger(2,20).Upper[0]) - (Bollinger(2,20).Lower[0])) / (Bollinger(2,20).Upper[0])) * 100 >= .2)


            This is the function that I have been trying to optimize by:
            ((((Bollinger(2,20).Upper[0]) - (Bollinger(2,20).Lower[0])) / (Bollinger(2,20).Upper[0])) * 100 >= Bbx/100 )


            This is the code I have under the properties region under strategy parameters:

            [NinjaScriptProperty]
            [Range(int.MinValue, int.MaxValue)]
            [Display(Name="Bbx", Order=5, GroupName="Custom Calculations")]
            public int Bbx
            { get; set; }

            I have also tried:

            [NinjaScriptProperty]
            [Range(int.MinValue, int.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name= "Bbx", Order=5, GroupName= "Custom Calculations")]
            public int Bbx
            { get; set; }

            I also have the default value already set under: if (State == State.SetDefaults)

            Whenever I try to optimize, I get the same value for Bbx over and over (even when I make it the only optimizable parameter).

            How can I fix this?

            Comment


              #7
              Hello thereyoflite,

              I am not able to reproduce this behavior.

              Below is a link to a video of the test.


              Attached is the exported reduced simple test script so that you may test this on your end.

              If following the exact steps shown in the video with this test script are you experiencing different behavior?
              Attached Files
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8

                When I use the script which you attached, the optimization results vary, as they should.
                I believe that the issue is the formula that I am trying to use for Bollinger bands, because when I input that formula, the results stay the same across optimization, even though I can see the parameter "Bbx" is producing different values, as it should. However, the values are not yielding different results.
                Do I somehow need to alter the Bollinger bands to produce different results?

                Comment


                  #9
                  Hello thereyoflite,

                  This would confirm that the input is not the issue.
                  If you are printing the value and it is changing in the prints, then this would not be where to focus.

                  Where are you using this value?
                  Have you added prints for the condition set where this value is used?
                  Are the values in the condition set different or the same?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    ((((Bollinger(2, 20).Upper[1]) - (Bollinger(2, 20).Lower[1])) / (Bollinger(2, 20).Upper[1])) * 100 >= Bbx/100)

                    This is the equation being used
                    How should I print this to see the different results being generated? The entries should be changing as Bbx changes, but they remain consistent in the optimization

                    The values in the condition set are the constant, Bbx is the only variable that can be optimized in the equation

                    Comment


                      #11
                      Hello thereyoflite,

                      Print the time of the bar, print Bollinger(2, 20).Upper[1], print Bollinger(2, 20).Lower[1], print (Bollinger(2, 20).Upper[1]) - (Bollinger(2, 20).Lower[1]), print ((Bollinger(2, 20).Upper[1]) - (Bollinger(2, 20).Lower[1])) / (Bollinger(2, 20).Upper[1]), print (((Bollinger(2, 20).Upper[1]) - (Bollinger(2, 20).Lower[1])) / (Bollinger(2, 20).Upper[1])) * 100, print Bbx, print Bbx/100.

                      Below is a link to a forum post that demonstrates using prints to understand behavior. Please watch the videos linked in this post.


                      I'll help you get started.

                      Print(string.Format( "{0} | ((Bollinger(2, 20).Upper[1]: {1}) - (Bollinger(2, 20).Lower[1]: {2})): {3} ", Time[0], Bollinger(2, 20).Upper[1], Bollinger(2, 20).Lower[1], ((Bollinger(2, 20).Upper[1]: {1}) - (Bollinger(2, 20).Lower[1]: {2})) ));
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      170 responses
                      2,271 views
                      0 likes
                      Last Post sidlercom80  
                      Started by Irukandji, Yesterday, 02:53 AM
                      2 responses
                      17 views
                      0 likes
                      Last Post Irukandji  
                      Started by adeelshahzad, Today, 03:54 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post adeelshahzad  
                      Started by CortexZenUSA, Today, 12:53 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post CortexZenUSA  
                      Started by CortexZenUSA, Today, 12:46 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post CortexZenUSA  
                      Working...
                      X