Using onchartevent in an advisor

David  

Hi

I've done a lot of digging around and can't find anything similar to this - which may mean that either my understanding/coding ability isn't good enough (I'm a 'cut and paste' coder) or this isn't possible.

What I'm doing is building an EA which uses a series of boolean to determine whether conditions are right to place a trade. i.e. if A happens, boolean 1=True, then if B happens whilst boolean 1 is true, boolean 2 is true and so on (so for example, if a MA cross happens, then stochastic >80 and then price returns to the MA and so on). The code has 6 booleans so far and I want to add a 7th and make a couple of them possible either way round. Additionally, I've got reset boolean to false triggers in there.

As its complex there's potential either for my coding to be wrong or for it trigger when I didn't expect it to. Working all this out I'm fine with - but what I'm trying to do is track it.

I can load the current status of the booleans to a comment field and see them no problems. What I want to be able to do is see what status the booleans had at any point in the past (or at least whilst it's been running on a demo account).

I have lifted the code from iCrosshair and put that in an EA - which works fine for seeing the values (for a given candle) for the arrays in iCrosshair (open, close etc - or something derived from those).

If I replace the code in iCrosshair with a calculation of one of my booleans, I can also get that to work (see example in the attached/embedded code). What I want to be able o do though is pass the value of the boolean as it was at that candle to the comment box (I may try to get funkier after that and do something graphical) - which I haven't been able to do.

The reason for wanting to do it that way is because the value of the later booleans are conditional on the earlier ones - so it not just as simple of copying the code (which would be messy for any code changes).

Does anybody know if this is possible and if so do you have any pointers as to how to go about doing it? I suspect if it is then I need to get to grips with arrays - but my use of arrays has been in basic indicators so far - nothing as complex as this!

So in the example attached (which I have stripped down to just one boolean - an MA cross) I want ' double EMA50AboveEMA100(int i)' to use the result created by 'void void_buy1_EMA50_Above_EMA100()' rather than to recreate the result

Thanks in advance for any comments :)

David


#property strict
#property indicator_chart_window



double pips2double;
bool boolean_EMA50_Above_EMA100 = False;



//cross hair
input bool            ShowTooltip = true; // Show Tooltip
input bool            ShowComment = true; // Show Comment
input color           Color  = clrTomato; // Line color
input ENUM_LINE_STYLE Style  = 2;         // Line style
input int             Width  = 1;         // Line width


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Draw("H Line",OBJ_HLINE,Time[0],High[0],Color,Style,Width,"Click me!");
   Draw("V Line",OBJ_VLINE,Time[0],High[0],Color,Style,Width,"Click me!");
   

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectDelete("H Line");
   ObjectDelete("V Line");
   
  }
  
  //+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(!ShowComment) Comment("");
   static bool keyPressed=false;
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="H Line" || sparam=="V Line")
        {
         if(!keyPressed) keyPressed=true;
         else keyPressed=false;
        }
      if(keyPressed) ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);
      else ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,false);
     }
//--- this is an event of a mouse move on the chart
   if(id==CHARTEVENT_MOUSE_MOVE)
     {
      //--- Prepare variables
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime time  =0;
      double   price =0;
      int      window=0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(ChartXYToTimePrice(0,x,y,window,time,price))
        {
         int bar=iBarShift(NULL,0,time);
         string tooltip="";
         if(ShowTooltip)
           {
            tooltip="EMA50 above EMA 100:  "+DoubleToString(EMA50AboveEMA100(bar),0);
            
           }
         //--- draw lines
         Draw("H Line",OBJ_HLINE,time,price,Color,Style,Width,tooltip);
         Draw("V Line",OBJ_VLINE,time,price,Color,Style,Width,tooltip);
         if(ShowComment)
           {
            double Pips=fabs(price-Close[0])/Point;
            Comment((string)bar+" / "+DoubleToStr(Pips,0)+" / "+DoubleToStr(price,Digits));
           }
        }
     }
   if(id==CHARTEVENT_CLICK && ShowComment)
     {
      //--- Prepare variables
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime time  =0;
      double   price =0;
      int      window=0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(ChartXYToTimePrice(0,x,y,window,time,price))
        {
         datetime NewTime=(datetime)ObjectGet("V Line",OBJPROP_TIME1);
         double NewPrice=ObjectGet("H Line",OBJPROP_PRICE1);
         int NewBar=iBarShift(NULL,0,NewTime)-iBarShift(NULL,0,time);
         double NewPips=fabs(price-NewPrice)/Point;
         Comment((string)NewBar+" / "+DoubleToStr(NewPips,0)+" / "+DoubleToStr(price,Digits));
        }
     }
//---
  }

//+------------------------------------------------------------------+



  double EMA50AboveEMA100(int i)
  {
   double FastMA=iMA(Symbol(),Period(),50,0,1,PRICE_MEDIAN,i);
   double SlowMA=iMA(Symbol(),Period(),100,0,1,PRICE_MEDIAN,i);
   double FastMA_Minus_SlowMA=FastMA-SlowMA;
   if(FastMA_Minus_SlowMA>0)
   return(boolean_EMA50_Above_EMA100);
   else
   return(False);



  }
  
//+------------------------------------------------------------------+

void Draw(string name,int type,datetime time,double price,color clr,int style,int width,string tooltip)
  {
//---
   ObjectDelete(0,name);
   ObjectCreate(0,name,type,0,time,price);
   ObjectSet(name,OBJPROP_COLOR,clr);
   ObjectSet(name,OBJPROP_STYLE,style);
   ObjectSet(name,OBJPROP_WIDTH,width);
   ObjectSet(name,OBJPROP_BACK,false);
   ObjectSet(name,OBJPROP_ZORDER,0);
   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);
   ChartRedraw(0);
//---
  }
//+------------------------------------------------------------------+




//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//buy//
  void_buy1_EMA50_Above_EMA100();
 
  //other voids removed for simplicity 
   
    
 }
 
//+------------------------------------------------------------------+
//| Buy trigger functions                                            |
//+------------------------------------------------------------------+

// BUY VOID 1 - EMA cross ///

void void_buy1_EMA50_Above_EMA100()

{

double EMA50_Current=iMA(NULL,0,50,0,1,4,0);
double EMA100_Current=iMA(NULL,0,100,0,1,4,0);


if(EMA50_Current>=EMA100_Current)
boolean_EMA50_Above_EMA100=True;
else
boolean_EMA50_Above_EMA100=False;

//boolean passed onto void two for use in next check

}
iCrosshair
iCrosshair
  • www.mql5.com
To make our life easier, this indicator will draw 2 lines (Vertical - Horizontal) acting like normal crosshair with extra bars information. On mouse hover at any bar will show: Added: Option to disable the tooltip. Added: a new Comment on the chart will display ( / / ) calculation. Just like how the default crosshair calculates these values but...
Reason: