练手MQL5

要添加评论,请登录注册
Xu Zhao
187
Xu Zhao  

2

Xu Zhao
187
Xu Zhao  
#property copyright "text"
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <shuju.mqh>
#include <jiaoyi.mqh>
input int      magic=888; //魔幻号
input double   lot  =0.1;//手数
input int      tp   =200;//止盈
input int      sl   =300;//止损
input int   盈利多少点=100;
input int   move_SL =50; //移动止损
input int   最近多少根Zg均值 = 11;//用于优化策略
input ENUM_TIMEFRAMES periodH1 = PERIOD_H1;//小时周期K
input ENUM_TIMEFRAMES periodH4 = PERIOD_H1;//大周期K
input int bars = 500;

input int      ExtDepth=12;
input int      ExtDeviation=5;
input int      ExtBackstep=3;
shuju sj;
jiaoyi jy;
int a;
double zigzagdata[11][3];
double zigzag[];
double start_index1;
double start_index2;//记录挂单在高点还是地点的状态
double start_price1;
double start_price2;
double sec_price1;
double sec_price2;
ulong   order_buy = 0; 
ulong   order_sell= 0;
int OnInit()
  {
   a = iCustom(Symbol(),periodH1,"Examples\\ZigZag",ExtDepth,ExtDeviation,ExtBackstep);
    ArraySetAsSeries(zigzag,true);
   EventSetTimer(3600);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
bool market(double &array[][2],double price_ask,double price_bid)
    {
      double  sum1 = 0.0;
      double  sum2 = 0.0;
      double average = 0.0;
      int   a  =0;
      
      
      for(int i = 最近多少根Zg均值-1; i>0 ; i--)
       { 
        if(array[i][0] = 1 && array[i-1][0]>array[i][0])
          {
            sum1 = array[i-1][0] - array[i][0];
          }
         if(array[i][0] = 2 && array[i-1][0]<array[i][0])
          {
            sum2 = array[i][0] - array[i-1][0];
          }
        }
       average = (sum1+sum2)/10;
       if(average > 0.001)
         a = 1;
       return(a);
        
    }
void OnTick()
  {
   double open_buy,open_sell;
   datetime optime_buy,optime_sell;
   double lots;
   int total       = OrdersTotal();
   int posit_total = PositionsTotal();
   double now_pice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   double now_pice2= SymbolInfoDouble(Symbol(),SYMBOL_BID);
   //if(market(zigzagdata,now_pice,now_pice2)==true)
//+------------------------------------------------------------------+
//|                     buy                                             |
//+------------------------------------------------------------------+
   //jy.getlasthistory(Symbol(),DEAL_TYPE_SELL ,0,open_buy,optime_buy,lots);//最后一张多单
   if(zigzagdata[1][1] == 1 )   //高点挂多单
           {
             //挂单且记录下挂单号
             double pricebuy = zigzagdata[1][0]+5*_Point;
             if(zigzagdata[1][0]>now_pice)
               {
                 if(total==0)
                  {
                    order_buy = jy.buyguaplus(pricebuy,Symbol(),lot,sl,tp,"挂多单",magic,0); 
                    start_index1 = zigzagdata[1][1];   
                    start_price1 = zigzagdata[1][0];
                    sec_price1   = zigzagdata[2][0];
                    
                  } 
                 if(total>1)
                  {
                    for(int i = 0; i < total;i++)
                      {
                        OrderGetTicket(i);
                        if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP && OrderGetInteger(ORDER_MAGIC)==magic)
                          {
                              break;
                          }
                        if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP && OrderGetInteger(ORDER_MAGIC)==magic && total==1)
                          {
                              order_buy = jy.buyguaplus(pricebuy,Symbol(),lot,sl,tp,"挂多单",magic,0); 
                              start_index1 = zigzagdata[1][1];   
                              start_price1 = zigzagdata[1][0]; 
                              sec_price1   = zigzagdata[2][0];
                          }
                      }
                  }            
               }         
           }  
           
           
           
  //---什么情况下删除定单

    if(start_index1 != zigzagdata[1][1] && start_price1 >now_pice && sec_price1>now_pice)
      {
         for(int i = 0; i <total ;i++)
            {
               OrderGetTicket(i);
               if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP)   
                 { 
                   if(OrdersTotal()>0)
                     jy.delor_one_ders(Symbol(),order_buy,magic);
                 }       
            }
      }
       
//+------------------------------------------------------------------+
//|                buy end ---next sell                                                 |
//+------------------------------------------------------------------+
   //jy.getlasthistory(Symbol(),DEAL_TYPE_BUY ,0,open_sell,optime_sell,lots);//最后一张空单

    if(zigzagdata[1][1] == 2 )   //低点挂空单
        {
       //挂单且记录下挂单号
          double pricesell = zigzagdata[1][0]-5*_Point;
          if(zigzagdata[1][0]<now_pice2)
            { 
              if(total == 0)
                {
                    order_sell = jy.sellguaplus(pricesell,Symbol(),lot,sl,tp,"挂空单",magic,0); 
                    start_index2 = zigzagdata[1][1];
                    start_price2 = zigzagdata[1][0]; 
                    sec_price2   = zigzagdata[2][0];
                } 
              if(total > 1) 
                {
                    for(int i = 0; i < total;i++)
                      {
                        OrderGetTicket(i);
                        if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP && OrderGetInteger(ORDER_MAGIC)==magic)
                          {
                              break;
                          }
                        if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP && OrderGetInteger(ORDER_MAGIC)==magic && total==1)
                          {
                              order_sell = jy.sellguaplus(pricesell,Symbol(),lot,sl,tp,"挂空单",magic,0); 
                              start_index2 = zigzagdata[1][1];
                              start_price2 = zigzagdata[1][0];
                              sec_price2   = zigzagdata[2][0]; 
                          }
                      }
                  }            
                }
            }

       
        
