Adding a Simple Alert

 

Hello Everyone,


I was hoping you could help me add a simple alert to a code. I have tried many times, but it seems that I cannot get it right.


This particular indicator makes trendlines the way that I like trendlines to be made. I base most of my trades off of this. Usually the currency price is between the upper and lower trendlines. However, I would like to be emailed every time the price breaks out of a trendline (I would actually use my cell/text email address).


This would be simple to code, or I thought it would be simple to code, because all you would need to do is check the current price, the upper trendline price, and the lower trendline price. Usually the price is between both trendlines. If the price breaks above the upper trendline before it is recalculated, then the price would be above the trendlines. If the price breaks below the lower trendline before it's recalculated, then the price is going to be below the two trendlines.


I would just like a simple alert that says "current price for xxx/yyy just went above upper trendline" or "current price for xxx/yyy just went below the lower trendline"


Since I am going to use this on different currency pairs and different time frames, I think that might need to be added into the code; or maybe if the chart is just open it would be fine.


Can anyone please help and point me in the right direction. I can't seem to do this correctly. Here is the original code. THANKS!


extern int cnt = 50;
extern color HHTL_color = DodgerBlue;
extern color LLTL_color = DeepPink;

string prefix = "xTop_";

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init() {
  clear();
  show();
}

void deinit() {
  clear();
}

void start() 
{
  show();
}

void show() {
  
  string name = "";

  double fr1, fr2;
  int bar1, bar2;

  int i = 2;
  for (int j=0; j < cnt; j++) {
  
    for (; i < Bars; i++) {
      fr1 = iFractals(NULL, 0, MODE_UPPER, i);
      if (fr1 > 0) {
        bar1 = i;
        break;
      }
    }

    for (i=i+2; i < Bars; i++) {
      fr2 = iFractals(NULL, 0, MODE_UPPER, i);
      if (fr2 > 0) {
        bar2 = i;
        break;
      }
    }
  

    name = prefix + "HHTL" + j;
    if (ObjectFind(name) == -1) {
      ObjectCreate(name, OBJ_TREND, 0, Time[bar2], fr2, Time[bar1], fr1);
      ObjectSet(name, OBJPROP_COLOR, HHTL_color);
      ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(name, OBJPROP_WIDTH, 0);
      ObjectSet(name, OBJPROP_RAY, j==0);
    }
    else
    {
      ObjectMove(name, 0, Time[bar2], fr2);
      ObjectMove(name, 1, Time[bar1], fr1);
    }
  }

  i = 2;
  for (j=0; j < cnt; j++) {
    for (; i < Bars; i++) {
      fr1 = iFractals(NULL, 0, MODE_LOWER, i);
      if (fr1 > 0) {
        bar1 = i;
        break;
      }
    }

    for (i=i+2; i < Bars; i++) {
      fr2 = iFractals(NULL, 0, MODE_LOWER, i);
      if (fr2 > 0) {
        bar2 = i;
        break;
      }
    }

    name = prefix + "LLTL" + j;
    if (ObjectFind(name) == -1) {
      ObjectCreate(name, OBJ_TREND, 0, Time[bar2], fr2, Time[bar1], fr1);
      ObjectSet(name, OBJPROP_COLOR, LLTL_color);
      ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(name, OBJPROP_WIDTH, 0);
      ObjectSet(name, OBJPROP_RAY, j==0);
    }
    else
    {
      ObjectMove(name, 0, Time[bar2], fr2);
      ObjectMove(name, 1, Time[bar1], fr1);
    }
  }  
}

void clear() 
{
  string name = "";
  int res = -1;

  for (int j=0; j < cnt; j++) {  
    name = prefix + "HHTL" + j;
    if (ObjectFind(name) != -1) ObjectDelete(name);

    name = prefix + "LLTL" + j;
    if (ObjectFind(name) != -1) ObjectDelete(name);
  }
}
 
  1. jamesmean:
    Since I am going to use this on different currency pairs and different time frames, I think that might need to be added into the code; or maybe if the chart is just open it would be fine.
    Pairs and TFs are irrelevant. The indicator run on it's own chart/TF/pair and is independent on any other.
  2. jamesmean:
    I would just like a simple alert that says "current price for xxx/yyy just went above upper trendline" or "current price for xxx/yyy just went below the lower trendline"
    int start(){
        string name = prefix + "HHTL0";
        if (ObjectFind(name) != -1) {
            double tl = ObjectGetValueByShift(name, 0);
            static time0H;
            if (Bid > tl && time0H != Time[0]){ 
                time0H = Time[0];   // One alert per bar.
                Alert("Cross upper");
            }
        }
        name = prefix + "LLTL0";
        if (ObjectFind(name) != -1) {
            double tl = ObjectGetValueByShift(name, 0);
            static time0L;
            if (Bid < tl && time0L != Time[0]){ 
                time0L = Time[0];   // One alert per bar.
                Alert("Cross lower");
            }
        }
    ...
>
 

Thank you very much! I am going to test it out on Sunday when the market opens back up.


If I wanted to be emailed every time the alert happens, do I just add my email address after the "Alert ("Cross lower")" ?


Thanks again!

 
SendMail
 

Hello again,


I really didn't want to keep on writing and have been trying to figure this out on my own. However, it doesn't seem to be working for me. Can you please check my code again.


I had put the code that you made at the bottom of the code I posted, in the middle of it, after the start, and a few other places; and it still cannot compile. I keep on getting the same errors (function already defined, unbalanced parameters, etc. etc.)


I tried to fix it on my own, but I still cannot figure it out.


Can you please help me?


ALSO, I added in the sendmail function and that doesn't work either (most likely because the rest of the code doesn't work). Can you please see if I added the sendmail function correctly; and how should I input this code into the original one.


Please help, THANKS!


int start(){
    string name = prefix + "HHTL0";
    if (ObjectFind(name) != -1) {
        double tl = ObjectGetValueByShift(name, 0);
        static time0H;
        if (Bid > tl && time0H != Time[0]){ 
            time0H = Time[0];   // One alert per bar.
            Alert("Cross upper");
        if(Bid > tl && time0H != Time[0])
            SendMail("from your expert", "Price went up to "+DoubleToStr(lastclose,Digits));
        }
    }
    name = prefix + "LLTL0";
    if (ObjectFind(name) != -1) {
        double tl = ObjectGetValueByShift(name, 0);
        static time0L;
        if (Bid < tl && time0L != Time[0]){ 
            time0L = Time[0];   // One alert per bar.
            Alert("Cross lower");
        if(Bid < tl && time0L != Time[0])
            SendMail("from your expert", "Price dropped down to "+DoubleToStr(lastclose,Digits));
        }
    }
 

Also, do you think it would be hard to use a screen capture and be emailed to me every time the alert goes off? I would be completely satisfied if someone helped me make this indicator work with sendmail; but if it's possible to send a screen shot as well I would be in heaven!


THANKS!