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.03k stars 261 forks source link

Why is the entry_price wrong? #222

Open huwei1024 opened 1 year ago

huwei1024 commented 1 year ago

the entry_price in position.exchange_specific is not correct? it seems 100 times of price?

exchange = futures.BinanceFutures()
strategy = futures.FuturesStrategy(exchange)

state.interface.limit_order(
    symbol, price=price, side=side, size=order_size)
EmersonDove commented 1 year ago

I'm not sure, can you give more examples of which symbols you're calling and the context? I'm not sure I can diagnose with just this information.

huwei1024 commented 1 year ago

this code 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):
    position = state.interface.get_position(symbol)
    his = state.variables.history
    his.append(price)
    # print(position)
    current_size = 0
    entry_price = 0
    if position:
        current_size = position['size']
        entry_price = float(position['exchange_specific']['entry_price'])

    print(position)
    state.variables['prev_price'] = price
    # print(position)
    print('entry_price:%1.5f price:%1.5f' %
          (entry_price, price))

    side = Side.BUY
    size = 0
    if current_size < 0:
        size = abs(current_size)
        side = Side.BUY
    elif current_size > 0:
        size = current_size
        side = Side.SELL
    else:
        size = 500

    if size > 0:
        print('order size:%s price:%1.5f  side:%s' %
              (size, price, side))
        state.interface.limit_order(
            symbol, price=price, side=side, size=size)

# 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
    # Download price data to give context to the algo
    state.variables.history = state.interface.history(symbol, to=1440, return_as='deque',
                                                      resolution=state.resolution)['close']

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

    strategy.add_price_event(
        price_event, init=init, symbol='DOGE-USDT', resolution='1h')

    if blankly.is_deployed:
        strategy.start()
    else:
        result = strategy.backtest(to='1d', initial_values={'USDT': 1000})
        print(result)

then i got log: order size:500 price:0.08198 side:Side.BUY {'symbol': 'DOGE-USDT', 'base_asset': 'DOGE', 'quote_asset': 'USDT', 'size': 500.0, 'position': <PositionMode.BOTH: 'both'>, 'leverage': 1, 'margin_type': <MarginType.CROSSED: 'crossed'>, 'contract_type': <ContractType.PERPETUAL: 'perpetual'>, 'exchange_specific': {'entry_price': 41.006396}} entry_price:41.00640 price:0.08173'

the 'entry_price' is total amount?