Bollinger Bands

Calculates the recommendation for buy/sell based on the Bollinger Bands strategy.

class src.lib.analysis.methods.bollinger_band.BOLLINGER_BANDS(name)

Bases: src.lib.analysis.basic.Basic, src.lib.analysis.arbitration.Arbitration, src.lib.analysis.performance_simulation.PerformanceSimulation, src.lib.analysis.report_analysis.ReportAnalysis, src.lib.analysis.summary.Summary

calc_BBANDS()

Calculate de Bollinger Bands indicator, which is based on first taking the closure value and calculating the 20 last entries moving average (simple):

\[SMA_{VC}^{(N=20)}(k) = \frac{1}{N} \sum_{i=k-N+1}^{k} VC_{i}\]

where VC represenst the closing value for the entry, and N the length in samples to be taken. For this case, \(N=20\). For the closing value, the adjusted one is the one applied.

Similarly, the moving standard deviation is calculated for the same length:

\[StdDev_{VC}^{(N=20)}(k) = \sqrt{\frac{1}{N-1} \sum_{i=k-N+1}^{k} \left( VC_{i} - \overline{VC_{i}}\right)^{2}}\]

From the 2 values calculated above, 2 bands will be defined, where:

\[ \begin{align}\begin{aligned}UB = SMA_{VC} + 2 StdDev_{VC}\\LB = SMA_{VC} - 2 StdDev_{VC}\end{aligned}\end{align} \]

on which UB and LB means upper band and lower band respectively.

The analysis or recommandation are based on the rules:

  1. When VC is above the UB, this indicates an overbought situation, and thus a sell indication.

  2. When VC is above the LB, this indicates an oversold situation, and thus a buy indication.

Parameters

None

The input for the calculation is based on the Pandas dataframe data which is already available. The expected column for this operation is:

  1. Close Final

Returns

The outcome from the calculation is not explicitly returned, but added to the Pandas dataframe as new columns. The new columns are:

  1. BBANDS SMA 20: Result from the simple moving average of the last 20 samples.

  2. BBANDS StdDev 20: Result from the moving sample standard deviation of the last 20 samples.

  3. BBANDS Upper: Resulting upper band.

  4. BBANDS Lower: Resulting lower band.

  5. BBANDS Recommendation: Resulting recommendation for the strategy.

  6. BBANDS Recommendation Events: Resulting recommendation events for the strategy.

  7. BBANDS Simulation: Resulted simulation based on the previous strategy recommendation.

  8. BBANDS Simulation Reference: Resulted simulation from the buy-hold strategy to be used as reference for the current strategy simulation.

Return type

None