blankly-finance / blankly

🚀 💸 Easily build, backtest and deploy your algo in just a few lines of code. Trade stocks, cryptos, and forex across exchanges w/ one package.
https://package.blankly.finance
GNU Lesser General Public License v3.0
2.09k stars 266 forks source link

'StrategyLogger' object has no attribute 'get_position' #220

Open huwei1024 opened 1 year ago

huwei1024 commented 1 year ago

Description

I run the file 'futures_tutorial.py' , then got this error:

'StrategyLogger' object has no attribute 'get_position'

settings.json

settings.json here

backtest.json (if applicable)

backtest.json here

Error (if applicable)

Error here

Platform Info

Additional context Add any other context about the problem here.

EmersonDove commented 1 year ago

Did you make any modifications to the file?

Make sure to run code like below:

import blankly
from blankly import futures, Side
from blankly.futures import FuturesStrategyState
from blankly.futures.utils import close_position

def price_event(price, symbol, state: FuturesStrategyState):
    prev_price = state.variables['prev_price']
    position = state.interface.get_position(symbol)

    # if the price rose more than 1,000 and we don't already have a short position, then short sell
    if not position and price - prev_price >= 1000:
        order_size = (state.interface.cash / price) * 0.99
        state.interface.market_order(symbol, Side.SELL, order_size)

    # if the price stablized and we *do* have a short position, close our position.
    elif position and abs(price - prev_price) <= 100:
        # we use abs(position['size']) here because position['size'] can (and will) be negative, since we have taken a short position.
        state.interface.market_order(symbol, Side.BUY, abs(position['size']), reduce_only=True)

    state.variables['prev_price'] = price

# This function will be run before our algorithm starts
def init(symbol, state: FuturesStrategyState):
    # Close any open positions
    close_position(symbol, state)

    # Give the algo the previous price as context
    last_price = state.interface.history(symbol, to=1, return_as='deque', resolution=state.resolution)['close'][-1]
    state.variables['prev_price'] = last_price

if __name__ == "__main__":
    exchange = futures.BinanceFutures()
    strategy = futures.FuturesStrategy(exchange)

    strategy.add_price_event(price_event, init=init, teardown=close_position, symbol='BTC-USDT', resolution='1d')

    if blankly.is_deployed:
        strategy.start()
    else:
        strategy.backtest(to='1y', initial_values={'USDT': 10000})
huwei1024 commented 1 year ago

just strategy.start() not backtest

EmersonDove commented 1 year ago

Looks like it could be some futures bug, I'll have to check into this

Minish144 commented 1 year ago

Hi guys! Is there any way to fix this? Backtests for FuturesStrategy class with futures exchange work fine. But cant get start() method to work

    def run_live(self):
        for scheduler in self.schedulers:
            kwargs = scheduler.get_kwargs()
            kwargs['state'].strategy.interface = self.interface
        self.__run_init()

adding these lines before calling run_init kinda fixes the problem, but im not a python dev at all so that was just copy-pasting from backtests initialization. Seems like the problem is in interface init. I hope this helps a little, really looking forward to fix in master from you! Thanks for such a cool framework