#define VOLUME 1.0 // 订单交易量
#define DEVIATION 100 // 用于设置挂单的距离
#define STOP_LIMIT 50 // 订单止损限价的距离
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
string currency=AccountInfoString(ACCOUNT_CURRENCY);
int array_types[8]={ORDER_TYPE_BUY,
ORDER_TYPE_SELL,
ORDER_TYPE_BUY_LIMIT,
ORDER_TYPE_SELL_LIMIT,
ORDER_TYPE_BUY_STOP,
ORDER_TYPE_SELL_STOP,
ORDER_TYPE_BUY_STOP_LIMIT,
ORDER_TYPE_SELL_STOP_LIMIT };
//--- 根据订单类型数组进行循环
for(int i=0; i<(int)array_types.Size(); i++)
{
//--- 根据订单类型, 取得 ORDER_TYPE_BUY 或 ORDER_TYPE_SELL 类型
ENUM_ORDER_TYPE type=MarketOrderByOrderType((ENUM_ORDER_TYPE)array_types[i]);
//--- 根据订单类型, 取得价格
double price=PriceByOrderType(_Symbol, type);
//--- 根据 'action' 中指定的订单类型取得所需的保证金金额
double margin=EMPTY_VALUE;
ResetLastError();
if(!OrderCalcMargin(type, _Symbol, VOLUME, price, margin))
{
Print("OrderCalcMargin() failed. Error ",GetLastError());
continue;
}
//--- 在日志中打印结果
PrintFormat("Margin required for %.2f %s order at price %.*f on %s symbol: %.2f %s", VOLUME, OrderTypeDescription((ENUM_ORDER_TYPE)i), _Digits, price, _Symbol, margin, currency);
}
/*
结果:
Margin required for 1.00 Buy order at price 1.31668 on GBPUSD symbol: 1316.68 USD
Margin required for 1.00 Sell order at price 1.31661 on GBPUSD symbol: 1316.61 USD
Margin required for 1.00 Buy Limit order at price 1.31568 on GBPUSD symbol: 1315.68 USD
Margin required for 1.00 Sell Limit order at price 1.31761 on GBPUSD symbol: 1317.61 USD
Margin required for 1.00 Buy Stop order at price 1.31768 on GBPUSD symbol: 1317.68 USD
Margin required for 1.00 Sell Stop order at price 1.31561 on GBPUSD symbol: 1315.61 USD
Margin required for 1.00 Buy Stop Limit order at price 1.31718 on GBPUSD symbol: 1317.18 USD
Margin required for 1.00 Sell Stop Limit order at price 1.31611 on GBPUSD symbol: 1316.11 USD
*/
}
//+------------------------------------------------------------------+
//| 返回买入或卖出订单类型 |
//+------------------------------------------------------------------+
ENUM_ORDER_TYPE MarketOrderByOrderType(ENUM_ORDER_TYPE type)
{
switch(type)
{
case ORDER_TYPE_BUY : case ORDER_TYPE_BUY_LIMIT : case ORDER_TYPE_BUY_STOP : case ORDER_TYPE_BUY_STOP_LIMIT :
return(ORDER_TYPE_BUY);
case ORDER_TYPE_SELL : case ORDER_TYPE_SELL_LIMIT : case ORDER_TYPE_SELL_STOP : case ORDER_TYPE_SELL_STOP_LIMIT :
return(ORDER_TYPE_SELL);
}
return(WRONG_VALUE);
}
//+------------------------------------------------------------------+
//| 根据订单类型返回开单价格 |
//+------------------------------------------------------------------+
double PriceByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
{
int digits=0;
double point=0;
MqlTick tick={};
//--- 取得交易品种每个点(Point)的值
ResetLastError();
if(!SymbolInfoDouble(symbol, SYMBOL_POINT, point))
{
Print("SymbolInfoDouble() failed. Error ", GetLastError());
return 0;
}
//--- 取得交易品种小数位(Digits)的值
long value=0;
if(!SymbolInfoInteger(symbol, SYMBOL_DIGITS, value))
{
Print("SymbolInfoInteger() failed. Error ", GetLastError());
return 0;
}
digits=(int)value;
//--- 取得交易品种的最新价格
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);
case ORDER_TYPE_BUY_LIMIT : return(NormalizeDouble(tick.ask - DEVIATION * point, digits));
case ORDER_TYPE_SELL_LIMIT : return(NormalizeDouble(tick.bid + DEVIATION * point, digits));
case ORDER_TYPE_BUY_STOP : return(NormalizeDouble(tick.ask + DEVIATION * point, digits));
case ORDER_TYPE_SELL_STOP : return(NormalizeDouble(tick.bid - DEVIATION * point, digits));
case ORDER_TYPE_BUY_STOP_LIMIT : return(NormalizeDouble(tick.ask + DEVIATION * point - STOP_LIMIT * point, digits));
case ORDER_TYPE_SELL_STOP_LIMIT : return(NormalizeDouble(tick.bid - DEVIATION * point + STOP_LIMIT * point, digits));
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");
case ORDER_TYPE_BUY_LIMIT : return("Buy Limit");
case ORDER_TYPE_SELL_LIMIT : return("Sell Limit");
case ORDER_TYPE_BUY_STOP : return("Buy Stop");
case ORDER_TYPE_SELL_STOP : return("Sell Stop");
case ORDER_TYPE_BUY_STOP_LIMIT : return("Buy Stop Limit");
case ORDER_TYPE_SELL_STOP_LIMIT : return("Sell Stop Limit");
case ORDER_TYPE_CLOSE_BY : return("Close By");
default : return("Unknown order type");
}
}
//+------------------------------------------------------------------+
|