Writing a mql5 function that calculates the profit and loss of a position in cash

Writing a mql5 function that calculates the profit and loss of a position in cash

Manage alerts

Loading saved threads...

Peiman Ghasemi · External communityPost link
External question — Stack Overflow Stack Exchange Author: Peiman Ghasemi Original post: https://stackoverflow.com/questions/79421135 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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 "https://twitter.com/PeimanGhasemi" #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...
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Peiman Ghasemi Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/79421135 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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 "https://twitter.com/PeimanGhasemi" #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...

Cancel quote

Checking account access…