有您的持续关注
我们会做得更好

angel-004 EA



angel-004 EA根据KD指标信号在进行交易。单量太少。

本网收集整理智能交易EA仅为MQL4编程爱好者提供语法及相关编程技巧的学习研究之用,智能交易存在极高的风险测试请用模拟盘,请勿用于实盘。angel-004 EA源码:

//+------------------------------------------------------------------+
//|                                                    angel-004.mq4 |
//|                                     Copyright 2014, QQ:125808047 |
//+------------------------------------------------------------------+
#property copyright "www.125808047.com"
#property link      "http://www.125808047.com/"

#include <stdlib.mqh>
#include <stderror.mqh> 


extern string EAName="anegl-004"; 
extern int  MagicNumber=1010101;


extern string  t0 = "---angel-004 Sets---"; 
extern int KPeriod=8;
extern int DPeriod=3;
extern int Slowing=3;
extern double SellLimit1=92;

extern double CloseSell=30;

extern double BuyLimit=8;
extern double CloseBuy=70;

extern string  t1 = "---Trades Sets---";
extern bool    TradeOnFixedSignal=false;
extern bool    ExitOnFixedSignal=false;
extern int     MaxSpread = 50;
extern int     Slippage = 3;
extern int     TakeProfit=33; //  0= no takeprofit   
extern int     StopLoss=30;   // 0= no StopLoss   
extern double  FixedLots=0.1; 
extern string t2="---Time Sets---" ;
extern int BokerStartHour=24;
extern int BokerEndHour=1;
bool cg = false;
 
 
string ComStr; 
int      B=0 ,S =0;
int      TradeShift,ExitShift;
int      TotalTries = 5; 
int      RetryDelay = 1000;
bool     Runnable=true;
 

 

int init()
{
   
   
   if(Digits==5||Digits==3)
      {  Slippage=Slippage*10;
         TakeProfit=TakeProfit*10; 
         StopLoss=StopLoss*10;
         MaxSpread=MaxSpread*10;
      }
 
   if(TradeOnFixedSignal) TradeShift=1;else TradeShift=0;
   if(ExitOnFixedSignal)  ExitShift=1;else  ExitShift=0;
   return(0);
}

int deinit()
{  
   return(0);
}


int start()
{
   if(!Runnable) return(0);
   B=CalOrders(OP_BUY);
   S=CalOrders(OP_SELL);
   
   
   CheckForClose(); 
   CheckForOpen(); 
   return(0);
}

int opensignal(int i)
{
   double d0= iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_SIGNAL,i);
   double d1= iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_SIGNAL,i+1);
   if(d1<=SellLimit1&&d0>SellLimit1)            {return(-1);}
   
   if(d1>=BuyLimit&& d0<BuyLimit)   {return(1);}
   return(0);
}

 
int closesignal(int i)
{
   double d0= iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_SIGNAL,i);
   double d1= iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_SIGNAL,i+1);
   if(d1<=CloseBuy&&d0>CloseBuy)    {return(-1);}
   if(d1>=CloseSell&&d0<CloseSell)  {return(1);}
   return(0);
}

void CheckForOpen()
{  
   int mSignal = 0 ;
   string mComment="";
   double mLots=0;
   //static datetime lastordertime=0;
   
   mSignal=opensignal(TradeShift);

  // if(MathAbs(Bid-Ask)/Point>MaxSpread )return; 
   
   if( (BokerStartHour<BokerEndHour&&(BokerStartHour<=Hour()&&Hour()<BokerEndHour)) ||
       (BokerStartHour>=BokerEndHour&&(BokerStartHour<=Hour()||Hour()<BokerEndHour)) )
   {
      if( B+S==0&&mSignal!=0)
         {  
             PlaySound("alert.wav");
             mLots=CB_GetLots();
             mComment=EAName;
             //if(Time[0]!=lastordertime)  {if(CB_OpenOrder(MagicNumber,mSignal,mLots,TakeProfit,StopLoss,mComment) && !ReEnter) lastordertime=Time[0];}
             CB_OpenOrder(MagicNumber,mSignal,mLots,TakeProfit,StopLoss,mComment);
         }
   }   
}  
 
 
 
