Download MetaTrader 5

Asking help for a coding...

To add comments, please log in or register
apremarathna225
28
apremarathna225  

I am new to coding and still try to learn it.  Can somebody tell me how to do this. The case is there is a  EMA with period of 9 and I need to get a signal after crossing that EMA from current candle close. I appreciate your kind help. thank you.



image

whroeder1
15917
whroeder1  
  1. Don't double post.
  2. Help you with what? You haven't stated a problem. Just code it:
    double emaPrev = iMA(..., i+1);
    double cloPrev = Close[   i+1];
    double emaCurr = iMA(..., i  );
    double cloCurr = Close[   i  ];
    bool wasAbove = cloPrev > emaPrev;
    bool  isAbove = cloCurr > emaCurr;
    bool  isCross = wasAbove != isAbove;
    
apremarathna225
28
apremarathna225  
whroeder1:
  1. Don't double post.
  2. Help you with what? You haven't stated a problem. Just code it:

No friend I have started. I can show you code also. I did not show it here b.coz it is a mess. see the results from the picture. image


Keith Watford
Moderator
10651
Keith Watford  

I have deleted your other 2 topics that were simply duplicates of this one.

Do not post duplicated topics.

Achmad Wijaya
696
Achmad Wijaya  
apremarathna225:

I am new to coding and still try to learn it.  Can somebody tell me how to do this. The case is there is a  EMA with period of 9 and I need to get a signal after crossing that EMA from current candle close. I appreciate your kind help. thank you.

 

hello, you can see the sample of moving average built in on your MT4, there was a logic for it

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditions
   if(Open[1]>ma && Close[1]<ma)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(Open[1]<ma && Close[1]>ma)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }
whroeder1
15917
whroeder1  
Achmad Wijaya:
if(Volume[0]>1) return;
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
Achmad Wijaya
696
Achmad Wijaya  
whroeder1:
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
let's see when market open in Monday, just check MT4 on M1 or M5, is that volume give us information about Tick ? or Trade of volume actually ?
apremarathna225
28
apremarathna225  

No friend I have started. I can show you code also. I did not show it here b.coz it is a mess. see the results from the picture. 


image

apremarathna225
28
apremarathna225  
whroeder1:

Stop double posting.

Am I correct ?????? 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//----
double CrossUp[];
double CrossDown[];
//double prevtime;
double Range,AvgRange;
double fasterMAnow_1;

extern int FasterMA    =   9;
extern int FasterShift =   0;
extern int FasterMode=1;
extern int MediumMA    =  9;
extern int MediumShift =  0;
extern int MediumMode=1; 


int init()
  {

   SetIndexStyle(0,DRAW_ARROW,EMPTY);
   SetIndexArrow(0,233);
   SetIndexBuffer(0,CrossUp);
   SetIndexStyle(1,DRAW_ARROW,EMPTY);
   SetIndexArrow(1,234);
   SetIndexBuffer(1,CrossDown);

   return(0);
  }

int deinit()
  {

   return(0);
  }

int start()
  {
   int i,counter;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
  if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+9;
//----   
   for(i=0; i<=limit; i++)
     {
      counter=i;
      Range=0;
      AvgRange=0;
      for(counter=i;counter<=i+9;counter++)
        {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
        }
     Range=AvgRange/10;
      //----       
     fasterMAnow_1      = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_OPEN, i);
    
      if((Open[i]<fasterMAnow_1)&& (Close[i]>fasterMAnow_1))
       {
        CrossUp[i]=Low[i]-Range*0.5;
       }
     if((Open[i]>fasterMAnow_1)&& (Close[i]<fasterMAnow_1))
       {
        CrossDown[i]=High[i]+Range*0.5;
       }
    }
  return(0);
 }
 

Keith Watford
Moderator
10651
Keith Watford  

You are checking everytick so if the condition is true sometime during the life of the bar, you will get an arrow.

Either only check when a bar closes or ke sure to remove arrows if the condition is no longer true.

To add comments, please log in or register