Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculating Risk on Forex Pairs

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

    Calculating Risk on Forex Pairs

    Hello,

    Developing an indicator that will give the position size of a given stop size via the Risk Reward tool. So far it works with the futures market. However, with some forex pairs, it does not. I'm using babypips to get the risk. I noticed on the forex pairs that the indicator doesn't work are the ones that ask for the current price of said pair. The indicator pulls data from ChartControl.Instrument.MasterInstrument and uses those values to calculate the position size. Down below are some examples to explain the problem.

    USD/JPY
    https://i.imgur.com/BvzgNr4.png
    https://i.imgur.com/dtHbsVi.png
    https://i.imgur.com/oAE05G4.png

    EUR/USD
    https://i.imgur.com/TBSbGhx.png
    https://i.imgur.com/Vd0WbNu.png
    https://i.imgur.com/6ivUKbT.png

    USD/CAD
    https://i.imgur.com/eCG6Hxp.png
    https://i.imgur.com/JI2Oesm.png
    https://i.imgur.com/6ehcQu0.png

    Is there a variable I'm forgetting to take into consideration?

    Thanks
    Unsuitable
    NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

    #2
    Hello Unsuitable,

    Thank you for your post.

    Please answer the following questions so we may accurately assist.
    • What specific NinjaScript property is returning an incorrect value?
    • What value are you expecting to see?
    • What data feed provider are you connected to?
    Thanks in advance, I look forward to assisting further.

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Bradon,

      What specific NinjaScript property is returning an incorrect value?
      The incorrect value is the unit size. On the chart, it's under "Contract Size". On BabyPips it's under Position Size (units). These values should be the same. According to BabyPips, the forex pairs that require the current price do not output the same unit size.

      What value are you expecting to see?
      I'm expecting the indicator to give the same position size as BabyPips.

      What data feed provider are you connected to?
      The forex feed is connected to FXCM. Perhaps is calculated differently? I don't know.

      Thanks
      Unsuitable
      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

      Comment


        #4
        Hello Unsuitable,

        Thank you for that information.

        I am not aware of a UnitSize NinjaScript property. Are you referring to something general and not referring to specific code.

        Is the unit size being coded in NinjaScript, is this a property on the chart, or is this an inquiry about the drawing tool properties window?

        If this is being coded in NinjaScript, what code is being used to return the unit size?

        See the help guide documentation below for information about RiskReward and Draw.RiskReward.
        https://ninjatrader.com/support/help...riskreward.htm
        https://ninjatrader.com/support/help...riskreward.htm

        I look forward to assisting further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hello Brandon,

          Here is the code that gets the position size. Keep in mind the word unit size is specific to forex. For the futures its the contract size. In the code the variable name is contract size, this does not matter as it's just a name. The contractSize variable will give the contract size for futures or in the case of forex, the units.

          Code:
          double moneyPercentageRisk = userAccountSize * (PercentageRisk / 100);
          
          double pointValue = ChartControl.Instrument.MasterInstrument.PointValue;
          double tickValue = pointValue * tickSize;
          
          double contractSize = moneyPercentageRisk / (tickValue * ticksAtRisk);
          userAccountSize: The is the total value of the account, in the example, it's set to $10k
          PercentageRisk: A variable set by the user to determine how much risk based on a percentage, in the example it's set to 1%
          ticksAtRisk: This is the stop size that is obtained from a Risk Reward object

          All the numbers the indicator get's from the Risk Reward tool and the value of a tick on a given chart is correct. The problem is that I may be forgetting to consider some value when it comes to forex. For eur/usd it works. The indicator calculates the same unit position size as BabyPips. When it comes to usd/jpy or usd/cad, the indicator does not report the same unit position as Baby Pips. HOWEVER, when BabyPips is calculating the unit position size for said pairs, it asks for the current price. I don't know in what way it uses the current price.

          Unsuitable
          NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

          Comment


            #6
            Hello Unsuitable,

            Thank you for that information.

            If you are reporting a specific NinjaScript property, such as ChartControl.Instrument.MasterInstrument.PointValu e, that is returning an incorrect value, what is the name of the property?

            If you are requesting assistance with custom calculations and math logic, 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 in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners. It would be up to you to code this logic in your script.

            That being said, most Forex instruments have a 1 point value with a .00001 tick size. The USD/JPY instrument has a point value .01 with a .001 tick size. This might be why your custom calculations work for some Forex instruments but not others.

            PointValue - https://ninjatrader.com/support/help...pointvalue.htm

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Hello BrandonH,

              The values that NinjaTrader is providing (from ChartControl.Instrument.MasterInstrument.PointValu e) are correct. Yeah, originally I thought it has something to do with the way JPY is calculated, but the order of magnitude is correct so it's been accounted for. Also, this problem occurs with usd/cad so that rules out that theory. The position size of USD/JPY is only off by 4,501 units so it's really odd. Not really asking for custom code. I'm asking if forex takes into consideration some additional values beyond just the stop size and position size? Perhaps BabyPips is including the spread cost but why would eur/usd work perfectly.

              Anyway, if this is something you cannot help with I understand. Thanks for the help.
              Last edited by Unsuitable; 02-25-2021, 04:59 PM.
              Unsuitable
              NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

              Comment


                #8
                Update

                I went on trading view and myfxbook to see what numbers they give. TradingView reports the same numbers that my indicator does, myfxbook does not. I really don't know what's going on.

                myfxbook: https://i.imgur.com/IOS1FHj.png
                trading view: https://i.imgur.com/uMnROoO.png
                indicator: https://i.imgur.com/eCG6Hxp.png
                Unsuitable
                NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                Comment


                  #9
                  Hello Unsuitable,

                  Thank you for your note.

                  I understand that this is unrelated to a specific NinjaScript property returning an incorrect value.

                  I am not aware of Forex instruments taking into consideration any additional values. Unfortunately, we would not be able to provide support regarding your custom calculations.

                  If you come across the issue where a NinjaScript property is returning incorrect values while working on your custom calculations, please let us know what the name of that property is and we will gladly assist.

                  Let us know if we may assist further.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    I actually have a script that calculates position size in FX. I planned on building an add-on, but never got around to it, as I just use it to plug in the code that does the work into various scripts I may be using.

                    Is your issue that you are not using the correct formulas, or something else?

                    I.e., to calculate position size, you need 4 separate formulas... and that is dependant on whether your base currency is in the pair your using, and which order they are in...

                    E.g., if account is in USD, and you are trading Eur/Usd, that is easy as the calculation is simply: ($Risk/Stop) * (1/pipSize)... where pipSize is 0.0001, unless its a JPY pair, then its 0.01.

                    It gets more complicated when the account has the match in the base currency.
                    E.g., if account is in USD, and you're trading Usd/Jpy, then the calculation is:
                    ((USD/JPY price*$Risk)/Stop) * (1/pipSize)

                    It gets even more complicated when account currency is present in neither the base or quote currency.

                    The formula is either:
                    (($Risk/exchangePrice)/StopSize) * (1/pipSize)
                    OR
                    (($Risk*exchangePrice)/StopSize) * (1/pipSize)

                    Which to use depends on if the quote currency leads or lags in the pairs that match your account currency...


                    Let me know if you need further explanation. Attached is an image of the first sizer I built, based on several pairs... its all hardcoded, and not convinient, as I kind of put the project down once I figured out how to do it, and figured I'd pick it back up once I finally got into learning how to create an add-on.

                    The second image, is just showing a separate one I built(pink text), that simply shows the unit size, based on trading a fixed dollar amount per trade(in this example it was $0.30 per trade), and based on a 12 period atr, on a 30 min chart.
                    Attached Files

                    Comment


                      #11
                      Hello forrestang,

                      Oh dear, it appears that this is more complicated than I originally thought. Based on your information, the currency itself and the exchange rate have to be considered. ARG!! Now that I think of it, I will have to set up a check for the account being in a different currency even for the futures. How does NT handle accounts in different currencies? Well, that's up to the broker I suppose. Does NT have any methods to get the exchange rate? Again this may be broker-specific.

                      Thanks, I have my work cut out for me haha
                      Unsuitable
                      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                      Comment


                        #12
                        Originally posted by Unsuitable View Post
                        Hello forrestang,

                        Oh dear, it appears that this is more complicated than I originally thought. Based on your information, the currency itself and the exchange rate have to be considered. ARG!! Now that I think of it, I will have to set up a check for the account being in a different currency even for the futures. How does NT handle accounts in different currencies? Well, that's up to the broker I suppose. Does NT have any methods to get the exchange rate? Again this may be broker-specific.

                        Thanks, I have my work cut out for me haha
                        Yes sir, it is not exactly straight forward with currencies.

                        Yes, you need to get the exchange rates(price for 7 different currencies), for each particular BASE currency you plan on using. So if you're interested in a USD conversion(how many units to by if you're trading in USD)... you need to add seven data series. You'll notice you have to add in EACH pair that contains USD, like so in State==State.Configure:
                        Code:
                        AddDataSeries("AUDUSD");  //INDEX OF 1
                        AddDataSeries("EURUSD");  //INDEX OF 2
                        AddDataSeries("GBPUSD");  //INDEX OF 3
                        AddDataSeries("NZDUSD");  //INDEX OF 4
                        AddDataSeries("USDCAD");  //INDEX OF 5
                        AddDataSeries("USDCHF");  //INDEX OF 6
                        AddDataSeries("USDJPY");   //INDEX OF 7

                        With the above, if you wanted to give the user the option to switch between THEIR OWN currencies, I.e., you wanted an indie that would work for those where their base in in GBP, or AUD, or whatever... you would need a different set of symbols. In setting this up, I was thinking about if it is possible to load different symbols, based on a user input(in State.Configure)... otherwise you'd have to load ALL 28 pairs, which would be unnecessary.

                        After you have all the prices...

                        THEN you can use the 4 basic formulas I described. Here is the layout, and this is assuming desired conversion is for someone who is interested in a USD conversion... this would be different for people wanting different conversions... This is a bit difficult to explain, but I'll try my best. I actually spent a TON of time figuring this out, as it seems all the initial explanations I could find where somewhat confusing.

                        CASE 1:
                        Assuming the pair is Eur/Usd... note ACCOUNT currency(usd) is SECOND(eur/USD):
                        Code:
                        lotSize = Convert.ToInt32( (myRisk/myStop) * (1/decimalVal) ); //decimalVal  is the pipsize(0.0001 for non-jpy pairs, 0.01 for jpy pairs)


                        CASE 2:
                        Assuming the pair is Usd/Jpy... note ACCOUNT currency(usd) is FIRST(USD/jpy):
                        Code:
                        lotSize = Convert.ToInt32( ((Closes[7][0] * myRisk) / myStop) * (1/decimalVal)  );
                        ^notice here, b/c of JPY, i'm multiplying by the SEVENTH INDEX.... noted where I added the dataSeries




                        CASE 3:
                        Assuming the pair is Eur/Gbp... note ACCOUNT currency(usd) and is in NEITHER the base or quote currency, First OR Second.
                        Here... the forumula is dependant on weather the SECOND symbol(Gbp in this case), matches the dataseries added. Looking at the series we added, we need to use Gbp/Usd, or the THIRD index. Notice the Gbp comes FIRST in the dataSeries section above, and SECOND in the symbol we are converting(Eur/Gbp).
                        Code:
                        lotSize = Convert.ToInt32( ((myRisk/Closes[3][0])/myStop) * (1/decimalVal) );



                        CASE 4:
                        Assuming the pair is Chf/Jpy... note ACCOUNT currency(usd) and is in NEITHER the base or quote currency, First OR Second.
                        Here... the forumula is dependant on weather the SECOND symbol(Jpy in this case), matches the dataseries added. Looking at the series we added, we need to use Usd/Jpy, or the SEVENTH index. Notice the Jpy comes SECOND in the dataSeries section above, and SECOND in the symbol we are converting(Chf/Jpy)... opposite of case 3
                        Code:
                        lotSize = Convert.ToInt32( ((myRisk*Closes[7][0])/myStop) * (1/decimalVal) );



                        So... all of this is probably confusing as I have explained it? And this is only for the user being interested in the USD conversion... but the others can be derived.

                        Other examples of which pairs(for the USD conversion) would fit into each case....
                        Case1: Eur/Usd, Aud/Usd, Gbp/Usd, Nzd/Usd
                        Case2: Usd/Cad, Usd/Chf, Usd/Jpy
                        Case3: Eur/Gbp, Aud/Cad, etc...
                        Case4: Chf/Jpy, etc...
                        Last edited by forrestang; 02-26-2021, 11:36 AM.

                        Comment


                          #13
                          Also of note... there may be a simpler way of doing this... I just didn't work it out at the time.

                          Comment


                            #14
                            Hello forrestang,

                            Thanks for all the info. Now that I know what to look for, figuring out a solution will be easier. I'll report back if I find a good way
                            Unsuitable
                            NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                            Comment


                              #15
                              Hello forrestang,

                              So if I understand if the account is based in USD

                              EUR/USD: No conversion needed
                              USD/CAD: Multiply the position size by the USD/CAD
                              CAD/CHF: Find the forex pair that has USD and CHF. This case USD/CHF. Since USD is the base pair, multiply the positions size by USD/CHF
                              EUR/GBP: Find the forex pair that has USD and GBP. This case GBP/USD. Since USD is the quote pair, divide the positions size by GBP/USD

                              As mentioned, other sets of data series need to be added if the account is in a different currency. For now, USD will be the only thing supported with others added later. There should be a check if the USD data sets should be added in the first place, in case that it's not forex and its futures. Hmm, what about futures that are not based in USD? Well all CME instruments are based in USD no?

                              I guess all currencies have to be added but programmatically only the ones required should be added which will be interesting to figure out. Maximum 2 data series should be added at a time.

                              Thanks again for the help! Was shooting in the dark as seemingly no websites to calculate position talk about it. They say, "use this calculator to figure it out".
                              Unsuitable
                              NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sinpeople, Today, 11:15 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post sinpeople  
                              Started by neshnix89, Today, 10:27 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bosajin, Today, 09:51 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post bltdavid  
                              Started by Newant, Today, 08:13 AM
                              2 responses
                              26 views
                              0 likes
                              Last Post Newant
                              by Newant
                               
                              Started by alejandro888, Yesterday, 10:14 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Working...
                              X