Troubles Calculating Pip value in Forex

Questions about MultiCharts and user contributed studies.
fepra
Posts: 8
Joined: 08 Mar 2017
Has thanked: 3 times

Troubles Calculating Pip value in Forex

Postby fepra » 12 May 2018

Hello everyone. I'd want to create a code made up for the forex that'd let me risk a fixed amount of dollars ( I wish though it'd be modifiable in any currency, for example €) for each trade even if the pips 'size of the stoploss varies at each one. I already made an encoding attempt that brought to nothing but unexpected results. The scheme is the following:
1. Choosing the dollars value to risk into the operation.
2. Calculating the pips' size number of stoploss (differentiation between entryprice and stoploss level)
3. Dividing the risk in dollars for the stoploss pips then obtaining how many dollars I could risk for, for every pip.
4. Commuting this value in Lots.

The following is a practical example on the couple audcad :
0. These are the settings on QuoteManager:
PriceScale: 1/100000 (xxx/JPY = 1/1000)
MinMovement: 1
BigPointValue: 1
1. I want to risk 50$ at each operation.
2. I want to calculate the stoploss range in pips.
entryprice = 0.96918
Stoploss = 0.95942
entryprice - Stoploss = 0.00976 * 10000 = 97.6 pips
3. 50/97.6 = 0.5 cents at pips.
4. 0.5 $ cents at pips represent 0.05 Lots meaning 0.5 *10000 correspond to 5000 contracts.
Here's the code:

Code: Select all

vars: Rng(0), Risk(0), EntryLvl(0), StopLossLvl(0), PipsSL(0), SizeTrade(0);

Risk = InitialCapital * 0.05; //I decide how much I want to risk for each trade.
Rng = EntryLvl - StopLossLvl; //I calculate the pips number of pips of stoploss.
PipsSL = Rng * (PriceScale/10); //I try to make this number out of a whole number with a decimal place.
CentsAtPip = Round(Risk/PipsSL, 1); //I divide the risk for the pips' number obtaining how many dollars I could risk for, at every pip.
Then rounding up this number at the first decimal place.
SizeTrade = (CentsAtPip * 10) * 1000; //At the end I want to calculate the trade size multiplying the value by 10000.
Example: CentsAtPips = 0.7, SizeTrade = 0.7 * 10000 = 7000 which means 0.07 Lots
I set the take profit at the same distance of the stoploss from the entryprice. In this way I'm supposed to lose or gain 50$ at each trade. This happens though just for eurusd trades, it doesn't seem to be working with the couples having just one different currency from the usd at the denominator. Neither on the ones having usd at the denominator (like gbpusd, nzdusd, etc.). Probably the error is given by this operation : the value of the single pip in the couples having a different currency as denominator is not 10$ each 100000 contracts . I hoped MC would take care of it automatically. Does someone how to solve this problem? Thanks to whoever is going to help.

User avatar
Anna MultiCharts
Posts: 560
Joined: 14 Jul 2017
Has thanked: 42 times
Been thanked: 140 times

Re: Troubles Calculating Pip value in Forex

Postby Anna MultiCharts » 15 May 2018

Hello, fepra!

There’re pre-built special orders in PowerLanguage. They are able to calculate this on their own.
Please refer to the PowerLanguage Help to find the description and usage examples for SetStopLoss and SetProfitTarget, and other special orders starting with Set.
For them to work properly the symbol settings (Price Scale, Min Move and Big Point Value) in QuoteManager should be configured correctly.

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Troubles Calculating Pip value in Forex

Postby MAZINGUER » 15 May 2018

Hi fepra,
As you know, the value of a pip depends on the number of lots invested.
The currency pairs are expressed through the agreement: Base Currency / Quoted Currency
The calculation of the value of a pip can be divided into three categories:
a) Currency pairs that have the denominator the USD (the currency is the USD and the base currency is any other)
In this case the value of the pip is constant and does not depend on the current exchange rate.
Its value is calculated by the formula:
Pip value = (Lot size) x (Pip size / exchange rate) where the exchange rate is always the ask.
For example, the value of a pip for a lot and assuming that the price at a given moment is 1.2600, will be:
Pip value = 100000 € x (0.0001) / 1.26000) = 7.94 €
but of course, to obtain the value of the pip in dollars, we must multiply by the current price (which we said was 1.2600), therefore,
Pip value = 7.94 x 1.2600 = 10 $ (value of the pip in dollars)
As you can see if the value of the pip you want in the quoted currency (dollars) in the calculation of the value of a pip, it is first divided by the quotation and then multiplied ... so it is really annulled and the value of the pip does not depend on the quote
b) Currency pairs in which the USD is the numerator (the quoted currency is any one and the base currency is the USD)
In this case, the value of the pip will depend on the price of the pair.
To calculate the value of a pip, the same formula applies as before:
Pip value = (Lot size) x (Pip size / exchange rate)
In this case, it would not be necessary to multiply later by the exchange rate, since the base currency is directly the $
c) Crossed pairs (the USD is neither a base currency nor a listed currency)
The case of the calculation of the value of a pip for crossed pairs is a little more complicated but if you are interested, you tell me and I pass it to you too.

In your code, what you do is the other way around ... having a value of a given pip ... you want to calculate the size of the batch that corresponds to that value of pip.
Then you just have to clear in the formula,
Lot size = (Pip value x type change) / size of the pip

fepra
Posts: 8
Joined: 08 Mar 2017
Has thanked: 3 times

Re: Troubles Calculating Pip value in Forex

Postby fepra » 16 May 2018

Hi fepra,
As you know, the value of a pip depends on the number of lots invested.
The currency pairs are expressed through the agreement: Base Currency / Quoted Currency
The calculation of the value of a pip can be divided into three categories:
a) Currency pairs that have the denominator the USD (the currency is the USD and the base currency is any other)
In this case the value of the pip is constant and does not depend on the current exchange rate.
Its value is calculated by the formula:
Pip value = (Lot size) x (Pip size / exchange rate) where the exchange rate is always the ask.
For example, the value of a pip for a lot and assuming that the price at a given moment is 1.2600, will be:
Pip value = 100000 € x (0.0001) / 1.26000) = 7.94 €
but of course, to obtain the value of the pip in dollars, we must multiply by the current price (which we said was 1.2600), therefore,
Pip value = 7.94 x 1.2600 = 10 $ (value of the pip in dollars)
As you can see if the value of the pip you want in the quoted currency (dollars) in the calculation of the value of a pip, it is first divided by the quotation and then multiplied ... so it is really annulled and the value of the pip does not depend on the quote
b) Currency pairs in which the USD is the numerator (the quoted currency is any one and the base currency is the USD)
In this case, the value of the pip will depend on the price of the pair.
To calculate the value of a pip, the same formula applies as before:
Pip value = (Lot size) x (Pip size / exchange rate)
In this case, it would not be necessary to multiply later by the exchange rate, since the base currency is directly the $
c) Crossed pairs (the USD is neither a base currency nor a listed currency)
The case of the calculation of the value of a pip for crossed pairs is a little more complicated but if you are interested, you tell me and I pass it to you too.

In your code, what you do is the other way around ... having a value of a given pip ... you want to calculate the size of the batch that corresponds to that value of pip.
Then you just have to clear in the formula,
Lot size = (Pip value x type change) / size of the pip

Thank you for your useful and complete answer.
Thanks to your explanation I was able to code the code for pairs with the USD in number and those with the USD in the denominator.
Now I have to code the point c) Crossed pairs (the USD is neither based on currency nor a listed currency).
The only idea that came to my mind is this:
Let's say, for example, that I want to know how much is the value of a pip of AUDNZD for a Lot (100,000 contracts) .
I know that each pip corresponds to an increase or decrease of 10 NZD. So I have to find the corresponding value in $ of 10 NZD and to do this I use the NZDUSD current price. But how can I call this value in real time and, above all, can this method be used in the BackTest?
If you could pass me the solution I would be very grateful.

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Troubles Calculating Pip value in Forex

Postby MAZINGUER » 16 May 2018

Hi fepra
Cross pairs are the currency pairs in which the dollar is neither the base currency nor the currency traded.
Although the dollar is not part of the pair, the dollar influences the behavior of the prices of the cross pair. why? because buying EUR / JPY is equivalent to buying EUR / USD and USD / JPY at the same time
This is why crossed pairs tend to have a more expensive spread
In cross pairs, the value of the pip is not constant, since it depends on the value of the exchange rate (as with the currency pairs in which the USD is the numerator).
Actually, the calculation of the value of the pip, is done just as we did with case a) but being careful with the exchange rates, that is,
- If we have the account in euros, and we want the value of the pip in euros:
Pip value = (lot size) x (pip size / exchange rate)
where the exchange rate will be that of the cross pair
For example, if we have the EUR / NZD and we assume that it is currently trading at 2.5040, the value of the pip for a lot will be
Value of the pip = 100000 x (0.0001 / 2.5040) = 3.99 €
But of course, to obtain the value of the pip in dollars, we must multiply also by the current price of the EUR / USD (which we assume is currently 1.2600), therefore,
Pip value = 3.99 x 1.2600 = 5.03 $
The general formula to obtain the value of the pip in the crossed pairs would be:
Pip value = (lot size) x (pip size) x (exchange rate of the base currency against the dollar / exchange rate of the cross pair)
As in your case, you are interested in the batch size for a given pip value, then it would be:
Lot size = (Value of the pip x exchange rate of the crossed pair) / (size of the pip x exchange rate of the base currency with respect to the dollar)

fepra
Posts: 8
Joined: 08 Mar 2017
Has thanked: 3 times

Re: Troubles Calculating Pip value in Forex

Postby fepra » 16 May 2018

Thank you MAZINGUER, but I have a doubt: how do I recall the NZDUSD price if my Signal is entered on the EURNZD chart? Is there a special function to do it or am I forced to insert NZDUSD as data2 and then call Close of data2?

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Troubles Calculating Pip value in Forex

Postby MAZINGUER » 16 May 2018

But how can I call this value in real time and, above all, can this method be used in the BackTest?
To get the historical exchange rate for backtest, I think you'll have to assign the quote to close, open, high or low.
The smaller the TF used, the more it will adjust to the real price at a given time
Try the word IntraBarPersist:

Vars: Intrabarpersist Price (close);
how do I recall the NZDUSD price if my Signal is entered on the EURNZD chart? Is there a special function to do it or am I forced to insert NZDUSD as data2 and then call Close of data2?
If you intend to do it with simple programming I think that if you have to resort to two datas.
Surely using global variables there will be some other way but on that topic I can not help you


Return to “MultiCharts”