int CalOrders(int type) 
{
   int num = 0;
   for(int i=0;i<OrdersTotal();i++)
     {
      cg = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!= Symbol() ||OrderMagicNumber()!= MagicNumber) continue;
      if(OrderType() == type)  num++;
     }
   return(num);
}

 
  
void CheckForClose()
{  double olots,ooprice,osoploss,oprofit;
   int  i,  oticket,otype;
   for(  i=OrdersTotal()-1;i>=0;i--)
   {
      cg = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber) continue;
      oticket=OrderTicket();olots=OrderLots();otype=OrderType();ooprice=OrderOpenPrice();osoploss=OrderStopLoss();oprofit=OrderProfit();
      
      
      if( (otype==OP_BUY&&closesignal(ExitShift)==-1)||(otype==OP_SELL&&closesignal(ExitShift)==1)) CloseOrder(oticket,olots);  
 
      
   }
 
}





double CB_GetLots()
{
   return(FixedLots);
}
  

 
bool CloseOrder(int ticket, double Lots) 
{
     bool exit_loop = false, result=false;
    int cnt=0 ;
    double myPrice;
    
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true)
    {
      while (cnt < TotalTries)
      {
        RefreshRates();
        if (OrderType() == OP_BUY)  myPrice = Bid;
        if (OrderType() == OP_SELL) myPrice = Ask;
        if (Digits > 0)  myPrice = NormalizeDouble( myPrice, Digits);

        result=OrderClose(ticket,Lots,myPrice,Slippage,Violet);
         if (result == true)     return(true); 
       
        if(GetLastError()>0)  cnt++;
          if(cnt<TotalTries) Sleep(RetryDelay);
        }  
     }   
   return(false);
}
 
bool CB_OpenOrder(int magic,int signal,double lots,int takeprofit,int stoploss,string comment )
{  
   int res, err;
   double  tp,sl;

   RefreshRates();
   if (signal>0) 
      {
       tp=CalTp(OP_BUY, takeprofit);
       sl=CalStop(OP_BUY, stoploss);
      } 
   else if (signal<0) 
      {
       tp=CalTp(OP_SELL,takeprofit);
       sl=CalStop(OP_SELL ,stoploss);
  
      }

   if (Digits > 0)   
      {
        sl = NormalizeDouble( sl, Digits);
        tp = NormalizeDouble( tp, Digits); 
      }
 
   if (signal>0) 
      {
         res=OrderSend(Symbol(),OP_BUY,lots,Ask,Slippage,sl,tp,comment,magic,0,Blue);
      } 
   else if (signal<0) 
      {
         res=OrderSend(Symbol(),OP_SELL,lots,Bid,Slippage,sl,tp,comment,magic,0,Red);
      }
      
   if (res > 0)
      {
         cg = OrderSelect(res, SELECT_BY_TICKET, MODE_TRADES); 
         return(true);
      }
   else
      {
        err = GetLastError();
        Print("OrderSend failed with error(" + err + ") " + ErrorDescription(err) + " SL:" + DoubleToStr(sl,Digits) + " TP:" + DoubleToStr(tp, Digits) + " Lots:" + DoubleToStr(lots, 2) );
        return(false);
      }
 
}
 


double CalTp(int type,int take)
{
 if(take==0)  return(0);
 RefreshRates(); 
 if (type == OP_BUY)     return(Ask+(take*Point));
 if (type == OP_SELL)    return(Bid-(take*Point));
return(0);
} 

double CalStop(int type, int stop)
{
   double minstop;
   
   if(stop==0)  return(0);
   minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (type == OP_BUY)
      {if( stop < minstop ) return(Ask - minstop*Point);  else return(Ask-(stop*Point));}
   if (type == OP_SELL)
      {if( stop < minstop ) return(Bid + minstop*Point);  else return(Bid+(stop*Point));}
return(0);
}

 

如您喜欢此文章请点下面分享按钮↴峰汇在线 » angel-004 EA
上一篇:
下一篇:
分享到:更多 ()