Calculating Volume
Calculating Volume
03 Mar 2013, 13:42
Hi,
I am having difficulty calculating Volume. I have programmed a solution for AUDUSD (x.xyzxz) (and this works for other currencies where there are 5 values after the decimal place) but this solution does not work for example with the EURJP (xzx.xyz) and USDJPY (xy.xyz).
I think I have to somehow factor in the Ask price to make the calculation valid for all currencies but I don't not know how to do this.
This is my solution (simplified) for AUDUSD, first some explanations:
- Risk is in EUR (but should also be possible for GBP, USD or whatever currency the trading account is in)
- riskInPips is calculated elsewhere, could be the size of a candle
- we have to convert from double to int
- we have to round off the lots/Volume somehow
A hardcoded version looks like this. Results are shown behind //:
riskInPips = 0.00123;
Risk = 500;
int lots = (int)(Risk/riskInPips); //406504
lots = lots/10000; lots = lots*10000; // 400000
Volume = lots;
Trade.CreateBuyLimitOrder(Symbol,Volume,entry,SL,TP,null);
Does anyone know how to do this better. If possible so that it works for all currency pairs and if possible for accounts in different currencies?
I would really appreciate your advice,
Thanks,
TraderM
Replies
TraderM
19 May 2013, 16:36
Hi,
This is the sort of calculation I needed.
// profit > 0 => Calculate Volume based on Risk and Pip value var risk = (int)(RiskPercent * Account.Balance / 100); var volumeOnRisk = (int)(risk * Symbol.Ask / (Symbol.PipSize * StopLoss));
I found this in the "Example Based on Martingale Robot".
Thanks for posting this!
TraderM
@TraderM
TraderM
10 Mar 2013, 15:35
RE:
Hi,
I have solved this as following:
// For xxx.yyy and x.yyyyy currencies listed in setVolumeConverter()
int lots = (int)(Risk/(riskInPips/volumeConverter));
lots = lots/10000; lots = lots*10000; // Round off using integer truncation
Volume = lots;
The volumeConverter is set when the robot starts using this: setVolumeConverter();
The setVolumeConverter() method looks like this:
private void setVolumeConverter()
{
switch (Symbol.Code)
{
// x.yyyyy currencies
case "EURUSD":volumeConverter = 1;
break;
case "USDCAD":volumeConverter = 1;
break;
case "EURCAD":volumeConverter = 1;
break;
case "GBPCHF":volumeConverter = 1;
break;
case "GBPUSD":volumeConverter = 1;
break;
case "EURAUD":volumeConverter = 1;
break;
case "AUDCAD":volumeConverter = 1;
break;
case "EURTRY":volumeConverter = 1;
break;
case "AUDUSD":volumeConverter = 1;
break;
case "GBPAUD":volumeConverter = 1;
break;
case "USDSGD":volumeConverter = 1;
break;
case "GBPCAD":volumeConverter = 1;
break;
case "USDCHF":volumeConverter = 1;
break;
case "EURGBP":volumeConverter = 1;
break;
case "NZDUSD":volumeConverter = 1;
break;
// xxx.yyy currencies
case "EURJPY":volumeConverter = 100;
break;
case "GBPJPY":volumeConverter = 100;
break;
case "USDJPY":volumeConverter = 100;
break;
case "CADJPY":volumeConverter = 100;
break;
case "AUDJPY":volumeConverter = 100;
break;
}
Print("volumeConverter: {0}", volumeConverter);
}
This solution is not particularly elegant. Maybe someone has a better solution?
TraderM
@TraderM