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

Percentage calculation of current vs. previous

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

    Percentage calculation of current vs. previous

    Hello everyone,
    I'm trying to change the colour of a candle when it exceeds 1.5 times the size of the previous candle indicated in the image below.
    from my understanding 0.1 is 10%, so I'm looking at 150% of the previous candle so I've put in 15. I know I've made a few mistakes
    I've attached a screenshot of the condition I have concocted, could someone point me in the right direction thankyou




    Click image for larger version

Name:	Script.jpg
Views:	326
Size:	113.9 KB
ID:	1174058Click image for larger version

Name:	Large candles.jpg
Views:	315
Size:	156.6 KB
ID:	1174059

    #2
    Hello Christopher Leggit,

    0.1 is %10, 1.0 is %100, 10.0 is %1000.

    If you want %150 that would be * 1.5.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thankyou for the quick reply ChelseaB, could you assist me with my coding blunders? I attached an image of my script
      Last edited by Christopher Leggit; 10-07-2021, 12:07 PM.

      Comment


        #4
        Hello Christopher Leggit,

        I am not able to write the code for you, but I can help you use prints to see if the value is what you are expecting.

        What is the issue you are having? (Please write this in a few short words)

        To confirm, you have changed the multiple value to 1.5 instead of 15 and this issue is corrected, correct?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I have corrected it to 1.5.

          but I am putting script in the wrong area or it's not the correct script.
          I see other people reply with a response how to correct the code is that only allowed by non customer service reps

          Comment


            #6
            Hello Christopher Leggit,

            That is correct, I cannot modify your code on your behalf. I can only assist you and answer any direct questions you have. This thread will remain open for any community members that would like to change your code as a convince to you.

            Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one on one educational support over the phone in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

            That said, through email we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

            We do have a large ecosystem available to help you with programming logic questions. You can post on our forum and ask the other 40,000+ forum members.

            https://ninjatrader.com/support/foru.../ninjatrader-8

            You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.


            That said, what is the issue you are having?

            Have you printed the calculated value and it is not what you are expecting?

            Below is a link to a forum post on using prints in case you are not familiar with how to use prints to understand behavior.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by Christopher Leggit View Post
              Hello everyone,
              I'm trying to change the colour of a candle when it exceeds 1.5 times the size of the previous candle indicated in the image below.
              from my understanding 0.1 is 10%, so I'm looking at 150% of the previous candle so I've put in 15. I know I've made a few mistakes
              I've attached a screenshot of the condition I have concocted, could someone point me in the right direction thankyou

              Howdy, threw this together real quick, as this is relatively simple.

              Basic flow of script is, check for at least one bar on chart(because you will be looking at previous bar.

              Get previous bar size in absolute value.

              Get current bar size in absolute value.

              Compare the new value to 150% of previous value.

              Change bar color if it fits the criteria.

              I also made it update on each tic, this way the bar changes color AS THE BAR IS DEVELOPING.

              The input variable I've created called 'MinPercent' is the multiplier. So the way I have done it, 1.5 equals 150%. You could scale this input variable however you wish with basic math obviously.

              Code:
              //Error checking to ensure at least one bar on chart
              if(CurrentBar < 1)
                  return;
              
              //Get size of previous bar
              double priorSize = Math.Abs(High[1] -Low[1]);
              
              //Get size of Current Bar
              double currentSize = Math.Abs(High[0] - Low[0]);
              
              if(currentSize > MinPercent * priorSize)
                 BarBrush = BarColor;

              Click image for larger version  Name:	2021-10-07_210021.png Views:	0 Size:	623.7 KB ID:	1174130
              Last edited by forrestang; 10-07-2021, 08:07 PM.

              Comment


                #8
                I understand regulations limit what you can do ChelseaB the advice, links and suggestions are always helpful thankyou.

                Thank you Forrestang for helping me with the code i read and follow them over and over to understand how it all meshes together. I had a brain opp in 2005 bit slow putting things together now but i dont want to give up thankyou both for taking the time to help me.

                Comment


                  #9
                  Hello I have a question, Is it possible to calculate the volume and size of the current bar to the previous volume and size in a non defined percentage.

                  For instance,
                  Previous bar travelled 60 ticks and used 360 contracts that's 6 contracts per tick
                  Current bar travelled 40 ticks and used 170 contracts that's 4.25 contracts per tick

                  Theoretically the current bar did better even though it travelled less. Could this be coded with a similar outcome painting the bar?

                  Comment


                    #10
                    Hello Christopher Leggit,

                    The previous bar will be using a barsAgo index of [1] for price series.

                    (High[1] - Low[1]) - this would be the distance from the highest price reached to the lowest price reached.
                    (High[1] - Low[1]) / TickSize - this would be that distance in ticks.
                    Volume[1] - this would be the volume (or number of contracts trades)





                    As far as 'painting the bar', you can use BarBrush to set the color of the bar.


                    Or Draw.Text() to draw text on the chart on that bar.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by Christopher Leggit View Post
                      Hello I have a question, Is it possible to calculate the volume and size of the current bar to the previous volume and size in a non defined percentage.

                      For instance,
                      Previous bar travelled 60 ticks and used 360 contracts that's 6 contracts per tick
                      Current bar travelled 40 ticks and used 170 contracts that's 4.25 contracts per tick

                      Theoretically the current bar did better even though it travelled less. Could this be coded with a similar outcome painting the bar?
                      Yes.

                      You need to divide the length of the bar(High-Low), by the TickSize Property. This will give you the number of Ticks in a bar.

                      You need to capture the volume of the bar, this is done accessing the volume object, reference HERE.

                      Once you have these values, you divide Volume by Ticks for the previous and current bar.

                      Note, this will give a completely different visual than the previous indicator, as you are not comparing lengths, but simply the Ticks per unit of volume. So you can have a shorter bar that is colored b/c it has a higher ratio.

                      Another note: You described it as "contracts per tick." I could be thinking wrong, but I think the example you gave is actually "ticks per contract?" I.e., the code sample I am attaching is exactly the same way as you described(the way you did the math in example). But if you want to reverse it to the way I 'think' you meant to have it, you would reverse the operation. E.g., "Previous bar travelled 60 ticks and used 360 contracts that's 0.16 contracts per tick"

                      However... this does not matter, as you can see how simple it is to simple reverse the division since everything is in a variable.

                      The comparison is still done using multiplier, if you just want to compare equal to equal, you set the multiplier to 1.0 in the user input screen.

                      Code:
                      //Error checking to ensure at least one bar on chart
                      if(CurrentBar < 1)
                          return;
                      
                      //Get size of previous bar IN TICKS
                      double priorSize = (Math.Abs(High[1] -Low[1]) ) / TickSize;
                      
                      //Get Volume of previous bar
                      double priorVolume = Volume[1];
                      
                      
                      
                      
                      //Get size of Current Bar IN TICKS
                      double currentSize = (Math.Abs(High[0] - Low[0]) ) / TickSize;
                      
                      //Get Volume of Current Bar
                      double currentVolume = Volume[0];
                      
                      
                      
                      //Get prior Volume/Ticks
                      double priorVol_Ticks = priorVolume/priorSize;
                      
                      //Get current Volume/Ticks
                      double currentVol_Ticks = currentVolume/currentSize;
                      
                      
                      
                      
                      //Co [ATTACH=JSON]{"data-align":"none","data-size":"medium","data-attachmentid":1174870}[/ATTACH] lor bar if conditions are met
                      if(currentVol_Ticks > MinPercent * priorVol_Ticks)
                         BarBrush = BarColor;
                      Attached Files

                      Comment


                        #12
                        Thank you both so much just got out of hotel quarantine

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by alifarahani, Today, 09:40 AM
                        6 responses
                        29 views
                        0 likes
                        Last Post alifarahani  
                        Started by Waxavi, Today, 02:10 AM
                        1 response
                        17 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by Kaledus, Today, 01:29 PM
                        5 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by Waxavi, Today, 02:00 AM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by gentlebenthebear, Today, 01:30 AM
                        3 responses
                        17 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X