Class: SQA::Strategy::VolumeBreakout

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

Overview

Volume Breakout strategy Buy when price breaks above resistance with high volume Sell when price breaks below support with high volume

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sqa/strategy/volume_breakout.rb', line 9

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

  prices = vector.prices
  volumes = vector.volumes

  # Calculate moving averages
  sma_20 = SQAI.sma(prices, period: 20)
  return :hold if sma_20.nil?

  current_price = prices.last
  prev_price = prices[-2]
  current_volume = volumes.last

  # Calculate average volume
  avg_volume = volumes.last(20).sum / 20.0

  # High volume threshold (1.5x average)
  volume_threshold = avg_volume * 1.5

  # Get recent high and low (resistance and support) from previous prices
  # Exclude current price to allow breakout detection
  lookback_prices = prices[...-1].last(20)  # Last 20 prices excluding current
  recent_high = lookback_prices.max
  recent_low = lookback_prices.min

  # Buy signal: price breaks above recent high with high volume
  if current_price > recent_high &&
     prev_price <= recent_high &&
     current_volume > volume_threshold
    :buy

  # Sell signal: price breaks below recent low with high volume
  elsif current_price < recent_low &&
        prev_price >= recent_low &&
        current_volume > volume_threshold
    :sell

  else
    :hold
  end
rescue => e
  warn "VolumeBreakout strategy error: #{e.message}"
  :hold
end