OrderCalcProfit
OrderCalcProfit
函数为当前账户计算利润,在当前的交易市场条件下,以参量为基础传递。函数使用交易操作的结构进行再评估,值是返回的当前账户的值。
bool OrderCalcProfit(
ENUM_ORDER_TYPE action, // 订单类型 (ORDER_TYPE_BUY 或者 ORDER_TYPE_SELL)
string symbol, // 交易品种名称
double volume, // 交易量
double price_open, // 开盘价
double price_close, // 收盘价
double& profit // 为获得利润值的变量
);参量
- action
[in] 订单类型,可以是 ENUM_ORDER_TYPE 项目里ORDER_TYPE_BUY 或 ORDER_TYPE_SELL值中的一个。
- symbol
[in] 交易品种名称。
- volume
[in] 交易操作成交量。
- price_open
[in] 开盘价。
- price_close
[in] 收盘价。
- profit
[out] 计算利润的变量值在函数成功执行后编写进去,估计利润值依据许多因素,并在不同的市场环境中加以区别。
返回值
如果成功,函数返回true,否则返回false。如果指定无效订单类型,函数返回false,为了获得错误信息,可以调用 GetLastError()。
示例:
#define VOLUME 1.0 // 订单交易量
#define DEVIATION 100 // 平仓价格之前的点数
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
string currency_profit=SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT); // 利润货币
double close_price=ChartPriceOnDropped(); // 价格坐标, 对应着使用鼠标拖曳脚本的点位, 用于平仓价格
//---
for(int i=0; i<=ORDER_TYPE_SELL; i++)
{
ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)i; // 订单类型: 0 - 买, 1 - 卖
double open_price=PriceOpenByOrderType(_Symbol, order_type); // 下单价格: 对于买 - Ask, 对于卖 - Bid
//--- 如果未获得开单价格, 继续循环
if(open_price==0)
continue;
//--- 如果平仓价格为0 (脚本不是通过拖曳到图表上而运行的), 计算价格
if(close_price==0)
close_price=(order_type==ORDER_TYPE_BUY ? open_price + DEVIATION * _Point : open_price - DEVIATION * _Point);
//--- 根据传入的参数计算当前账户和市场环境下的利润大小
double profit=0;
ResetLastError();
if(!OrderCalcProfit(order_type, _Symbol, VOLUME, open_price, close_price, profit))
{
Print("OrderCalcProfit() failed. Error ", GetLastError());
continue;
}
//--- 在日志中打印收到的利润值
PrintFormat("Estimated profit for %.2f %s position on %s with opening price of %.*f and closing price of %.*f: %.2f %s",
VOLUME, OrderTypeDescription(order_type), _Symbol, _Digits, open_price, _Digits, close_price, profit, currency_profit);
}
/*
结果:
Estimated profit for 1.00 Buy position on EURUSD with opening price of 1.10757 and closing price of 1.10881: 124.00 USD
Estimated profit for 1.00 Sell position on EURUSD with opening price of 1.10753 and closing price of 1.10881: -128.00 USD
*/
}
//+------------------------------------------------------------------+
//| 根据订单类型返回开单价格 |
//+------------------------------------------------------------------+
double PriceOpenByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
{
MqlTick tick={};
//--- 取得交易品种的最新价格
if(!SymbolInfoTick(symbol, tick))
{
Print("SymbolInfoTick() failed. Error ", GetLastError());
return 0;
}
//--- 返回根据订单类型的价格
switch(order_type)
{
case ORDER_TYPE_BUY : return(tick.ask);
case ORDER_TYPE_SELL : return(tick.bid);
default : return(0);
}
}
//+------------------------------------------------------------------+
//| 返回订单类型的描述 |
//+------------------------------------------------------------------+
string OrderTypeDescription(const ENUM_ORDER_TYPE order_type)
{
switch(order_type)
{
case ORDER_TYPE_BUY : return("Buy");
case ORDER_TYPE_SELL : return("Sell");
default : return("Unknown order type");
}
}