Code snippets - page 11

 
baraozemo:

Hi,

 

I tried it in MT5 but fails..

after add the time code , the experts compile as indicator... don't know what happened 

It has to be like this (and then you have to call the filter form the OnTick(), OnCalculate() exists only in indicators, you don't need it in an EA)
Files:
 
mladen:
It has to be like this (and then you have to call the filter form the OnTick(), OnCalculate() exists only in indicators, you don't need it in an EA)

dear mlanden.. one thousand thanks for you help... ;-)

and I'm trying to find tutorials for mt5 to learn creating experts and indicators..  because I want to program in mt5 too
in native mql5 site I don't like them to much , tutorials are confuse and not simple

 
mladen:
It has to be like this (and then you have to call the filter form the OnTick(), OnCalculate() exists only in indicators, you don't need it in an EA)

Hi mladen,

 rsi 2t-with-time-code__1.mq5 

I tested here is not working..


for example:

StartHour   =  9; // Start hour

StartMinute =  10; // Start minute

StartSecond =  0; // start seconds

EndHour     =16; // End hour

EndMinute   =  30; // end minute

EndSecond   =  0; // End second


in this "sample" I want the EA start at 9:10:00

and stop (and close) all operation at: 16:30:00 

 

but it's not working in this way.. It's still trading in different times

 
baraozemo:

Hi mladen,

 rsi 2t-with-time-code__1.mq5 

I tested here is not working..


for example:

StartHour   =  9; // Start hour

StartMinute =  10; // Start minute

StartSecond =  0; // start seconds

EndHour     =16; // End hour

EndMinute   =  30; // end minute

EndSecond   =  0; // End second


in this "sample" I want the EA start at 9:10:00

and stop (and close) all operation at: 16:30:00 

 

but it's not working in this way.. It's still trading in different times

Try it out now

Files:
 
baraozemo:

now it's solved... it's working only between the specified period.. thanks..

 

I having another problem..  in back testing I can see the indicators working in the screen  (visual mode in strategy tester)

but when I put the EA in a window and load a set and make it work , the indicators are hidden...  

For that you would have to explicitly load the indicators from the EA to the chart, but that is not necessary for the EA to work

To load an indicator that your EA is using you would have to use ChartIndicatorAdd() function - something like this

input int MacdFast    = 12;  // Macd fast
input int MacdSlow    = 26;  // Macd slow
input int MacdSignal =  9;   // Macd signal
input ENUM_APPLIED_PRICE Price=PRICE_CLOSE; // Price

int macdHandle;

int OnInit(void)
{
   macdHandle = iMACD(_Symbol,_Period,MacdFast,MacdSlow,MacdSignal,Price);
   if (macdHandle!=INVALID_HANDLE)
      ChartIndicatorAdd(0,(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL),macdHandle);
      return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) { }
void OnTick()                   { }
 

PS: what is less obvious : using that mode you can open an indicator in a window that it is usually not opened at. See this example :

input int                  tenkan_sen=9;              // period of Tenkan-sen
input int                  kijun_sen=26;              // period of Kijun-sen
input int                  senkou_span_b=52;          // period of Senkou Span B

int ichiHandle;

int OnInit(void)
{
   ichiHandle = iIchimoku(_Symbol,_Period,tenkan_sen,kijun_sen,senkou_span_b);
   if (ichiHandle!=INVALID_HANDLE)
      ChartIndicatorAdd(0,(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL),ichiHandle);
      return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) { }
void OnTick()                   { }


Without the usage of ChartIndicatorAdd() you would not be able to have that indicator in a separate window

Doing the opposite (sending the indicator to the main chart window) works too, but if the range of values produced by the indicator is not the same as the range of value of the current chart, the indicator will be off the chart and it will not be visible)

 
baraozemo:

I'm doing something wrong.. when I change the timeframe and the EA is running , I'm getting +indicators on screen (allways add a the "same indicator")

 

//-------RSI 1

input ENUM_TIMEFRAMES    Signal_0_RSI_Timeframe=PERIOD_M1;   // RSI1 Tempo

input int                Signal_0_RSI_PeriodRSI=8;           // RSI1 Período

input ENUM_APPLIED_PRICE Signal_0_RSI_Applied  =PRICE_CLOSE; // RSI1 Preço

input double             Signal_0_RSI_Weight   =1.0;         // RSI1 Peso (%)

//-------RSI 2

input ENUM_TIMEFRAMES    Signal_1_RSI_Timeframe=PERIOD_M5;   // RSI1 Tempo

input int                Signal_1_RSI_PeriodRSI=8;           // RSI1 Período

input ENUM_APPLIED_PRICE Signal_1_RSI_Applied  =PRICE_CLOSE; // RSI1 Preço

input double             Signal_1_RSI_Weight   =1.0;         // RSI1 Peso (%)

//-------RSI 3

input ENUM_TIMEFRAMES    Signal_2_RSI_Timeframe=PERIOD_M15;  // RSI1 Tempo

input int                Signal_2_RSI_PeriodRSI=8;           // RSI1 Período

input ENUM_APPLIED_PRICE Signal_2_RSI_Applied  =PRICE_CLOSE; // RSI1 Preço

input double             Signal_2_RSI_Weight   =1.0;         // RSI1 Peso (%)



int RSI1Handle;

int RSI2Handle;

int RSI3Handle;



int OnInit(void)

  {

//---adicionando indicadores RSI1 ao gráfico

   RSI1Handle = iRSI(_Symbol,Signal_0_RSI_Timeframe,Signal_0_RSI_PeriodRSI,Signal_0_RSI_Applied);

   if (RSI1Handle!=INVALID_HANDLE)

      ChartIndicatorAdd(0,(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL),RSI1Handle);

      return(INIT_SUCCEEDED);

//---adicionando indicadores RSI2 ao gráfico

   RSI2Handle = iRSI(_Symbol,Signal_1_RSI_Timeframe,Signal_1_RSI_PeriodRSI,Signal_1_RSI_Applied);

   if (RSI2Handle!=INVALID_HANDLE)

      ChartIndicatorAdd(1,(int)ChartGetInteger(1,CHART_WINDOWS_TOTAL),RSI2Handle);

      return(INIT_SUCCEEDED);

//---adicionando indicadores RSI3 ao gráfico

   RSI3Handle = iRSI(_Symbol,Signal_2_RSI_Timeframe,Signal_2_RSI_PeriodRSI,Signal_2_RSI_Applied);

   if (RSI3Handle!=INVALID_HANDLE)

      ChartIndicatorAdd(2,(int)ChartGetInteger(2,CHART_WINDOWS_TOTAL),RSI3Handle);

      return(INIT_SUCCEEDED);      


You have to know the exact name of the indicator and then use ChartIndicatorDelete() to remove it on deinit
 

A possible way hot to do that (when you know the names of the indicators)

input int MacdFast    = 12;  // Macd fast
input int MacdSlow    = 26;  // Macd slow
input int MacdSignal =  9;   // Macd signal
input ENUM_APPLIED_PRICE Price=PRICE_CLOSE; // Price

int OnInit(void)
{
   int macdHandle = iMACD(_Symbol,_Period,MacdFast,MacdSlow,MacdSignal,Price);
   if (macdHandle!=INVALID_HANDLE)
      ChartIndicatorAdd(0,(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL),macdHandle);
      return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
   for (int w = (int)ChartGetInteger(0,CHART_WINDOWS_TOTAL); w>=0; w--)
   {
      string name = ChartIndicatorName(0,w,0);
      if (StringFind(name,"MACD("+(string)MacdFast+","+(string)MacdSlow+","+(string)MacdSignal+")",0)==0)
            ChartIndicatorDelete(0,w,name);
   }      
}
void OnTick() { }
 
baraozemo:

 Hi,

 after added the ChartIndicatoradd function, the EA hangs in "backtest",

I think that I'm doing something wrong while passing the "timeframe" in the indicators.

 

 

Your code in the init section is completely wrong

"return" in the code means that all that is behind it is ignored making the Expert.Init() function never being called. Please revise your code (that code can not work normally neither in regular nor in back-test mode and it is not due to ChartIndicatorAdd() function or time frames passed to the indicators)


PS: even when the above coding issues in the init are resolved, ChartIndicatorAdd() will not work in back-test. It only works in real time (for multiple reasons)

 
baraozemo:

I know that the problem isn't the function charindicatoradd, but I need help to understand what I was doing wrong.. 

now , I added the   "//"  comment before the "return" line behind in all lines in "on init" 

 

 int RSI1Handle = iRSI(_Symbol,Signal_0_RSI_Timeframe,Signal_0_RSI_PeriodRSI,Signal_0_RSI_Applied);

   if (RSI1Handle!=INVALID_HANDLE)

      ChartIndicatorAdd(0,(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL),RSI1Handle);

===>      //return(INIT_SUCCEEDED);   

Now backtest is working again...thanks for the specialist "tips"   ;-)

but I have to revise something in "chartadd" because the "moving averages" and "wpr" aren't displayed in the main graph

Please transfer your questions here : https://www.mql5.com/en/forum/174385

This thread is more for solutions (and "snippets") than for coding questions