Bollinger Band Indicator with alert and arrows

Created at 06 Aug 2015, 19:54
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
JI

jimtang

Joined 06.08.2015

Bollinger Band Indicator with alert and arrows
06 Aug 2015, 19:54


Dear Spotware

Is there a bollinger band indicator with alert and arrows.

When current candle closing outside upper or lower band, an alert and arrow appear

at start of next candle.

A red sell put arrow if crossed top band.

A green buy long arrow if crossed bottom band.

Idea is a reversal trade.

Settings are standard but deviation set at 2.5.

Below is MT4 code for BB Break 2.mq4 but I Have visited MQ42C++ ( MQ4 to cAlgo Converter @ 2calgo.com ) but was unable to convert BBreak2.mq4 

//+------------------------------------------------------------------+
//|                                        Indicator: BB Break 2.mq4 |
//|                                       Created with EABuilder.com |
//|                                             http://eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "http://eabuilder.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 0x00FF00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

datetime time_alert; //used when sending alert
bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | BB Break 2 @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | BB Break 2 @ "+Symbol()+","+Period()+" | "+message);
      if(Audible_Alerts) Alert(type+" | BB Break 2 @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 233);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 234);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      if(Open[1+i] > iBands(NULL, PERIOD_CURRENT, 20, 2.5, 0, PRICE_CLOSE, MODE_LOWER, 1+i) //Candlestick Open > Bollinger Bands
      && Close[1+i] < iBands(NULL, PERIOD_CURRENT, 20, 2.5, 0, PRICE_CLOSE, MODE_LOWER, 1+i) //Candlestick Close < Bollinger Bands
      )
        {
         Buffer1[i] = Low[1+i]; //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      //Indicator Buffer 2
      if(Open[1+i] < iBands(NULL, PERIOD_CURRENT, 20, 2.5, 0, PRICE_CLOSE, MODE_UPPER, 1+i) //Candlestick Open < Bollinger Bands
      && Close[1+i] > iBands(NULL, PERIOD_CURRENT, 20, 2.5, 0, PRICE_CLOSE, MODE_UPPER, 1+i) //Candlestick Close > Bollinger Bands
      )
        {
         Buffer2[i] = High[1+i]; //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

 


@jimtang
Replies

Spotware
17 Aug 2015, 18:22

Dear Trader,

Many users upload their Indicators/cBots in cTDN. You can try to find the Indicator you are searching for in Indicators library. If you cannot find it there you can also contact one of our Partners for further help or post a job in Development Jobs section.


@Spotware

LSR2412
15 Sep 2015, 10:53

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class BBREVERSALCODE : Indicator
    {

        private BollingerBands bbds;

        protected override void Initialize()
        {

            bbds = Indicators.BollingerBands(MarketSeries.Close, 20, 2.5, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {


            if (bbds.Top[index - 1] < MarketSeries.High[index - 1])
            {
                ChartObjects.DrawText(index.ToString(), "▼", index, MarketSeries.High[index - 1] + Symbol.PipSize, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Red);
            }



            if (bbds.Bottom[index - 1] > MarketSeries.Low[index - 1])
            {
                ChartObjects.DrawText(index.ToString(), "▲", index, MarketSeries.Low[index - 1] - Symbol.PipSize, VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Green);
            }



        }
    }
}

try this one...it marks with arrows bars which had extreme outside of Bollinger bands..after bar close it puts arrow on start of new bar


@LSR2412