Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+------------------------------------------------------------------+
- //| TesteRobo1.mq5 |
- //| Ronaldo Lemos |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #include "CheckEntryBollingerBands.mq5";
- #include "CheckEntryADX.mq5";
- #include "CheckEntryMACD.mq5";
- #include<Trade\Trade.mqh>
- #property copyright "Ronaldo Lemos"
- #property link "https://www.mql5.com"
- #property version "1.00"
- CTrade trade;
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //--- create timer
- EventSetTimer(60);
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //--- destroy timer
- EventKillTimer();
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double currentPrice=0;
- bool buy=false;
- int currentOpen=0;
- int lastHourOperate=0;
- int lastMinuteOperate=0;
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- MqlRates PriceInfo[];
- ArraySetAsSeries(PriceInfo,true);
- int Data=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInfo);
- int NumberOfCandles=Bars(Symbol(),Period());
- string NumberOfCandlesText=IntegerToString(NumberOfCandles);
- bool bollinger=checkEntryBollinger();
- bool adx=checkEntryADX();
- bool macd=checkEntryMACD();
- int amplitude=PriceInfo[1].high-PriceInfo[1].low;
- if(bollinger)
- {
- if(adx)
- {
- if(macd)
- {
- if(PriceInfo[1].open<PriceInfo[1].close && PriceInfo[2].open<PriceInfo[2].close)
- {
- //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");
- int fechamento=PriceInfo[1].close-PriceInfo[1].open;
- if((fechamento*2)>amplitude)
- {
- Buy();
- //ObjectCreate(_Symbol,NumberOfCandlesText,OBJ_ARROW_BUY,0,TimeCurrent(),(PriceInfo[0].close));
- }
- }
- else if(PriceInfo[1].open>PriceInfo[1].close && PriceInfo[2].open>PriceInfo[2].close)
- {
- //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");
- int fechamento=PriceInfo[1].open-PriceInfo[1].close;
- if((fechamento*2)>amplitude)
- {
- Sell();
- //ObjectCreate(_Symbol,NumberOfCandlesText,OBJ_ARROW_SELL,0,TimeCurrent(),(PriceInfo[0].close));
- }
- }
- }
- }
- }
- CheckStop();
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void CheckStop()
- {
- if(PositionsTotal()>0)
- {
- if(currentPrice>0)
- {
- MqlDateTime dt_struct;
- datetime dtSer=TimeCurrent(dt_struct);
- double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
- if(currentOpen>0)
- printf("current open: "+IntegerToString(currentOpen));
- //STOP POR CONTA DO HORARIO
- if(dt_struct.hour>=17 && dt_struct.min>=30)
- {
- if(buy)
- {
- //Operando comprado
- trade.Sell(currentOpen,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=0;
- currentOpen = 0;
- }
- else if(buy==false)
- {
- //Operando vendido
- trade.Buy(currentOpen,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=0;
- currentOpen = 0;
- }
- return;
- }
- //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);
- //STOP POR VARIAÇÃO
- if(buy)
- {
- //Operando comprado
- int priceDifference=Ask-currentPrice;
- if(priceDifference<=-200)
- {
- trade.Sell(currentOpen,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=0;
- currentOpen=0;
- lastHourOperate=dt_struct.hour;
- lastMinuteOperate=dt_struct.min;
- }
- else if(priceDifference>=195 && currentOpen>1)
- {
- trade.Sell(1,_Symbol,Ask,0,0,"");
- currentOpen=1;
- }
- else if(priceDifference>=495 && currentOpen>0)
- {
- trade.Sell(currentOpen,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=0;
- currentOpen=0;
- lastHourOperate=dt_struct.hour;
- lastMinuteOperate=dt_struct.min;
- }
- }
- else
- {
- //Operando vendido
- int priceDifference=Ask-currentPrice;
- if(priceDifference>=200)
- {
- trade.Buy(currentOpen,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=0;
- currentOpen=0;
- lastHourOperate=dt_struct.hour;
- lastMinuteOperate=dt_struct.min;
- }
- else if(priceDifference<=-195 && currentOpen>1)
- {
- trade.Buy(2,_Symbol,Ask,0,0,"");
- currentOpen=1;
- }
- else if(priceDifference<=-495 && currentOpen>0)
- {
- trade.Buy(currentOpen,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=0;
- currentOpen=0;
- lastHourOperate=dt_struct.hour;
- lastMinuteOperate=dt_struct.min;
- }
- }
- }
- }
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void Buy()
- {
- double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
- double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
- MqlDateTime dt_struct;
- datetime dtSer=TimeCurrent(dt_struct);
- if((OrdersTotal()==0) && (PositionsTotal()==0) && dt_struct.hour<17 && CheckTime())
- {
- trade.BuyLimit(2,Ask,_Symbol,0,0,ORDER_TIME_GTC,0,NULL);
- //trade.Buy(2,_Symbol,Ask,0,0,"");
- buy=true;
- currentPrice=Ask;
- currentOpen=2;
- //trade.SellStop(2,(Ask-(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
- //trade.SellLimit(1,(Ask+(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, 0);
- //trade.SellLimit(1,(Ask+(400*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, 0);
- }
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void Sell()
- {
- double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
- double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
- MqlDateTime dt_struct;
- datetime dtSer=TimeCurrent(dt_struct);
- if((OrdersTotal()==0) && (PositionsTotal()==0) && dt_struct.hour<17 && CheckTime())
- {
- printf("sell");
- //trade.Sell(2,_Symbol,Ask,0,0,"");
- buy=false;
- currentPrice=Ask;
- trade.SellLimit(2,Ask,_Symbol,0,0,ORDER_TIME_GTC,0,NULL);
- currentOpen=2;
- //trade.SellLimit(2,Ask,_Symbol,(Ask+(200*_Point)),0,ORDER_TIME_GTC,0,NULL);
- //trade.BuyStop(2,(Ask+(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
- //trade.BuyLimit(1,(Ask-(200*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
- //trade.BuyLimit(1,(Ask-(400*_Point)), _Symbol,0,0, ORDER_TIME_GTC, 0, NULL);
- }
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- bool CheckTime()
- {
- MqlDateTime dt_struct;
- datetime dtSer=TimeCurrent(dt_struct);
- int total_minutes=0;
- if(dt_struct.min>lastMinuteOperate)
- {
- total_minutes=dt_struct.min-lastMinuteOperate;
- }
- else
- {
- total_minutes=(60-lastMinuteOperate)+dt_struct.min;
- }
- return total_minutes > 5;
- }
- //+------------------------------------------------------------------+
- //| Trade function |
- //+------------------------------------------------------------------+
- void OnTrade()
- {
- //CancelOrder();
- //---
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void CancelOrder()
- {
- for(int i=OrdersTotal()-1;i>=0;i--)
- {
- ulong OrderTicket=OrderGetTicket(i);
- trade.OrderDelete(OrderTicket);
- }
- }
- //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement