PriceSound_Bands

Author: Roll
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It plays sound alerts
0 Views
0 Downloads
0 Favorites
PriceSound_Bands
//+------------------------------------------------------------------+
//|                                             PriceSound_Bands.mq4 |
//|                                                            Roll  |
//+------------------------------------------------------------------+
#property copyright "Roll"
#property link      "http://www.mql4.com/ru/users/Roll"
/*Èñòî÷íèêè:
1.- èíäèêàòîð Bands èç íàáîðà ïîëüçîâàòåëüñêèõ èíäèêàòîðîâ òåðìèíàëà ÌÒ4 
           MetaQuotes Software Corp.http://www.metaquotes.net/
2. - èíäèêàòîð PriceSound_trendline http://codebase.mql4.com/ru/7601   */
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_style1  1
#property indicator_width2  2
#property indicator_width3  2
#property indicator_color1 Gold
#property indicator_color2 Blue
#property indicator_color3 Blue 
extern int    BandsPeriod=20;
extern double BandsDeviations=2.0;//Îòêëîíåíèå
extern int    MA_Method=0;       //ìåòîä ïîñòðîåíèÿ êàíàëà ïî ÌÀ: sma, ema, smma, lwma 
extern bool   Sound_Play=true;   //ðàçðåøåíèå íà çâóê
extern bool   Show_middle_Line=true;//ïîêàçàòü òðåíäîâóþ íà ñåðåäèíå
extern string UniqueName1="TL11";//íàçâàíèå âåðõíåé òðåíäîâîé ëèíèè
extern color  LineColor1=Yellow; //öâåò âåðõíåé òðåíäîâîé ëèíèè
extern int    LineWidth1=2;      //øèðèíà âåðõíåé òðåíäîâîé ëèíèè
extern string Sound1="ready.wav";//íàçâàíèå çâóêîâîãî ôàéëà äëÿ âåðõíåé òðåíäîâîé ëèíèè
extern string UniqueName2="TL12";//íàçâàíèå íèæíåé òðåíäîâîé ëèíèè
extern color  LineColor2=Yellow; //öâåò íèæíåé òðåíäîâîé ëèíèè
extern int    LineWidth2=2;      //øèðèíà íèæíåé òðåíäîâîé ëèíèè
extern string Sound2="ready.wav";//íàçâàíèå çâóêîâîãî ôàéëà äëÿ íèæíåé òðåíäîâîé ëèíèè
extern string UniqueName3="TL13";//íàçâàíèå òðåíäîâîé ëèíèè íà ñåðåäèíå 
extern color  LineColor3=Yellow; //öâåò òðåíäîâîé ëèíèè íà ñåðåäèíå 
extern int    LineWidth3=2;      //øèðèíà òðåíäîâîé ëèíèè íà ñåðåäèíå 
extern string Sound3="ready.wav";//íàçâàíèå çâóêîâîãî ôàéëà òðåíäîâîé ëèíèè íà ñåðåäèíå 
extern int    Ind_Bar=0;         //íîìåð áàðà (ñìåùåíèå) äëÿ èíäèêàöèè ïóíêòîâ
extern int    DistPips=0; //Åñëè ìåíüøå 10-àâò.óñòàíîâêà.Ðåãóëèðîâêà ðàññòîÿíèÿ îò èçîáðàæåíèÿ ÷èñëà ïóíêòîâ äî ñîîòâ.ïðÿìîé
int ArrShift,t;double MovingBuffer[],UpperBuffer[],LowerBuffer[],LastLevel,LastPrice,TradePoint;
bool Active;string PipsTextName;static double lastLevel_1,lastPrice_1,lastLevel_2,lastPrice_2,lastLevel_3,lastPrice_3;
//+------------------------------------------------------------------+
int init()
  {
   if(Digits==3 || Digits==5)TradePoint=Point*10;
   else TradePoint=Point;LastPrice=0;lastLevel_1=0;
   SetIndexStyle(0,DRAW_LINE);SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle(2,DRAW_LINE);SetIndexBuffer(2,LowerBuffer);
   SetIndexDrawBegin(0,BandsPeriod);SetIndexDrawBegin(1,BandsPeriod);
   SetIndexDrawBegin(2,BandsPeriod);return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   if(UninitializeReason()==REASON_REMOVE)
     {
      ObjectDelete(UniqueName1);PipsTextName=UniqueName1+" Pips";ObjectDelete(PipsTextName);
      ObjectDelete(UniqueName2);PipsTextName=UniqueName2+" Pips";ObjectDelete(PipsTextName);
      ObjectDelete(UniqueName3);PipsTextName=UniqueName3+" Pips";ObjectDelete(PipsTextName);
      lastLevel_1=0;lastPrice_1=0;lastLevel_2=0;lastPrice_2=0;lastLevel_3=0;lastPrice_3=0;
     }
   Comment(" ");return(0);
  }
