MT4 指标代码:添加 MACD 指标到副图表
在上述代码中添加副图表加载 MACD 指标的部分,可以使用iMACD函数来计算和绘制 MACD 指标。以下是更新后的代码:
//+------------------------------------------------------------------+
//| 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
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// 初始化副图表
IndicatorShortName('MACD');
IndicatorBuffers(3);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexStyle(2, DRAW_LINE);
SetIndexLabel(0, 'MACD');
SetIndexLabel(1, 'Signal');
SetIndexLabel(2, 'Histogram');
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
// 定义指标缓冲区
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
//+------------------------------------------------------------------+
// 定义常量
#define MAGIC_NUMBER 92133
#define INITIAL_LOTS 0.5
#define REPLENISH_MULTIPLIER 0.6
#define REPLENISH_ATTEMPTS 5
#define PROFIT_TARGET 40
#define MIN_FLUCTUATION MarketInfo(Symbol(), MODE_POINT) * 100
// 定义变量
double currentLots = 0;
double totalProfit = 0;
// 定义函数:开仓多单
bool OpenBuyOrder(double lots)
{
int ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 0, 0, 'Buy Order', MAGIC_NUMBER);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
currentLots = OrderLots();
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send buy order');
}
return false;
}
// 定义函数:开仓空单
bool OpenSellOrder(double lots)
{
int ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 0, 0, 'Sell Order', MAGIC_NUMBER);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
currentLots = OrderLots();
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send sell order');
}
return false;
}
// 定义函数:补仓多单
bool ReplenishBuyOrder(double lots)
{
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() * REPLENISH_MULTIPLIER;
int ticket = OrderSend(Symbol(), OP_BUY, currentLots, Ask, 0, 0, 'Buy Order', MAGIC_NUMBER);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
currentLots = OrderLots();
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send buy order');
}
}
return false;
}
// 定义函数:锁仓空单
bool LockSellOrder()
{
int ticket = OrderSend(Symbol(), OP_SELL, INITIAL_LOTS, Bid, 0, 0, 'Sell Order', MAGIC_NUMBER);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
currentLots = OrderLots();
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send sell order');
}
return false;
}
// 定义函数:补仓空单
bool ReplenishSellOrder(double lots)
{
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() * REPLENISH_MULTIPLIER;
int ticket = OrderSend(Symbol(), OP_SELL, currentLots, Bid, 0, 0, 'Sell Order', MAGIC_NUMBER);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
currentLots = OrderLots();
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send sell order');
}
}
return false;
}
// 定义函数:平仓逻辑
void CloseOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
{
if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0))
{
Print('Closed order with ticket ', OrderTicket());
}
else
{
Print('Failed to close order with ticket ', OrderTicket());
}
}
}
}
void OnTick()
{
// 检查当前魔术码下的多单持仓是否为0
bool hasLongPosition = false;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == OP_BUY)
{
hasLongPosition = true;
break;
}
}
// MACD金叉的时候做多
if (!hasLongPosition)
{
OpenBuyOrder(INITIAL_LOTS);
}
// 绘制MACD指标
int macdHandle = iMACD(Symbol(), PERIOD_CURRENT, 12, 26, 9, PRICE_CLOSE);
if (macdHandle != INVALID_HANDLE)
{
CopyBuffer(macdHandle, 0, 0, 100, ExtMapBuffer1);
CopyBuffer(macdHandle, 1, 0, 100, ExtMapBuffer2);
CopyBuffer(macdHandle, 2, 0, 100, ExtMapBuffer3);
}
CloseOrders();
return;
}
在OnInit函数中,我们添加了指标相关的设置,定义了三个指标缓冲区用于绘制 MACD 指标的线和直方图。
在OnTick函数中,我们使用iMACD函数计算 MACD 指标,并使用CopyBuffer函数获取指标的值,并将其存储在指标缓冲区中。这样,我们就可以在副图表中绘制出 MACD 指标。
请确保您已经正确设置了指标的输入参数和图表属性。如果还有问题,请提供更多详细信息,以便我能够帮助您进一步调试。
原文地址: https://www.cveoy.top/t/topic/bTdP 著作权归作者所有。请勿转载和采集!