最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

finance - Writing a mql5 function that calculates the profit and loss of a position in cash - Stack Overflow

programmeradmin2浏览0评论

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...

发布评论

评论列表(0)

  1. 暂无评论