夜雨聆风学习资料网

ARTICLE · 1374304

MT4 MACD 金叉死叉 EA源码分享:非常简单的做单EA!

MT4 MACD 金叉死叉 EA源码分享:非常简单的做单EA!
策略逻辑:MACD 主线(Main)上穿信号线(Signal)= 金叉做多;主线下穿信号线 = 死叉做空;用已收盘 K 线 (shift1、shift2)判断交叉,避免盘中反复闪信号、重绘;可自由设置手数、止盈止损(点数)、MACD 参数、魔术号、滑点;默认同一品种只持有一单,新信号会先平反向单子再开仓,免费分享的源码,只能用于测试哈!
源码:
#property copyright "MACD Cross EA"#property link      "浪人微信:waihuiapi"#property version   "1.01"#property strict//===== 外部可调参数 =====input double Lots          = 0.10;    // 开单手数input int    StopLossPips  = 30;      // 止损点数(填0=不带止损)input int    TakeProfitPips= 60;      // 止盈点数(填0=不带止盈)input int    MACDFast      = 12;      // MACD快线input int    MACDSlow      = 26;      // MACD慢线input int    MACDSignal    = 9;       // MACD信号线周期input int    MagicNumber   = 8866;    // EA魔术号,区分订单input int    Slippage      = 3;       // 最大滑点(点)input bool   CloseReverse  = true;    // 出现反向信号,是否平掉原有单子//===== 全局变量 =====datetime lastBarTime = 0;//+------------------------------------------------------------------+//| Expert tick function                                             |//+------------------------------------------------------------------+voidOnTick(){   // 只在新K线生成后判断一次,减少重复发单   if(Time[1]==lastBarTime) return;   lastBarTime = Time[1];   // 获取MACD 主线、信号线(前两根已收盘K线)   double macd1  = iMACD(_Symbol,_Period,MACDFast,MACDSlow,MACDSignal,PRICE_CLOSE,MODE_MAIN,1);   double sig1   = iMACD(_Symbol,_Period,MACDFast,MACDSlow,MACDSignal,PRICE_CLOSE,MODE_SIGNAL,1);   double macd2  = iMACD(_Symbol,_Period,MACDFast,MACDSlow,MACDSignal,PRICE_CLOSE,MODE_MAIN,2);   double sig2   = iMACD(_Symbol,_Period,MACDFast,MACDSlow,MACDSignal,PRICE_CLOSE,MODE_SIGNAL,2);   // 金叉:2根前主线<信号,上一根主线>信号   bool BuyCross  = (macd2 <= sig2 && macd1 > sig1);   // 死叉:2根前主线>信号,上一根主线<信号   bool SellCross = (macd2 >= sig2 && macd1 < sig1);   // 读取当前持仓   int currType = GetCurrentOrderType();   if(BuyCross)   {      if(CloseReverse && currType==OP_SELL) CloseAllOrder();      if(currType!=OP_BUY) OpenOrder(OP_BUY);   }   if(SellCross)   {      if(CloseReverse && currType==OP_BUY) CloseAllOrder();      if(currType!=OP_SELL) OpenOrder(OP_SELL);   }}//+------------------------------------------------------------------+//| 开单函数                                                         |//+------------------------------------------------------------------+voidOpenOrder(int cmd){   double ask=Ask,bid=Bid;   double sl=0,tp=0;   double pipval = Point;   // 5位报价品种调整pip(黄金、欧美大部分是5位)   if(Digits==3 || Digits==5) pipval = Point*10;   if(cmd==OP_BUY)   {      if(StopLossPips>0)   sl = ask - StopLossPips*pipval;      if(TakeProfitPips>0) tp = ask + TakeProfitPips*pipval;      int ticket=OrderSend(_Symbol,OP_BUY,Lots,ask,Slippage,sl,tp,"MACD金叉多",MagicNumber,0,clrGreen);      if(ticket<0) Print("BUY开单失败,错误码:",GetLastError());   }   if(cmd==OP_SELL)   {      if(StopLossPips>0)   sl = bid + StopLossPips*pipval;      if(TakeProfitPips>0) tp = bid - TakeProfitPips*pipval;      int ticket=OrderSend(_Symbol,OP_SELL,Lots,bid,Slippage,sl,tp,"MACD死叉空",MagicNumber,0,clrRed);      if(ticket<0) Print("SELL开单失败,错误码:",GetLastError());   }}//+------------------------------------------------------------------+//| 获取当前该品种EA持仓方向:-1无仓,OP_BUY多,OP_SELL空             |//+------------------------------------------------------------------+intGetCurrentOrderType(){   for(int i=0;i<OrdersTotal();i++)   {      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))      {         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber)         {            return(OrderType());         }      }   }   return -1;}//+------------------------------------------------------------------+//| 平掉当前品种EA的全部订单                                          |//+------------------------------------------------------------------+voidCloseAllOrder(){   for(int i=0;i<OrdersTotal();i++)   {      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))      {         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber)         {            double cprice;            if(OrderType()==OP_BUY) cprice=Bid;            else cprice=Ask;            bool res=OrderClose(OrderTicket(),OrderLots(),cprice,Slippage,clrYellow);            if(!res) Print("平仓失败,错误:",GetLastError());         }      }   }}//+------------------------------------------------------------------+

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料