Jump to content

Indicator Help


petewright

Recommended Posts

Hi,

 

I am not the best at mql4 programming but I have a question.

 

I am using an indicator called N_StepMA_1 - attached

 

Now i have an EA that opens trades when the yellow line crosses the teal line of the indicator. I have that all working fine.

 

The problem is, afterthis happens and the next bar comes, you get a beep and then either a blue arrow or red arrow confirming this. When my EA trades, sometimes during a bar the lines can cross, but the bar closes without a signal (an arrow) to confirm it, which gives me a lot of false signals and I end up entering in the wrong direction.

 

I don't know the code to make sure there is a blue or red arrow to enter the trade. Is there a way I can trade on the arrow signals rather than the crossing of the lines?

 

Any help would be greatly appreciated

 

FOR SOME REASON I CAN'T UPLOAD THE ATTACHMENT

 

I will post the Indicator Code Here:

 

//+------------------------------------------------------------------+
//|                                                Nina_StepMA_1.mq4 |
//+------------------------------------------------------------------+
#property indicator_separate_window

#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Aqua
#property indicator_color3 Blue
#property indicator_color4 Red

#property indicator_minimum 0
#property indicator_maximum 1

//----input parameters
extern int PeriodWATR=10;
extern double Kwatr=1.0000;
extern int HighLow=0;
extern int CalculatedBars = 500;
extern bool alert_ON=true; // ON=true, OFF=false

//---- indicator buffers
double LineMinBuffer[];
double LineMidBuffer[];
double LineBuyBuffer[];
double LineSellBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
 {
 string short_name;
 IndicatorBuffers(4);
//---- indicator line
  SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
  SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
  SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,1);
  SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1);
  
  SetIndexEmptyValue(2,0);
  SetIndexArrow(2,233);
  SetIndexEmptyValue(3,0);
  SetIndexArrow(3,234);
  
  SetIndexBuffer(0,LineMinBuffer);
  SetIndexBuffer(1,LineMidBuffer);
  SetIndexBuffer(2,LineBuyBuffer);
  SetIndexBuffer(3,LineSellBuffer);

  IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
  short_name="StepMA_Stoch("+PeriodWATR+","+Kwatr+","+HighLow+")";
  IndicatorShortName(short_name);
//----
  SetIndexDrawBegin(0,PeriodWATR);
  SetIndexDrawBegin(1,PeriodWATR);