//---什么情况下删除定单
    
    if( start_index2 != zigzagdata[1][1] && start_price2 <now_pice && sec_price2<now_pice)
      {
         for(int i = 0; i <total ;i++)
            {
               OrderGetTicket(i);
               if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP)   
                 { 
                   if(OrdersTotal()>0)
                     {
                       jy.delor_one_ders(Symbol(),order_sell,magic);
                       start_index2 = 0;
                     }
                 }       
            }
      }
//+------------------------------------------------------------------+
//|                            sell_end                                      |
//+------------------------------------------------------------------+

       /*if(OrdersTotal() == 0 && posit_total == 0)
         {
             if(zigzagdata[1][1] == 1  && zigzagdata[1][0] > now_pice && zigzagdata[2][0] < now_pice2)
                {
                   //挂单且记录下挂单号
                   double pricebuy = zigzagdata[1][0]+5*_Point;
                   double pricesell =zigzagdata[2][0]- 5*_Point;
                   jy.buyguaplus(pricebuy,Symbol(),lot,sl,tp,"挂多单",magic,0);
                   jy.sellguaplus(pricesell,Symbol(),lot,sl,tp,"挂空单",magic,0);
                } 
                    //后加的策略二号
             if(zigzagdata[1][1] == 2  && zigzagdata[1][0] < now_pice && zigzagdata[2][0] > now_pice2)
                {
                   //挂单且记录下挂单号
                    double pricesell = zigzagdata[1][0]-5*_Point;
                    double pricebuy =zigzagdata[2][0]+ 5*_Point;
                   jy.buyguaplus(pricebuy,Symbol(),lot,sl,tp,"挂多单",magic,0);
                   jy.sellguaplus(pricesell,Symbol(),lot,sl,tp,"挂空单",magic,0);
                }                 
          }
        
     if(total == 1)
       {
         for(int i = 0; i < total; i++)
          {
            OrderGetTicket(i);
            jy.delorders(Symbol(),magic);
          }
       }
//--- 遍历挂单成交的情况

     if(posit_total != 0)
       {
         for(int i = 0; i<posit_total ; i++)
            {
              string name =  PositionGetSymbol(i);
              double open = PositionGetDouble( POSITION_PRICE_OPEN);
              //搜索多单且盈利超过10个点时,修改止损到盈利5点
              if(PositionGetInteger(POSITION_MAGIC) ==magic && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_BUY
                  && (PositionGetDouble( POSITION_PRICE_CURRENT)- PositionGetDouble( POSITION_PRICE_OPEN)) >= 盈利多少点* _Point)
                {
                  double sll_1 = move_SL*_Point+PositionGetDouble( POSITION_PRICE_OPEN);
                  double tpl_1 = tp*_Point+PositionGetDouble( POSITION_PRICE_OPEN);
                  jy.modifysltp(Symbol(),POSITION_TYPE_BUY,sll_1,tpl_1,magic);
                  //jy.modifysltp(Symbol(),POSITION_TYPE_BUY,(move_SL+PositionGetDouble( POSITION_PRICE_OPEN)),(tp*_Point+PositionGetDouble( POSITION_PRICE_OPEN)),magic);
                  //jy.modifysltp(Symbol(),POSITION_TYPE_BUY,1.13484,1.1399,0);
                }
               //搜索空单且盈利超过10个点时,修改止损到盈利5点 
              if(PositionGetInteger(POSITION_MAGIC) ==magic && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL
                  && (PositionGetDouble( POSITION_PRICE_OPEN)-PositionGetDouble( POSITION_PRICE_CURRENT)) >= 盈利多少点* _Point)
                {
                  double sll = PositionGetDouble( POSITION_PRICE_OPEN)-move_SL*_Point;
                  double tpl = PositionGetDouble( POSITION_PRICE_OPEN)-tp*_Point;
                  jy.modifysltp(Symbol(),POSITION_TYPE_SELL,sll,tpl,magic);
                  //jy.modifysltp(Symbol(),POSITION_TYPE_SELL,move_SL,tp,magic);
                }                
            }
          
       } */   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   double high[];
   ArraySetAsSeries(high,true);
   CopyHigh(Symbol(),0,0,bars,high);
   /*datetime time[];
   ArraySetAsSeries(time,true);
   CopyTime(Symbol(),0,0,bars,time);*/
   CopyBuffer(a,0,0,bars,zigzag);
   int index = 0;
   for(int i = 0; i < bars; i++)
     {
      if(zigzag[i] > 0 && zigzag[i] == high[i] && index < 10)
       {
        zigzagdata[index][0] = zigzag[i];
        zigzagdata[index][1] = 1;
        zigzagdata[index][2] = 6;//记录下高点的下标index
                index ++;
       }
       if(zigzag[i] > 0 && zigzag[i] != high[i] && index < 10)
       {
        zigzagdata[index][0] = zigzag[i];
        zigzagdata[index][1] = 2;
        zigzagdata[index][2] = 9;//记录下地址的时间                
                index ++;     
        }
     }   
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
要添加评论,请登录注册