Giter VIP home page Giter VIP logo

alpaca-backtrader-api's People

Contributors

albertpurnama avatar camelpac avatar devination avatar haxdds avatar ic-gcp avatar jtbales avatar jzderadicka avatar pyup-bot avatar ronnyli avatar shlomiku avatar smartchris84 avatar tantalon avatar ttt733 avatar umitanuki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

alpaca-backtrader-api's Issues

concurrent.futures._base.CancelledError

Sometime after my live paper trading program reaches data.status of LIVE I get this error.

May 28 11:22:12 sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 28 11:22:16 sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 28 11:22:16 sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 28 11:22:20 Reached LIVE data
May 28 11:41:59 Exception in thread Thread-6:
May 28 11:41:59 Traceback (most recent call last):
May 28 11:41:59   File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
May 28 11:41:59     self.run()
May 28 11:41:59   File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
May 28 11:41:59     self._target(*self._args, **self._kwargs)
May 28 11:41:59   File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 483, in _t_streaming_prices
May 28 11:41:59     streamer.run()
May 28 11:41:59   File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 132, in run
May 28 11:41:59     self.conn.run(channels)
May 28 11:41:59   File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/stream2.py", line 276, in run
May 28 11:41:59     loop.run_until_complete(self.consume())
May 28 11:41:59   File "/app/.heroku/python/lib/python3.6/asyncio/base_events.py", line 488, in run_until_complete
May 28 11:41:59     return future.result()
May 28 11:41:59 concurrent.futures._base.CancelledError
May 28 11:41:59 
May 28 12:08:07 WARNING:root:code = 1006 (connection closed abnormally [internal]), no reason
May 28 12:08:07 Exception in thread Thread-2:
May 28 12:08:07 Traceback (most recent call last):
May 28 12:08:07   File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
May 28 12:08:07     self.run()
May 28 12:08:07   File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
May 28 12:08:07     self._target(*self._args, **self._kwargs)
May 28 12:08:07   File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 341, in _t_streaming_events
May 28 12:08:07     streamer.run()
May 28 12:08:07   File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 132, in run
May 28 12:08:07     self.conn.run(channels)
May 28 12:08:07   File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/stream2.py", line 276, in run
May 28 12:08:07     loop.run_until_complete(self.consume())
May 28 12:08:07   File "/app/.heroku/python/lib/python3.6/asyncio/base_events.py", line 488, in run_until_complete
May 28 12:08:07     return future.result()
May 28 12:08:07 concurrent.futures._base.CancelledError

This code in the strategy logs the switch to LIVE

    def notify_data(self, data, status, *args, **kwargs):
        if status == data.LIVE:
            print("Reached LIVE data")
            self.reached_live_data = True

This error happens eventually every time I run it live.

This occurred when using the latest stable release and using git+https://github.com/alpacahq/alpaca-backtrader-api

Pandas time zone error when running the example

As I run the README example, I am getting an exception from pandas, complaining about comparing tz aware and naive objects.

Exception in thread Thread-4:
Traceback (most recent call last):
  File "pandas/_libs/index.pyx", line 463, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 997, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1004, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 1581298739323852000
...
    "Cannot compare tz-naive and tz-aware datetime-like objects"
TypeError: Cannot compare tz-naive and tz-aware datetime-like objects_

Here is the code I am running, I tried tz aware (start_time) or naive for the fromdate in the DataFactory object, no dice. Any idea?

import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime, timedelta
import pandas as pd
import pytz

class SmaCross(bt.SignalStrategy):
  def __init__(self):
    sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
    crossover = bt.ind.CrossOver(sma1, sma2)
    self.signal_add(bt.SIGNAL_LONG, crossover)

ALPACA_PAPER = True
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)

store = alpaca_backtrader_api.AlpacaStore(
    key_id=APCA_API_KEY_ID,
    secret_key=APCA_API_SECRET_KEY,
    paper=ALPACA_PAPER
)

timezone = pytz.timezone("America/Los_Angeles")
start_time = datetime.now() - timedelta(days=60)
start_time = timezone.localize(start_time)

DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
data0 = DataFactory(dataname='AAPL', historical=True, fromdate=pd.Timestamp('2018-11-15'), timeframe=bt.TimeFrame.Days)
cerebro.adddata(data0)

print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()

Stop as a percent of trade.

Is there a way to submit the stop price as the percent instead of a pre-calculated value? i.e. We want to submit a market order with a stop loss of .1%. Our preference is to submit a stop loss as a percent of the executed price, not calculated based on the current price or close price. Let's take an example. Assume the close price is 100 when we placed a market order which gets filled at 100.99 (executed price), in this case, we want the stop loss to be 100.99*.999 instead of 100 *.999.

Thanks in advance.

data not recognized by backtrader

if use
data = DataFactory(dataname='AAPL', historical=True, fromdate=datetime.datetime(2008, 1, 1), timeframe=bt.TimeFrame.Days)
reports error
'NoneType' object has no attribute 'isoformat'
if use
data = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime.datetime(2011, 1, 1), todate=datetime.datetime(2019, 12, 31))
runs fine.

How do I start paper trade from the recent time?

I was under the impression that if you start paper trading and set historical=False with no date it should start getting data from the most recent time. When I try with other broker such as backtrader with Oanda, it does not seem necessary to set fromdate.

    paper = True
    live = True    

    store = alpaca_backtrader_api.AlpacaStore(
        key_id=key_id,
        secret_key=secret_key,
        paper=paper,
    )

    DataFactory = store.getdata
    data0 = DataFactory(dataname='SHOP',
            timeframe=bt.TimeFrame.TFrame("Minutes"),
            historical=False,
    )

    if live:
        broker = store.getbroker(use_positions=True)
        cerebro.setbroker(broker)
    else:
        cerebro.broker.setcash(100000.0)
       
    data0.addfilter(bt.filters.Renko, size=0.5)
    cerebro.adddata(data0)

and then logging out the time in next():

        txt = list()
        txt.append('Data0')
        txt.append('%04d' % len(self.data0))
        dtfmt = '%Y-%m-%dT%H:%M:%S.%f'
        txt.append('%s' % self.data.datetime.datetime(0).strftime(dtfmt))
        txt.append('C {:2f}'.format(self.data.close[0]))
        txt.append('O {:2f}'.format(self.data.open[0]))
        print(', '.join(txt))

Today is currently 5/12 18:05, but the data will feeding from 5/11 13:56.

Any suggestions? Also in general, I can't seem to get any sort of Renko chart to operate correctly e.g. it always looks and acts differently from what I'd see on TradingView and fires multiple signals for the same bar if anyone has any experience with that. Any help is greatly appreciated.

Cache Feed Responses

I've been running a few different strategies and tweaking using the same data feed. Each time it makes a call out to the alpaca servers for the data. Would be nice if we could cache the request/responses locally for faster iterations and less load on your servers.

Notify acts unexpectedly. Only ever Order.Submitted. No notify_trade.

I have the following notify_ methods in my strategy on my paper account to log events.

def notify_order(self, order):
    print(f"notify_order Status: {order.getstatusname()}, Ref {order.ref}")

def notify_trade(self, trade):
    print(f"notify_trade --- Placing trade for {trade.getdataname()}. Target size: {trade.size}")

def notify_fund(self, cash, value, fundvalue, shares):
    if value != self.last_portfolio_value:
        print(f"notify_fund --- Cash: {cash:.2f}, Value: {value:.2f}, Fundvalue: {fundvalue:.2f}, Shares: {shares:.0f}")
        self.last_portfolio_value = value

notify_order

I only ever received order status of Submitted

('notify_order Status: Submitted, Ref 1',)
('notify_order Status: Submitted, Ref 2',)
...
('notify_order Status: Submitted, Ref 117',)
('notify_order Status: Submitted, Ref 119',)
...

No Completed or anything else.

But as you can see from the image below, orders definitely went through to Alpaca (119 filled paper orders). The UP arrows show BUYS.

Screen Shot 2020-06-01 at 9 25 58 AM

notify_trade

Never received a notify_trade log.

notify_fund

Weird thing here, I got tons of notifications for this, but the all said I had only 1 share.

...
('notify_fund --- Cash: 99749.92, Value: 99967.08, Fundvalue: 99967.08, Shares: 1',)
...
('notify_fund --- Cash: 99749.92, Value: 99967.97, Fundvalue: 99967.97, Shares: 1',)
...

Orders Limit Price and Stop Price not working

Placing a bracket order as follows should result in an order with a limit and a stop price:

self.buy_bracket(data=d, exectype=bt.Order.Market, stopprice=stop_price, limitprice=take_prof, 
 size=qty)

However, it looks like this order just ends up being a regular buy order with no limit/stop price. Also it looks like all 3 orders of the bracket are accepted, which is strange that we just end up with one regular buy order.

TypeError: 'Entity' object is not subscriptable

I've often come across this error either right after executing an order or when loading non-historical data.

Here's an example after execution

May 27 10:11:57  /app/.heroku/python/lib/python3.6/site-packages/alpaca_trade_api/stream2.py:151: UserWarning: Discarding nonzero nanoseconds in conversion
May 27 10:11:57    await handler(self, channel, ent)
May 27 10:12:12  sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 27 10:12:15  sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 27 10:12:19  sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 27 10:12:22  sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...
May 27 10:12:25  buy_bracket - limitprice=826.360, price=822.569, stopprice=820.885
May 27 10:12:25  Exception in thread Thread-1:
May 27 10:12:25  Traceback (most recent call last):
May 27 10:12:25    File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
May 27 10:12:25      self.run()
May 27 10:12:25    File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
May 27 10:12:25      self._target(*self._args, **self._kwargs)
May 27 10:12:25    File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 328, in _t_streaming_listener
May 27 10:12:25      self._transaction(trans)
May 27 10:12:25    File "/app/.heroku/python/lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 650, in _transaction
May 27 10:12:25      oid = trans['id']
May 27 10:12:25  TypeError: 'Entity' object is not subscriptable

This happens both using polygon and not using polygon.

API Never Gets to Strategy.next() on Unsupported Exchange Data

I came across the situation this week where when one of the stocks I add data to Cerebro using default datafactory, the API will never get to Strategy.next() call (it will hang forever). I didn't realize the symbol (ADRNY from OTCQX) was not traded in a supported exchange until digging into it further. I would think that would trigger an error somewhere instead of the API hanging. If I should be doing this check myself, can I get pointed to where the data going into the datafactory contains the exchange (or other location if that isn't where I should be looking)?

Support for Oauth

Hi, does alpaca-backtrader-api support authenticating with Oauth access tokens? A quick look through the source code seems to imply that we can only authenticate with API keys.

Would adding support for Oauth be as simple as adding the oauth param to AlpacaStore and passing that parameter to API(tradeapi.REST)? If so, I can make a PR to add this functionality

stop loss orders in brackets are not notified when filled

the stop loss order, when filled is replaced by a different order and then filled
it's a broker implementation. the take profit order doesn't, it is filled directly
when the order is replaced, it is not found in the backtrader's cache.
we need to make sure that either way the order is completed.

next() is run every second in live mode, even though it is set to days

Hello,
I started running my strategy with live paper trading. During my backtesting I had the data initialized like this:

    spy_data = DataFactory(
            dataname="SPY",
            timeframe=bt.TimeFrame.Days,
            plot=True)
    cerebro.adddata(spy_data)

My goal is to just run the model once a day. However, when I run the algorithm in live mode with the same setup as the backtesting, I noticed the algorithm is running the next() every second.

This is what I get in my log file

2020-07-07 06:31:14,766 INFO Running next()
2020-07-07 06:31:14,817 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:14,869 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:14,919 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:14,920 INFO Running next()
2020-07-07 06:31:14,927 DEBUG client - event = data_received(<109 bytes>)
2020-07-07 06:31:14,928 DEBUG client < Frame(fin=True, opcode=1, data=b'[{"ev":"Q","sym":"SPY","c":1,"bx":19,"ax":12,"bp":315,"ap":315.04,"bs":10,"as":10,"t":1594121474909,"z":1}]', rsv1=False, rsv2=False, rsv3=False)
2020-07-07 06:31:14,938 DEBUG client - event = data_received(<108 bytes>)
2020-07-07 06:31:14,938 DEBUG client < Frame(fin=True, opcode=1, data=b'[{"ev":"Q","sym":"SPY","c":1,"bx":8,"ax":12,"bp":315,"ap":315.04,"bs":11,"as":10,"t":1594121474920,"z":1}]', rsv1=False, rsv2=False, rsv3=False)
2020-07-07 06:31:14,970 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:15,023 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:15,073 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:15,074 INFO Running next()
2020-07-07 06:31:15,125 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:15,177 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:15,226 DEBUG https://paper-api.alpaca.markets:443 "GET /v2/account HTTP/1.1" 200 672
2020-07-07 06:31:15,228 INFO Running next()

Notice, how the "INFO Running next()" is popping up every second or so. Am I misunderstanding a behaviour of live trading or is there a setting I am missing?

Authorization Error

Hey everyone,

thanks for this nice tool. I would like to use alpaca paper trading data, but I always get the following error:

nats.aio.errors.NatsError: nats: 'Authorization Violation'

I am just running a basic example with (triple checked) valid api key and api secret for the paper api. Could you point me in the right direction to resolve this issue?

I am using the latest backtrader, Python 3.7 and pipenv.

Any help is appreciated. Thanks!

your connection is rejected while another connection is open under the same account

Hey, I am running into an error with multiple data feeds, e.g. with the example https://github.com/alpacahq/alpaca-backtrader-api/blob/master/sample/strategy_multiple_datas.py