//----
  return(0);
 }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
 {
//----
  
//----
  return(0);
 }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
 {
  int      i,shift,TrendMin,TrendMax,TrendMid;
  double   SminMin0,SmaxMin0,SminMin1,SmaxMin1,SumRange,dK,WATR0,WATRmax,WATRmin,WATRmid;
  double   SminMax0,SmaxMax0,SminMax1,SmaxMax1,SminMid0,SmaxMid0,SminMid1,SmaxMid1;
  double   linemin,linemax,linemid,Stoch1,Stoch2,bsmin,bsmax;
  static bool turn_alarm = true;
  	
  int StepSizeMin,StepSizeMax,StepSizeMid;
  double min,max,mid,h,l,c;
  int position_ind0=0, position_ind1=0;
  // position_ind0 = -1, below
  //               =  1, up
  	
  ArrayInitialize(LineBuyBuffer,0);
  ArrayInitialize(LineSellBuffer,0);
 
  if(CalculatedBars>Bars || CalculatedBars == 0 ) CalculatedBars = Bars;

  for(shift=CalculatedBars-1;shift>=0;shift--){	
  SumRange=0;
  for (i=PeriodWATR-1;i>=0;i--){ 
        dK = 1+1.0*(PeriodWATR-i)/PeriodWATR;
        SumRange+= dK*MathAbs(High[i+shift]-Low[i+shift]);
        }
  WATR0 = SumRange/PeriodWATR;

  WATRmax=MathMax(WATR0,WATRmax);
  if (shift==CalculatedBars-1-PeriodWATR) WATRmin=WATR0;
  WATRmin=MathMin(WATR0,WATRmin);

  StepSizeMin=MathRound(Kwatr*WATRmin/Point);
  StepSizeMax=MathRound(Kwatr*WATRmax/Point);
  StepSizeMid=MathRound(Kwatr*0.5*(WATRmax+WATRmin)/Point);
  
    min = Kwatr*WATRmin;
    max = Kwatr*WATRmax;
    mid = Kwatr*0.5*(WATRmax+WATRmin);
  
  //b = iBarShift(Symbol(),tPeriod,Time[shift]);
  c = Close[shift];//iClose(Symbol(),tPeriod,b);
  h = High[shift];//iHigh(Symbol(),tPeriod,b)
  l = Low[shift];//iLow(Symbol(),tPeriod,b)
  
  if (HighLow>0){
    SmaxMin0=l+2*min;
    SminMin0=h-2*min;
  
    SmaxMax0=l+2*max;
    SminMax0=h-2*max;
  
    SmaxMid0=l+2*mid;
    SminMid0=h-2*mid;
  
    if(c>SmaxMin1) TrendMin=1; 
    if(c<SminMin1) TrendMin=-1;
  
    if(c>SmaxMax1) TrendMax=1; 
    if(c<SminMax1) TrendMax=-1;
  
    if(c>SmaxMid1) TrendMid=1; 
    if(c<SminMid1) TrendMid=-1;
    }
 
  if (HighLow == 0){
    SmaxMin0=c+2*min;
    SminMin0=c-2*min;
  
    SmaxMax0=c+2*max;
    SminMax0=c-2*max;
  
    SmaxMid0=c+2*mid;
    SminMid0=c-2*mid;
  
    if(c>SmaxMin1) TrendMin=1; 
    if(c<SminMin1) TrendMin=-1;
  
    if(c>SmaxMax1) TrendMax=1; 
    if(c<SminMax1) TrendMax=-1;
  
    if(c>SmaxMid1) TrendMid=1; 
    if(c<SminMid1) TrendMid=-1;
    }
 	
  if(TrendMin>0 && SminMin0<SminMin1) SminMin0=SminMin1;
  if(TrendMin<0 && SmaxMin0>SmaxMin1) SmaxMin0=SmaxMin1;
	
  if(TrendMax>0 && SminMax0<SminMax1) SminMax0=SminMax1;
  if(TrendMax<0 && SmaxMax0>SmaxMax1) SmaxMax0=SmaxMax1;
  
  if(TrendMid>0 && SminMid0<SminMid1) SminMid0=SminMid1;
  if(TrendMid<0 && SmaxMid0>SmaxMid1) SmaxMid0=SmaxMid1;
  
  
  if (TrendMin>0) linemin=SminMin0+min;
  if (TrendMin<0) linemin=SmaxMin0-min;
  
  if (TrendMax>0) linemax=SminMax0+max;
  if (TrendMax<0) linemax=SmaxMax0-max;
  
  if (TrendMid>0) linemid=SminMid0+mid;
  if (TrendMid<0) linemid=SmaxMid0-mid;
  
  bsmin=linemax-max;
  bsmax=linemax+max;
  Stoch1=(linemin-bsmin)/(bsmax-bsmin);
  Stoch2=(linemid-bsmin)/(bsmax-bsmin);
  
  LineMinBuffer[shift]=Stoch1;
  LineMidBuffer[shift]=Stoch2;
  
  
  SminMin1=SminMin0;
  SmaxMin1=SmaxMin0;
  
  SminMax1=SminMax0;
  SmaxMax1=SmaxMax0;
  
  SminMid1=SminMid0;
  SmaxMid1=SmaxMid0;
  
  // don't do anything for current bar since it is keeping changing.
  if(shift != 0) {  
  	  position_ind0 = position_ind1;
  	  if(LineMinBuffer[shift]>LineMidBuffer[shift]){
  	     position_ind1=1;
  	  }
  	  else {
  	     position_ind1=-1;
  	  }
  // current bar is keeping on changing
  // so don't put arrow on it
  // that means we only set on signal for confirmed bar
     if((position_ind0==-1) && (position_ind1==1)) {
     // cross up
        LineBuyBuffer[shift] = LineMidBuffer[shift];
     }
     if((position_ind0==1) && (position_ind1==-1)) {
     // cross down
        LineSellBuffer[shift] = LineMidBuffer[shift];
     }
   }
  }

if(Volume[0]>1) return(0);
// for the current crossing, make the sound
// still have problem for this mechanism, leave it for later modification	   
if(alert_ON) {   
     if(position_ind0*position_ind1<0) {  // time for alarm
        PlaySound("clock-alarm3.wav");
        turn_alarm=false;
     }
  }
     
return(0);	
}
//+------------------------------------------------------------------+

 

Regards

 

pete

Link to comment
Share on other sites

Re: Indicator Help

 

Hi petewright,

 

Good day,

 

Can you put in indicator on the chart and explain there. That would be a lot clearer.

 

If the problem is not complicated, I wouldn't mind to solve it. However, If it is, I can't help right now.

 

Best wishes,

a New Year 2011 has come, and the challenge has just started 8-)
Link to comment
Share on other sites

Re: Indicator Help

 

Here is the Picture

 

Direct Link for bigger picture here: http://img51.imageshack.us/img51/6730/stepma.jpg

 

