Jump to content
Tom Next - Daytrading Community

SuperTrend Iindikator


mmsknecht

Recommended Posts

Hallo Zusammen,

 

ich verwende beim traden auch den „SuperTrend“ Indikator und versuche einen Alarm einzubauen, wenn ein Signal wechselt: z.B. Grüne Linie zu Roter Linie oder Rote Line zu Grüner Linie.

 

Leider bekomme ich es nicht hin dass beim Richtungswechsel einmal die „alert.wav“ ausgelöst wird.

 

Könnt ihr mir weiterhelfen?

 

Danke vorab

 

//SuperTrend Indikator

#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_buffers 2
double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods = 10;
extern double Multiplier = 3.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(0, "Trend Up");
   SetIndexBuffer(1, TrendDown);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(1, "Trend Down");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit, i, flag, flagh, trend[5000];
   double up[5000], dn[5000], medianPrice, atr;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   limit=Bars-counted_bars;
   //Print(limit);
   
//----
   for (i = Bars; i >= 0; i--) {
      TrendUp[i] = EMPTY_VALUE;
      TrendDown[i] = EMPTY_VALUE;
      atr = iATR(NULL, 0, Nbr_Periods, i);
      //Print("atr: "+atr[i]);
      medianPrice = (High[i]+Low[i])/2;
      //Print("medianPrice: "+medianPrice[i]);
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;
   
      
      if (Close[i]>up[i+1]) {
         trend[i]=1;
         if (trend[i+1] == -1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
         
      }
      else if (Close[i]<dn[i+1]) {
         trend[i]=-1;
         if (trend[i+1] == 1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
      }
      else if (trend[i+1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i+1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i+1]>0) {
         flag=1;
         //Print("flag: "+flag);
      }
      else {
         flag=0;
         //Print("flagh: "+flag);
      }
      
      if (trend[i]>0 && trend[i+1]<0) {
         flagh=1;
         //Print("flagh: "+flagh);
      }
      else {
         flagh=0;
         //Print("flagh: "+flagh);
      }
      
      if (trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];
      
      if (trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];
      
      if (flag==1)
         up[i]=medianPrice+(Multiplier*atr);
         
      if (flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);
         
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            changeOfTrend = 0;
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+

 

post-2074-0-60434900-1382527723_thumb.jpg

Link to comment
Share on other sites

Danke Wolf,

 

wenn ich den alert allerdings an diese Stelle einbau, klingelt er permanent!?

 

    //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            PlaySound("alert.wav");
            changeOfTrend = 0;
           
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            PlaySound("alert.wav");
            changeOfTrend = 0;
            
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
Link to comment
Share on other sites

Bitte, gerne.

 

Diese Stellen werden nur bei Trendwechsel aufgerufen, der Sound sollte also nur einmalig beim Trendwechsel aufgerufen werden.

Zuerst wird ja ChangeOfTrend auf 1 abgefragt und danach auf 0 gesetzt.

 

Sollte ChangeOfTrend beim nächsten Tick wieder auf 1 gesezt werden, dann stimmt was an der Logik vom Supertrend-Indikator nicht.

 

 

  • Upvote 1
Link to comment
Share on other sites

Also wenn er z.B. im M5 ein neues Signal produziert, kommt der alert mit jedem Tick und das fünf Minuten lang? kb-smile.gif

 

Ich weiß nicht, dass hört sich bei mir irgendwie anders an. Teste es mal auf kurz auf dem M1, bei mir brummen da nur die Boxen, aber kein alert.wav?

 

#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_buffers 2
double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods = 10;
extern double Multiplier = 3.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(0, "Trend Up");
   SetIndexBuffer(1, TrendDown);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(1, "Trend Down");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit, i, flag, flagh, trend[5000];
   double up[5000], dn[5000], medianPrice, atr;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   limit=Bars-counted_bars;
   //Print(limit);
   
   

 
//----
   for (i = Bars; i >= 0; i--) {
      TrendUp[i] = EMPTY_VALUE;
      TrendDown[i] = EMPTY_VALUE;
      atr = iATR(NULL, 0, Nbr_Periods, i);
      //Print("atr: "+atr[i]);
      medianPrice = (High[i]+Low[i])/2;
      //Print("medianPrice: "+medianPrice[i]);
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;
   
      
      if (Close[i]>up[i+1]) {
         trend[i]=1;
         if (trend[i+1] == -1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
         
      }
      else if (Close[i]<dn[i+1]) {
         trend[i]=-1;
         if (trend[i+1] == 1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
      }
      else if (trend[i+1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i+1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i+1]>0) {
         flag=1;
         //Print("flag: "+flag);
      }
      else {
         flag=0;
         //Print("flagh: "+flag);
      }
      
      if (trend[i]>0 && trend[i+1]<0) {
         flagh=1;
         //Print("flagh: "+flagh);
      }
      else {
         flagh=0;
         //Print("flagh: "+flagh);
      }
      
      if (trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];
      
      if (trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];
      
      if (flag==1)
         up[i]=medianPrice+(Multiplier*atr);
         
      if (flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);
         
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            PlaySound("alert.wav");
            changeOfTrend = 0;
           
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            PlaySound("alert.wav");
            changeOfTrend = 0;
            
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
Edited by mmsknecht
Link to comment
Share on other sites

Habe den Indi aus Post # 6 grade mal getestet.

Es kommt bei jedem Tick ein PiepTon, aber der eigentliche Alarmton (Original) kommt nur in unregelmässigen Abständen, meist bei einem neuen Tief/Hoch.

Schmeisse ich den Indi dann aus dem Chart, läuft der Ton und der Alarm noch ca. 20 sekunden im Chart weiter, obwohl der Indikator sich gar nicht mehr in diesem befindet.

Link to comment
Share on other sites

Die "alert.wav" ist ein Standartsound im Mt4-Pfad, deswegen sollte es eigentlich gehen?

 

Freue mich auf deinen Test.

klar, der ist ein Standardsound, aber der hätte ja auch in Deiner Installation aus irgendeinem Grunde fehlen können und dann hättest Du den Effekt, das gar kein Ton kommt.

Das wollte ich nur sicherheitshalber verifizieren.

 

Ich habe jetzt die static-Variable AlertTime eingebaut und zusätzlich noch eine Abfrage auf i==0, sodaß nur beim letzten Trendwechsel "gebimmelt" wird.

 

Habe paar Minuten gewartet, aber da war gerade kein Trendwechsel in Realtime, versuchs doch mal bitte, ob es jetzt funktioniert.

 

#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_buffers 2
double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods = 10;
extern double Multiplier = 3.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(0, "Trend Up");
   SetIndexBuffer(1, TrendDown);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(1, "Trend Down");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   static datetime AlertTime = 0;
  
   int limit, i, flag, flagh, trend[5000];
   double up[5000], dn[5000], medianPrice, atr;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   limit=Bars-counted_bars;
   //Print(limit);
   
   

 
//----
   for (i = Bars; i >= 0; i--) {
      TrendUp[i] = EMPTY_VALUE;
      TrendDown[i] = EMPTY_VALUE;
      atr = iATR(NULL, 0, Nbr_Periods, i);
      //Print("atr: "+atr[i]);
      medianPrice = (High[i]+Low[i])/2;
      //Print("medianPrice: "+medianPrice[i]);
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;
   
      
      if (Close[i]>up[i+1]) {
         trend[i]=1;
         if (trend[i+1] == -1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
         
      }
      else if (Close[i]<dn[i+1]) {
         trend[i]=-1;
         if (trend[i+1] == 1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
      }
      else if (trend[i+1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i+1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i+1]>0) {
         flag=1;
         //Print("flag: "+flag);
      }
      else {
         flag=0;
         //Print("flagh: "+flag);
      }
      
      if (trend[i]>0 && trend[i+1]<0) {
         flagh=1;
         //Print("flagh: "+flagh);
      }
      else {
         flagh=0;
         //Print("flagh: "+flagh);
      }
      
      if (trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];
      
      if (trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];
      
      if (flag==1)
         up[i]=medianPrice+(Multiplier*atr);
         
      if (flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);
         
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            if (i == 0 && AlertTime != Time[0])
               PlaySound("alert.wav");
            changeOfTrend = 0;
            AlertTime = Time[0];
           
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            if (i==0 && AlertTime != Time[0])
               PlaySound("alert.wav");
            changeOfTrend = 0;
            AlertTime = Time[0];
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
  • Upvote 4
Link to comment
Share on other sites

Habe den Indi aus Post # 6 grade mal getestet.

Es kommt bei jedem Tick ein PiepTon, aber der eigentliche Alarmton (Original) kommt nur in unregelmässigen Abständen, meist bei einem neuen Tief/Hoch.

Schmeisse ich den Indi dann aus dem Chart, läuft der Ton und der Alarm noch ca. 20 sekunden im Chart weiter, obwohl der Indikator sich gar nicht mehr in diesem befindet.

Der Piepton kommt, weil die Abfrage auf i==0, also nur auf den letzten Trendwechsel noch nicht drin war und somit die ganze Kette an Bars abgearbeitet und geklingeltwurde.

Der Inidkator war halt von vornherein nicht fürs "bimmeln" vorgesehen, sondern nur für die Anzeige von otpischen Trendwechseln.

Link to comment
Share on other sites

Habe mal etwas auf dem Rechner gekramt, hatte da eine .doc drauf, vllt. hilft es ja weiter.

Glaube zwar nicht wirklich dran, aber wer weiß:

 

How to add alarm to any indicator

 

http://www.forex-tsd.com/indicators-metatrader-4/87-indicators-alert-signal-12.html

POST # 113

Hi
I see a lot of people here asking to add alarm when an indicator is light up. Especially when a arrow/bullet is turn on.

Actually, it is very simple task. All you have to do is follow the following instructions.

1) Every indicator has an indicator array that keep the values of the indicator. Values can be 0 or not 0. To find out the indicator name look for the command: SetIndexBuffer
In the indicator editor it should look like this

SetIndexBuffer(0,ExtMapBuffer);
SetIndexBuffer(1,ExtMapBuffer2);


This lines tell us that we have 2 indicator buffers

2) Look when the indicator buffer change its value.
In the example above we will look to line that contains the following patter:
ExtMapBuffer[number] = where number can be a figure or a name. For example: if(res!=0.0) ExtMapBuffer[shift]=res;
if after the equal sign there is 0 do nothing.

3) Change it to make an alarm.
After finding the value setting line we will add an alarm to it.
a) add { before the setting. In the example above :
if(res!=0.0) {ExtMapBuffer[shift]=res;
b) add the Alert command after the ;. In the example above:
if(res!=0.0) { ExtMapBuffer[shift]=res;
Alert("Indicator set");
}

Do not forget to close the }


4) repeat for every buffer value setting


Compile and you have an indicator with alert

Hope it will help you

Link to comment
Share on other sites

Der Piepton kommt, weil die Abfrage auf i==0, also nur auf den letzten Trendwechsel noch nicht drin war und somit die ganze Kette an Bars abgearbeitet und geklingeltwurde.

Der Inidkator war halt von vornherein nicht fürs "bimmeln" vorgesehen, sondern nur für die Anzeige von otpischen Trendwechseln.

 

Habe ihn jetzt 30 Minuten auf M1 und 2 Std. auf M5 getestet. Allerdings passiert nichts - kein Alertsound. Ich weiß noch nicht warum?

Link to comment
Share on other sites

Ich werde das am Montag mal testen, gebe dann wieder Bescheid.

So funktioniert es:

Ich habe zusätzlich noch Print-Statements zur Kontrolle, wann gebimmelt wurde, eingebaut.

 

 

//-- Draw the indicator

if (trend==1) {
TrendUp=dn;
if (changeOfTrend == 1) {
TrendUp[i+1] = TrendDown[i+1];
changeOfTrend = 0;
if (i==0 && trend[i+1] == -1 && AlertTime != Time[0]) {
PlaySound("alert.wav");
Print("UP: playsound at " + TimeToStr(Time[0], TIME_DATE|TIME_MINUTES));
AlertTime = Time[0];
}
}
}
else if (trend==-1) {
TrendDown=up;
if (changeOfTrend == 1) {
TrendDown[i+1] = TrendUp[i+1];
changeOfTrend = 0;
if (i==0 && trend[i+1] == 1 && AlertTime != Time[0]) {
PlaySound("alert.wav");
Print("DOWN: playsound at " + TimeToStr(Time[0], TIME_DATE|TIME_MINUTES));
AlertTime = Time[0];
}
}
}

 

Der vollständige Indikator: supertrend.mq4

 

Und der Test:

supertrend-playsound.jpg

 

Start des Indikators in einem Downtrend.

Von 6:37 bis 6:45 fünf mal gebimmelt (Kurs ging über die obere rote Linie des Supertrend-Indikators) aber wegen "Repaint" des Supertrend-Indikators beibt dieser rot

Um 7:21 erfolgte dann der nachhaltige Durchbruch des Downtrends.

Um 7:28 und 7:29 Bruch des Uptrends, doppeltes Bimmeln wegen Indikator-Repaint.

  • Upvote 4
Link to comment
Share on other sites

...aber wegen "Repaint" des Supertrend-Indikators beibt dieser rot

Um 7:21 erfolgte dann der nachhaltige Durchbruch des Downtrends.

Um 7:28 und 7:29 Bruch des Uptrends, doppeltes Bimmeln wegen Indikator-Repaint.

 

Deswegen ist dieser auch nicht für EA´s zum Empfehlen, schaut gut aus, bringt aber nix bis wenig. captn.gif blackjack.gif

Link to comment
Share on other sites

Hallo Wolf,

 

ich habe das Script jetzt getestet. Auf dem M1 (EUR/USD)

 

Wegen dem repainting kommt es zum Fehlalarm.

 

Im Moment ist es so, dass der Alarm ausgelöst wird, wenn die aktuelle Kerze den letzten Supertrend berührt. Schließt der ST dann z.B. im shortfall unter dem letzten ST-Signal, kommt es zum repaint.

 

Wenn man sich das im M1 (Balken Chart) anschaut gibt es dafür aber eine Lösung. Es dürfte erst zu einem Alarm kommen, wenn die candel auch über dem letzten ST-Signal schließt.

 

Quasi eine Periode später, wenn die letze Kerze über dem Supertrend geschlossen hat. Dann gibt’s auch kein repaint mehr!

Das Signal wäre dann eindeutig.

 

Wie das allerdings in mql aussehen muss? cleanglasses.gif

Edited by mmsknecht
Link to comment
Share on other sites

Wie das allerdings in mql aussehen muss? http://1.1.1.4/bmi/www.tom-next.com/community//public/style_emoticons/default/cleanglasses.gif

 

Versuchs mal in der for-Schleife mit i >= 1 anstelle von i >= 0

 

for (i = Bars; i >= 1; i--) {

. . .

 

zur Problematik Supertrend und Repaint siehe auch: http://codebase.mql4.com/8584

  • Upvote 2
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.

Loading...
×
×
  • Create New...