Open huwei1024 opened 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})
just strategy.start() not backtest
Looks like it could be some futures bug, I'll have to check into this
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
Description
I run the file 'futures_tutorial.py' , then got this error:
'StrategyLogger' object has no attribute 'get_position'
settings.json
backtest.json (if applicable)
Error (if applicable)
Platform Info
Additional context Add any other context about the problem here.