MQL4挂单程序:设置有效期
以下是一个用MQL4编写的可以设置有效时间的挂单程序:
//+------------------------------------------------------------------+
//| SetExpiry.mq4|
//| Copyright © 2021, John Smith |
//| https://www.example.com |
//+------------------------------------------------------------------+
#property copyright 'Copyright © 2021, John Smith'
#property link 'https://www.example.com'
#property version '1.0'
#property strict
// Input parameters
input double Lots = 0.01; // Trading volume
input double StopLoss = 100; // Stop loss level
input double TakeProfit = 200; // Take profit level
input int MagicNumber = 1234; // Magic number for trades
input int Slippage = 3; // Maximum allowed slippage
input int Expiration = 3600; // Order expiration time in seconds
// Trading function
void trade()
{
// Calculate stop loss and take profit prices
double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double Point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double Sl = Bid - StopLoss * Point;
double Tp = Ask + TakeProfit * Point;
// Open a buy stop order
int ticket = OrderSend(_Symbol, OP_BUYSTOP, Lots, Ask + Point, Slippage, Sl, Tp, 'BuyStop', MagicNumber, 0, Green);
if (ticket == -1)
{
Print('OrderSend error: ', GetLastError());
}
else
{
Print('Buy stop order opened: ', OrderTicket());
bool result = OrderExpiration(ticket, Expiration);
if (result)
{
Print('Order expiration set to ', Expiration, ' seconds');
}
else
{
Print('OrderExpiration error: ', GetLastError());
}
}
}
// Init function
void OnInit()
{
// Allow live trading
if (!IsTradeAllowed())
{
Print('Live trading is not allowed');
return;
}
// Print input parameters
Print('Lots = ', Lots);
Print('StopLoss = ', StopLoss);
Print('TakeProfit = ', TakeProfit);
Print('MagicNumber = ', MagicNumber);
Print('Slippage = ', Slippage);
Print('Expiration = ', Expiration, ' seconds');
// Open a trade
trade();
}
// Deinit function
void OnDeinit(const int reason)
{
// Close all open orders
int total = OrdersTotal();
for (int i = total - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber)
{
Print('Closing order ', OrderTicket());
bool result = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Red);
if (!result)
{
Print('OrderClose error: ', GetLastError());
}
}
}
}
}
该程序会在当前价格上方设置一个买入挂单,买入量为输入参数中的'Lots',止损价格为当前价格减去输入参数中的'StopLoss'乘以点值,止盈价格为当前价格加上输入参数中的'TakeProfit'乘以点值。该挂单的有效期为输入参数中的'Expiration'秒。
在程序中,我们使用了OrderExpiration函数来设置挂单的有效期。该函数的第一个参数为挂单的订单号,第二个参数为有效时间(以秒为单位)。如果设置成功,该函数会返回true,否则返回false。如果要查询函数的错误代码,可以使用GetLastError函数。
在OnInit函数中,我们首先检查是否允许实盘交易。然后我们打印输入参数,并调用trade函数来开启一个买入挂单。在trade函数中,我们使用OrderSend函数来发送买入挂单,并在挂单成功开启后调用OrderExpiration函数来设置挂单的有效期。如果有任何错误发生,我们将使用Print函数来打印该错误的代码。在OnDeinit函数中,我们使用OrderClose函数来关闭所有挂单。我们将遍历所有已打开的订单,并检查订单是否为当前交易品种,并且其魔术数字是否与输入参数中的'MagicNumber'相同。如果是,我们将使用OrderClose函数来关闭该订单。
请注意,该程序仅用于演示目的。在实际交易中,请务必进行充分的测试和验证,以确保程序的正确性和稳定性。
原文地址: https://www.cveoy.top/t/topic/oGu3 著作权归作者所有。请勿转载和采集!