Download MetaTrader 5

Would you please help me in mql to print indicators

To add comments, please log in or register
What is the best place to find a programmer? Freelance service!
mareks1
131
mareks1 2015.05.12 14:46 

Hi, I am trying to lern mql and trying to print indicator on the actual candle [i] and the same on history data.

I can not find why this simple test doesn't wok. It prints random historical labels when [i+2] changed into [i-2] bu this is obiously wrong.

Please help

int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],const double &open[],const double &high[],
                const double &low[],const double &close[],const long &tick_volume[],const long &volume[],const int &spread[])
  {
  //new bar
if (Time[0] == prevtime) return(rates_total);
   prevtime = Time[0];

//test
if (Close[2]<Open[2]) draw_labe(code_120_down,color_120_down,0);

//History
if(draw_labels && show_history_labels)
{
for(int i=Bars-IndicatorCounted()-1;i>0;i--)

{
//test
if (Close[i+2]<Open[i+2]) draw_labe(code_120_down,color_120_down,i);

}//end for

}

   return(rates_total);
  }
 
  //func
//////////////////////////////////////////////////////////////////
void draw_labe(int code,color col,int f)
{
string name=identif+string(Time[f]);

if(ObjectFind(name)==-1)
{
ObjectCreate(name,OBJ_ARROW,0,Time[f],Open[f]);
ObjectSet(name,OBJPROP_ARROWCODE,code);
ObjectSet(name,OBJPROP_COLOR,col);
ObjectSet(name,OBJPROP_WIDTH,label_width);
}
Alain Verleyen
Moderator
30739
Alain Verleyen 2015.05.12 14:49  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


mareks1
131
mareks1 2015.05.12 14:52  
Alain Verleyen:

I am sorry, I will
Vladimir Karputov
Moderator
45955
Vladimir Karputov 2015.05.12 16:18  
Your code is incomplete. Your code contains errors. It mql5 or mql5?
Alain Verleyen
Moderator
30739
Alain Verleyen 2015.05.12 16:30  
Karputov Vladimir:
Your code is incomplete. Your code contains errors. It mql5 or mql5?
It's mql4.
Vladimir Karputov
Moderator
45955
Vladimir Karputov 2015.05.12 16:38  
Alain Verleyen :
It's mql4.

Thank you. But still, a lot of mistakes:

'prevtime' - undeclared identifier                  	test_indicator.mq4
'code_120_down' - undeclared identifier       		test_indicator.mq4
'color_120_down' - undeclared identifier              	test_indicator.mq4
'draw_labels' - undeclared identifier            	test_indicator.mq4
'show_history_labels' - undeclared identifier   	test_indicator.mq4
'code_120_down' - undeclared identifier       		test_indicator.mq4
'color_120_down' - undeclared identifier              	test_indicator.mq4
'identif' - undeclared identifier                   	test_indicator.mq4
'label_width' - undeclared identifier            	test_indicator.mq4
9 error(s), 0 warning(s)
mareks1
131
mareks1 2015.05.12 17:32  
Karputov Vladimir:

Thank you. But still, a lot of mistakes:

Yes, it is just a part of code, but errors ar in this part, regarding to the conditions [0], [i], [2], [i+2]. When I make calculations based on iMAs it prints indicators.

Formula is just for test if it prints well

Here is all code:

#property copyright "ee"
#property link      "http://"
#property version   "2.1"
#property strict
#property indicator_chart_window

#property indicator_buffers 0


extern string up_alert = "Call, time 60 sec.";
extern string up_alert2 = "Call, time 90 sec.";
extern string down_alert = "Put, time 60 sec.";
extern string down_alert2 = "Put, time 90 sec.";


extern int code_110_down = 226; 
extern color color_110_down = clrRed;
extern int code_110_up = 225;
extern color color_110_up = clrBlue;


extern int code_120_down = 120; // Skrzynki
extern color color_120_down = clrRed;
extern int code_120_up = 121;
extern color color_120_up = clrBlue;

extern string emp1 = "/////////////////////////////////////";
extern string la_set = "Labels settings";
extern bool draw_labels = true;
extern bool show_history_labels = true;
extern int label_width = 2;
extern int code_60_up = 169;
extern color color_60_up = clrBlue;
extern int code_60_down = 169;
extern color color_60_down = clrRed;
extern int code_90_up = 170;
extern color color_90_up = clrBlue;
extern int code_90_down = 170;
extern color color_90_down = clrRed;

extern string emp2 = "/////////////////////////////////////";
extern string ma_set = "MA's settings";
extern int Min_MAs_distance = 40;
extern string ind1_name = "Moving average";
extern int ma_fast_period = 6;
extern ENUM_MA_METHOD ma_fast_method = 1;
extern ENUM_APPLIED_PRICE ma_fast_price = 0;
extern int ma_slow_period = 26;
extern ENUM_MA_METHOD ma_slow_method = 1;
extern ENUM_APPLIED_PRICE ma_slow_price = 0;

extern string ind2_name = "buzzer";
extern int     Price          = 0;  
extern int     Length         = 20;
input ENUM_TIMEFRAMES TIME_FRAMES=0;
extern bool AlertON=false;
extern bool EmailON=false;



datetime prevtime;
string identif="binal_arr";



//////////////////////////////////////////////////////////////
void OnDeinit(const int reason)
{
string name_delete;
for(int i=ObjectsTotal()-1;i>=0;i--)
{
name_delete=ObjectName(i);
if(StringFind(name_delete,identif)!=-1) ObjectDelete(name_delete);
}

}

//////////////////////////////////////////////////////////////////
int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],const double &open[],const double &high[],
                const double &low[],const double &close[],const long &tick_volume[],const long &volume[],const int &spread[])
  {
  //new bar
if (Time[0] == prevtime) return(rates_total);
   prevtime = Time[0];

//test
if (Close[2]<Open[2]) draw_labe(code_120_down,color_120_down,0);


//History
if(draw_labels && show_history_labels)
{
for(int i=Bars-IndicatorCounted()-1;i>0;i--)
//for ( int i = Bars - IndicatorCounted(); i >= 0; i-- ) 
{
//test
if (Close[2]<Open[2]) draw_labe(code_120_down,color_120_down,i);

}//end for

}

   return(rates_total);
  }
 
  //func
