Converting/ Calculating currencies as a function or Var ?

 
Hi all,

I am looking for a way (function) to get the equity in a selected (EA choice) currency or in the current trading base currency.
i.e. if my equity is $10,000 while trading EURUSD, I need to know it in Yens when trading the AUDJPY



Thanks,


James
 

I'm pretty sure I can answer your question with either the code I've already uploaded here or with a slight modification to it, but first I need your help to explain your question a little better because as written it doesn't make much sense to me.

Can you explain it differently, or use an example with made-up numbers just to show us what you mean?

 

Well, it all started when I wanted to know in advance, what is the maximum lots I can use to open a trade according to a rule
that the maximum risk I'll take is 10% of my current equity.


If I trade the EURUSD and the account is calculated in dollars, it simple.

Let's say, my equity is $6,000, so my highest risk to take is $600 (10%).

That means, if I want to place the stoploss 20 pips away, the maximum lots allowed here is 30=600/20

where:
30 = UNITS of $1 LOTS (3.0 LOTS in EURUSD)
600 = MAX_RISK (10% of my current equity)
20 = GAP_2_SL (The gap between the OpenPrice and the StopLoss)

Of course, I can take larger SL_GAP with less lots, or shorter SL_GAP with bigger leverage but at least I'll know where are my limits.

It is more complicated to figure that limit by yourself when there are multiple orders running on multiple currencies.
My biggest problem, is to calculate the accurate value of the pair I'm trading on ('GAP_2_SL' value) - in the same currency of the equity.

Hope that my example helped to understand the problem.

James
 

so you want a formula to calculate profit (or loss) as a function of symbol, lotsize and pip-distance or by solving it for lotsize a function of symbol, profit an pip distance that will give you the needed lot size, something like that:


double GetNeededLotSize(string symbol, double delta_profit, int delta_pips)


something like this? This should be easily possible. I have done this before, its a one liner including MODE_LOZSIZE, MODE_TICKSIZE and MODE_TICKVALUE, I will leave it to the next poster in this thread to write down this function because I cannot test it at the moment and i don't like to release untested code.

 
I know how to calculate the risk when the base currency is equal to the equity currency.
The problem is to know that value in the equity currency when you trade a 'non_equity_currency' pair.

Or in another way: If I'm going to risk 20 pips on the GBPCHF, how much dollars I risk here (assuming my equity is in dollars).

The trick is to know it all in advance.
I thought using OrderProfit() function, but for pending orders, it return zero !

James
 

Hi JJF, yes this code is already in the forum, see MY last post in this thread: https://www.mql5.com/en/forum/129553

(I'd link to the post itself directly but the forum search is badly broken and nothing I do in search will pull up the post you need to see...)

The code will do multiple things for you, it is symbol() agnostic, and it will compute the max lotsize to take for a position based on the equity you are willing to risk and the stoploss price. Once you have that lotsize value it can also compute for you the profit potential based on your takeprofit price.

It computes the crosses correctly as well, which is what you were after...however you need to be aware that you cannot correctly backtest on crosses because of a fundamental design limitation of MT4's backtester which prevents your EA from accessing the historical price data for other currency pairs during the backtest. In practice what this means is that all monetary valuations that come from trading cross-pairs in a backtest are fundamentally wrong, meaning the profit/loss of the trades themselves when converted into the account's currency by the backtester are simply wrong.

The code I linked to computes them correctly, analytically, no shortcuts taken. But they can't be used in backtesting on crosses because the code will try and calculate market valuations correctly and the backtester will prevent it from doing so and as such the code will simply return error alerts (as it would in real-life forward testing if the broker somehow broke their currency definitions).

 
Thanks Philip,

It seem very impressive.
It will take me some time to embed it into my indicators and EAs

Thank you for now,

James
Reason: