Rsi ea with moving average instead of rsi line - page 3

 
ENEZA PETER MKIRAMWENI :

i have tried but the ea wont take any trade i want to use 5 sma line instead of rsi line

In this case, create an iMA indicator and get values from it.

 
Vladimir Karputov:

In this case, create an iMA indicator and get values from it.

 How can i do that

the MA in EA is different from The MA of an indicator 

please help me 

strategy tester visualization

real indicator

 
ENEZA PETER MKIRAMWENI :

 How can i do that

the MA in EA is different from The MA of an indicator 

please help me 


If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.

 
Vladimir Karputov:

If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.

Like this one

//--- create handle of the indicator
   if(type==Call_iMA)
      handle=iMA(name,period,ma_period,ma_shift,ma_method,applied_price);
   else
     {
      //--- fill the structure with parameters of the indicator
      MqlParam pars[4];
      //--- period
      pars[0].type=TYPE_INT;
      pars[0].integer_value=ma_period;
      //--- shift
      pars[1].type=TYPE_INT;
      pars[1].integer_value=ma_shift;
      //--- type of smoothing
      pars[2].type=TYPE_INT;
      pars[2].integer_value=ma_method;
      //--- type of price
      pars[3].type=TYPE_INT;
      pars[3].integer_value=applied_price;
      handle=IndicatorCreate(name,period,IND_MA,4,pars);
     }
//--- if the handle is not created
   if(handle==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  name,
                  EnumToString(period),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//--- show the symbol/timeframe the Moving Average indicator is calculated for
   short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",name,EnumToString(period),
                           ma_period, ma_shift,EnumToString(ma_method),EnumToString(applied_price));
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- normal initialization of the indicator
   return(INIT_SUCCEEDED);
 
Vladimir Karputov:

If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.

i have tried but it is working check the code below

//+------------------------------------------------------------------+
//|                                                         mimi.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.001"
//+----------------------------------------------+
//  Trading algorithms                           |
//+----------------------------------------------+
#include <TradeAlgorithms.mqh>
//#include <Trade\PositionInfo.mqh>
//#include <Trade\Trade.mqh>
//#include <Trade\SymbolInfo.mqh>
//#include <Trade\AccountInfo.mqh>
//#include <Trade\DealInfo.mqh>
//#include <Trade\OrderInfo.mqh>
//#include <SmoothAlgorithms.mgh>
//+----------------------------------------------+
//|  Enumeration for lot calculation options    |
//+----------------------------------------------+
/*enum MarginMode - the enumeration is declared in the TradeAlgorithms.mqh file
  {
   FREEMARGIN=0,     //MM from free funds on the account
   BALANCE,          //MM from the balance of funds on the account
   LOSSFREEMARGIN,   //MM for losses from free funds on the account
   LOSSBALANCE,      //MM for losses from the balance of funds on the account
   LOT               //Lot unchanged
  }; */
//+----------------------------------------------+
//|  Enumeration for defining a trend        |
//+----------------------------------------------+
enum TrendMode
  {
   DIRECT=0,         //by signals
   AGAINST           //against signals
  };
  enum Creation
  {
   Call_iMA,               // use iMA
   Call_IndicatorCreate    // use IndicatorCreate
  };
//+----------------------------------------------+
//| Expert indicator input parameters        |
//+----------------------------------------------+
input double MM=0.1;              //Money Managemnt
input MarginMode MMMode=LOT;      //Money Managent Mode
input int    StopLoss_=1000;      //stop loss in points
input int    TakeProfit_=2000;    //take profit in points
input int    Deviation_=10;       //Max. price deviation in points
input bool   BuyPosOpen=true;     //Permission to enter long
input bool   SellPosOpen=true;    //Permission to enter short
input bool   BuyPosClose=true;     //Permission to exit longs
input bool   SellPosClose=true;    //Permission to get out of shorts
//+----------------------------------------------+
//| Input parameters of the RSI indicator            |
//+----------------------------------------------+
input ENUM_TIMEFRAMES InpInd_Timeframe=PERIOD_M1; //indicator timeframe
input TrendMode            Trend=DIRECT;          //trade
//----
input uint                 RSIPeriod=10;          //  RSI period
input ENUM_APPLIED_PRICE   RSIPrice=PRICE_CLOSE;  // price
input uint                 HighLevel=88;          // overbought level
input uint                 LowLevel=12;           // oversold level
//----
input uint                 SignalBar=1;           // bar number to receive the entry signal
//+----------------------------------------------+
//| Input parameters of the MA indicator            |
//+----------------------------------------------+
input int                  Inp_MA_ma_period     = 5;             // MA: averaging period
input int                  Inp_MA_ma_shift      = 0;              // MA: horizontal shift
input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_SMA;       // MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE;  // MA: type of price
//+----------------------------------------------+
//---- Declaring integer variables to store the chart period in seconds
int TimeShiftSec;
//---- Declaring integer variables for indicator handles
//int InpInd_Handle;
int    handle_iRSI;                          // variable for storing the handle of the iRVI indicator
int    handle_iMA;                           // variable for storing the handle of the iMA indicator
//---- declaring integer data origin variables
int min_rates_total;
CIsNewBar NB;
input Creation             type=Call_iMA;                // type of the function 
//--- name of the indicator on a chart
string short_name;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---- create handle of the indicator RSI
   handle_iRSI=iRSI(Symbol(),InpInd_Timeframe,RSIPeriod,RSIPrice);
   if(handle_iRSI==INVALID_HANDLE)
     {
      Print(" Failed to get the indicator handle RSI");
      return(INIT_FAILED);
     }
//--- create handle of the indicator iMA
   handle_iMA=iMA(Symbol(),InpInd_Timeframe,Inp_MA_ma_period,Inp_MA_ma_shift,Inp_MA_ma_method,handle_iRSI);
   if(handle_iMA==INVALID_HANDLE)
     {
      Print(" Failed to get the indicator handle RSI");
      return(INIT_FAILED);
     }
//---- initialization of a variable to store the chart period in seconds
   TimeShiftSec=PeriodSeconds(InpInd_Timeframe);
//---- Initializing data origin variables
   min_rates_total=int(Inp_MA_ma_period);
   min_rates_total+=int(3+SignalBar);
//--- completion of initialization
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
   GlobalVariableDel_(Symbol());
//----
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
     if(type==Call_iMA)
      handle_iMA=iMA(Symbol(),InpInd_Timeframe,Inp_MA_ma_period,Inp_MA_ma_shift,Inp_MA_ma_method,Inp_MA_applied_price);
   else
     {
      //--- fill the structure with parameters of the indicator
      MqlParam pars[4];
      //--- period
      pars[0].type=TYPE_INT;
      pars[0].integer_value=Inp_MA_ma_period;
      //--- shift
      pars[1].type=TYPE_INT;
      pars[1].integer_value=Inp_MA_ma_shift;
      //--- type of smoothing
      pars[2].type=TYPE_INT;
      pars[2].integer_value=Inp_MA_ma_method;
      //--- type of price
      pars[3].type=TYPE_INT;
      pars[3].integer_value=Inp_MA_applied_price;
      handle_iMA=IndicatorCreate(Symbol(),InpInd_Timeframe,IND_MA,4,pars);
     }
//--- if the handle is not created
   if(handle_iMA==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(InpInd_Timeframe),
                  GetLastError());
      //--- the indicator is stopped early
      //return(INIT_FAILED);
     }
//--- show the symbol/timeframe the Moving Average indicator is calculated for
   short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",Symbol(),EnumToString(InpInd_Timeframe),
                           Inp_MA_ma_period, Inp_MA_ma_shift,EnumToString(Inp_MA_ma_method),EnumToString(Inp_MA_applied_price));
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- normal initialization of the indicator
   //return(INIT_SUCCEEDED);
//---- checking the number of bars for sufficiency for calculation
   if(BarsCalculated(handle_iMA)<min_rates_total)
      return;
//---- loading history for normal operation of functions IsNewBar() и SeriesInfoInteger()
//LoadHistory(TimeCurrent()-PeriodSeconds(InpInd_Timeframe)-1,Symbol(),InpInd_Timeframe);
//---- Declaring static variables
   bool Recount=true;
   bool BUY_Open=false,BUY_Close=false;
   bool SELL_Open=false,SELL_Close=false;
   datetime UpSignalTime=0,DnSignalTime=0;
//+----------------------------------------------+
//| Definition of signals for deals              |
//+----------------------------------------------+
   if(!SignalBar || NB.IsNewBar(Symbol(),InpInd_Timeframe) || Recount) // checking for a new bar
     {
      //---- reset trading signals
      BUY_Open=false;
      SELL_Open=false;
      BUY_Close=false;
      SELL_Close=false;
      Recount=false;
      //---- Declaring local variables
      double RSI[];
      ArraySetAsSeries(RSI,true);
      double MA[];
      ArraySetAsSeries(MA,true);
      //---- copying the newly appeared data into arrays
      if(CopyBuffer(handle_iRSI,0,SignalBar,2,RSI)!=2)
        {
         Recount=true;
         return;
        }
      if(CopyBuffer(handle_iMA,0,SignalBar,2,MA)!=2)
        {
         Recount=true;
         return;
        }
      if(Trend==DIRECT)
        {
         //---- Get signals to buy
         if(MA[1]>LowLevel)
           {
            if(MA[0]<=LowLevel)
              {
               if(BuyPosOpen)
                  BUY_Open=true;
               if(SellPosClose)
                  SELL_Close=true;
              }
            UpSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
           }
         //---- Get signals for selling
         if(MA[1]<HighLevel)
           {
            if(MA[0]>=HighLevel)
              {
               if(SellPosOpen)
                  SELL_Open=true;
               if(BuyPosClose)
                  BUY_Close=true;
              }
            DnSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
           }
        }
      if(Trend==AGAINST)
        {
         //---- Get signals to buy
         if(MA[1]>LowLevel)
           {
            if(MA[0]<=LowLevel)
              {
               if(SellPosOpen)
                  SELL_Open=true;
               if(BuyPosClose)
                  BUY_Close=true;
              }
            DnSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
           }
         //---- Get signals for selling
         if(MA[1]<HighLevel)
           {
            if(MA[0]>=HighLevel)
              {
               if(BuyPosOpen)
                  BUY_Open=true;
               if(SellPosClose)
                  SELL_Close=true;
               UpSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
              }
           }
        }
      //Print(BUY_Open,BUY_Close);
     }
//+----------------------------------------------+
//| Making deals                           |
//+----------------------------------------------+
//---- Close the long
   BuyPositionClose(BUY_Close,Symbol(),Deviation_);
//---- Close the short
   SellPositionClose(SELL_Close,Symbol(),Deviation_);
//---- Opening long
   BuyPositionOpen(BUY_Open,Symbol(),UpSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_);
//---- Opening the short
   SellPositionOpen(SELL_Open,Symbol(),DnSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_);
//----
  }
//+------------------------------------------------------------------+



Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
ENEZA PETER MKIRAMWENI :

i have tried but it is working check the code below



I won't even look - I repeated a hundred times - THE INDICATOR HANDLE SHOULD BE CREATED ONCE! IT IS NECESSARY TO DO IT IN OnInit () !!!

Remember this! Get a forehead tattoo! Write in your journal.

 

Did you finally get it?, because am working on a similar project though its still kinda hard for me to have my EA execute trades based on the MA as the RSI line

 
Ssebaana Kizito E Joel #:

Did you finally get it?, because am working on a similar project though its still kinda hard for me to have my EA execute trades based on the MA as the RSI line

No my friend I didnt get it


 
ENEZA PETER MKIRAMWENI #:
No my friend I didnt get it


I'm in the same situation, you managed to fix it