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

Append bar to history raises either a TypeError or a FutureWarning #229

Open
femtotrader opened this issue Feb 17, 2023 · 2 comments
Open

Comments

@femtotrader
Copy link

Hello,

the following code

import pandas as pd
import numpy as np
import blankly
from blankly.data.data_reader import PriceReader
from blankly import Strategy, StrategyState


def init(symbol, state: StrategyState):
    print(symbol, state)

    state.variables['history'] = state.interface.history(symbol, 50, state.resolution)

def bar_event(bar, symbol, state: StrategyState):
    # state.variables['history'].append(bar)
    state.variables['history'].append(bar, ignore_index=True)


if __name__ == "__main__":
    # This downloads an example CSV
    # data = requests.get(
    #    'https://firebasestorage.googleapis.com/v0/b/blankly-6ada5.appspot.com/o/demo_data.csv?alt=media&token=acfa5c39-8f08-45dc-8be3-2033dc2b7b28').text
    # with open('./price_examples.csv', 'w') as file:
    #    file.write(data)

    # Run on the keyless exchange, starting at 100k
    # exchange = blankly.KeylessExchange(price_reader=PriceReader('./price_examples.csv', 'BTC-USD'))

    """
    df = pd.read_csv("XBTUSDT.csv", names=["time", "price", "volume"])
    df["time"] = pd.to_datetime(df["time"], unit="s")
    df = df.set_index("time")
    prices = df.resample("2H")["price"].ohlc().fillna(method="ffill")
    volume = df.resample("2H")["volume"].sum().fillna(value=0)
    volume.name = "volume"
    df_1Min = pd.concat([prices, volume], axis=1)
    df_1Min = df_1Min.reset_index()
    df_1Min["time"] = df_1Min["time"].map(pd.Timestamp.timestamp)
    df_1Min.to_csv("XBTUSDT_2H.csv", index=False)
    """

    exchange = blankly.KeylessExchange(
        price_reader=PriceReader("./XBTUSDT_2H.csv", "BTC-USD")
    )

    # Use our strategy helper
    strategy = Strategy(exchange)

    # Make the bar event function above run every 2h
    strategy.add_bar_event(bar_event, symbol="BTC-USD", resolution="2h", init=init)

    # Backtest the strategy
    results = strategy.backtest(
        start_date="2019-12-19 18:06:18",
        end_date="2022-06-30 23:59:17",
        initial_values={"USD": 10000},
    )

    print(results)

especially

state.variables['history'].append(bar)

raises a warning

Traceback (most recent call last):
  File "C:\Users\femto\AppData\Roaming\Python\Python38\site-packages\blankly\frameworks\strategy\strategy.py", line 127, in rest_event
    callback(*args)
  File "mini.py", line 15, in bar_event
    state.variables['history'].append(bar)
  File "C:\tools\Anaconda3\lib\site-packages\pandas\core\frame.py", line 9048, in append
    return self._append(other, ignore_index, verify_integrity, sort)
  File "C:\tools\Anaconda3\lib\site-packages\pandas\core\frame.py", line 9061, in _append
    raise TypeError("Can only append a dict if ignore_index=True")
TypeError: Can only append a dict if ignore_index=True

or

state.variables['history'].append(bar, ignore_index=True)

raises

mini.py:16: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  state.variables['history'].append(bar, ignore_index=True)

Same also occurs to example mentioned at https://docs.blankly.finance/strategy/functions/#add_bar_eventcallback-typingcallable-symbol-str-resolution-str-or-float-init-typingcallable--none-teardown-typingcallable--none-variables-dict--none

Any workaround?

Kind regards

@femtotrader
Copy link
Author

Maybe not the easiest solution... but

df_bar = pd.DataFrame([bar])
state.variables['history'] = pd.concat([state.variables['history'], df_bar], ignore_index=True)

adds that bar to history dataframe

But DataFrame size inflates beyond 50 rows.
Except if I add

if len(state.variables['history']) > 50:
    state.variables['history'] = state.variables['history'].drop([0])

Maybe API should be refine here

@EmersonDove
Copy link
Member

You're right the package should be updated. When i wrote it, the implementation didn't throw a warning.

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

2 participants