普乐智能

查看: 4410|回复: 0

简单的MQL4代码范例,网格EA。

[复制链接]

338

主题

424

帖子

1670

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1670
发表于 2023-6-14 17:24:56 | 显示全部楼层 |阅读模式

//+------------------------------------------------------------------+
//|                                                GridTrading.mq4  |
//|                                     Copyright 2021, ChatGPT     |
//|                                                  https://chatgpt.com  |
//+------------------------------------------------------------------+

// EA参数
extern double Lots=0.1;         // 手数
extern int TakeProfit=10;       // 止盈点数
extern int GridSize=10;         // 网格的间距
extern int MaxPositions=5;      // 最大持仓数

// EA全局变量
int PositionCount=0;           // 当前已开仓数量
double EntryPrice=0;           // 最近一次开仓价格
double BuyStopPrice=0;         // Buy止损价格
double SellStopPrice=0;        // Sell止损价格

//+------------------------------------------------------------------+
//| EA入口函数                                                       |
//+------------------------------------------------------------------+
void OnTick()
{
   // 获取当前的Ask和Bid价格
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   // 如果当前没有仓位则开新仓
   if(PositionCount==0)
   {
      if(Ask<(EntryPrice-GridSize)) // Buy
      {
         EntryPrice=EntryPrice-GridSize; // 调整新订单的价格
         BuyStopPrice=EntryPrice-TakeProfit*_Point; // 计算新订单的止盈价格
         OrderSend(_Symbol,OP_BUYSTOP,Lots,EntryPrice,3,BuyStopPrice,0,"Grid Trading",0,0,clrGreen);
         PositionCount++; // 更新当前仓位数量
      }
      else if(Bid>(EntryPrice+GridSize)) // Sell
      {
         EntryPrice=EntryPrice+GridSize; // 调整新订单的价格
         SellStopPrice=EntryPrice+TakeProfit*_Point; // 计算新订单的止盈价格
         OrderSend(_Symbol,OP_SELLSTOP,Lots,EntryPrice,3,SellStopPrice,0,"Grid Trading",0,0,clrRed);
         PositionCount++; // 更新当前仓位数量
      }
   }

   // 如果当前仓位已满,则不执行任何操作
   if(PositionCount>=MaxPositions) return;

   // 如果有仓位,则调整止盈价格
   if(PositionCount>0)
   {
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
      {
         // 如果当前持有的是Buy仓位,则调整Buy仓位的止盈价格
         if(OrderType()==OP_BUYSTOP)
         {
            BuyStopPrice=Ask-TakeProfit*_Point; // 计算新的Buy止盈价格
            if(OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,0,0,clrGreen))
               Print("BuyStopPrice has been modified to "+BuyStopPrice);
         }
         // 如果当前持有的是Sell仓位,则调整Sell仓位的止盈价格
         else if(OrderType()==OP_SELLSTOP)
         {
            SellStopPrice=Bid+TakeProfit*_Point; // 计算新的Sell止盈价格
            if(OrderModify(OrderTicket(),OrderOpenPrice(),SellStopPrice,0,0,clrRed))
               Print("SellStopPrice has been modified to "+SellStopPrice);
         }
      }
   }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|11wo.com

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4( 备案号:桂ICP备18000909号-1 )QQ

快速回复 返回顶部 返回列表