This script is designed to run within the MetaTrader platform, a popular tool for trading financial markets. Its goal is to identify potential buying and selling opportunities based on a combination of technical indicators: CCI (Commodity Channel Index), Moving Averages, and Bollinger Bands.
Here's a breakdown of what the script does, step-by-step:
-
Setting the Stage:
- The script begins by defining several adjustable settings (called "extern inputs"). These settings control how the indicators are calculated and what conditions trigger potential trade signals. For instance, you can customize the periods used for the CCI, Moving Average, and Bollinger Bands calculations, along with levels that are considered "high" or "low" for the CCI. Think of them like knobs that allow you to fine-tune the strategy.
-
Preparing the Canvas:
- The
init()function runs once when the script is loaded. It prepares the visual representation by setting up "arrows" on the chart, each one displayed at the potential buy and sell signals.
- The
-
Analyzing the Market:
- The
start()function is the heart of the script. It's executed repeatedly, bar by bar, as new price data becomes available. - For each bar, the script calculates the values for the following indicators:
- CCI: Measures how far the current price deviates from the average price over a specific period. It helps identify overbought or oversold conditions.
- Moving Average: Calculates an average of the CCI values for a specific period.
- Bollinger Bands: Creates a band around the moving average by calculating a standard deviation over a specific period.
- It also measures de average trading range over the last 10 bars,
- The
-
Identifying Potential Sell Signals:
- The script first checks if the CCI value is above a predefined "high" level. If it is, it suggests the price is overbought.
- Then, it checks if the current CCI value is lower than its moving average. This suggests that the overbought condition might be weakening.
- It compares the current price with the upper band of the Bollinger Bands. If the high price is less than the upper band, it checks if the most recent high is lower than the previous high.
- If that is the case, and if the price is not too far below the upper band of the Bollinger Bands, a "sell" arrow will show on the chart.
- It also marks a sell if the price exceeds the upper Bollinger Band.
-
Identifying Potential Buy Signals:
- The script checks if the CCI value is below a predefined "low" level. If it is, it suggests the price is oversold.
- It then checks if the current CCI value is higher than its moving average. This suggests that the oversold condition might be strengthening.
- It compares the current price with the lower band of the Bollinger Bands. If the low price is above the lower band, it checks if the most recent low is higher than the previous low.
- If that is the case, a "buy" arrow is displayed on the chart.
- It also marks a buy if the price is lower than the lower Bollinger Band.
-
Displaying the Results:
- Based on the logic described above, the script places arrows on the chart, where up arrows represent potential buy opportunities, and down arrows represent potential sell opportunities.
In essence, this script automates a specific trading strategy based on the interplay of the CCI, Moving Averages, and Bollinger Bands. It uses these indicators to identify moments when the price might be reversing its direction, thus presenting potential buying or selling opportunities.
//+------------------------------------------------------------------+
//| BB_CCI_CrossOver.mq4 |
//| Daniel Vieira Costa |
//| seilatrader.blogspot.com |
//+------------------------------------------------------------------+
#property copyright "Daniel Vieira Costa"
#property link "seilatrader.blogspot.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DarkTurquoise
#property indicator_color2 Red
//--
extern int CCI_Periodo = 21;
extern double NivelCCI_MIN = -80;
extern double NivelCCI_MAX = 80;
extern int MM_Periodo = 14;
extern int BB_Periodo = 21;
extern int BB_Desvio = 2;
extern double PIP_DesvioMAX = 0.0010;
extern double PIP_Oscilacao = 0.0015;
//---
double compra[];
double venda[];
//--
double CCI[];
double MM[];
double BB_UP[],BB_LOW[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0,DRAW_ARROW,EMPTY);
SetIndexArrow(0,228);
SetIndexBuffer(0,compra);
SetIndexStyle(1,DRAW_ARROW,EMPTY);
SetIndexArrow(1,230);
SetIndexBuffer(1,venda);
//---- indicators
ArraySetAsSeries(CCI,true);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
// indicadores
double mm,cci,Range,AvgRange;
int i,limite,Counter;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limite=Bars-counted_bars;
if(counted_bars==0) limite-=1+9;
int xsize=ArraySize(compra);
// seto o tamanho das listas
ArrayResize(CCI,xsize);
ArrayResize(MM,xsize);
ArrayResize(BB_UP,xsize);
ArrayResize(BB_LOW,xsize);
i=limite;
while(i>0)
{
Counter =0;
Range =0;
AvgRange =0;
for(Counter=i; Counter>=i+9; Counter--)
AvgRange=AvgRange+MathAbs(High[Counter]-Low[Counter]);
Range=AvgRange/10;
CCI[i] = iCCI(NULL, 0, CCI_Periodo, PRICE_WEIGHTED, i);
MM[i] = iMAOnArray(CCI, limite, MM_Periodo, 0, MODE_EMA, i);
BB_UP[i] = iBands(NULL, 0, BB_Periodo, BB_Desvio, 0, PRICE_WEIGHTED, MODE_UPPER, i);
BB_LOW[i]= iBands(NULL, 0, BB_Periodo, BB_Desvio, 0, PRICE_WEIGHTED, MODE_LOWER, i);
// primeiro verifica se CCI está acima do nivel positivo
if(CCI[i]>NivelCCI_MAX)
{
if(CCI[i]<MM[i])
// verifica se o preço está dentro da banda superior
if(High[i]<=BB_UP[i])
{
// verifica se a máxima é menor que a máxima anterior
// o motivo é para ratificar a confirmação da tendência
if(High[i+1]>High[i])
// verifica a distância entre a o preço máximo e a banda de bollinger
// só se posiciona se a distância for considerável, ou seja, não tão distante
if((BB_UP[i]-High[i])<=PIP_DesvioMAX)
venda[i]=High[i]+Range*0.10;
}
else
// quando o preço está fora da banda superior
if(High[i]>=BB_UP[i])
{
// verifica se a máxima é menor que a máxima anterior
// o motivo é para ratificar a confirmação da tendência
if(High[i+1]>High[i])
venda[i]=High[i]+Range*0.10;
}
}
else
// verifica se o nivel de CCI está abaixo da mínima
if(CCI[i]<NivelCCI_MIN)
{
if(CCI[i]>MM[i])
// quando o preço está dentro da banda inferior
if(Low[i]>=BB_LOW[i])
{
// verifica se a mínima é maior que a mínima anterior
// o motivo é para ratificar a confirmação da tendência
if(Low[i+1]<Low[i])
compra[i]=Low[i]-Range*0.10;
}
else
// quando o preço ainda se encontra fora da banda inferior
if(Low[i]<=BB_LOW[i])
{
if(Low[i+1]<Low[i])
compra[i]=Low[i]-Range*0.10;
}
}
i--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments