Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run bta-lib inside a thread #9

Open
karaolidis opened this issue Jan 8, 2021 · 4 comments
Open

Can't run bta-lib inside a thread #9

karaolidis opened this issue Jan 8, 2021 · 4 comments

Comments

@karaolidis
Copy link

Hello, I'm trying to use btalib and I'm having some trouble when having it run from inside a thread.

The below code works fine:

import [...]

symbol = 'BTCEUR'

with open('./data/' + symbol + '/' + symbol + '_kline.p', 'rb') as handle:
    kline_df = pickle.load(handle)

sma = btalib.sma(kline_df).df
print(sma)

But when I try to use threads...

import [...]

symbol = 'BTCEUR'

with open('./data/' + symbol + '/' + symbol + '_kline.p', 'rb') as handle:
    kline_df = pickle.load(handle)

def thread():
    sma = btalib.sma(kline_df).df
    print(sma)
    time.sleep(5)

x = threading.Thread(target=thread)
x.start()

... I get the following error:

Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/nick/storage/Documents/_Projects/Python/Crypto/btalib_test.py", line 13, in thread
    sma = btalib.sma(kline_df).df
  File "/home/nick/storage/Documents/_Projects/Python/Crypto/venv/lib/python3.9/site-packages/btalib/indicator.py", line 110, in __call__
    self.outputs = self.o = meta.outputs._from_class(cls)
  File "/home/nick/storage/Documents/_Projects/Python/Crypto/venv/lib/python3.9/site-packages/btalib/meta/outputs.py", line 30, in _from_class
    return _CLSOUTPUTS[cls]()  # defvals params in dict format
  File "/home/nick/storage/Documents/_Projects/Python/Crypto/venv/lib/python3.9/site-packages/btalib/meta/lines.py", line 630, in __init__
    metadata.minperiods[self] = [1] * len(self)
AttributeError: '_thread._local' object has no attribute 'minperiods'

Any help would be appreciated!

@vijay-r
Copy link

vijay-r commented Jan 8, 2021

Team, getting same error
any solution ?

@karaolidis
Copy link
Author

I found a workaround which is less than ideal but will have to do until we get an update from the maintainers.

I created a separate script for the bta-lib calculations that had looked something like this...

import pandas as pd
import pickle
import btalib
import sys

symbol = sys.argv[1]

with open('./data/' + symbol + '/' + symbol + '_kline.p', 'rb') as handle:
    kline_df = pickle.load(handle)

sma = btalib.sma(kline_df).df

with open('./data/' + symbol + '/sma.p', 'wb') as handle:
    pickle.dump(sma, handle)

...and then, from the main script, from inside the thread, I run...

subprocess.Popen(['python', 'bta.py', 'BTCEUR']).wait()

with open('./data/' + symbol + '/sma.p', 'rb') as handle:
    sma = pickle.load(handle)

It's clunky but it will have to do for now.

@Beauj34
Copy link

Beauj34 commented Feb 1, 2021

Has anyone found a better explanation why BTA-Lib doesn't work within threading? Getting the same errors with different indicators

@drhighliner
Copy link

Take a look at the pull request section. Someone found a solution for this (thread) issue which works quite nicely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants