Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.72 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //|                                                   TesteRobo1.mq5 |
  3. //|                                                    Ronaldo Lemos |
  4. //|                                             https://www.mql5.com |
  5. //+------------------------------------------------------------------+
  6. #include "CheckEntryBollingerBands.mq5";
  7. #include "CheckEntryADX.mq5";
  8. #include "CheckEntryMACD.mq5";
  9.  
  10. #include<Trade\Trade.mqh>
  11.  
  12. #property copyright "Ronaldo Lemos"
  13. #property link      "https://www.mql5.com"
  14. #property version   "1.00"
  15.  
  16. CTrade trade;
  17. //+------------------------------------------------------------------+
  18. //| Expert initialization function                                   |
  19. //+------------------------------------------------------------------+
  20. int OnInit()
  21.   {
  22. //--- create timer
  23.    EventSetTimer(60);
  24.  
  25. //---
  26.    return(INIT_SUCCEEDED);
  27.   }
  28. //+------------------------------------------------------------------+
  29. //| Expert deinitialization function                                 |
  30. //+------------------------------------------------------------------+
  31. void OnDeinit(const int reason)
  32.   {
  33. //--- destroy timer
  34.    EventKillTimer();
  35.  
  36.   }
  37. //+------------------------------------------------------------------+
  38. //| Expert tick function                                             |
  39. //+------------------------------------------------------------------+
  40.  
  41. //+------------------------------------------------------------------+
  42. //|                                                                  |
  43. //+------------------------------------------------------------------+
  44. double currentPrice=0;
  45. bool buy=false;
  46. int currentOpen=0;
  47.  
  48. int lastHourOperate=0;
  49. int lastMinuteOperate=0;
  50. //+------------------------------------------------------------------+
  51. //|                                                                  |
  52. //+------------------------------------------------------------------+
  53. void OnTick()
  54.   {
  55. //---
  56.    MqlRates PriceInfo[];
  57.  
  58.    ArraySetAsSeries(PriceInfo,true);
  59.  
  60.    int Data=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInfo);
  61.  
  62.    int NumberOfCandles=Bars(Symbol(),Period());
  63.  
  64.    string NumberOfCandlesText=IntegerToString(NumberOfCandles);
  65.  
  66.    bool bollinger=checkEntryBollinger();
  67.    bool adx=checkEntryADX();
  68.    bool macd=checkEntryMACD();
  69.  
  70.    int amplitude=PriceInfo[1].high-PriceInfo[1].low;
  71.  
  72.    if(bollinger)
  73.      {
  74.       if(adx)
  75.         {
  76.          if(macd)
  77.            {
  78.             if(PriceInfo[1].open<PriceInfo[1].close && PriceInfo[2].open<PriceInfo[2].close)
  79.               {
  80.                //printf("Abertura Atual: "+IntegerToString(PriceInfo[0].open)+"\n Fechamento Atual:"+IntegerToString(PriceInfo[0].close)+"\n Abertura Anterior:"+IntegerToString(PriceInfo[1].open)+"\n Fechamento Anterior:"+IntegerToString(PriceInfo[1].close)+"\n");
  81.                int fechamento=PriceInfo[1].close-PriceInfo[1].open;
  82.  
  83.                if((fechamento*2)>amplitude)
  84.                  {
  85.                   Buy();
  86.                   //ObjectCreate(_Symbol,NumberOfCandlesText,OBJ_ARROW_BUY,0,TimeCurrent(),(PriceInfo[0].close));
  87.                  }
  88.               }
  89.             else if(PriceInfo[1].open>PriceInfo[1].close && PriceInfo[2].open>PriceInfo[2].close)
  90.               {
  91.                //printf("Abertura Atual: "+IntegerToString(PriceInfo[0].open)+"\n Fechamento Atual:"+IntegerToString(PriceInfo[0].close)+"\n Abertura Anterior:"+IntegerToString(PriceInfo[1].open)+"\n Fechamento Anterior:"+IntegerToString(PriceInfo[1].close)+"\n");
  92.                int fechamento=PriceInfo[1].open-PriceInfo[1].close;
  93.                if((fechamento*2)>amplitude)
  94.                  {
  95.                   Sell();
  96.                   //ObjectCreate(_Symbol,NumberOfCandlesText,OBJ_ARROW_SELL,0,TimeCurrent(),(PriceInfo[0].close));
  97.                  }
  98.               }
  99.            }
  100.         }
  101.      }
  102.  
  103.    CheckStop();
  104.   }
  105. //+------------------------------------------------------------------+
  106. //|                                                                  |
  107. //+------------------------------------------------------------------+
  108. void CheckStop()
  109.   {
  110.    if(PositionsTotal()>0)
  111.      {
  112.       if(currentPrice>0)
  113.         {
  114.          MqlDateTime dt_struct;
  115.          datetime dtSer=TimeCurrent(dt_struct);
  116.  
  117.          double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  118.  
  119.          if(currentOpen>0)
  120.             printf("current open: "+IntegerToString(currentOpen));
  121.  
  122.          //STOP POR CONTA DO HORARIO
  123.          if(dt_struct.hour>=17 && dt_struct.min>=30)
  124.            {
  125.             if(buy)
  126.               {
  127.                //Operando comprado
  128.                trade.Sell(currentOpen,_Symbol,Ask,0,0,"");
  129.                buy=false;
  130.                currentPrice=0;
  131.                currentOpen = 0;
  132.               }
  133.             else if(buy==false)
  134.               {
  135.                //Operando vendido
  136.  
  137.                trade.Buy(currentOpen,_Symbol,Ask,0,0,"");
  138.                buy=false;
  139.                currentPrice=0;
  140.                currentOpen = 0;
  141.               }
  142.             return;
  143.            }
  144.  
  145.          //printf("Server time: %d:%d:%d ; Just hours: %d ; Just minutes: %d",dt_struct.hour,dt_struct.min,dt_struct.sec,dt_struct.hour,dt_struct.min);
  146.  
  147.          //STOP POR VARIAÇÃO
  148.          if(buy)
  149.            {
  150.             //Operando comprado
  151.             int priceDifference=Ask-currentPrice;
  152.  
  153.             if(priceDifference<=-200)
  154.               {
  155.                trade.Sell(currentOpen,_Symbol,Ask,0,0,"");
  156.                buy=false;
  157.                currentPrice=0;
  158.                currentOpen=0;
  159.  
  160.                lastHourOperate=dt_struct.hour;
  161.                lastMinuteOperate=dt_struct.min;
  162.               }
  163.             else if(priceDifference>=195 && currentOpen>1)
  164.               {
  165.                trade.Sell(1,_Symbol,Ask,0,0,"");
  166.                currentOpen=1;
  167.               }
  168.             else if(priceDifference>=495 && currentOpen>0)
  169.               {
  170.                trade.Sell(currentOpen,_Symbol,Ask,0,0,"");
  171.                buy=false;
  172.                currentPrice=0;
  173.                currentOpen=0;
  174.  
  175.                lastHourOperate=dt_struct.hour;
  176.                lastMinuteOperate=dt_struct.min;
  177.               }
  178.            }
  179.          else
  180.            {
  181.             //Operando vendido
  182.             int priceDifference=Ask-currentPrice;
  183.  
  184.             if(priceDifference>=200)
  185.               {
  186.                trade.Buy(currentOpen,_Symbol,Ask,0,0,"");
  187.                buy=false;
  188.                currentPrice=0;
  189.                currentOpen=0;
  190.  
  191.                lastHourOperate=dt_struct.hour;
  192.                lastMinuteOperate=dt_struct.min;
  193.               }
  194.             else if(priceDifference<=-195 && currentOpen>1)
  195.               {
  196.                trade.Buy(2,_Symbol,Ask,0,0,"");
  197.                currentOpen=1;
  198.               }
  199.             else if(priceDifference<=-495 && currentOpen>0)
  200.               {
  201.                trade.Buy(currentOpen,_Symbol,Ask,0,0,"");
  202.                buy=false;
  203.                currentPrice=0;
  204.                currentOpen=0;
  205.  
  206.                lastHourOperate=dt_struct.hour;
  207.                lastMinuteOperate=dt_struct.min;
  208.               }
  209.            }
  210.         }
  211.      }
  212.   }
  213. //+------------------------------------------------------------------+
  214. //|                                                                  |
  215. //+------------------------------------------------------------------+
  216. void Buy()
  217.   {
  218.    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  219.    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
  220.  
  221.    MqlDateTime dt_struct;
  222.    datetime dtSer=TimeCurrent(dt_struct);
  223.  
  224.    if((OrdersTotal()==0) && (PositionsTotal()==0) && dt_struct.hour<17 && CheckTime())
  225.      {
  226.       trade.BuyLimit(2,Ask,_Symbol,0,0,ORDER_TIME_GTC,0,NULL);
  227.  
  228.       //trade.Buy(2,_Symbol,Ask,0,0,"");
  229.       buy=true;
  230.       currentPrice=Ask;
  231.       currentOpen=2;
  232.       //trade.SellStop(2,(Ask-(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
  233.       //trade.SellLimit(1,(Ask+(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, 0);
  234.       //trade.SellLimit(1,(Ask+(400*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, 0);
  235.  
  236.      }
  237.  
  238.   }
  239. //+------------------------------------------------------------------+
  240. //|                                                                  |
  241. //+------------------------------------------------------------------+
  242. void Sell()
  243.   {
  244.    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  245.    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
  246.  
  247.    MqlDateTime dt_struct;
  248.    datetime dtSer=TimeCurrent(dt_struct);
  249.  
  250.    if((OrdersTotal()==0) && (PositionsTotal()==0) && dt_struct.hour<17 && CheckTime())
  251.      {
  252.       printf("sell");
  253.       //trade.Sell(2,_Symbol,Ask,0,0,"");
  254.       buy=false;
  255.       currentPrice=Ask;
  256.       trade.SellLimit(2,Ask,_Symbol,0,0,ORDER_TIME_GTC,0,NULL);
  257.       currentOpen=2;
  258.       //trade.SellLimit(2,Ask,_Symbol,(Ask+(200*_Point)),0,ORDER_TIME_GTC,0,NULL);
  259.       //trade.BuyStop(2,(Ask+(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
  260.       //trade.BuyLimit(1,(Ask-(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
  261.       //trade.BuyLimit(1,(Ask-(400*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
  262.      }
  263.   }
  264. //+------------------------------------------------------------------+
  265. //|                                                                  |
  266. //+------------------------------------------------------------------+
  267. bool CheckTime()
  268.   {
  269.    MqlDateTime dt_struct;
  270.    datetime dtSer=TimeCurrent(dt_struct);
  271.  
  272.    int total_minutes=0;
  273.  
  274.    if(dt_struct.min>lastMinuteOperate)
  275.      {
  276.       total_minutes=dt_struct.min-lastMinuteOperate;
  277.      }
  278.    else
  279.      {
  280.       total_minutes=(60-lastMinuteOperate)+dt_struct.min;
  281.      }
  282.  
  283.    return total_minutes > 5;
  284.   }
  285. //+------------------------------------------------------------------+
  286. //| Trade function                                                   |
  287. //+------------------------------------------------------------------+
  288. void OnTrade()
  289.   {
  290. //CancelOrder();
  291. //---
  292.   }
  293. //+------------------------------------------------------------------+
  294. //|                                                                  |
  295. //+------------------------------------------------------------------+
  296.  
  297. //+------------------------------------------------------------------+
  298. //|                                                                  |
  299. //+------------------------------------------------------------------+
  300. void CancelOrder()
  301.   {
  302.    for(int i=OrdersTotal()-1;i>=0;i--)
  303.      {
  304.       ulong OrderTicket=OrderGetTicket(i);
  305.  
  306.       trade.OrderDelete(OrderTicket);
  307.      }
  308.   }
  309. //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement