MACD金叉做多策略:自动交易补仓与锁仓
本策略使用魔术码'92133',在MACD金叉时开仓多单,初始手数为0.5手。
- 价格上涨100微点以上,以持仓第一个订单手数的0.6倍进行补仓做多。
- 如果当前价格低于第一个订单100微点以下,则以0.5手数进行锁仓做空。
- 价格下跌100微点,以持仓第一个订单手数的0.6倍进行补仓做空。
- 当多空总盈利达到40美金时,自动平仓。
以下是将您的设定转换为MQ4(MetaQuotes 4的编程语言)的示例代码:
// 定义常量
#define MAGIC_NUMBER 92133
#define INITIAL_LOTS 0.5
#define PROFIT_TARGET 40
#define MIN_FLUCTUATION MarketInfo(Symbol(), MODE_POINT) * 100
// 定义变量
double currentLots = 0;
double totalProfit = 0;
void OnTick()
{
// 检查当前魔术码下的持仓是否为0
bool hasPosition = false;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
{
hasPosition = true;
break;
}
}
// MACD金叉做多
if (!hasPosition && iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0) > iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 0))
{
// 开仓多单
currentLots = INITIAL_LOTS;
int ticket = OrderSend(Symbol(), OP_BUY, currentLots, Ask, Slippage, 0, 0, 'Buy Order', MAGIC_NUMBER);
if (ticket > 0)
{
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
}
// 补仓多单
if (hasPosition && OrderType() == OP_BUY && OrderMagicNumber() == MAGIC_NUMBER && MarketInfo(Symbol(), MODE_ASK) > OrderOpenPrice() + MIN_FLUCTUATION)
{
int totalOrders = 0;
double averagePrice = 0;
double totalLots = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == OP_BUY)
{
totalOrders++;
averagePrice += OrderOpenPrice() * OrderLots();
totalLots += OrderLots();
}
}
if (totalLots > 0)
{
averagePrice /= totalLots;
}
if (totalOrders > 0 && MarketInfo(Symbol(), MODE_ASK) - averagePrice > MIN_FLUCTUATION * 100)
{
currentLots = OrderLots() * 0.6;
int ticket = OrderSend(Symbol(), OP_BUY, currentLots, Ask, Slippage, 0, 0, 'Buy Order', MAGIC_NUMBER);
if (ticket > 0)
{
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
}
}
// 锁仓做空
if (hasPosition && OrderType() == OP_BUY && OrderMagicNumber() == MAGIC_NUMBER && MarketInfo(Symbol(), MODE_ASK) < OrderOpenPrice() - MIN_FLUCTUATION)
{
// 开仓空单
currentLots = INITIAL_LOTS;
int ticket = OrderSend(Symbol(), OP_SELL, currentLots, Bid, Slippage, 0, 0, 'Sell Order', MAGIC_NUMBER);
if (ticket > 0)
{
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
}
// 补仓做空
if (hasPosition && OrderType() == OP_SELL && OrderMagicNumber() == MAGIC_NUMBER && MarketInfo(Symbol(), MODE_BID) < OrderOpenPrice() - MIN_FLUCTUATION)
{
int totalOrders = 0;
double averagePrice = 0;
double totalLots = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == OP_SELL)
{
totalOrders++;
averagePrice += OrderOpenPrice() * OrderLots();
totalLots += OrderLots();
}
}
if (totalLots > 0)
{
averagePrice /= totalLots;
}
if (totalOrders > 0 && averagePrice - MarketInfo(Symbol(), MODE_BID) > MIN_FLUCTUATION * 100)
{
currentLots = OrderLots() * 0.6;
int ticket = OrderSend(Symbol(), OP_SELL, currentLots, Bid, Slippage, 0, 0, 'Sell Order', MAGIC_NUMBER);
if (ticket > 0)
{
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
}
}
// 计算持仓盈利
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)
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
{
OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage);
}
}
}
}
请注意,以上代码仅作为示例,可能需要根据您的具体需求进行修改。在使用之前,请务必进行充分的回测和验证。如果您对MQ4编程语言不熟悉,建议参考MetaTrader 4的官方文档和资源进行学习。
原文地址: https://www.cveoy.top/t/topic/bPIR 著作权归作者所有。请勿转载和采集!