📅  最后修改于: 2022-03-11 14:57:03.761000             🧑  作者: Mango
double realDouble(double x)
{
return NormalizeDouble(x,_Digits);
}
void OpenOrder(ENUM_ORDER_TYPE actiontype)
{
MqlTradeRequest mrequest; // To be used for sending our trade requests
MqlTradeResult mresult; // To be used to get our trade results
MqlRates mrate[]; // To be used to store the prices, volumes and spread of each bar
ZeroMemory(mrequest); // Initialization of mrequest structure
mrequest.action = TRADE_ACTION_DEAL; // immediate order execution
if (actiontype==ORDER_TYPE_BUY)
mrequest.price = NormalizeDouble(latest_price.ask,_Digits); // latest ask price
if (actiontype==ORDER_TYPE_SELL)
mrequest.price = NormalizeDouble(latest_price.bid,_Digits); // latest ask price
if (actiontype==ORDER_TYPE_BUY)
mrequest.tp = realDouble(realDouble(latest_price.ask)+realDouble(400/MathPow(10,_Digits ))); // Stop Loss
if (actiontype==ORDER_TYPE_SELL)
mrequest.tp = realDouble(realDouble(latest_price.bid)-realDouble(400/MathPow(10,_Digits ))); // Stop Loss
if (actiontype==ORDER_TYPE_BUY)
mrequest.sl = realDouble(realDouble(latest_price.ask)-realDouble(2500/MathPow(10,_Digits ))); // Stop Loss
if (actiontype==ORDER_TYPE_SELL)
mrequest.sl = realDouble(realDouble(latest_price.bid)+realDouble(2500/MathPow(10,_Digits ))); // Stop Loss
//mrequest.tp = NormalizeDouble(take_profit,_Digits); // Take Profit
mrequest.symbol = _Symbol; // currency pair
mrequest.volume = 0.01; // number of lots to trade
mrequest.magic = 99999; // Order Magic Number
mrequest.type = actiontype; // Buy Order
mrequest.type_filling = ORDER_FILLING_FOK; // Order execution type
mrequest.deviation=100; // Deviation from current price
//--- send order
OrderSend(mrequest,mresult);
// get the result code
if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
{
Alert("An order has been successfully placed with Ticket#:",mresult.order,"!!");
}
else
{
Alert("The order request could not be completed -error:",GetLastError());
ResetLastError();
return;
}
}