Пример #1
0
def squareoff(context, asset=None):
    """
        Square off assets. If `asset` is `None`, square of all assets.
        Else the specified asset.
        
        Args:
            `context (obj)`: Algo context object.
            
            `asset (obj)`: Asset to square off, or `None` for all.
            
        Returns:
            None.
    """
    if asset:
        if PLATFORM_ENGINE == 'blueshift':
            square_off(asset)
        else:
            order_target(asset, 0)
        return

    cancel_open_orders(context)

    if PLATFORM_ENGINE == 'blueshift':
        square_off()
    else:
        positions = context.portfolio.positions
        for asset in positions:
            order_target(asset, 0)
def check_sl_tp(asset, context, data):
    """ this function is called on every data update. """
    px = data.current(asset, 'close')
    if px > context.target[asset] or px < context.stop[asset]:
        if px > context.target[asset]:
            print_msg(f'book profit on asset {asset} and stop sl tp monitor.')
        else:
            print_msg(
                f'got stopped out on asset {asset} and stop sl tp monitor.')

        order_target(asset, 0)
        off_data(context.data_monitors[asset])
        terminate()
Пример #3
0
def check_exit(context, data, asset):
    """ this function is called on every data update. """
    px = data.current(asset, 'close')
    move = (px - context.entry_price) / context.entry_price

    if move > context.take_profit:
        # we hit the take profit target, book profit and terminate
        order_target(asset, 0)
        off_data()
        print_msg(f'booking profit at {px} and turn off data monitor.')
        terminate()
    elif move < -context.stop_loss:
        # we hit the stoploss, sqaure off and terminate
        order_target(asset, 0)
        off_data()
        print_msg(f'booking loss at {px} and turn off data monitor.')
        terminate()
Пример #4
0
def enter_trade(context, data):
    """ this function is called only once at the beginning. """
    if not context.traded:
        px = data.current(context.asset, 'close')
        # place a limit order at the last price
        order_id = order_target(context.asset, 1000, px)
        ## NOTE: do not use lambda for more than one asset inside a loop
        ## (or comprehension), you will only retain the last value of
        ## closure variable. Use partial from functools. See other
        ## samples under this folder.
        f = lambda context, data: check_order(context, data, order_id)
        on_trade(f)
        context.traded = True
        msg = f'placed a new trade {order_id} at {px} and set up order monitor.'
        print_msg(msg)
def enter_trade(context, data):
    """ this function is called only once at the beginning. """
    if not context.traded:
        px = data.current(context.assets, 'close')
        # for more than one asset, set up a loop and create
        # the monitoring function using partial from functools
        for asset in context.assets:
            # place a limit order at the last price
            order_id = order_target(asset, 1, px[asset])
            f = partial(check_order, order_id, asset)
            context.order_monitors[asset] = f
            on_trade(f)
            msg = f'placed a new trade {order_id} for {asset},'
            msg = msg + ' and set up order monitor.'
            print_msg(msg)
        context.traded = True
def rebalance(context,data):
    """
        A function to rebalance - all execution logic goes here
    """
    for security in context.securities:
        order_target(security, context.target_position[security])
def place_order(asset, context, price):
    context.target[asset] = price * 1.005
    context.stop[asset] = price * 0.995
    order_target(asset, 1, limit_price=price)
    context.ordered[asset] = True