//////////////////////////////////////////////////////////////////
void draw_labe(int code,color col,int f)
{
string name=identif+string(Time[f]);

if(ObjectFind(name)==-1)
{
ObjectCreate(name,OBJ_ARROW,0,Time[f],Open[f]);
ObjectSet(name,OBJPROP_ARROWCODE,code);
ObjectSet(name,OBJPROP_COLOR,col);
ObjectSet(name,OBJPROP_WIDTH,label_width);
}


}

Vladimir Karputov
Moderator
45955
Vladimir Karputov 2015.05.12 18:02  
Your goal: to draw array just for the bar [0] and Bar [1]?
mareks1
131
mareks1 2015.05.12 18:28  
Karputov Vladimir:
Your goal: to draw array just for the bar [0] and Bar [1]?

Hi Vladimir

The goal will be to analise 2-4 bars back and print an indicator on just opened bar, but in this test just wont to print indicator when bar prev 2 is down.

Will be something like this:

// just ilustration without logical sens

if((Close[2]=Open[1])&&(Close[3]>Open[3])&&....) draw_labe(code_120_down,color_120_down,0);

History is for fast testing different strategies.

Vladimir Karputov
Moderator
45955
Vladimir Karputov 2015.05.12 19:12  
mareks1 :

Hi Vladimir

The goal will be to analise 2-4 bars back and print an indicator on just opened bar, but in this test just wont to print indicator when bar prev 2 is down.

History is for fast testing different strategies.

You just need to use only four names for the label: "zero", "first", "second" and "third":

1 

 At each new bar the label are being redrawn. Their will always be only four.

mareks1
131
mareks1 2015.05.12 20:09  

First, thank you very much for your time.

As you can see, there are two parts:

1.  named //new bar - draw indicator on new bar if xyz - in real time, normal work

2. named //History - draw indicator on the bar [i] when bar [i+1] like xx && bar [i+2] like xyz... where bar[i] is any  bar related to its bars [i+1], [i+2]....

Becase Close and Open in the past do not change, if it can draw on history properly, backtesting is not needed and it is easy to alalise what to change in the algiritm (instead of the situation when the indicator analise actual bar also).

It should be like this when "extern bool show_history_labels = true;"


The picture is generatet by compile when conditions as below, so it show historical situation.

//test

if (Close[i]<Open[i]) draw_labe(code_120_down,color_120_down,i);

Perfect.

It dranwn down signal on every bar [i] wich was down. But I wont it to draw a signal on the bar [i] only if the bar [i+2] to the bar [i] is down .

For some reasons it can not understand [i+1], [i+2]..... and do not draw.

For some reasons it draw real time signal just once after compilations.



12
To add comments, please log in or register