#property copyright Copyright 2023 AI Trading Expert#property link httpswwwexamplecom#property version 100#property strict 外部参数extern double Lots = 001;extern int StopLoss = 500;extern int Take
// 外部参数 extern double Lots = 0.01; // 手数 extern int StopLoss = 500; // 止损点数 extern int TakeProfit = 1000; // 止盈点数 extern int ADX_Period = 14; // ADX指标周期 extern int MACD_FastEMA = 12; // MACD快速指数移动平均线周期 extern int MACD_SlowEMA = 26; // MACD慢速指数移动平均线周期 extern int MACD_SignalEMA = 9; // MACD信号线移动平均线周期 extern int RSI_Period = 14; // RSI指标周期 extern int RSI_UpperLevel = 70; // RSI上限 extern int RSI_LowerLevel = 30; // RSI下限
// 计算点数 double point;
// 交易信号 enum TradeSignal { SELL, // 卖出信号 BUY, // 买入信号 NONE // 无信号 };
// 初始化 int OnInit() { // 设置点数 point = Point; if (Digits == 3 || Digits == 5) point *= 10;
return (INIT_SUCCEEDED);
}
// 每个tick处理 void OnTick() { // 获取当前时间 datetime currentTime = TimeCurrent();
// 每30秒执行一次
if (currentTime % 30 != 0)
return;
// 获取交易信号
TradeSignal signal = GetTradeSignal();
// 执行交易
ExecuteTrade(signal);
}
// 获取交易信号 TradeSignal GetTradeSignal() { double adx = iADX(Symbol(), PERIOD_H1, ADX_Period, PRICE_CLOSE, MODE_MAIN, 0); // 计算ADX指标值 double macdMain = iMACD(Symbol(), PERIOD_H1, MACD_FastEMA, MACD_SlowEMA, MACD_SignalEMA, PRICE_CLOSE, MODE_MAIN, 0); // 计算MACD主线值 double macdSignal = iMACD(Symbol(), PERIOD_H1, MACD_FastEMA, MACD_SlowEMA, MACD_SignalEMA, PRICE_CLOSE, MODE_SIGNAL, 0); // 计算MACD信号线值 double rsi = iRSI(Symbol(), PERIOD_H1, RSI_Period, PRICE_CLOSE, 0); // 计算RSI指标值
if (adx > 25 && macdMain > macdSignal && rsi < RSI_LowerLevel)
return BUY;
if (adx > 25 && macdMain < macdSignal && rsi > RSI_UpperLevel)
return SELL;
return NONE;
}
// 执行交易 void ExecuteTrade(TradeSignal signal) { if (signal == NONE) return;
bool hasBuyOrder = false;
bool hasSellOrder = false;
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == 0)
{
if (OrderType() == OP_BUY)
hasBuyOrder = true;
if (OrderType() == OP_SELL)
hasSellOrder = true;
}
}
}
// 开仓条件:无其他订单,并且满足买入信号
if (signal == BUY && !hasBuyOrder&& !hasSellOrder)
{
double openPrice = NormalizeDouble(Ask, Digits); // 获取当前卖价作为开仓价格
double stopLossPrice = NormalizeDouble(openPrice - StopLoss * point, Digits); // 计算止损价
double takeProfitPrice = NormalizeDouble(openPrice + TakeProfit * point, Digits); // 计算止盈价
int ticket = OrderSend(Symbol(), OP_BUY, Lots, openPrice, 3, stopLossPrice, takeProfitPrice, "AI Trading Expert Buy", 0, 0, Blue); // 下单买入
if (ticket > 0)
{
Comment("AI Trading Expert: 成功开仓多单,手数:", Lots, ",开仓价格:", DoubleToString(openPrice));
}
else
{
Print("AI Trading Expert: 开仓多单失败,错误代码:", GetLastError());
}
}
// 开仓条件:无其他订单,并且满足卖出信号
if (signal == SELL && !hasSellOrder && !hasBuyOrder)
{
double openPrice = NormalizeDouble(Bid, Digits); // 获取当前买价作为开仓价格
double stopLossPrice = NormalizeDouble(openPrice + StopLoss * point, Digits); // 计算止损价
double takeProfitPrice = NormalizeDouble(openPrice - TakeProfit * point, Digits); // 计算止盈价
int ticket = OrderSend(Symbol(), OP_SELL, Lots, openPrice, 3, stopLossPrice, takeProfitPrice, "AI Trading Expert Sell", 0, 0, Red); // 下单卖出
if (ticket > 0)
{
Comment("AI Trading Expert: 成功开仓空单,手数:", Lots, ",开仓价格:", DoubleToString(openPrice));
}
else
{
Print("AI Trading Expert: 开仓空单失败,错误代码:", GetLastError());
}
}
}
// 在程序卸载时进行清理 void OnDeinit(const int reason) { Comment("");
原文地址: https://www.cveoy.top/t/topic/cb8b 著作权归作者所有。请勿转载和采集!