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

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.
    Chris L.NinjaTrader Customer Service

    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
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fiddich, Today, 05:25 PM
        0 responses
        3 views
        0 likes
        Last Post fiddich
        by fiddich
         
        Started by gemify, 11-11-2022, 11:52 AM
        6 responses
        803 views
        2 likes
        Last Post ultls
        by ultls
         
        Started by ScottWalsh, Today, 04:52 PM
        0 responses
        4 views
        0 likes
        Last Post ScottWalsh  
        Started by ScottWalsh, Today, 04:29 PM
        0 responses
        7 views
        0 likes
        Last Post ScottWalsh  
        Started by rtwave, 04-12-2024, 09:30 AM
        2 responses
        22 views
        0 likes
        Last Post rtwave
        by rtwave
         
        Working...
        X