Class: SQA::Strategy::BollingerBands

Inherits:
Object
  • Object
show all
Defined in:
lib/sqa/strategy/bollinger_bands.rb

Overview

Bollinger Bands trading strategy Buy when price touches lower band (oversold) Sell when price touches upper band (overbought)

Class Method Summary collapse

Class Method Details

.trade(vector) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sqa/strategy/bollinger_bands.rb', line 9

def self.trade(vector)
  return :hold unless vector.respond_to?(:prices) && vector.prices&.size >= 20

  prices = vector.prices
  period = 20
  std_dev = 2.0

  # Calculate Bollinger Bands using SQAI
  upper, middle, lower = SQAI.bbands(prices, period: period, nbdev_up: std_dev, nbdev_down: std_dev)

  return :hold if upper.nil? || lower.nil?

  current_price = prices.last
  upper_band = upper.last
  lower_band = lower.last
  middle_band = middle.last

  # Buy signal: price at or below lower band (oversold)
  if current_price <= lower_band
    :buy

  # Sell signal: price at or above upper band (overbought)
  elsif current_price >= upper_band
    :sell

  # Hold: price between bands
  else
    :hold
  end
rescue => e
  warn "BollingerBands strategy error: #{e.message}"
  :hold
end