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

risk calculating

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

    risk calculating

    Dear Support,

    I am programming an indicator that manages the prices of several instruments at the same time. I use the AddDataSeries() function to add instruments and I calculate different stoploss prices on these instruments. I would need a function that would determine the cost of the price distance for 1 unit trade on any instrument. I tried several methods, but none of them worked exactly on all instruments, so I would like to request a solution that gives me the same result as the risk incurrency when using ctrl+F4​. Do you have any solution?

    Br

    #2
    Hello wenn34,

    Thank you for your post.

    Can you clarify what you mean by "determine the cost of the price distance for 1 unit trade"?

    This Risk/Reward calculator on the Ecosystem may be a relevant example to what you are looking for:

    This is a port of the NinjaTrader 7 AdvancedRiskReward.v3 indicator. This indicator helps you define your entry, stop and target (1 or 3) visually on the chart with the help of horizontal lines, calculates your potential P/L ,unrealized P/L,Risk/Reward ratio and contracts sizing per account size. Once started click on the chart 3 times there […]


    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      When I use the Risk-Reward calculator on the ninjatrader charts (CTRL + F4) and I change it to Currency mode in the "Y value display unit" variable, it shows me the profit for the selected takeprofit distance. I need the same value in my indicator (for any instument) when I set 2 prices or a price distance in point as parameter of the function. I tried the PointValue value but it didn't worked correctly. Could you suggest another solution that would show the same value?

      Comment


        #4
        Hello wenn34,

        Thank you for your response.

        You could use PointValue from the instrument and multiply that by TickSize to get the price per tick. Then, you can take the difference between the two prices in question and divide that by TickSize to get the difference in ticks, and multiply that by whatever the calculated price per tick is.

        This forum post demonstrates getting the difference in ticks (this is for NT7 but the concept is the same for NT8):



        You can get the "TickValue" by multiplying PointValue by TickSize.





        Lastly, if needed you can use RoundToTickSize():



        Please let us know if you have any further questions.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Hello Gaby,

          I tested your solution and it worked well on most instruments, but I found exceptions. For example, on @NAS100, the two prices I want to calculate are 18140.88 and 18150.42. The risk-reward tool shows $0.95. The TickSize is 0.01, and the PointValue is 0.1 for this instrument. So, the calculated value is approximately $95. Could you please tell me how to solve this to work well on every instrument?
          Thank you

          Comment


            #6
            Hello wenn34,

            I recommend debugging using prints to see where the calculation is going wrong for this instrument.

            The prints should print every value used in the calculation and the result of the calculation so we can see where it arrived to the incorrectly calculated value.

            It is very important to include a text label for each value and for each comparison or mathematical operator in the print to understand what is being calculated.

            Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

            I am happy to assist you with analyzing the output from the output window.​
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Thank you for the response Gaby.

              I have prepared a short program that demonstrates the error. The printed line includes all important values, and the attached images show the contents of the output window as well as the cost measured on the chart.
              Code:
              protected override void OnBarUpdate()
                      {
                          if(CurrentBar == Bars.Count - 2){
                              
                              double point_value = Instrument.MasterInstrument.PointValue;
                              double tick_size = Instrument.MasterInstrument.TickSize;
                              double tick_value = point_value * tick_size;
                              double high = Highs[BarsInProgress][0];
                              double low = Lows[BarsInProgress][0];
                              double price_dist = (high - low);
                              int tick_dist = (int)Math.Round(price_dist / tick_size);
                              double result = tick_dist * tick_value;
                              Print(
                                  Instrument.FullName
                                  + "   High:" + high.ToString("F99").TrimEnd('0')
                                  + "   Low:" + low.ToString("F99").TrimEnd('0')
                                  + "   Point_value:" + point_value.ToString("F99").TrimEnd('0')
                                  + "   Tick_size:" + tick_size.ToString("F99").TrimEnd('0')
                                  + "   Tick_value:" + tick_value.ToString("F99").TrimEnd('0')
                                  + "   Price_dist:" + price_dist.ToString("F5").TrimEnd('0')
                                  + "   Tick_dist:" + tick_dist
                                  + "   Result:" + result.ToString("F99").TrimEnd('0')
                              );
                          }
                      }
              ​
              .
              Attached Files

              Comment


                #8
                Hello wenn34,

                Thank you for sharing.

                When testing your code, I am seeing the correct value (see attached screenshot)

                From your previous post, this value seems to be correct as well.

                the two prices I want to calculate are 18140.88 and 18150.42. The risk-reward tool shows $0.95. The TickSize is 0.01, and the PointValue is 0.1 for this instrument. So, the calculated value is approximately $95.
                Point value (0.1) * Tick Size (0.01) = 0.001 price per tick.
                18150.42 (High) - 18140.88 (Low) = 9.54 price distance.
                Price distance (9.54) / Tick Size (0.01) = 954 (difference in ticks).
                954 (difference in ticks) * 0.001 (Tick Size) = $0.954

                From your latest post, I am also showing the value in the output to be correct.

                Point value (1) * Tick Size (0.00001) = 0.00001 price per tick.
                1.08435 (High) - 1.08415 (Low) = 0.00002 price distance.
                Price distance (0.00002) / Tick Size (0.00001) = 20 (difference in ticks).
                20 (difference in ticks) * 0.00001 (Tick Size) = $0.00002

                Can you explain why you are expecting to see different values, such as $95 for the first example?

                I look forward to your response. ​
                Attached Files
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Dear Gaby,

                  My first calculation was based on another formula that I found in the forum you suggested. It worked for most instruments, but not for all, and the example was based on it. I have now rewritten the method to the one you suggested and you can see that the EURUSD gives an incorrect value. I would like to know why this formula does not work for all instruments: (price_distance / tick_size) * (point_value * tick_size)
                  You could use PointValue from the instrument and multiply that by TickSize to get the price per tick. Then, you can take the difference between the two prices in question and divide that by TickSize to get the difference in ticks, and multiply that by whatever the calculated price per tick is.


                  Please take a picture of the char that has the risk-reward tool on it and measures the distance between the high and low price of the last closed candle (in Currency mode), as it was on my picture.

                  Thank you
                  Last edited by wenn34; 03-20-2024, 08:49 AM.

                  Comment


                    #10
                    Hello wenn34,

                    Thank you for your response.

                    We were able to do some testing and figure out the difference. The formula for risk/reward is calculated differently for Forex instruments vs. futures.

                    For calculating this value on Forex instruments, try this:

                    Code:
                    double result = tick_dist * (tick_size * point_value * Account.All[0].ForexLotSize);
                    Attached is also sample code.

                    Please let me know if you have any further questions.
                    Attached Files
                    Gaby V.NinjaTrader Customer Service

                    Comment


                      #11
                      So will this be the correct solution and will it work on all instruments or do I need to test some more?

                      Code:
                      protected override void OnBarUpdate()
                              {
                                  if(CurrentBar == Bars.Count - 2){
                                      
                                      double point_value = Instrument.MasterInstrument.PointValue;
                                      double tick_size = Instrument.MasterInstrument.TickSize;
                                      double tick_value = point_value * tick_size;
                                      double high = Highs[BarsInProgress][0];
                                      double low = Lows[BarsInProgress][0];
                                      DateTime candle_time = Times[BarsInProgress][0];
                                      double price_dist = (high - low);
                                      int tick_dist = (int)Math.Round(price_dist / tick_size);
                                      double result = tick_dist * tick_value * (Instrument.GetInstrumentType(Instrument.FullName) == InstrumentType.Forex ? Account.All[0].ForexLotSize : 1.0);
                                      Print(
                                          Instrument.FullName
                                          + "   Time:" + candle_time.ToString()
                                          + "   High:" + high.ToString("F10").TrimEnd('0')
                                          + "   Low:" + low.ToString("F10").TrimEnd('0')
                                          + "   Point_value:" + point_value.ToString("F10").TrimEnd('0')
                                          + "   Tick_size:" + tick_size.ToString("F10").TrimEnd('0')
                                          + "   Tick_value:" + tick_value.ToString("F10").TrimEnd('0')
                                          + "   Price_dist:" + price_dist.ToString("F10").TrimEnd('0')
                                          + "   Tick_dist:" + tick_dist
                                          + "   Result:" + result.ToString("F10").TrimEnd('0')
                                          + "   Type:" + Instrument.GetInstrumentType(Instrument.FullName)
                                      );
                                  }
                              }​

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by carnitron, Today, 08:42 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post carnitron  
                      Started by strategist007, Today, 07:51 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post strategist007  
                      Started by StockTrader88, 03-06-2021, 08:58 AM
                      44 responses
                      3,974 views
                      3 likes
                      Last Post jhudas88  
                      Started by rbeckmann05, Today, 06:48 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post rbeckmann05  
                      Started by rhyminkevin, Today, 04:58 PM
                      4 responses
                      58 views
                      0 likes
                      Last Post dp8282
                      by dp8282
                       
                      Working...
                      X