//+------------------------------------------------------------------+
int start()
  {
   int i,k;//,counted_bars=IndicatorCounted();
   
   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+BandsPeriod;   
      
   double deviation,sum,oldval,newres;
   switch(Period())
     {
      case PERIOD_M1: ArrShift=10;break; case PERIOD_M5: ArrShift=12;break;
      case PERIOD_M15:ArrShift=15;break; case PERIOD_M30:ArrShift=30;break;
      case PERIOD_H1: ArrShift=50;break; case PERIOD_H4:ArrShift=100;break;
      case PERIOD_D1: ArrShift=200;break;case PERIOD_W1:ArrShift=500;break;case PERIOD_MN1:ArrShift=1000;break;
     }

   for(i=0;i<limit;i++) {MovingBuffer[i]=iMA(NULL,0,BandsPeriod,0,MA_Method,PRICE_CLOSE,i);}

   i=limit;
   while(i>=0)
     {
      sum=0.0;
      k=i+BandsPeriod-1;
      oldval=MovingBuffer[i];
      while(k>=i){newres=Close[k]-oldval;sum+=newres*newres;k--;}
      deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
      UpperBuffer[i]=oldval+deviation;LowerBuffer[i]=oldval-deviation;i--;
     }
   double c1=UpperBuffer[1],c2=LowerBuffer[1],c3=MovingBuffer[1];
   datetime c0=Time[0]+3*Period()*60;
   if(ObjectFind(UniqueName1)!=0)
     {
      ObjectCreate(UniqueName1,OBJ_TREND,0,Time[0],c1,c0,c1);ObjectSetText(UniqueName1,"Price alert level1",0);
      ObjectSet(UniqueName1,OBJPROP_COLOR,LineColor1);ObjectSet(UniqueName1,OBJPROP_WIDTH,LineWidth1);
     }
   if(ObjectFind(UniqueName1)==0)
     {
      ObjectMove(UniqueName1,0,Time[2],c1);ObjectMove(UniqueName1,1,c0,c1);
      PriceSound_trendline(UniqueName1,LineColor1,Sound1,ArrShift,lastLevel_1,lastPrice_1);
     }
   if(ObjectFind(UniqueName2)!=0)
     {
      ObjectCreate(UniqueName2,OBJ_TREND,0,Time[0],c2,c0,c2);ObjectSetText(UniqueName2,"Price alert level2",0);
      ObjectSet(UniqueName2,OBJPROP_COLOR,LineColor2);ObjectSet(UniqueName2,OBJPROP_WIDTH,LineWidth2);
     }
   if(ObjectFind(UniqueName2)==0)
     {
      ObjectMove(UniqueName2,0,Time[2],c2);ObjectMove(UniqueName2,1,c0,c2);
      PriceSound_trendline(UniqueName2,LineColor2,Sound2,ArrShift,lastLevel_2,lastPrice_2);
     }
   if(Show_middle_Line)
     {
      if(ObjectFind(UniqueName3)!=0)
        {
         ObjectCreate(UniqueName3,OBJ_TREND,0,Time[0],c3,c0,c3);ObjectSetText(UniqueName3,"Price alert level3",0);
         ObjectSet(UniqueName3,OBJPROP_COLOR,LineColor2);ObjectSet(UniqueName3,OBJPROP_WIDTH,LineWidth3);
        }
      if(ObjectFind(UniqueName3)==0)
        {
         ObjectMove(UniqueName3,0,Time[2],c3);ObjectMove(UniqueName3,1,c0,c3);
         PriceSound_trendline(UniqueName3,LineColor3,Sound3,ArrShift,lastLevel_3,lastPrice_3);
        }
     }
   else{ObjectDelete(UniqueName3);PipsTextName=UniqueName3+" Pips";ObjectDelete(PipsTextName);}return(0);
  }
//+------------------------------------------------------------------+
void PriceSound_trendline(string UniqueName,color LineColor,string Sound,int arrShift,double lastLevel,double lastPrice)
  {
   PipsTextName=StringConcatenate(UniqueName," ","Pips");if(DistPips>=10)ArrShift=DistPips;else ArrShift=arrShift;
   LastLevel=lastLevel;LastPrice=lastPrice;
   if(ObjectFind(PipsTextName)==-1)
     {
      double cp=LastLevel;if(cp==0)cp=LastLevel+(10+ArrShift)*TradePoint;
      ObjectCreate(PipsTextName,OBJ_TEXT,0,Time[0],cp);
     }
   if(Ind_Bar>0)t=Ind_Bar;else t=0;
   ObjectSet(PipsTextName,OBJPROP_TIME1,Time[t]);double newlevel=ObjectGetValueByShift(UniqueName,0);
   if(Active)
     {
      ObjectSet(UniqueName,OBJPROP_COLOR,LineColor);ObjectSetText(UniqueName,"Price sound level - Active",0);
      double Pips=MathAbs(NormalizeDouble((newlevel-Close[0])/TradePoint,Digits));
      if(Digits==3 || Digits==5)ObjectSetText(PipsTextName,DoubleToStr(Pips,1),14,"Terminal",LineColor);
      else ObjectSetText(PipsTextName,DoubleToStr(Pips,0),14,"Terminal",LineColor);
     }
   if(Close[0]>newlevel)ObjectSet(PipsTextName,OBJPROP_PRICE1,newlevel-ArrShift*TradePoint);
   if(Close[0]<newlevel)ObjectSet(PipsTextName,OBJPROP_PRICE1,newlevel+ArrShift*TradePoint);
   if(LastLevel!=newlevel)
     {LastLevel=newlevel;LastPrice=0;ObjectSetText(UniqueName,"Price sound level - Active",0);Active=true;}
   if(Active && LastPrice!=0 && ((LastPrice<LastLevel && Close[0]>=LastLevel) || (LastPrice>LastLevel && Close[0]<=LastLevel)))
     {
      if(Sound_Play)PlaySound(Sound);else Active=false;ObjectSetText(PipsTextName,"",14,"Terminal",LineColor);
      ObjectSetText(UniqueName,"Price sound level - Inactive",0);ObjectSet(UniqueName,OBJPROP_COLOR,Blue);
     }
   if(UniqueName==UniqueName1){lastLevel_1=LastLevel;lastPrice_1=Close[0];}
   if(UniqueName==UniqueName2){lastLevel_2=LastLevel;lastPrice_2=Close[0];}
   if(UniqueName==UniqueName3){lastLevel_3=LastLevel;lastPrice_3=Close[0];}
  }
//----------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---