PriceSound_MA_Shift_Y

Author: Roll
Miscellaneous
Implements a curve of type %1It plays sound alerts
0 Views
0 Downloads
0 Favorites
PriceSound_MA_Shift_Y
//+------------------------------------------------------------------+
//|                                        PriceSound_MA_Shift_Y.mq4 |
//|                                                            Roll  |
//+------------------------------------------------------------------+
#property copyright "Roll"
#property link      "http://www.mql4.com/ru/users/Roll"
/*Èñòî÷íèêè:
1.- èíäèêàòîð Moving Averages(Custom Moving Average.mq4)èç øòàòíîãî íàáîðà ïîëüçîâàòåëüñêèõ èíäèêàòîðîâ òåðìèíàëà ÌÒ4.
           MetaQuotes Software Corp.http://www.metaquotes.net/
2. - èíäèêàòîð PriceSound_trendline http://codebase.mql4.com/ru/7601   */
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Magenta
#property indicator_width1 2
//---- indicator parameters
extern int    MA_Period=13;
extern int    MA_Shift_Y=5;//Ñäâèã ÌÀ ïî âåðòèêàëè (â ïóíêòàõ)
extern int    MA_Shift=10;//Ñäâèã ÌÀ ïî ãîðèçîíòàëè (â áàðàõ)
extern int    MA_Method=0;//ìåòîä ïîñòðîåíèÿ ÌÀ: sma, ema, smma, lwma
extern string UniqueName1="TL1"; //íàçâàíèå òðåíäîâîé ëèíèè
extern bool   Sound_Play=true;   //ðàçðåøåíèå íà çâóê
extern color  LineColor_UP=Yellow; //öâåò òðåíäîâîé ëèíèè (íàïðàâëåíèå ââåðõ)
extern color  LineColor_DN=Magenta; //öâåò òðåíäîâîé ëèíèè (íàïðàâëåíèå âíèç)
extern int    LineWidth1=2;      //øèðèíà òðåíäîâîé ëèíèè
extern string Sound1="ready.wav";//íàçâàíèå çâóêîâîãî ôàéëà äëÿ òðåíäîâîé ëèíèè
extern int    Ind_Bar=0;         //íîìåð áàðà (ñìåùåíèå) äëÿ èíäèêàöèè ïóíêòîâ
extern int    DistPips=0; //Åñëè ìåíüøå 10-àâò.óñòàíîâêà.Ðåãóëèðîâêà ðàññòîÿíèÿ îò èçîáðàæåíèÿ ÷èñëà ïóíêòîâ äî ñîîòâ.ïðÿìîé

int ExtCountedBars=0,i,k,pos,ArrShift,t,Shift=1;
double ExtMapBuffer[],MA_ShiftY,sum,LastLevel,LastPrice,TradePoint; 
bool Active;string PipsTextName;
static double lastLevel_1,lastPrice_1;
//+------------------------------------------------------------------+
int init(){if(Digits==3||Digits==5){MA_Shift_Y=MA_Shift_Y*10;TradePoint=Point*10;}
  else{TradePoint=Point;MA_ShiftY=MA_Shift_Y*Point;}LastPrice=0;lastLevel_1=0;
  int draw_begin;string short_name;
  SetIndexStyle(0,DRAW_LINE);SetIndexShift(0,MA_Shift);
  IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
  if(MA_Period<2)MA_Period=13;draw_begin=MA_Period-1;
 switch(MA_Method)
  {case 1:short_name="EMA(";draw_begin=0;break;case 2:short_name="SMMA(";break;
   case 3:short_name="LWMA(";break;default:MA_Method=0;short_name="SMA(";}
   IndicatorShortName(short_name+MA_Period+")");
   SetIndexDrawBegin(0,draw_begin);SetIndexBuffer(0,ExtMapBuffer);
 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;}return(0);}
int deinit(){if(UninitializeReason()==REASON_REMOVE)
 {ObjectDelete(UniqueName1);PipsTextName=UniqueName1+" Pips";ObjectDelete(PipsTextName);
  lastLevel_1=0;lastPrice_1=0;}return(0);}