http://img51.imageshack.us/img51/6730/stepma.jpg

 

Thanks Scarface

 

Regards

 

Pete

 

-- 19 Dec 2009, 21:58 --

 

I believe I have solved this now by using the indicators buffers :D.

 

Thanks for the interest though I really appreciate it.

 

Regards

Link to comment
Share on other sites

Re: Indicator Help

 

 

Now i have an EA that opens trades when the yellow line crosses the teal line of the indicator. I have that all working fine.

 

The problem is, afterthis happens and the next bar comes, you get a beep and then either a blue arrow or red arrow confirming this. When my EA trades, sometimes during a bar the lines can cross, but the bar closes without a signal (an arrow) to confirm it, which gives me a lot of false signals and I end up entering in the wrong direction.

[/code]

 

Hi petewright,

 

Good day,

 

Thanks for posting the picture of the indicator.

 

I have tested the indicator and checked the code as well, and there is nothing wrong with it. The problem you are referring to is actually not a problem. what does that mean?? here is the explanation below:

 

When the cross happen for example at bar[0], the arrow show up at the next bar[1] and if you use EA it will trade at the next next bar[2], which is a late or false signal.

 

This is actually what's happening. Sometimes the cross happen at bar[0], but suddenly reverse to the other direction at bar[1] which cause the arrow not to show up, and this is normal because of the condition of the indicator itself.

 

If you want to use this indicator for your EA, you need to change the code slightly to the cross, not the indicator you use.

 

so in this case the EA will trade at bar[1] instead of bar[2] since you based your EA on the cross.

 

I hope this is clear.

 

Best wishes,

a New Year 2011 has come, and the challenge has just started 8-)
Link to comment
Share on other sites

Re: Indicator Help

 

Hi Scarface,

 

Thanks for getting back to me.

 

Initially I had my EA trading on the cross of the lines, but now using the indicator buffers I have managed to get it to trade as soon as the arrow appears.

 

So although the signal is late, is it not a whole extra bar late if you get me.

 

Arrow appears = Enters trade straight away. It doesn't enter trade on the bar after the arrow, so all seems to be working quite well.

 

I have been backtesting my EA using a trailing stop, and it seems to be working quite well on most currency pairs.

 

I have been using H4 for my testing with 0 stoploss and for it to cancel / open a new trade on signal reversal.

 

It seems to take over 3 hours for a one year backtest thought on H4 timeframe :S. I think this has a lot to do with the indicator coding. I am not sure how to speed this up, so it is very slow going and I cannot really optimize it using the optimizer in MT4.

 

So far I have tested EURUSD on H4 and I am getting about 20% drawdown with a 100% profit.

So nothing amazing but I would much prefer to use this indicator on lower timeframes such as M30. I think that it seems to enter the trades slightly late. Again, if only backtesting was faster then I could try and optimize stoplosses and trailing stops.

 

Do you have any idea how to speed it up at all?

 

Again, thanks for your responses you have been very helpful.

 

Regards

Link to comment
Share on other sites

Re: Indicator Help

 

Hi,

 

Good day,

 

There is a quick solution to speed up the backtest. If you use the indicator with your EA, reduce the counted bars to less than 100 so the indicator doesn't take long time to recalculate again and again.

 

By the way, your EA uses this indicator as iCustom or the whole indicator is written in the EA?

 

Please let me know.

 

P.S if you want to thank me, you can give me a Kudo by clicking the green button of the right side of the post. :)

 

I hope this help.

 

Best wishes,

a New Year 2011 has come, and the challenge has just started 8-)
Link to comment
Share on other sites

Re: Indicator Help

 

Hello again,

 

I am using iCustom as I am totally unsure how to write the indicator into the EAi had a quick look into it and got lost within 5 minutes :P

 

I will try reducing the calculated bars from 500 to 100 and see if it makes a difference. Thanks again :)))

 

-- EDIT -- I'm not sure it really speeds up the backtest much. It seems to take just as long :(

Although.. I seem to be getting better results with the bars set at 100 than 500! And I think the backtest is faster so ignore previous statement :)

But not fast enough for optimization

Link to comment
Share on other sites

Re: Indicator Help

 

Hi,

 

Good day,

 

I know that some EA re-calculate previous bars many times and verify after each position and this is a reason why it takes time to backtest or even to optimize.

 

I guess you know that iCustom function in the EA cause the Backtest to slow down because it calls many times.

 

Another thing you should know that It also uses a lot of memory and no wonder sometimes why your PC gets slow as well.

 

I hope you find this info useful.

 

Best wishes,

a New Year 2011 has come, and the challenge has just started 8-)
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...