Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Division for Open and Close Price Does Not Produce Correct Result

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

    Division for Open and Close Price Does Not Produce Correct Result

    I started a new blank Strategy and I wanted to see the gap in the the Current Open price compared to the Previous Candle Close Price.

    I added the below simple one line of code to the script to print the results (I skip the first candle as well when starting the script so as to not throw an error) and when there is a decimal in the result, it is not correct:

    Print(Time[0]+" - "+Open[0]+" - "+Close[1]+" - "+Open[0]/Close[1]);


    I am using 5min candles on the ES. When I run this script on the 5min ES using the Strategy Analyzer, here are some results I get in the NinajaScript Output window:
    2/18/2021 3:20:00 PM - 3916.75 - 3916.5 - 1.00006383250351
    2/18/2021 3:25:00 PM - 3915.25 - 3915 - 1.00006385696041
    2/18/2021 3:30:00 PM - 3913.5 - 3913.5 - 1
    2/18/2021 3:35:00 PM - 3914.75 - 3915 - 0.999936143039591
    2/18/2021 3:40:00 PM - 3915.5 - 3915.25 - 1.00006385288296
    2/18/2021 3:45:00 PM - 3915.75 - 3915.75 - 1
    2/18/2021 3:50:00 PM - 3916 - 3916 - 1
    2/18/2021 3:55:00 PM - 3912.75 - 3912.5 - 1.00006389776358

    For example, the last line of results above, when you divide 3912.75/3912.5, the answer should be 1.02 and not 1.00006389776358. I tried multiplying the numbers together and that appeared to provide the correct result, but when I divide I get the incorrect result and I have no clue why. If I just hard code those numbers into the Print line (3912.75/3912.5) I get 1.02, so it is only when I use the Open[0]/Close[1]. There is no errors in the Log as this is the only line of code in the strategy. I also tried closing and restarting NinjaTrader and that did not fix the issue either.

    Any assistance would be greatly appreciated.


    #2
    Hello vizts, thanks for your post and welcome to the NinjaTrader forum.

    I plugged it into a calculator and:
    3912.75/3912.5 = 1.00006 not 1.02

    If you want to get rid of the insignificant figures in a decimal number, you can use RoundToTickSize() on the number:

    https://ninjatrader.com/support/help...toticksize.htm

    Please let me know if I can assist any further.

    Comment


      #3

      I'm very new to programming and automated trading, but I am working to automate a trading system I have developed in the NT8 Strategy Builder. One thing I would like to include in my system an automated position sizing calculator. This would calculate based on the difference between two moving averages and then factoring in account equity and % risk. I am unsure how to go about this. I am open to learning to code in C# if that is what this would require. If someone could offer some direction I would appreciate it. Thanks!

      Comment


        #4
        Hi Rmiles, thanks for your post.

        A position sizing calculator would need to be coded by hand, that would not be possible through the builder. All strategies come initialized with an Account object where you can get account data values:
        https://ninjatrader.com/support/help...gy_account.htm

        Best regards,
        -ChrisL

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        20 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        120 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        63 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        45 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X