Writing an #include function for the forum members that calculates the profit and loss in cash$

GeraldMann

Trader
Nov 5, 2024
3
0
7
37
Hello guys. There are thousands of people here like me that would appreciate you that if you are an experienced programmer, if you could write an #include function in mql5 language for us that would calculate the profit and loss in cash for us. It's an essential function for all the profitable EAs to trade based on risk, but the symbol points asap.

You can write the include function to work for the community members here, like this:

CurrentSymbol.ProfitPerPoint (meaning cents per every change in one price unit)

(And loss =) CurrentSymbol.LossPerPoint

And also regarding opened positions with definitive SL Level and TP Level:

Position(i).TpProfit (meaning gaining cash)

(And loss =) Position(i).SlLoss


Can you make it over weekend holidays?
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,446
1,571
144
Odesa
www.earnforex.com
There's already a profit per point function for 99% of cases in MQL5:
MQL5:
SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE_LOSS)
For the remaining 1%, things get complicated.
However, you can always just take the position size calculating functions from our Position Sizer and adapt them to your needs.

Can you make it over weekend holidays?
:D:D:D
 
  • 👍
Reactions: GeraldMann

GeraldMann

Trader
Nov 5, 2024
3
0
7
37
Interesting. I would download and review how does it calculate the profit and loss in account's currency base. Also already I asked a programmer to write an include class for me... I would share it later...
It's functions are including:
MoneyRateOfOnePrecentLotPerPoint
Money.PositionSl
CalcSlOfTheLastPosition regarding currency base.
and CalcSlOfThePosition(i) regarding currency base.
 

GeraldMann

Trader
Nov 5, 2024
3
0
7
37

I wrote the first part of the code.​

I think that it's the first ever open-source EA that calculates the StopLoss in Cash!!!!!
It works nice:
MQL5:
#property copyright "Gerald Mann (P. Ghasemi) and other forum members of the earnforex.com community"
#property link      "https://twitter.com/PeimanGhasemi"
#property version   "1.00"
 
void OnTick()
{
   if (PositionsTotal() > 0)
   {
      double SymbolPointValue = SymbolInfoDouble(Symbol(), SYMBOL_POINT);
      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);
      long type = PositionGetInteger(POSITION_TYPE);
      double Price = PositionGetDouble(POSITION_PRICE_OPEN);
      double StopLossLevel = PositionGetDouble(POSITION_SL);
      double LotSize = PositionGetDouble(POSITION_VOLUME);
      //Calculations
      double Spent = LotSize * ContractSize;
      double Distance = Price - StopLossLevel;
      double PositionSlLossValueInCurrencyBaseValue = -(Spent * Distance);
 
      Print("EntryPrice:", EntryPrice);
      Print("StopLossLevel:", StopLossLevel);
      Print("Spent:", Spent);
      Print("PositionSlLossValueInCurrencyBaseValue:", PositionSlLossValueInCurrencyBaseValue);
   }
 
}
It calculates everything nice when one side of the pair is USD alike EURUSD. But when USD is not on any sides it calculates a wrong answer. Does anybody have any idea?