Error:
alpaca_trade_api\stream2.py", line 52, in _connect raise Exception(f"Error while connecting to {self._endpoint}:" Exception: Error while connecting to wss://data.alpaca.markets/stream:your connection is rejected while another connection is open under the same account


It's working with one data feed, and I restarted my computer, tried another account, ... & the same message appears.

Running Windows 10, Python 3.6

My guess is, that it is related to me not having a "full" account because I'm non-US and the datafeed might only allow one feed per paper account?

question about live mode

I have a question about live mode. What if you have an indicator that takes like 50 candles or 100 candles in backdata, then you run live mode. Does it use your polygon data (or w/e data you're using for backtests) and run through the backtest first in the background? Or do you have to wait 50 or 100 candles just for it to start trading live using your strategy?

I know this is a dumb question I just don't understand how this works and it's very important to my strategy.

Use `order.data._name` instead of `order.data._dataname` in store.order_create

I'm trying to submit a paper order with the following okwargs

{'symbol':               TMF
2020-03-02  38.00
2020-03-03  40.78
2020-03-04  39.64
2020-03-05  42.51
2020-03-06  48.01
...           ...
2020-05-27  41.79
2020-05-28  40.73
2020-05-29  41.27
2020-06-01  41.45
2020-06-02  40.30

[65 rows x 1 columns], 'qty': 162, 'side': 'buy', 'type': 'market', 'time_in_force': 'gtc'}

The error message I get back from Alpaca is: {'code': 596, 'message': 'Network Error', 'description': 'Object of type DataFrame is not JSON serializable'}

Likely the issue is with this line where data._dataname is being used instead of data._name.

For context I am using backtrader.feeds.PandasData which has a meaningful difference between dataname and name like so:

backtrader.feeds.PandasData(
        dataname=market_data[['TMF']],
        name='TMF',
        **kwargs
    )

EDIT: I switched to order.data._name and confirm that the orders went through to Alpaca

get_account() return a string, but a number is needed

REST API: get_account() return cash and portfolio as a string, but a number is needed to create a line in a broker.

see _t_account() in AlpacaStore.py

REST API return:

{
    ...
    "cash": "89120.36",
    "portfolio_value": "99371.06",
    ...
}

Exception:

Traceback (most recent call last):
  File ".\demo_backtest.py", line 56, in <module>
    cerebro.run(exactbars=False)
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies
    self._runnext(runstrats)
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext
    strat._next()
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\strategy.py", line 351, in _next
    self._next_observers(minperstatus)
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\strategy.py", line 379, in _next_observers
    observer._next()
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\lineiterator.py", line 282, in _next
    self.nextstart()  # only called for the 1st value
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\lineiterator.py", line 347, in nextstart
    self.next()
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\observers\broker.py", line 114, in next
    self.lines.value[0] = value = self._owner.broker.getvalue()
  File "C:\Users\Test\Miniconda3\envs\alpaca-trade\lib\site-packages\backtrader\linebuffer.py", line 222, in __setitem__
    self.array[self.idx + ago] = value

multiple_data_strategy.py throwing exception: your connection is rejected

Hey there,

I'm encountering this issue while running the unmodified multiple_data_strategy.py sample in paper trading mode. I have no other scripts running on the account. This is the exception:

Starting Portfolio Value: 100000.0
Exception in thread Thread-7:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_backtrader_api/alpacastore.py", line 483, in _t_streaming_prices
streamer.run()
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_backtrader_api/alpacastore.py", line 132, in run
self.conn.run(channels)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 275, in run
loop.run_until_complete(self.subscribe(initial_channels))
File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 250, in subscribe
await self.data_ws.subscribe(data_channels)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 104, in subscribe
await self._ensure_ws()
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 88, in _ensure_ws
await self._connect()
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 52, in _connect
raise Exception(f"Error while connecting to {self._endpoint}:"
Exception: Error while connecting to wss://data.alpaca.markets/stream:your connection is rejected while another connection is open under the same account

backfill data not filled properly due to wrong timezone info

when doing a backfill we get the data from alpaca/polygon at the beginning of the execution
then we change the tz info to match the NY tz
due to day light savings, we get an offset by M minutes (changes with the day and year)
how does it influence us?
e.g we need minute data from X to Y, but we get Y - M
M is the offset
so, backtrader doesn't "think" it has all the data it requires to start

we change replace() to localize() which takes care of the issue.
we could see the difference here:
image

you can read more about it here:
https://stackoverflow.com/a/1592837/2739124
https://stackoverflow.com/questions/35462876/python-pytz-timezone-function-returns-a-timezone-that-is-off-by-9-minutes
http://pytz.sourceforge.net/

No current event loop in thread when running the example

I tried to run the example in README.md, but I got a "No current event loop in thread" exception.

I have the following packages installed

  • Python 3.6.8
  • alpaca-backtrader-api 0.3
  • alpaca-trade-api 0.35
  • backtrader 1.9.74.123

Python script that I used:

import backtrader as bt
import alpaca_backtrader_api

from datetime import datetime

import sceret

class SmaCross(bt.SignalStrategy):
    def __init__(self):
        sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
        crossover = bt.ind.CrossOver(sma1, sma2)
        self.signal_add(bt.SIGNAL_LONG, crossover)

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

store = alpaca_backtrader_api.AlpacaStore(
    key_id=sceret.api_key_id,
    secret_key=sceret.api_secret,
    paper=True
)

broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
cerebro.setbroker(broker)

DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
data0 = DataFactory(dataname='AAPL', 
                    historical=True, 
                    fromdate=datetime(2015, 1,1), 
                    timeframe=bt.TimeFrame.TFrame("Days"))  # Supported timeframes: "Days"/"Minutes"
cerebro.adddata(data0)

cerebro.run(exactbars=1)
cerebro.plot()

The exception that I got.

Exception in thread Thread-97:
Traceback (most recent call last):
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 309, in _t_streaming_events
    base_url=self.p.base_url)
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 91, in __init__
    self.conn = tradeapi.StreamConn(api_key, api_secret, base_url)
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\site-packages\alpaca_trade_api\stream2.py", line 21, in __init__
    self.loop = asyncio.get_event_loop()
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\asyncio\events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "C:\Users\SomeUser\Miniconda3\envs\alpaca-trade\lib\asyncio\events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-97'.

Error in display of order completed using notify_order

Hi,

I am getting 'dict' object has no attribute 'order' error whenever order is completed. I have used on of the sample test. Example of an error:

2020-04-27: Order ref: 22 / Type Sell / Status Submitted
2020-04-27: Order ref: 22 / Type Sell / Status Accepted
2020-04-27 16:35:00: O 282.08, H 282.2, L 282.08, C 282.11
'dict' object has no attribute 'order'
2020-04-27: Order ref: 23 / Type Sell / Status Submitted
2020-04-27: Order ref: 23 / Type Sell / Status Accepted
2020-04-27 16:36:00: O 282.11, H 282.39, L 282.08, C 282.22
'dict' object has no attribute 'order'

Corrupt data with LIVE trading for the first few seconds from starting up

I am experiencing some corrupt data upon starting up the script and printing data from next().
The data I have seen is the value in:
self.datetime.date()
self.getposition(d).size

DATE  TIME     SYMBOL       SIZE             time.time()
06-10 00:00:00 TVIX: CHECK  121.96 0.00 0.00 1591899222.070981
06-10 00:00:00 TVIX: CHECK  121.96 0.00 0.00 1591899222.0719814
06-11 00:00:00 TVIX: CHECK  171.85 0.00 0.00 1591899222.2689812
06-11 14:13:36 TVIX: CHECK  170.90 0.00 0.00 1591899222.5389812
06-11 14:13:37 TVIX: CHECK  170.90 0.00 0.00 1591899222.809981
06-11 14:13:38 TVIX: CHECK  171.12 0.00 0.00 1591899223.090981

For the live data feed, the first two rows have the DATA/TIME incorrect as well as the position size. The third row shows the time incorrect.
The data starts becoming valid on the 4th row.

Multiple live data feed is not working

Hi,

I tried running sample program on live data with multiple symbol but there is no display of data (I had added the log for data display). Please let me know if you need my code.

Error running samples - Paper Trading

I've added my credentials to the samples and I'm run the samples and set paper trading to True. This occurs on both windows and linux

Exception in thread Thread-2: Traceback (most recent call last): File "C:\Users\Tyler Collins\anaconda3\envs\AlgoTrading\lib\threading.py", line 926, in _bootstrap_inner self.run() File "C:\Users\Tyler Collins\anaconda3\envs\AlgoTrading\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\Tyler Collins\anaconda3\envs\AlgoTrading\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 316, in _t_streaming_events base_url=self.p.base_url) File "C:\Users\Tyler Collins\anaconda3\envs\AlgoTrading\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 94, in __init__ self.conn = tradeapi.StreamConn(api_key, api_secret, base_url) File "C:\Users\Tyler Collins\anaconda3\envs\AlgoTrading\lib\site-packages\alpaca_trade_api\stream2.py", line 27, in __init__ self.loop = asyncio.get_event_loop() File "C:\Users\Tyler Collins\anaconda3\envs\AlgoTrading\lib\asyncio\events.py", line 644, in get_event_loop % threading.current_thread().name) RuntimeError: There is no current event loop in thread 'Thread-2'.

RuntimeError: There is no current event loop in thread

When trying to go live, I am getting the following error. This was previously brought up by people but dismissed as caused because a live account is needed (paper account can't access polygon's api). I have opened a live account to get this working but still the same issue. Note that I have confirmed by live account API key ID works fine against Polygon's end point when I try it directly.

I have the latest package installed:

pip freeze | findstr alpaca-backtrader-api
alpaca-backtrader-api==0.7

Here is the code I am running:

# This is the example code from the repo's README
import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime
from secrets import alpaca_live_id, alpaca_live_key

class TestAlgo(bt.Strategy):
    def next(self):
        dt = self.datetime.datetime(ago=0)
        for d in self.datas:
            dn = d._name
            print(dt, dn, d.close[0])

if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.addstrategy(TestAlgo)

    store = alpaca_backtrader_api.AlpacaStore(
        key_id=alpaca_live_id,
        secret_key=alpaca_live_key,
        paper=False
    )

    # or just alpaca_backtrader_api.AlpacaBroker()
    broker = store.getbroker()
    cerebro.setbroker(broker)

    DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
    data0 = DataFactory(dataname='AAPL',
                        historical=False,
                        timeframe=bt.TimeFrame.Minutes)
    cerebro.adddata(data0)

    print('Starting Portfolio Value: {}'.format(cerebro.broker.getvalue()))
    cerebro.run()
    print('Final Portfolio Value: {}'.format(cerebro.broker.getvalue()))
    cerebro.plot()

Here is the output I am getting:

Starting Portfolio Value: 1.0
Exception in thread Thread-7:
Traceback (most recent call last):
  File "C:\Users\kr\Miniconda3\envs\kr\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\kr\Miniconda3\envs\kr\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\kr\Miniconda3\envs\kr\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 316, in _t_streaming_events
    base_url=self.p.base_url)
  File "C:\Users\kr\Miniconda3\envs\kr\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 94, in __init__
    self.conn = tradeapi.StreamConn(api_key, api_secret, base_url)
  File "C:\Users\kr\Miniconda3\envs\kr\lib\site-packages\alpaca_trade_api\stream2.py", line 27, in __init__
    self.loop = asyncio.get_event_loop()
  File "C:\Users\kr\Miniconda3\envs\kr\lib\asyncio\events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "C:\Users\kr\Miniconda3\envs\kr\lib\asyncio\events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-7'.

Exception in thread Thread-11:
Traceback (most recent call last):
  File "C:\Users\kr\Miniconda3\envs\kr\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\kr\Miniconda3\envs\kr\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\kr\Miniconda3\envs\kr\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 449, in _t_streaming_prices
    base_url=self.p.base_url)
  File "C:\Users\kr\Miniconda3\envs\kr\lib\site-packages\alpaca_backtrader_api\alpacastore.py", line 94, in __init__
    self.conn = tradeapi.StreamConn(api_key, api_secret, base_url)
  File "C:\Users\kr\Miniconda3\envs\kr\lib\site-packages\alpaca_trade_api\stream2.py", line 27, in __init__
    self.loop = asyncio.get_event_loop()
  File "C:\Users\kr\Miniconda3\envs\kr\lib\asyncio\events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "C:\Users\kr\Miniconda3\envs\kr\lib\asyncio\events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-11'.

backfill data is fetched incorrectly (polygon and alpaca both)

we have 3 issues:

  1. dates range is too large
  • polygon api doesn't indicate a limit for 1 api call but you can't get six months of data in 1 api call (makes since). data is returned corrupted (patchy data)
  • Alpaca has a limit of 1000 records per api call
  1. compression - or not 1 minute/daily bars (e.g 5 minute, 30 minute, 60 minute bars)
  • polygon supports it. alpaca doesn't
  1. out of market data
  • both api return out of market hours data. that is of course not what we want when we do these calls

all issues should be addressed

Invalid Polygon credentials

`ValueError: Invalid Polygon credentials, Failed to authenticate: {'ev': 'status', 'status': 'auth_failed', 'message': 'authentication failed'}

I've found similar issues with original alpaca-trade-api-python. But what about alpaca-backtrader-api?

`

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

getcash() returns 0

Looks like alpaca broker's getcash() is returning 0. getvalue() seems to be OK.
I also want to ask what is the best way to get buying power (now that Alpaca has leverage). For now getcash doesn't even work (not sure if this is supposed to show actual cash or buying power), and I see other things like get_fundvalue() returns my 100k cash. I can't seem to get the 400k that my paper account shows as my buying power.
I am using version 0.7.1.

`backfill_start` no longer working as before

If I start a strategy in paper mode with backfill_start=True I get no backfill data. I've been looking through the change history and I haven't been able to nail it down yet, although I am just becoming familiar with the codebase.

Paper trading broken- due to wrong data type for amounts or size (str vs int or float)

Versions:

alpaca-backtrader-api==0.6
alpaca-trade-api==0.46

Try example at (with ALPACA_PAPER = True):

https://github.com/alpacahq/alpaca-backtrader-api/blob/master/sample/sma_crossover_strategy.py

Hit error:

Traceback (most recent call last):
  File "sma_crossover_strategy.py", line 81, in <module>
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
TypeError: must be real number, not str

Change the line to:

 print('Starting Portfolio Value: %s' % cerebro.broker.getvalue())

Try again and hit:

Exception in thread Thread-2:
Traceback (most recent call last):
File "lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 316, in _t_streaming_events
base_url=self.p.base_url)
File "lib/python3.6/site-packages/alpaca_backtrader_api/alpacastore.py", line 94, in __init__
self.conn = tradeapi.StreamConn(api_key, api_secret, base_url)
File "lib/python3.6/site-packages/alpaca_trade_api/stream2.py", line 27, in __init__
self.loop = asyncio.get_event_loop()
File "lib/python3.6/asyncio/events.py", line 694, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "lib/python3.6/asyncio/events.py", line 602, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-2'.

  Traceback (most recent call last):
File "sma_crossover_strategy.py", line 82, in <module>
                                                                                   cerebro.run()
File "lib/python3.6/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "lib/python3.6/site-packages/backtrader/cerebro.py", line 1187, in runstrategies
self._broker.start()
File "lib/python3.6/site-packages/alpaca_backtrader_api/alpacabroker.py", line 107, in start
self.positions = self.update_positions()
File "lib/python3.6/site-packages/alpaca_backtrader_api/alpacabroker.py", line 97, in update_positions
broker_positions_mapped_by_symbol[name].avg_entry_price
File "lib/python3.6/site-packages/backtrader/position.py", line 64, in __init__
self.set(size, price)
File "lib/python3.6/site-packages/backtrader/position.py", line 75, in set
if self.size > 0:
  TypeError: '>' not supported between instances of 'str' and 'int'

Basically seems like the alpaca broker implementation doesn't convert the str values to correct data type like float or int

Position/Notifications not updated for bracket orders

After a buy order has been placed with the LIVE data feed, and was properly accepted. Monitoring the position from next() does not match what is shown on Alpaca's site.
That can be easily tested by performing a buy order through the alpaca-trade-api, then closing the position on the Alpaca website. The position does not get updated in the next() function.

I am also experiencing missing notifications for bracket orders (buy limit, sell stop, sell limit).
The first buy order notifications is received, but once the stop or sell limits are eventually triggered, no notifications are sent to notify_order().

This is working fine for non-live trading.

Unable to Run Sample

Hello,

I am trying to run the sample but looks like the API is not working with backtrader.

I created a new conda enviroment with python 3.6 in mac and install these packages with pip

alpaca-backtrader-api 0.3
alpaca-trade-api 0.24
backtrader 1.9.69.122

This line is showing error after I put the key.

store = alpaca_backtrader_api.AlpacaStore(
key_id='',
secret_key='',
paper=True
)
screen shot 2019-01-02 at 10 00 15 pm

Also, I uncomment alpaca_backtrader_api.AlpacaBroker() alpaca_backtrader_api.AlpacaData and try to use them directly show authorization error even I have set this in my .bash_profile and source it.

export APCA_API_BASE_URL="https://paper-api.alpaca.markets"
export APCA_API_KEY_ID="mykeyid"
export APCA_API_SECRET_KEY="mysecret"

However, if only using alpaca_trade_api just works to prove that my environment variable setup is good.

import alpaca_trade_api as tradeapi

api = tradeapi.REST()
account = api.get_account()
print(account.status)

Do you have any ideas ?

This depends on Polygon Aggregate v1, which has been depreciated.

When using this library, it now throws an error when getting data from Polygon (see alpacastore.py line 355). The v1 aggregate library has been depreciated (see https://polygon.io/blog/action-required-v1-tick-api-will-be-deprecated-on-01-01-2020/) and thus this library needs to be updated to use the v2 endpoint.

I get the following error when running a strategy:

requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.polygon.io/v1/historic/agg/minute/AAPL?from=2019-03-17T00% ... (cut off so as not to include my API key)

question about candle data

Sorry if not supposed to post this here I don't use Github a lot. I was wondering how do you use 5m candles? I saw that you can change

DataFactory(dataname='AAPL', historical=True, fromdate=datetime(
            2015, 1, 1), timeframe=bt.TimeFrame.Days)

to Minutes instead of Days, but where do you define how many minutes? Or can this not be done in which case I would like to request this feature :D

Bug in broker.getvalue

I ran into an issue when trying to run self.order_target_percent and noticed that I was only creating SELL orders despite having an empty paper portfolio and using a positive target percentage. Digging deeper I noticed that this line in backtrader.Strategy is necessary to determine the portfolio value for specific assets.

I believe the issue will be fixed by modifying the alpacabroker.getvalue method here to use the datas argument.

Latest API breaks resample

If I start a Strategy in Paper trading mode I get the following error if I try to do resample on the data stream.

Exception: Error while connecting to wss://data.alpaca.markets/stream:your connection is rejected while another connection is open under the same account

This may be an issue with how Backtrader does resampling. Unsure yet, but it it makes it almost impossible to use an Hourly or Daily strategy effectively. I might be able to use a filter to filter out all but the open/close ticks so that my non-Tick strategy still has the right bar timelength.

Datafactory compression not affecting when Next() is called

I'm currently trying to paper trade an algo that works well on 1H bars. I've used the samples to get paper trading to stream data in. I add the "compression=60" to my DataFactory which has timeframe=bt.TimeFrame.Minutes.

When I set historical=True, everything works great, my indicators run on the 1H bar. However when i switch to historical=False and connect to paper trading, it seems next() is getting called for every tick, and the rate that trades are being made it seems like the indicators are being calculated not on 1H bars but maybe the ticks as trades are constantly being triggered.

Do you have any samples showing how to use compression on bars in live/paper trading?

Stock tickers with missing data cause backtest not to run

I don't believe this issue has been addressed yet. I found this problem when running a list of stocks. ticker "BFYT" was causing me issues. It turns out ticker "BFYT" used to be ticker "HIIQ" before March 6, 2020. In the polygon api BFYT has no data before March 5th, and HIIQ has no data past March 5th. If you try to run a backtest with BFYT from September 2019 - April 2020 you will not get any errors, however, the backtest just doesn't run at all. Maybe the missing data could be filled with NaNs or something like that?

Portfolio Value Deprecated

It looks like portfolio_value was deprecated from the alpaca api, but the change is yet to be represented in this library. Would it be possible to update?

store.getdata points limited to 4820

I'm doing this:

DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData data0 = DataFactory(dataname=stock, historical=True, fromdate=startday, todate=datetime.today() - timedelta(days=1), timeframe=bt.TimeFrame.Minutes)

However no matter what I choose as my start date, the number of data points I get is limited to a maximum of 4820.

How can I get more (minute) data points than this?

Edit: It was an issue with the todate

AttributeError: 'Entity' object has no attribute 'symbol'

Hey there. I'm running a modified version of the multiple_data_strategy sample on the shared_websocket_streamer branch. It seems to work great for a couple of minutes, then hits the exception below after making a few trades. It seems like the exception caused it to lose connection to the live data stream, as it stops trading but also doesn't return to the shell. I'm happy to provide more details for troubleshooting.

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_backtrader_api/streamcon_service.py", line 70, in _subscription_listener
self.conn.run(channels)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 276, in run
loop.run_until_complete(self.consume())
File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 266, in consume
self.data_ws.consume(),
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 64, in consume
await self._consume_task
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 76, in _consume_msg
await self._dispatch(stream, msg)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/stream2.py", line 151, in _dispatch
await handler(self, channel, ent)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_backtrader_api/streamcon_service.py", line 109, in on_trade
self.q_mapping[msg.symbol].put(msg)
File "/home/pi/.local/lib/python3.7/site-packages/alpaca_trade_api/entity.py", line 29, in getattr
return super().getattribute(key)
AttributeError: 'Entity' object has no attribute 'symbol'

401 Client Error: Unauthorized

Hi,

I sign up on https://app.alpaca.markets and I get the api_key and secret_key. Then, I try to run the README example, but I get this error:

401 Client Error: Unauthorized for url: https://api.polygon.io/v1/historic/agg/day/AAPL?from=2015-01-01T00%3A00%3A00&to=2019-06-20T11%3A42%3A17.626323&apiKey=<my_api_key>

What can I do?

Best,
Dario

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.