I would like to make an EA that calculates the loss in cash in the account's currency base. This function is considered as an essential function for all the profitable multi-currency trading EAs that they do trade based on risk, but the symbol points alas. I've already written the first part of the code. I think that it's the first ever open-source EA that calculates the StopLoss in Cash!!!!! Later we can add NetLossPerOnePrecentLotPerPoint into the code also, but at first, we should improve its calculation functions therefore it never gives us a wrong answer. It calculates everything perfectly right now when one side of the forex currency pairs is USD like EURUSD. But when USD is not on any sides then it calculates a wrong answer. Does anybody have any ideas to improve it? Here is the code:
#property copyright "Gerald Mann (P. Ghasemi)"
#property link ";
#property version "1.00"
void OnTick()
{
if (PositionsTotal() > 0)
{
double ContractSize = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_CONTRACT_SIZE);
// Check Handle
if(!PositionSelect(_Symbol))
{
PrintFormat("PositionSelect(%s) failed. Error %d",_Symbol, GetLastError());
return;
}
ResetLastError();
long ticket=PositionGetInteger(POSITION_TICKET);
//Check ticket
if(ticket==0)
{
PrintFormat("Failed to get %s position ticket. Error %d", _Symbol, GetLastError());
return;
}
// Position info
double EntryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double Price = PositionGetDouble(POSITION_PRICE_OPEN);
double StopLossLevel = PositionGetDouble(POSITION_SL);
double LotSize = PositionGetDouble(POSITION_VOLUME);
//Calculations
double Spent = LotSize * ContractSize;
double Distance = MathAbs(Price - StopLossLevel);
double PositionSlLossValueInCurrencyBaseValue = -(Spent * Distance);
Print("EntryPrice:", EntryPrice);
Print("StopLossLevel:", StopLossLevel);
Print("Spent:", Spent);
Print("PositionSlLossValueInCurrencyBaseValue:", PositionSlLossValueInCurrencyBaseValue);
}
}
I would like to see what you think...