Experimenting with Cerebro

This commit is contained in:
Eric Meehan
2024-12-12 13:57:42 -05:00
parent b78637490a
commit 43f5ae3c53
10 changed files with 971 additions and 47 deletions

View File

@@ -77,28 +77,3 @@ class KrakenBot(KrakenAPI):
order.close_price, order.close_price2, order.deadline, order.validate, order.otp)
self.log.info(order)
self.update()
def update(self, full=False):
self.log.debug(f"update: full={full}")
self._update_tradable_asset_pairs()
self._filter_tradable_asset_pairs()
self._update_account_balance()
self._filter_account_balance()
self._update_trade_balance()
if full:
self._update_ohlc_data()
self.model.update(
self.tradable_asset_pairs,
self.ohlc_data
)
self.trading_strategy.update(
self.tradable_asset_pairs,
self.ohlc_data,
self.model.analysis,
self.account_balance
)
def execute(self):
self.log.debug(f"execute")
for order in self.trading_strategy.orders:
self._add_standard_order(order)

View File

@@ -0,0 +1,64 @@
import backtrader
class MA_CrossOver(backtrader.Strategy):
'''This is a long-only strategy which operates on a moving average cross
Note:
- Although the default
Buy Logic:
- No position is open on the data
- The ``fast`` moving averagecrosses over the ``slow`` strategy to the
upside.
Sell Logic:
- A position exists on the data
- The ``fast`` moving average crosses over the ``slow`` strategy to the
downside
Order Execution Type:
- Market
'''
alias = ('SMA_CrossOver',)
params = (
# period for the fast Moving Average
('fast', 24),
# period for the slow moving average
('slow', 72),
# moving average to use
('_movav', backtrader.ind.MovAv.SMA)
)
def __init__(self):
sma_fast = self.p._movav(period=self.p.fast)
sma_slow = self.p._movav(period=self.p.slow)
self.buysig = backtrader.ind.CrossOver(sma_fast, sma_slow)
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
if order.status in [order.Completed]:
if order.isbuy():
self.log('BUY EXECUTED, %.6f' % order.executed.price)
elif order.issell():
self.log('SELL EXECUTED, %.6f' % order.executed.price)
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def next(self):
if self.position.size:
if self.buysig < 0:
self.sell()
elif self.buysig > 0:
self.buy()

17
kraken_bot/SMAcross.py Normal file
View File

@@ -0,0 +1,17 @@
from backtesting import Strategy
from backtesting.lib import crossover
class SMAcross(Strategy):
n1 = 50
n2 = 100
def __init__(self):
close = self.data.Close
self.sma1 = self.I(ta.trend.sma_indicator, pd.Series(close), self.n1)
self.sma2 = self.I(ta.trend.sma_indicator, pd.Series(close), self.n2)
def next(self):
if crossover(self.sma1, self.sma2):
self.buy()
elif crossover(self.sma2, self.sma1):
self.sell()

10
kraken_bot/cerebro_bot.py Normal file
View File

@@ -0,0 +1,10 @@
class KrakenBot(KrakenAPI)

4
kraken_bot/test.py Normal file
View File

@@ -0,0 +1,4 @@
from backtesting import Backtest
def main():
backtest = Backtest(data, strategy, cash, commission, exclusive_orders

0
kraken_bot/trade.py Normal file
View File