Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
update backtesting for optimization
Browse files Browse the repository at this point in the history
rename nbdev to stddev in bb
rename test.py to backtesting.py
add rsi strategy
  • Loading branch information
CelestialCrafter committed Dec 5, 2023
1 parent 00871fd commit 3125d92
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
8 changes: 5 additions & 3 deletions algorithms/bollinger_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ def signal(prices, data):
return 'no_action', 0

class Strategy(bt.Strategy):
params = (('timeperiod', 20), ('nbdev', 2.0))
params = (('timeperiod', 20), ('stddev', 2.0))

def __init__(self):
super().__init__()

indicator = bt.talib.BBANDS(self.data.close,
timeperiod=self.p.timeperiod,
nbdevup=self.p.nbdev,
npdevdn=self.p.nbdev)
nbdevup=self.p.stddev,
npdevdn=self.p.stddev)

self.close_over_upper_band = self.data.close > indicator.upperband
self.close_under_lower_band = self.data.close < indicator.lowerband
Expand Down
18 changes: 18 additions & 0 deletions algorithms/rsi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import backtrader as bt
from talib import RSI
from price import get_periods, get_max_periods

Expand All @@ -21,3 +22,20 @@ def signal(prices, data, high=70, low=30):
return 'buy', strength

return 'no_action', 0

class Strategy(bt.Strategy):
params = (('timeperiod', 20),)

def __init__(self):
super().__init__()

indicator = bt.talib.RSI(self.data.close, timeperiod=self.p.timeperiod)

self.real_over_upper = indicator.real > 70
self.real_under_lower = indicator.real < 30

def next(self):
if self.real_over_upper:
self.sell()
elif self.real_under_lower:
self.buy()
35 changes: 35 additions & 0 deletions backtesting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import backtrader as bt
import numpy as np
from datetime import datetime
from importlib import import_module

def create_cerebro():
cerebro = bt.Cerebro()

cerebro.broker = bt.brokers.BackBroker(slip_perc=0.002)
cerebro.broker.set_cash(1000)
cerebro.broker.setcommission(commission=0.005)
cerebro.addsizer(bt.sizers.FixedSize, stake=0.05)

data = bt.feeds.YahooFinanceCSVData(dataname='data/ETH-USD.csv', fromdate=datetime(2021, 1, 1))
cerebro.adddata(data)

return cerebro

def optimize_algorithm(algorithms):
cerebro = create_cerebro()

for (algorithm, params) in algorithms.items():
module = import_module(f'algorithms.{algorithm}')
if hasattr(module, 'Strategy'):
cerebro.optstrategy(module.Strategy, **params)
else:
raise Exception('No Strategy in Module')

backtest = zip(algorithms.keys(), cerebro.run()[0])

return {
algorithm: {
param: strategy.p.__getattribute__(param) for param in strategy.p._getkeys()
} for algorithm, strategy in backtest
}
32 changes: 0 additions & 32 deletions test.py

This file was deleted.

0 comments on commit 3125d92

Please sign in to comment.