//+------------------------------------------------------------------+
int start()
{if(Bars<=MA_Period)return(0);ExtCountedBars=IndicatorCounted();if(ExtCountedBars<0)return(-1);
 if(ExtCountedBars>0)ExtCountedBars--;
 switch(MA_Method){case 0:sma();break;case 1:ema();break;case 2:smma();break;case 3:lwma();break;}
 double c1=ExtMapBuffer[MA_Shift];datetime c0=Time[0]+Shift*Period()*60;
 double c2=ExtMapBuffer[Shift];color LineColor;
 if(c1>c2)LineColor= LineColor_DN;if(c1<=c2)LineColor=LineColor_UP;
 if(ObjectFind(UniqueName1)!=0)
 {ObjectCreate(UniqueName1,OBJ_TREND,0,Time[0],c1,c0,c1);ObjectSetText(UniqueName1,"Price alert level1",0);
  ObjectSet(UniqueName1,OBJPROP_COLOR,LineColor);ObjectSet(UniqueName1,OBJPROP_WIDTH,LineWidth1);}
 if(ObjectFind(UniqueName1)==0){ObjectMove(UniqueName1,0,Time[2],c1);ObjectMove(UniqueName1,1,c0,c1);
  PriceSound_trendline(UniqueName1,LineColor,Sound1,ArrShift,lastLevel_1,lastPrice_1);}return(0);}
//+---------------Simple Moving Average------------------------------+
void sma()
{sum=0;pos=Bars-ExtCountedBars-1;
 if(pos<MA_Period) pos=MA_Period;
 for(i=1;i<MA_Period;i++,pos--)sum+=Close[pos];
 while(pos>=0)
 {sum+=Close[pos];
  ExtMapBuffer[pos]=sum/MA_Period+MA_ShiftY;
  sum-=Close[pos+MA_Period-1];pos--;}
  if(ExtCountedBars<1)for(i=1;i<MA_Period;i++)ExtMapBuffer[Bars-i]=0;}
//+--------------Exponential Moving Average--------------------------+
void ema()
{double pr=2.0/(MA_Period+1);pos=Bars-2;if(ExtCountedBars>2)pos=Bars-ExtCountedBars-1;
 while(pos>=0)
 {if(pos==Bars-2)ExtMapBuffer[pos+1]=Close[pos+1]+MA_ShiftY;
  ExtMapBuffer[pos]=(Close[pos]+MA_ShiftY)*pr+ExtMapBuffer[pos+1]*(1-pr);pos--;}}
//+---------------Smoothed Moving Average ---------------------------+
void smma()
{sum=0;pos=Bars-ExtCountedBars+1;pos=Bars-MA_Period;
 if(pos>Bars-ExtCountedBars) pos=Bars-ExtCountedBars;
 while(pos>=0)
 {if(pos==Bars-MA_Period)
  {for(i=0,k=pos;i<MA_Period;i++,k++){sum+=Close[k];ExtMapBuffer[k]=0;}}
   else sum=ExtMapBuffer[pos+1]*(MA_Period-1)+Close[pos]+MA_ShiftY;
   ExtMapBuffer[pos]=sum/MA_Period;pos--;}}
//+--------------Linear Weighted Moving Average------------------------+
void lwma()
{sum=0.0;double price,lsum=0.0;int weight=0;pos=Bars-ExtCountedBars-1;
 if(pos<MA_Period)pos=MA_Period;
 for(i=1;i<=MA_Period;i++,pos--){price=Close[pos];sum+=price*i;lsum+=price;weight+=i;}
 pos++;i=pos+MA_Period;
 while(pos>=0)
 {ExtMapBuffer[pos]=sum/weight+MA_ShiftY;
  if(pos==0)break; pos--; i--; price=Close[pos];
  sum=sum-lsum+price*MA_Period; lsum-=Close[i]; lsum+=price;}
 if(ExtCountedBars<1)for(i=1;i<MA_Period;i++)ExtMapBuffer[Bars-i]=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];}}
//-----------------------------------------------------+

Comments