//+------------------------------------------------------------------+
//| 网格交易策略参数 |
//+------------------------------------------------------------------+
input double LotSize = 0.1; // 每次交易的手数
input int MaxBuyOrders = 5; // 最大买单数
input int MaxSellOrders = 5; // 最大卖单数
input double GridDistance = 20; // 网格的距离,单位为点
input int Slippage = 3; // 最大滑点
input int StopLoss = 100; // 止损,单位为点
input int TakeProfit = 100; // 止盈,单位为点
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// 初始化信息
Print("网格交易EA已成功启动");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// 卸载时清理工作
Print("网格交易EA已卸载");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 获取当前的买单和卖单数量
int buyOrders = 0;
int sellOrders = 0;
// 计算当前持有的买单和卖单数量
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY) buyOrders++;
if (OrderType() == OP_SELL) sellOrders++;
}
}
// 获取当前价格
double askPrice = Ask;
double bidPrice = Bid;
// 检查买单数量是否少于最大买单限制
if (buyOrders < MaxBuyOrders)
{
// 执行一个新的买单
double openBuyPrice = askPrice + GridDistance * Point; // 设置网格间距
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, openBuyPrice, Slippage, openBuyPrice - StopLoss * Point, openBuyPrice + TakeProfit * Point, "Buy Grid", 0, 0, clrGreen);
if (ticket > 0)
{
Print("已成功开设买单,订单票号:", ticket);
}
else
{
Print("买单开设失败,错误代码:", GetLastError());
}
}
// 检查卖单数量是否少于最大卖单限制
if (sellOrders < MaxSellOrders)
{
// 执行一个新的卖单
double openSellPrice = bidPrice - GridDistance * Point; // 设置网格间距
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, openSellPrice, Slippage, openSellPrice + StopLoss * Point, openSellPrice - TakeProfit * Point, "Sell Grid", 0, 0, clrRed);
if (ticket > 0)
{
Print("已成功开设卖单,订单票号:", ticket);
}
else
{
Print("卖单开设失败,错误代码:", GetLastError());
}
}
// 监控所有订单,查看是否有订单已平仓,并立即补单
CheckAndReopenOrders();
}
//+------------------------------------------------------------------+
//| 检查并补开已平仓的订单 |
//+------------------------------------------------------------------+
void CheckAndReopenOrders()
{
// 遍历当前所有订单
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
// 选择订单
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
// 如果订单已经平仓,补充相同方向的订单
if (OrderCloseTime() != 0)
{
// 只在这里声明 ticket 变量
int ticket;
// 如果是买单
if (OrderType() == OP_BUY)
{
// 补开买单
double openBuyPrice = Ask + GridDistance * Point;
ticket = OrderSend(Symbol(), OP_BUY, LotSize, openBuyPrice, Slippage, openBuyPrice - StopLoss * Point, openBuyPrice + TakeProfit * Point, "Reopen Buy Grid", 0, 0, clrGreen);
if (ticket > 0)
{
Print("已成功补开买单,订单票号:", ticket);
}
else
{
Print("买单补开失败,错误代码:", GetLastError());
}
}
// 如果是卖单
if (OrderType() == OP_SELL)
{
// 补开卖单
double openSellPrice = Bid - GridDistance * Point;
ticket = OrderSend(Symbol(), OP_SELL, LotSize, openSellPrice, Slippage, openSellPrice + StopLoss * Point, openSellPrice - TakeProfit * Point, "Reopen Sell Grid", 0, 0, clrRed);
if (ticket > 0)
{
Print("已成功补开卖单,订单票号:", ticket);
}
else
{
Print("卖单补开失败,错误代码:", GetLastError());
}
}
}
}
}
}
//+------------------------------------------------------------------+
|