Skip to content

Commit

Permalink
Merge pull request #255 from alpacahq/fix-data-v2-issues
Browse files Browse the repository at this point in the history
Quick fixes to remove calls to market data v1 from alpaca SDK
  • Loading branch information
drew887 authored Apr 1, 2022
2 parents 2d9bf97 + 5d951a3 commit c9cce67
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
10 changes: 7 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ jobs:
- run:
command: |
. venv/bin/activate
python3 setup.py flake8 test
pip install -r requirements/requirements_test.txt
flake8 ./pylivetrader && echo "Flake8 passed"
pytest
build-python37:
docker:
Expand All @@ -43,10 +45,12 @@ jobs:
- run:
command: |
. venv/bin/activate
python3 setup.py flake8 test
pip install -r requirements/requirements_test.txt
flake8 ./pylivetrader && echo "Flake8 passed"
pytest
workflows:
version: 2
build:
jobs:
- build-python36
- build-python36
2 changes: 1 addition & 1 deletion pylivetrader/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

VERSION = '0.6.0'
VERSION = '0.7.0'
42 changes: 21 additions & 21 deletions pylivetrader/backend/alpaca.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def cancel_order(self, zp_order_id):
return

def get_last_traded_dt(self, asset):
trade = self._api.get_last_trade(asset.symbol)
trade = self._api.get_latest_trade(asset.symbol)
return trade.timestamp

def get_spot_value(
Expand Down Expand Up @@ -496,7 +496,7 @@ def _get_symbols_last_trade_value(self, symbols):

@skip_http_error((404, 504))
def fetch(symbol):
return self._api.get_last_trade(symbol)
return self._api.get_latest_trade(symbol)

return parallelize(fetch)(symbols)

Expand Down Expand Up @@ -645,25 +645,25 @@ def wrapper():
_from = params['_from']
to = params['to']
size = params['size']
df = self._api.get_barset(symbols,
size,
limit=params['limit'],
start=_from.isoformat(),
end=to.isoformat()).df[symbols]

if df.empty:
# we got an empty response. We will try to use the updated
# V2 api to get the data. we cannot do 1 api call for all
# symbols so we will iterate them
r = {}
for sym in symbols:
r[sym] = self._api.get_bars(sym, TimeFrame.Minute,
_from.isoformat(),
to.isoformat(),
adjustment='raw').df
df = pd.concat(r, axis=1)
# data is received in UTC tz but without tz (naive)
df.index = df.index.tz_localize("UTC")

timeframe = TimeFrame.Minute if size == "minute" else TimeFrame.Day

# Using V2 api to get the data. we cannot do 1 api call for all
# symbols because the v1 `limit` was per symbol, where v2 it's for
# overall response size; so we will iterate over each symbol with
# the limit for each to replicate that behaviour
r = {}
for sym in symbols:
r[sym] = self._api.get_bars(sym,
limit=params['limit'],
timeframe=timeframe,
start=_from.isoformat(),
end=to.isoformat(),
adjustment='raw').df
df = pd.concat(r, axis=1)
# data is received in UTC tz but without tz (naive)
df.index = df.index.tz_localize("UTC")

if size == 'minute':
df.index += pd.Timedelta('1min')

Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ trading_calendars>=1.11
click==8.0.1
PyYAML>=5, <6
ipython>=7
alpaca-trade-api>=1.2.2
alpaca-trade-api==1.5.0
pandas>=0.18.1, <=0.22.0 # pyup: ignore # limit to work properly with zipline 1.3.0
pandas-datareader<=0.8.1 # pyup: ignore # higher requires pandas>=0.23, zipline limits to 0.22
3 changes: 2 additions & 1 deletion requirements/requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest>=5.0.0
pytest-cov
pytest-cov
flake8

0 comments on commit c9cce67

Please sign in to comment.