Combine Two indicators into one EA

To add comments, please log in or register
Kevin Svoboda
17
Kevin Svoboda  

So I did some research and found this video on youtube-

https://www.youtube.com/watch?v=o4jVeJdYTkM


And that got me thinking, what if I could combine all the indicators I use for manual trading into one program. I know python and how it works, but I don't know c so I'm having some trouble understanding the mql language. For manual trading I have bollinger bands, rsi, cci, and stochastic. I can provide more information on my manual trading but I'm really stuck on what to do here. I would also like to include a stoploss function inside of this ea since it will be automatically trading on 5 min candles.The first part is bollinger bands, and the second is supposed to close the program after 15 min.

Here is the code:

Any help on this program, or errors, or ideas, or documentation on adding multiple indicators would be much appreciated!

#include<Trade\Trade.mqh>

CTrade trade;


void OnTick()
  {
   string entry ="";
   
   //Get ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   //Get bid price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   MqlRates PriceInfo[];
   
   ArraySetAsSeries(PriceInfo,true);
   
   int PriceData =CopyRates(Symbol(),Period(),0,3,PriceInfo);
   
   double UpperBandArray[];
   double LowerBandArray[];
   
   ArraySetAsSeries(UpperBandArray,true);
   ArraySetAsSeries(LowerBandArray,true);
   
   //define bollinger bands
   int BollingerBandsDefinition=iBands(_Symbol,_Period,20,0,2,PRICE_CLOSE);
   
   CopyBuffer(BollingerBandsDefinition,1,0,3,UpperBandArray);
   CopyBuffer(BollingerBandsDefinition,2,0,3,LowerBandArray);
   
   //calc ea for the current candle
   double myUpperBandValue=UpperBandArray[0];
   double myLowerBandValue=LowerBandArray[0];
   
   //calc ea for the candle before
   double myLastUpperBandValue=UpperBandArray[1];
   double myLastLowerBandValue=LowerBandArray[1];
   
   
      if (   //Check for re-entry from below <buy> ----------CHANGE------------
            (PriceInfo[0].close>myLowerBandValue)
         && (PriceInfo[1].close<myLastLowerBandValue)
         )
            {
            entry="buy";
            }  
            
            
    if (   //Check for re-entry from above <sell> ----------CHANGE------------
            (PriceInfo[0].close<myUpperBandValue)
         && (PriceInfo[1].close>myLastUpperBandValue)
         )
            {
            entry="sell";
            }  
   
   
    if (entry =="sell" && PositionsTotal()<1) 
    trade.Sell(0.10,NULL,Bid,0,(Bid-150 * _Point),NULL);
    
    if (entry =="buy" && PositionsTotal()<1)
    trade.Buy(0.10,NULL,Ask,0,(Ask+150 * _Point),NULL); 
   
    Comment ("Entry Signal: ",entry);
   
   
 //-----------------------------------------------------------------------------  
   
   
int init()
   {
   return(0);  
   }
   
int deinit()
   {
   return(0);
   }

int start()
   {
int maxDuration = 15 * 60;
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
   OrderSelect(pos, SELECT_BY_POS)
&& OrderSymbol()== "EURUSD"){
   int duration = TimeCurrent() - OrderOpenTime();
   if (duration >= maxDuration)
      OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),
                  3*Point);
               }  return(0);
                  }
 }
nicholi shen
1870
nicholi shen  

Alright, another Python guy!

First off, MQL5 doesn't have init, deinit, and start event handlers so make sure you check the docs and use the correct ones. Also, if you know python and you're trying to make the switch to MQL then stick to the high level programming you're used to. The standard library has indicator classes that take care of all the low level abstractions behind the scenes. This would by the closest thing to "pythonic" in MQL. 


//+------------------------------------------------------------------+
//|                                                     Mql5Help.mq5 |
//|                                                      nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#define MAGIC 123456
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>

CiBands     g_bands;
CiRSI       g_rsi;
CIndicators g_indicators;

CTrade      g_trade;
CPositionInfo g_pi;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   if(!g_bands.Create(_Symbol, _Period, 20, 0, 2, PRICE_CLOSE)
      || !g_rsi.Create(_Symbol, _Period, 14, PRICE_CLOSE)
      || !g_indicators.Add(&g_bands)
      || !g_indicators.Add(&g_rsi)
   )
      return INIT_FAILED;
   g_trade.SetExpertMagicNumber(MAGIC);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   g_indicators.Refresh();
   MqlTick tick;
   double close[];
   ArraySetAsSeries(close, true);
   if(!SymbolInfoTick(_Symbol, tick) 
      || CopyClose(_Symbol, _Period, 0, 2, close) != 2
   )
      return;
   if(!g_pi.SelectByMagic(_Symbol, MAGIC)){
      if(close[1] < g_bands.Lower(1) && close[0] > g_bands.Lower(0)){
         if(!g_trade.Buy(...)){
            ...
         }
      }
      else if(close[1] > g_bands.Upper(1) && close[0] < g_bands.Upper(0)){
         if(!g_trade.Sell(...)){
               ...
         }
      }
   }
}
To add comments, please log in or register