print ("Position not filled yet")
    return
  emaS = strategy.get_indicators()['emaS']
  emaL = strategy.get_indicators()['emaL']
  if emaS.v() > emaL.v():
    await strategy.close_position_market(mtsCreate=update.mts)

@strategy.on_update_long
async def update_long(update, position):
  emaS = strategy.get_indicators()['emaS']
  emaL = strategy.get_indicators()['emaL']
  if emaS.v() < emaL.v():
    await strategy.close_position_market(mtsCreate=update.mts)

from hfstrategy import Executor
exe = Executor(strategy,  timeframe='30m')

# Backtest offline
exe.offline(file='btc_candle_data.json')

# Backtest with data-backtest server
# import time
# now = int(round(time.time() * 1000))
# then = now - (1000 * 60 * 60 * 24 * 15) # 15 days ago
# exe.with_data_server(then, now)

# Execute live
# import os
# API_KEY=os.getenv("BFX_KEY")
# API_SECRET=os.getenv("BFX_SECRET")
# exe.live(API_KEY, API_SECRET)
Beispiel #2
0
    emaS = strategy.get_indicators()['emaS']
    emaL = strategy.get_indicators()['emaL']
    # Market is going to change direction so exit position
    if emaS.v() < emaL.v():
        return await strategy.close_position_market(mtsCreate=update.mts)
    # Same as above, take profit at 2% and set stop to entry
    # get entry of initial order
    entry = position.get_entry_order().price
    half_position = abs(position.amount) / 2
    if half_position < 0.1:
        return
    if update.price > entry + (position.price * 0.002):
        print("Reached profit target, take 2%")
        await strategy.update_position_market(mtsCreate=update.mts,
                                              amount=-half_position,
                                              tag="Hit mid profit target")
        # set our stop loss to be our original entry price
        await strategy.set_position_stop(entry,
                                         exit_type=Position.ExitType.MARKET)


from hfstrategy import Executor

Executor(strategy, timeframe='1hr').offline(file='btc_candle_data.json')
# Executor(strategy, timeframe='1m').backtest_live()

# import time
# now = int(round(time.time() * 1000))
# then = now - (1000 * 60 * 60 * 24 * 15) # 15 days ago
# Executor(strategy, timeframe='30m').with_data_server(then, now)

@strategy.on_update_short
async def update_short(update, position):
    if (position.amount == 0):
        print("Position not filled yet")
        return
    emaS = strategy.get_indicators()['emaS']
    emaL = strategy.get_indicators()['emaL']
    if emaS.v() > emaL.v():
        await strategy.close_position_market(mtsCreate=update.mts)


@strategy.on_update_long
async def update_long(update, position):
    emaS = strategy.get_indicators()['emaS']
    emaL = strategy.get_indicators()['emaL']
    if emaS.v() < emaL.v():
        await strategy.close_position_market(mtsCreate=update.mts)


from hfstrategy import Executor
exe = Executor(strategy, timeframe='30m')

# Backtest offline
now = int(round(time.time() * 1000))
then = now - (1000 * 60 * 60 * 24 * 36)  # 5 days ago

loop = asyncio.get_event_loop()
loop.run_until_complete(exe.with_local_database(then, now))
Beispiel #4
0

@strategy.on_ready
async def ready():
    wait_time = 5
    loop_time = 0.1
    await strategy._process_new_candle(fake_candle)
    await strategy.open_long_position_market(mtsCreate=get_mts(), amount=0.01)
    # sleep for 5 seconds
    await wait(wait_time)
    await strategy.set_position_target(18000,
                                       exit_type=Position.ExitType.LIMIT)
    # wait for 5 seconds
    await wait(wait_time)
    await strategy.set_position_stop(14000, exit_type=Position.ExitType.LIMIT)
    # update position with 50 market orders every 10 milliseconds
    for i in range(0, 50):
        await wait_mills(10)
        await strategy.update_position_market(mtsCreate=get_mts(), amount=0.01)
    # wait for 5 seconds
    await wait(wait_time)
    await strategy.set_position_stop(14000, exit_type=Position.ExitType.MARKET)
    # # wait for 5 seconds
    await wait(wait_time)
    await strategy.close_position_market(mtsCreate=get_mts())


API_KEY = os.getenv("BFX_KEY")
API_SECRET = os.getenv("BFX_SECRET")
Executor(strategy, timeframe='30m').live(API_KEY, API_SECRET)
    print ("Position not filled yet")
    return
  emaS = strategy.get_indicators()['emaS']
  emaL = strategy.get_indicators()['emaL']
  if emaS.v() > emaL.v():
    await strategy.close_position_market(mtsCreate=update.mts)

@strategy.on_update_long
async def update_long(update, position):
  emaS = strategy.get_indicators()['emaS']
  emaL = strategy.get_indicators()['emaL']
  if emaS.v() < emaL.v():
    await strategy.close_position_market(mtsCreate=update.mts)

from hfstrategy import Executor
exe = Executor(strategy,  timeframe='30m', show_chart=False)

# Backtest offline
# exe.offline(file='btc_candle_data.json')

# Backtest with data-backtest server
import time
now = int(round(time.time() * 1000))
then = now - (1000 * 60) # 15 days ago
exe.with_data_server(then, now)

# Execute live
# import os
# API_KEY=os.getenv("BFX_KEY")
# API_SECRET=os.getenv("BFX_SECRET")
# exe.live(API_KEY, API_SECRET)
Beispiel #6
0
def single_run(ema_l, ema_s, quick_profit_target_percentage,
               profit_target_percentage, stop_loss_percentage):

    # Initialise strategy
    strategy = Strategy(
        symbol='tBTCUSD',
        indicators={
            # see https://github.com/bitfinexcom/bfx-hf-indicators-py for more info
            'emaL': EMA(ema_l),
            'emaS': EMA(ema_s)
        },
        exchange_type=Strategy.ExchangeType.EXCHANGE,
        logLevel='INFO')

    async def enter_long(update):
        amount = 1000 / update.price
        await strategy.open_long_position_market(mtsCreate=update.mts,
                                                 amount=amount)
        # set profit target to 5% above entry
        profit_target = update.price + (update.price *
                                        profit_target_percentage)
        # set a tight stop los of %2 below entry
        stop_loss = update.price - (update.price * stop_loss_percentage)
        # update positions with new targets
        await strategy.set_position_target(profit_target)
        await strategy.set_position_stop(stop_loss)

    async def enter_short(update):
        amount = 1000 / update.price
        await strategy.open_short_position_market(mtsCreate=update.mts,
                                                  amount=amount)
        # same as above, take full proft at 5%
        profit_target = update.price - (update.price *
                                        profit_target_percentage)
        # set stop loss to %2 below entry
        stop_loss = update.price + (update.price * stop_loss_percentage)
        await strategy.set_position_target(profit_target)
        await strategy.set_position_stop(stop_loss)

    @strategy.on_enter
    async def enter(update):
        # We are going to use the ema cross for entrance
        emaS = strategy.get_indicators()['emaS']
        emaL = strategy.get_indicators()['emaL']
        # enter market if ema crosses
        if emaS.crossed(emaL.v()):
            if emaS.v() > emaL.v():
                await enter_long(update)
            else:
                await enter_short(update)

    @strategy.on_update_short
    async def update_short(update, position):
        emaS = strategy.get_indicators()['emaS']
        emaL = strategy.get_indicators()['emaL']
        # if emas cross then just exit the position
        if emaS.v() > emaL.v():
            return await strategy.close_position_market(mtsCreate=update.mts)
        ## if we are up by 2% then take 50% profit and set stop loss to
        ## entry price
        # get entry of initial order
        entry = position.get_entry_order().price
        half_position = abs(position.amount) / 2
        if half_position < 0.1:
            return
        if update.price < entry - (position.price *
                                   quick_profit_target_percentage):
            print("Reached profit target, take 2%")
            await strategy.update_position_market(mtsCreate=update.mts,
                                                  amount=half_position,
                                                  tag="Hit profit target")
            # set our stop loss to be our original entry price
            # here we will set our stop exit type to be a limit order.
            # This will mean we will only be charged maker fees and since we are in profit
            # we dont need to exit the position instantly with a market order
            await strategy.set_position_stop(
                entry, exit_type=Position.ExitType.MARKET)

    @strategy.on_update_long
    async def update_long(update, position):
        emaS = strategy.get_indicators()['emaS']
        emaL = strategy.get_indicators()['emaL']
        # Market is going to change direction so exit position
        if emaS.v() < emaL.v():
            return await strategy.close_position_market(mtsCreate=update.mts)
        # Same as above, take profit at 2% and set stop to entry
        # get entry of initial order
        entry = position.get_entry_order().price
        half_position = abs(position.amount) / 2
        if half_position < 0.1:
            return
        if update.price > entry + (position.price *
                                   quick_profit_target_percentage):
            print("Reached profit target, take 2%")
            await strategy.update_position_market(mtsCreate=update.mts,
                                                  amount=-half_position,
                                                  tag="Hit mid profit target")
            # set our stop loss to be our original entry price
            await strategy.set_position_stop(
                entry, exit_type=Position.ExitType.MARKET)

    from hfstrategy import Executor

    os.mkdir("current_run")

    Executor(strategy, timeframe='1hr',
             show_chart=False).offline(file='btc_candle_data.json')
    # Executor(strategy, timeframe='1m').backtest_live()

    # import time
    # now = int(round(time.time() * 1000))
    # then = now - (1000 * 60 * 60 * 24 * 15) # 15 days ago
    # Executor(strategy, timeframe='30m').with_data_server(then, now)

    with open('current_run/results.json') as json_file:
        total_profit_loss = json.load(json_file)['total_profit_loss']

    shutil.rmtree("current_run")
    return total_profit_loss