//+------------------------------------------------------------------+ //| Price channel.mq5 | //| original developed by igorad | //| metatrader 5 version by mladen | //+------------------------------------------------------------------+ #property copyright "www.forex-tsd.com" #property link "www.forex-tsd.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 9 #property indicator_plots 4 // // // // // #property indicator_type1 DRAW_ARROW #property indicator_type2 DRAW_ARROW #property indicator_type3 DRAW_ARROW #property indicator_type4 DRAW_ARROW #property indicator_color1 Blue #property indicator_color2 Red #property indicator_color3 Blue #property indicator_color4 Red // // // // // input int Length = 9; // Price channel length input ENUM_APPLIED_PRICE Price = PRICE_CLOSE; // Aplied price input double Risk = 3; // Risk in percents input bool UseReEntry = true; // Re-entry usage // // // // // enum ENUM_ALERTS_MAIN { alerts_MainOn = 1, // Allerts tuned on alerts_MainOff = 0 // Do not alert }; enum ENUM_ALERTS_ONCURRENT { alerts_OnCurrent = 1, // Alert on current (opened) bar alerts_OnPrevious = 0 // Alert on previous (closed) bar }; enum ENUM_ALERTS_MESSAGE { alerts_MessagesOn = 1, // Show message in a message window when alert happens alerts_MessagesOff = 0 // Do not show message in a message window }; enum ENUM_ALERTS_SOUND { alerts_SoundOn = 1, // Play a sound when alert happens alerts_SoundOff = 0 // Do not play a sound when alert happens }; enum ENUM_ALERTS_EMAIL { alerts_EmailOn = 1, // Send an email when alert happens alerts_EmailOff = 0 // Do not send an email when alert happens }; // // // // // input ENUM_ALERTS_MAIN alertsOn = alerts_MainOff; // Alerts main switch (no alerts if this is disabled) : input ENUM_ALERTS_ONCURRENT alertsOnCurrent = alerts_OnPrevious; // Alert - on current or previous bar input ENUM_ALERTS_MESSAGE alertsMessage = alerts_MessagesOn; // Alert - show messages or not input ENUM_ALERTS_SOUND alertsSound = alerts_SoundOff; // Alert - play sound or not input ENUM_ALERTS_EMAIL alertsEmail = alerts_EmailOff; // Alert - send email or not // // // // // double UpSignal[]; double DnSignal[]; double UpEntry[]; double DnEntry[]; double smin[]; double smax[]; double trend[]; double prices[]; double atr[]; int HandleForPrices; int HandleForATR; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // int OnInit() { SetIndexBuffer(0,UpSignal,INDICATOR_DATA); ArraySetAsSeries(UpSignal ,true); SetIndexBuffer(1,DnSignal,INDICATOR_DATA); ArraySetAsSeries(DnSignal ,true); SetIndexBuffer(2,UpEntry ,INDICATOR_DATA); ArraySetAsSeries(UpEntry ,true); SetIndexBuffer(3,DnEntry ,INDICATOR_CALCULATIONS); ArraySetAsSeries(DnEntry ,true); SetIndexBuffer(4,smin ,INDICATOR_CALCULATIONS); ArraySetAsSeries(smin ,true); SetIndexBuffer(5,smax ,INDICATOR_CALCULATIONS); ArraySetAsSeries(smax ,true); SetIndexBuffer(6,trend ,INDICATOR_CALCULATIONS); ArraySetAsSeries(trend ,true); SetIndexBuffer(7,prices ,INDICATOR_CALCULATIONS); ArraySetAsSeries(prices ,true); SetIndexBuffer(8,atr ,INDICATOR_CALCULATIONS); ArraySetAsSeries(atr ,true); // // // // // PlotIndexSetInteger(0,PLOT_ARROW,108); PlotIndexSetInteger(1,PLOT_ARROW,108); PlotIndexSetInteger(2,PLOT_ARROW,217); PlotIndexSetInteger(3,PLOT_ARROW,218); // // // // // IndicatorSetString(INDICATOR_SHORTNAME,"Price channel ("+(string)Length+","+DoubleToString(Risk,_Digits)+")"); HandleForPrices = iMA(NULL,0,1,0,MODE_SMA,Price); HandleForATR = iATR(NULL,0,Length); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // #define rangeSmoothLength 27 int OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spread[]) { // // // // // if (!checkCalculated(HandleForPrices,rates_total,"prices")) return(0); if (!checkCalculated(HandleForATR ,rates_total,"ATR" )) return(0); int to_copy; if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total; else { to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++; } if (!doCopy(HandleForPrices,prices,to_copy,"prices buffer")) return(0); if (!doCopy(HandleForATR ,atr ,to_copy,"ATR buffer" )) return(0); if (!ArrayGetAsSeries(high)) ArraySetAsSeries(high ,true); if (!ArrayGetAsSeries(low)) ArraySetAsSeries(low ,true); if (!ArrayGetAsSeries(time)) ArraySetAsSeries(time ,true); // // // // // int limit = rates_total-prev_calculated; int last = Length; // // // // // if (Bars(Symbol(),0) 0) limit++; if (prev_calculated == 0) { limit -= last; for(int i=1; i<=last; i++) { smin[rates_total-i] = 0; smax[rates_total-i] = 0; trend[rates_total-i] = 0; } } // // // // // for(int i=limit; i>=0; i--) { double price = prices[i]; double price1 = prices[i+1]; double price2 = prices[i+2]; double hi = high[i]; for(int k=1; k smax[i]) trend[i]= 1; if ( price < smin[i]) trend[i]=-1; // // // // // DnSignal[i] = EMPTY_VALUE; DnEntry[i] = EMPTY_VALUE; UpEntry[i] = EMPTY_VALUE; UpSignal[i] = EMPTY_VALUE; if(trend[i]>0) { if (trend[i+1]<0) UpSignal[i] = low[i]-0.5*atr[i]; if (UseReEntry && price > smax[i] && price1 <= smax[i+1]) UpEntry[i] = low[i]-0.5*atr[i]; } if(trend[i]<0) { if (trend[i+1]>0) DnSignal[i] = high[i]+0.5*atr[i]; if (UseReEntry && price < smin[i] && price1 >= smin[i+1]) DnEntry[i] = high[i]+0.5*atr[i]; } } // // // // // if (alertsOn) { static datetime basicAlertedTime = 0; static string basicAlertedWhat = ""; static datetime reentryAlertedTime = 0; static string reentryAlertedWhat = ""; int forBar=1; if (alertsOnCurrent) forBar = 0; if (UpSignal[forBar] != EMPTY_VALUE) doAlert(basicAlertedWhat ,basicAlertedTime ,time[0],"Signal for buy"); if (DnSignal[forBar] != EMPTY_VALUE) doAlert(basicAlertedWhat ,basicAlertedTime ,time[0],"Signal for sell"); if (UpEntry[forBar] != EMPTY_VALUE) doAlert(reentryAlertedWhat,reentryAlertedTime,time[0],"Re-entry for buy"); if (DnEntry[forBar] != EMPTY_VALUE) doAlert(reentryAlertedWhat,reentryAlertedTime,time[0],"Re-entry for sell"); } // // // // // return(rates_total); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // bool checkCalculated(int bufferHandle, int total, string checkDescription) { int calculated=BarsCalculated(bufferHandle); if (calculated