-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example code: how to resample data from quotes to minute bars in pape…
…r/live
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |