订单平仓后取盈亏的代码
平仓后想拿到盈亏数据,写了个函数,可以用于统计。分享下。
// 平仓后获取盈亏
void CloseOrderWithProfit(ulong ticket) {
if(!OrderSelect(ticket)) return;
double volume = OrderGetDouble(ORDER_VOLUME);
double openPrice = OrderGetDouble(ORDER_OPENPRICE);
double closePrice;
int type = OrderGetInteger(ORDER_TYPE);
if(type == OP_BUY) {
closePrice = Bid;
} else {
closePrice = Ask;
}
// 计算盈亏
double profit = (closePrice - openPrice) * volume * 100000; // 外汇标准手
// 平仓
if(type == OP_BUY) {
OrderClose(ticket, volume, closePrice, 3, clrGreen);
} else {
OrderClose(ticket, volume, closePrice, 3, clrRed);
}
Print("订单 #", ticket, " 已平仓,盈亏:", DoubleToString(profit, 2), " USD");
}全部回复(4)
发表回复
请先登录后再回复。
