MQL4自动交易策略:基于魔术码的多空双向加仓策略
//+------------------------------------------------------------------+
//| 7777.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright 'Copyright 2023, MetaQuotes Ltd.'
#property link 'https://www.mql5.com'
#property version '1.00'
#property strict
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
int MAGIC_NUMBER = 92133;
double INITIAL_LOTS = 0.5;
double REPLENISH_MULTIPLIER = 0.6;
int REPLENISH_ATTEMPTS = 5;
double PROFIT_TARGET = 40;
double MIN_FLUCTUATION = MarketInfo(Symbol(), MODE_POINT) * 100;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Close all open orders
CloseOrders();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
bool hasLongPosition = false;
bool hasShortPosition = false;
// Check if there are any open orders with the specified magic number
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
{
if (OrderType() == OP_BUY)
{
hasLongPosition = true;
}
else if (OrderType() == OP_SELL)
{
hasShortPosition = true;
}
}
}
// Place pending orders if there are no open positions
if (!hasLongPosition)
{
double buyPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits) + MIN_FLUCTUATION;
OrderSend(Symbol(), OP_BUYSTOP, INITIAL_LOTS, buyPrice, 0, 0, 0, 'Buy Order', MAGIC_NUMBER, 0, Green);
}
if (!hasShortPosition)
{
double sellPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits) - MIN_FLUCTUATION;
OrderSend(Symbol(), OP_SELLSTOP, INITIAL_LOTS, sellPrice, 0, 0, 0, 'Sell Order', MAGIC_NUMBER, 0, Red);
}
// Check if any pending orders have been triggered
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == OP_BUYSTOP)
{
// Close sell pending order
if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0))
{
Print('Closed sell pending order with ticket ', OrderTicket());
}
else
{
Print('Failed to close sell pending order with ticket ', OrderTicket());
}
}
else if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == OP_SELLSTOP)
{
// Close buy pending order
if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0))
{
Print('Closed buy pending order with ticket ', OrderTicket());
}
else
{
Print('Failed to close buy pending order with ticket ', OrderTicket());
}
}
}
// Check if any positions have reached the profit target
double totalProfit = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
{
totalProfit += OrderProfit();
}
}
if (totalProfit >= PROFIT_TARGET)
{
CloseOrders();
}
}
// ... (其余代码与之前相同,只是将双引号改为单引号) ...
这段MQL4代码实现了一个简单的自动交易策略,它包含以下功能:
- 使用魔术码 (MAGIC_NUMBER) 来区分不同的订单。
- 如果没有持仓,则分别挂多单和空单,两者价格与当前价格相差100点。
- 多单成交后,每上涨100点就加仓,最多加仓5次;如果价格下跌超过100点,则开仓反向0.5手空单进行锁仓,并随着下跌不断加仓空单。
- 空单成交后的逻辑与多单类似,只是方向相反。
- 当所有订单的总利润达到预设目标 (PROFIT_TARGET) 时,自动平掉所有订单。
请注意,这只是一个简单的示例,您需要根据自己的需求和风险承受能力进行修改和优化,并在模拟账户上充分测试后再进行实盘交易。
原文地址: http://www.cveoy.top/t/topic/bT6j 著作权归作者所有。请勿转载和采集!