Skip to content

Commit

Permalink
Merge pull request #149 from alpacahq/data_v2
Browse files Browse the repository at this point in the history
Update to Alapca Data v2
  • Loading branch information
camelpac authored Jun 19, 2021
2 parents 7f2c006 + 55b2857 commit 7362a88
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 210 deletions.
2 changes: 1 addition & 1 deletion alpaca_backtrader_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
__all__ = [
'AlpacaStore', 'AlpacaBroker', 'AlpacaData',
]
__version__ = '0.13.1'
__version__ = '0.14.0'
2 changes: 1 addition & 1 deletion alpaca_backtrader_api/alpacabroker.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def update_positions(self):

for name, data in iteritems(self.cerebro.datasbyname):
if name in broker_positions_symbols:
size = int(broker_positions_mapped_by_symbol[name].qty)
size = float(broker_positions_mapped_by_symbol[name].qty)
positions[data] = Position(
size,
float(broker_positions_mapped_by_symbol[
Expand Down
18 changes: 10 additions & 8 deletions alpaca_backtrader_api/alpacadata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from datetime import datetime, timedelta

from datetime import timedelta
import pandas as pd
from backtrader.feed import DataBase
from backtrader import date2num, num2date
from backtrader.utils.py3 import queue, with_metaclass
Expand Down Expand Up @@ -133,6 +133,7 @@ class AlpacaData(with_metaclass(MetaAlpacaData, DataBase)):
('reconnect', True),
('reconnections', -1), # forever
('reconntimeout', 5.0),
('data_feed', 'iex'), # options iex/sip for pro
)

_store = alpacastore.AlpacaStore
Expand Down Expand Up @@ -233,7 +234,8 @@ def _st_start(self, instart=True, tmout=None):
return True
self.qlive = self.o.streaming_prices(self.p.dataname,
self.p.timeframe,
tmout=tmout)
tmout=tmout,
data_feed=self.p.data_feed)
if instart:
self._statelivereconn = self.p.backfill_start
else:
Expand Down Expand Up @@ -338,7 +340,7 @@ def _load(self):
# passing None to fetch max possible in 1 request
dtbegin = None

dtend = datetime.utcfromtimestamp(int(msg['time']))
dtend = pd.Timestamp(msg['time'], unit='ns')

self.qhist = self.o.candles(
self.p.dataname, dtbegin, dtend,
Expand Down Expand Up @@ -401,7 +403,7 @@ def _load(self):
return False

def _load_tick(self, msg):
dtobj = datetime.utcfromtimestamp(msg['time'])
dtobj = pd.Timestamp(msg['time'], unit='ns')
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
Expand All @@ -413,8 +415,8 @@ def _load_tick(self, msg):

# Put the prices into the bar
tick = float(
msg['askprice']) if self.p.useask else float(
msg['bidprice'])
msg['ask_price']) if self.p.useask else float(
msg['bid_price'])
self.lines.open[0] = tick
self.lines.high[0] = tick
self.lines.low[0] = tick
Expand All @@ -425,7 +427,7 @@ def _load_tick(self, msg):
return True

def _load_agg(self, msg):
dtobj = datetime.utcfromtimestamp(int(msg['time']))
dtobj = pd.Timestamp(msg['time'], unit='ns')
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
Expand Down
Loading

0 comments on commit 7362a88

Please sign in to comment.