Skip to content

Commit

Permalink
example code: how to resample data from quotes to minute bars in pape…
Browse files Browse the repository at this point in the history
…r/live
  • Loading branch information
shlomiku committed Oct 14, 2020
1 parent 082c8e3 commit c975ce3
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sample/strategy_resampling_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime, timedelta

# Your credentials here
ALPACA_API_KEY = "<key_id>"
ALPACA_SECRET_KEY = "<secret_key>"


IS_LIVE = False
symbol = "GOOG"
USE_POLYGON = True


class Resample(bt.Strategy):
def notify_data(self, data, status, *args, **kwargs):
super().notify_data(data, status, *args, **kwargs)
print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args)
if data._getstatusname(status) == "LIVE":
self.live_bars = True

def __init__(self):
self.live_bars = False

def next(self):
if not self.live_bars:
return
print(datetime.utcnow())


if __name__ == '__main__':
import logging
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

cerebro = bt.Cerebro()
cerebro.addstrategy(Resample)

store = alpaca_backtrader_api.AlpacaStore(
key_id=ALPACA_API_KEY,
secret_key=ALPACA_SECRET_KEY,
paper=not IS_LIVE,
usePolygon=USE_POLYGON
)
DataFactory = store.getdata
data0 = DataFactory(dataname=symbol,
historical=False,
timeframe=bt.TimeFrame.Minutes,
qcheck=10.0,
backfill_start=True,
fromdate=datetime.utcnow() - timedelta(
minutes=20)
)
broker = store.getbroker()
cerebro.setbroker(broker)
cerebro.resampledata(data0,
compression=2,
timeframe=bt.TimeFrame.Minutes)
cerebro.run()

0 comments on commit c975ce3

Please sign in to comment.