Convert function average price MQL4 to MQL5
Convert function average price MQL4 to MQL5
Loading saved threads...
Tim · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Tim
Original post: https://stackoverflow.com/questions/78131272
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
My code below calculate average price in MQL4 and it run ok How to re-write it in MQL5.
void getAveragePrice()
{
double sum_lot_b = 0;
double sum_price_b = 0;
double sum_profit_b = 0;
double ave_price_b = 0;
double sum_lot_s = 0;
double sum_price_s = 0;
double sum_profit_s = 0;
double ave_price_s = 0;
int countBuy = 0;
int countSell = 0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == _Symbol)
{
double _profit = OrderProfit() + OrderSwap() + OrderCommission();
if(OrderType() == OP_BUY)
{
sum_lot_b += OrderLots();
sum_price_b += OrderOpenPrice() * OrderLots();
sum_profit_b += _profit;
countBuy++;
}
if(OrderType() == OP_SELL)
{
sum_lot_s += OrderLots();
sum_price_s += OrderOpenPrice() * OrderLots();
sum_profit_s += _profit;
countSell++;
}
}
}
if(sum_price_b > DBL_EPSILON)
{
sum_price_b /= sum_lot_b;
ave_price_b = NormalizeDouble(((sum_profit_b)/(MathAbs(sum_lot_b*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits);
}
if(sum_price_s > DBL_EPSILON)
{
sum_price_s /= sum_lot_s;
ave_price_s = NormalizeDouble(((sum_profit_s)/(MathAbs(sum_lot_s*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits);
}
}
How to re-write in mql5.
How calculate profit in mql5.
double _profit = OrderProfit() + OrderSwap() + OrderCommission();
How to get order commission?
Quote
Report
Mark SdS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Mark SdS
Original post: https://stackoverflow.com/a/78135187
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Apparently, after reading some posts
in this thread
,
orderCommission
is available only
after
a deal is done.
Maybe you can get an indication of it by calculating the average in your trades history?
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Tim Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/78131272 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. My code below calculate average price in MQL4 and it run ok How to re-write it in MQL5. void getAveragePrice() { double sum_lot_b = 0; double sum_price_b = 0; double sum_profit_b = 0; double ave_price_b = 0; double sum_lot_s = 0; double sum_price_s = 0; double sum_profit_s = 0; double ave_price_s = 0; int countBuy = 0; int countSell = 0; for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == _Symbol) { double _profit = OrderProfit() + OrderSwap() + OrderCommission(); if(OrderType() == OP_BUY) { sum_lot_b += OrderLots(); sum_price_b += OrderOpenPrice() * OrderLots(); sum_profit_b += _profit; countBuy++; } if(OrderType() == OP_SELL) { sum_lot_s += OrderLots(); sum_price_s += OrderOpenPrice() * OrderLots(); sum_profit_s += _profit; countSell++; } } } if(sum_price_b > DBL_EPSILON) { sum_price_b /= sum_lot_b; ave_price_b = NormalizeDouble(((sum_profit_b)/(MathAbs(sum_lot_b*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits); } if(sum_price_s > DBL_EPSILON) { sum_price_s /= sum_lot_s; ave_price_s = NormalizeDouble(((sum_profit_s)/(MathAbs(sum_lot_s*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits); } } How to re-write in mql5. How calculate profit in mql5. double _profit = OrderProfit() + OrderSwap() + OrderCommission(); How to get order commission?
Checking account access…