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

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

Open
huwei1024 opened this issue Jan 6, 2023 · 4 comments
Open

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

huwei1024 opened this issue Jan 6, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@huwei1024
Copy link

huwei1024 commented Jan 6, 2023

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

  • Python version:
  • Platform:

Additional context
Add any other context about the problem here.

@EmersonDove
Copy link
Member

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
Copy link
Author

just strategy.start()
not backtest

@EmersonDove
Copy link
Member

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

@EmersonDove EmersonDove added the bug Something isn't working label Jan 15, 2023
@Minish144
Copy link

Minish144 commented Feb 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants