Class: SQA::Backtest
- Inherits:
-
Object
- Object
- SQA::Backtest
- Defined in:
- lib/sqa/backtest.rb
Defined Under Namespace
Classes: Results
Instance Attribute Summary collapse
-
#equity_curve ⇒ Object
readonly
Returns the value of attribute equity_curve.
-
#portfolio ⇒ Object
readonly
Returns the value of attribute portfolio.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#stock ⇒ Object
readonly
Returns the value of attribute stock.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
Instance Method Summary collapse
-
#initialize(stock:, strategy:, start_date: nil, end_date: nil, initial_capital: 10_000.0, commission: 0.0, position_size: :all_cash) ⇒ Backtest
constructor
Initialize a backtest.
-
#run ⇒ Results
Run the backtest.
Constructor Details
#initialize(stock:, strategy:, start_date: nil, end_date: nil, initial_capital: 10_000.0, commission: 0.0, position_size: :all_cash) ⇒ Backtest
Initialize a backtest
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sqa/backtest.rb', line 85 def initialize(stock:, strategy:, start_date: nil, end_date: nil, initial_capital: 10_000.0, commission: 0.0, position_size: :all_cash) @stock = stock @strategy = strategy @start_date = start_date ? Date.parse(start_date.to_s) : Date.parse(stock.df["timestamp"].first) @end_date = end_date ? Date.parse(end_date.to_s) : Date.parse(stock.df["timestamp"].last) @initial_capital = initial_capital @commission = commission @position_size = position_size @portfolio = SQA::Portfolio.new(initial_cash: initial_capital, commission: commission) @equity_curve = [] # Track portfolio value over time @results = Results.new end |
Instance Attribute Details
#equity_curve ⇒ Object (readonly)
Returns the value of attribute equity_curve.
8 9 10 |
# File 'lib/sqa/backtest.rb', line 8 def equity_curve @equity_curve end |
#portfolio ⇒ Object (readonly)
Returns the value of attribute portfolio.
8 9 10 |
# File 'lib/sqa/backtest.rb', line 8 def portfolio @portfolio end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
8 9 10 |
# File 'lib/sqa/backtest.rb', line 8 def results @results end |
#stock ⇒ Object (readonly)
Returns the value of attribute stock.
8 9 10 |
# File 'lib/sqa/backtest.rb', line 8 def stock @stock end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
8 9 10 |
# File 'lib/sqa/backtest.rb', line 8 def strategy @strategy end |
Instance Method Details
#run ⇒ Results
Run the backtest
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/sqa/backtest.rb', line 134 def run # Get data for the backtest period df = @stock.df.data # Filter to backtest period = df["timestamp"].to_a start_idx = .index { |t| Date.parse(t) >= @start_date } || 0 end_idx = .rindex { |t| Date.parse(t) <= @end_date } || .length - 1 prices = df["adj_close_price"].to_a ticker = @stock.ticker.upcase # Track current position current_position = nil # :long, :short, or nil # Run through each day (start_idx..end_idx).each do |i| date = Date.parse([i]) price = prices[i] # Get historical prices up to this point for strategy historical_prices = prices[0..i] # Generate signal from strategy signal = generate_signal(historical_prices) # Execute trades based on signal case signal when :buy if current_position.nil? && can_buy?(price) shares = calculate_shares_to_buy(price) @portfolio.buy(ticker, shares: shares, price: price, date: date) current_position = :long end when :sell if current_position == :long pos = @portfolio.position(ticker) @portfolio.sell(ticker, shares: pos.shares, price: price, date: date) if pos current_position = nil end end # Record equity curve current_value = @portfolio.value(ticker => price) @equity_curve << { date: date, value: current_value, price: price } end # Close any open positions at end if current_position == :long final_price = prices[end_idx] final_date = Date.parse([end_idx]) pos = @portfolio.position(ticker) @portfolio.sell(ticker, shares: pos.shares, price: final_price, date: final_date) if pos end # Calculate results calculate_results @results end |