Exemple #1
0
def algo_close_delta_15_00():
    DELTA_H, DELTA_M = config.get_config().ALGO_CLOSE_DELTA_15_00_DELTA_TIME
    DELTA_TIME = config.get_config().RUN_TIME.replace(hour=DELTA_H, minute=DELTA_M, second=0, microsecond=0)

    # find all futures
    positions_df = config.get_config().positions_df
    time_bars_df = config.get_config().time_bars_df

    fut_codes = utils.get_fut_codes(positions_df)

    for fut_code in fut_codes:
        # get undelying px
        last_bar = utils.get_last_bar(fut_code)

        BAR_4H_S_H, BAR_4H_S_M = config.get_config().ALGO_CLOSE_DELTA_15_00_4H_BAR
        bar = utils.get_specific_bar(fut_code, hour=BAR_4H_S_H, minute=BAR_4H_S_M)

        delta_px = bar.Close
        delta = utils.get_portfolio_delta(fut_code, delta_px, utils.default_time_shift_strategy, DELTA_TIME)

        # if bar.Open < bar.Close:
        #    buy_dist = bar.High - bar.Close
        #    sell_dist = bar.Close - bar.Open
        # elif bar.Close < bar.Open:
        #    buy_dist = bar.Open - bar.Close
        #    sell_dist = bar.Close - bar.Low
        # else:
        #    buy_dist = bar.High - bar.Close
        #    sell_dist = bar.Close - bar.Low

        # if (round(delta) > 0 and sell_dist > buy_dist) or (round(delta) < 0 and buy_dist > sell_dist):
        utils.add_stop_orders(fut_code, np.array([bar.Close]), np.array([delta]), order_type='MKT')
        utils.set_order_sequence(np.array([0]))
Exemple #2
0
def algo_close_delta():
    # find all futures
    positions_df = config.get_config().positions_df
    time_bars_df = config.get_config().time_bars_df

    fut_codes = utils.get_fut_codes(positions_df)

    for fut_code in fut_codes:
        # get undelying px
        last_bar = utils.get_last_bar(fut_code)
        under_px = last_bar.Close

        px_grid = np.zeros((1))
        px_grid[0] = under_px
        delta_grid = utils.get_portfolio_delta_on_grid(
            fut_code, px_grid, utils.default_time_shift_strategy,
            config.get_config().RUN_TIME)
        utils.add_stop_orders(fut_code, px_grid, delta_grid, order_type='MKT')
        utils.set_order_sequence(np.array([0]))
Exemple #3
0
def algo_basic_fix_19_00():
    DELTA_H, DELTA_M = config.get_config().ALGO_BASIC_FIX_19_00_DELTA_TIME
    DELTA_TIME = config.get_config().RUN_TIME.replace(hour=DELTA_H,
                                                      minute=DELTA_M,
                                                      second=0,
                                                      microsecond=0)

    stops_num = config.get_config().STOPS_NUM_BASIC_FIX_19_00

    # find all futures
    positions_df = config.get_config().positions_df
    time_bars_df = config.get_config().time_bars_df

    fut_codes = utils.get_fut_codes(positions_df)

    for fut_code in fut_codes:

        BAR_4H_S_H, BAR_4H_S_M = config.get_config(
        ).ALGO_BASIC_FIX_19_00_4H_BAR
        bar = utils.get_specific_bar(fut_code,
                                     hour=BAR_4H_S_H,
                                     minute=BAR_4H_S_M)

        H = bar.High
        L = bar.Low

        if H != L:
            new_step = (H - L) / 2 + 5 / 320
        else:
            new_step = config.get_config().STOP_PX_STEP_BASIC_FIX_19_00

        # get undelying px
        last_bar = utils.get_last_bar(fut_code)
        under_px = last_bar.Close

        strikes = positions_df[positions_df.Underlying ==
                               fut_code].Strike.values
        # only futures in position
        if strikes.shape[0] == 0:
            # push delta hedging order
            px_grid = np.zeros((1))
            px_grid[0] = under_px
            delta_grid = utils.get_portfolio_delta_on_grid(
                fut_code, px_grid, utils.default_time_shift_strategy,
                DELTA_TIME)
            utils.add_stop_orders(fut_code,
                                  px_grid,
                                  delta_grid,
                                  order_type='MKT')
            utils.set_order_sequence(np.array([0]))
            continue

        min_strike = np.min(strikes)
        max_strike = np.max(strikes)

        strike_step = config.get_config().STRIKE_STEP
        min_px_step = config.get_config().PRICE_STEP
        lower_bound = min_strike - strike_step
        upper_bound = max_strike + strike_step
        steps = (upper_bound - lower_bound) / min_px_step + 1

        px_grid = np.linspace(lower_bound, upper_bound, steps)

        delta_grid = utils.get_portfolio_delta_on_grid(
            fut_code, px_grid, utils.default_time_shift_strategy, DELTA_TIME)
        roll_right_signs = np.sign(delta_grid) * np.sign(np.roll(
            delta_grid, 1))
        roll_right_mask = roll_right_signs <= 0
        roll_right_mask = roll_right_mask[1:]

        roll_left_signs = np.sign(delta_grid) * np.sign(np.roll(
            delta_grid, -1))
        roll_left_mask = roll_left_signs <= 0
        roll_left_mask = roll_left_mask[:-1]

        change_size_mask = np.full(delta_grid.shape, False)
        change_size_mask[1:] |= roll_right_mask
        change_size_mask[:-1] |= roll_left_mask

        change_idx = np.where(change_size_mask)[0]
        change_px = px_grid[change_idx]

        # options exists & delta doesn't change sign
        if change_px.shape[0] == 0:
            px_grid = np.zeros((1))
            px_grid[0] = under_px
            delta_grid = utils.get_portfolio_delta_on_grid(
                fut_code, px_grid, utils.default_time_shift_strategy,
                DELTA_TIME)

            # delta is zero at current price
            if round(delta_grid[0]) == 0:
                if under_px < min_strike:
                    buy_px_grid = np.zeros(shape=(stops_num))
                    for idx in range(stops_num):
                        buy_px_grid[idx] = min_strike + new_step * idx

                    delta_grid = utils.get_portfolio_delta_on_grid(
                        fut_code, buy_px_grid,
                        utils.default_time_shift_strategy, DELTA_TIME)
                    utils.add_stop_orders(fut_code, buy_px_grid, delta_grid)
                    utils.set_order_sequence(
                        np.linspace(0,
                                    stops_num - 1,
                                    stops_num,
                                    dtype=np.int32))

                elif under_px > max_strike:
                    sell_px_grid = np.zeros(shape=(stops_num))
                    for idx in range(stops_num):
                        sell_px_grid[idx] = max_strike - new_step * idx

                    delta_grid = utils.get_portfolio_delta_on_grid(
                        fut_code, sell_px_grid,
                        utils.default_time_shift_strategy, DELTA_TIME)
                    utils.add_stop_orders(fut_code, sell_px_grid, delta_grid)
                    utils.set_order_sequence(
                        np.linspace(0,
                                    stops_num - 1,
                                    stops_num,
                                    dtype=np.int32))
                else:
                    right_strike_mask = strikes >= under_px
                    strikes_selection = strikes[right_strike_mask]
                    if strikes_selection.shape[0] == 0:
                        exit(0)
                    target_strike = np.min(strikes_selection)

                    buy_px_grid = np.zeros(shape=(stops_num))
                    for idx in range(stops_num):
                        buy_px_grid[idx] = target_strike + new_step * idx

                    delta_grid = utils.get_portfolio_delta_on_grid(
                        fut_code, buy_px_grid,
                        utils.default_time_shift_strategy, DELTA_TIME)
                    utils.add_stop_orders(fut_code, buy_px_grid, delta_grid)

                    left_strike_mask = strikes <= under_px
                    strikes_selection = strikes[left_strike_mask]
                    if strikes_selection.shape[0] == 0:
                        exit(0)
                    target_strike = np.max(strikes_selection)

                    sell_px_grid = np.zeros(shape=(stops_num))
                    for idx in range(stops_num):
                        sell_px_grid[idx] = target_strike - new_step * idx

                    delta_grid = utils.get_portfolio_delta_on_grid(
                        fut_code, sell_px_grid,
                        utils.default_time_shift_strategy, DELTA_TIME)
                    utils.add_stop_orders(fut_code, sell_px_grid, delta_grid)

                    orders_px_grid = np.concatenate(
                        (buy_px_grid, sell_px_grid))
                    orders_zero_delta_px_distance_grid = np.abs(
                        orders_px_grid - under_px)
                    num_orders = orders_zero_delta_px_distance_grid.shape[0]
                    order_idxs = np.zeros(num_orders, dtype=np.int32)
                    for order_idx, idx in zip(
                            range(num_orders),
                            np.argsort(orders_zero_delta_px_distance_grid)):
                        order_idxs[idx] = order_idx
                        utils.set_order_sequence(order_idxs)
            # place market stop
            else:
                utils.add_stop_orders(fut_code,
                                      px_grid,
                                      delta_grid,
                                      order_type='MKT')
                utils.set_order_sequence(np.array([0]))
        # delta changes sign
        else:
            px_dist = np.abs(change_px - under_px)
            zero_delta_px_idx = np.argsort(px_dist)[0]
            zero_delta_px = change_px[zero_delta_px_idx]

            buy_px_grid = np.zeros(shape=(stops_num))
            for idx in range(stops_num):
                buy_px_grid[idx] = zero_delta_px + new_step * (idx + 1)

            delta_grid = utils.get_portfolio_delta_on_grid(
                fut_code, buy_px_grid, utils.default_time_shift_strategy,
                DELTA_TIME)
            rounded_delta_grid = np.round(delta_grid)
            mask = rounded_delta_grid == 0
            # all buy stops are zero
            if np.all(mask):
                right_strike_mask = strikes > zero_delta_px
                strikes_selection = strikes[right_strike_mask]
                if strikes_selection.shape[0] == 0:
                    exit(0)
                target_strike = np.min(strikes_selection)

                buy_px_grid = np.zeros(shape=(stops_num))
                for idx in range(stops_num):
                    buy_px_grid[idx] = target_strike + new_step * idx

                delta_grid = utils.get_portfolio_delta_on_grid(
                    fut_code, buy_px_grid, utils.default_time_shift_strategy,
                    DELTA_TIME)
                utils.add_stop_orders(fut_code, buy_px_grid, delta_grid)
            else:
                utils.add_stop_orders(fut_code, buy_px_grid, delta_grid)

            stops_num = config.get_config().STOPS_NUM_BASIC
            sell_px_grid = np.zeros(shape=(stops_num))
            for idx in range(stops_num):
                sell_px_grid[idx] = zero_delta_px - new_step * (idx + 1)

            delta_grid = utils.get_portfolio_delta_on_grid(
                fut_code, sell_px_grid, utils.default_time_shift_strategy,
                DELTA_TIME)
            rounded_delta_grid = np.round(delta_grid)
            mask = rounded_delta_grid == 0
            # all sell stops are zero
            if np.all(mask):
                left_strike_mask = strikes < zero_delta_px
                strikes_selection = strikes[left_strike_mask]
                if strikes_selection.shape[0] == 0:
                    exit(0)
                target_strike = np.max(strikes_selection)

                sell_px_grid = np.zeros(shape=(stops_num))
                for idx in range(stops_num):
                    sell_px_grid[idx] = target_strike - new_step * idx

                delta_grid = utils.get_portfolio_delta_on_grid(
                    fut_code, sell_px_grid, utils.default_time_shift_strategy,
                    DELTA_TIME)
                utils.add_stop_orders(fut_code, sell_px_grid, delta_grid)
            else:
                utils.add_stop_orders(fut_code, sell_px_grid, delta_grid)

            orders_px_grid = np.concatenate((buy_px_grid, sell_px_grid))
            orders_zero_delta_px_distance_grid = np.abs(orders_px_grid -
                                                        zero_delta_px)
            num_orders = orders_zero_delta_px_distance_grid.shape[0]
            order_idxs = np.zeros(num_orders, dtype=np.int32)
            for order_idx, idx in zip(
                    range(num_orders),
                    np.argsort(orders_zero_delta_px_distance_grid)):
                order_idxs[idx] = order_idx
            utils.set_order_sequence(order_idxs)
def main_levels_algo(fut_code, high, low, DELTA_TIME, last_px, buy_stps,
                     sell_stps):
    # buy orders
    buy_orders_px = []
    buy_orders_delta = []

    current_px = high
    prev_delta = 0
    for p in buy_stps:
        SHIFT_PX, NUM, GRID_STEP_PX, FWD_PX, IS_SAFE, IS_SEARCH = p

        if NUM == 0:
            continue

        #convert to real pxs
        SHIFT_PX = SHIFT_PX / config.get_config().FUT_PRICE_COEFF
        GRID_STEP_PX = GRID_STEP_PX / config.get_config().FUT_PRICE_COEFF
        FWD_PX = FWD_PX / config.get_config().FUT_PRICE_COEFF

        current_px += SHIFT_PX
        if IS_SAFE:
            current_px += GRID_STEP_PX

        stops_submitted = 0
        for _ in range(100 if IS_SEARCH else NUM):
            delta_px = current_px + FWD_PX
            stop_px = current_px
            delta = utils.get_portfolio_delta(
                fut_code, delta_px, utils.default_time_shift_strategy,
                DELTA_TIME)

            order_delta = round(delta)
            prev_order_delta = round(prev_delta)
            if order_delta < 0 and order_delta != prev_order_delta:
                buy_orders_px.append(stop_px)
                buy_orders_delta.append(delta)
                stops_submitted += 1
            else:
                if not IS_SEARCH:
                    stops_submitted += 1

            prev_delta = delta

            if stops_submitted >= NUM:
                break
            current_px += GRID_STEP_PX
        current_px += FWD_PX

    # sell order
    sell_orders_px = []
    sell_orders_delta = []

    current_px = low
    prev_delta = 0
    for p in sell_stps:
        SHIFT_PX, NUM, GRID_STEP_PX, FWD_PX, IS_SAFE, IS_SEARCH = p
        if NUM == 0:
            continue

        # convert to real pxs
        SHIFT_PX = SHIFT_PX / config.get_config().FUT_PRICE_COEFF
        GRID_STEP_PX = GRID_STEP_PX / config.get_config().FUT_PRICE_COEFF
        FWD_PX = FWD_PX / config.get_config().FUT_PRICE_COEFF

        current_px -= SHIFT_PX
        if IS_SAFE:
            current_px -= GRID_STEP_PX

        stops_submitted = 0
        for _ in range(100 if IS_SEARCH else NUM):
            delta_px = current_px - FWD_PX
            stop_px = current_px
            delta = utils.get_portfolio_delta(
                fut_code, delta_px, utils.default_time_shift_strategy,
                DELTA_TIME)

            order_delta = round(delta)
            prev_order_delta = round(prev_delta)
            if order_delta > 0 and order_delta != prev_order_delta:
                sell_orders_px.append(stop_px)
                sell_orders_delta.append(delta)
                stops_submitted += 1
            else:
                if not IS_SEARCH:
                    stops_submitted += 1
            prev_delta = delta
            if stops_submitted >= NUM:
                break
            current_px -= GRID_STEP_PX
        current_px -= FWD_PX

    buy_orders_px_np = np.array(buy_orders_px)
    buy_orders_px_delta_np = np.array(buy_orders_delta)

    sell_orders_px_np = np.array(sell_orders_px)
    sell_orders_delta_np = np.array(sell_orders_delta)

    utils.add_stop_orders(fut_code, buy_orders_px_np, buy_orders_px_delta_np)
    utils.add_stop_orders(fut_code, sell_orders_px_np, sell_orders_delta_np)

    orders_px_grid = np.concatenate((buy_orders_px_np, sell_orders_px_np))
    orders_distance_grid = np.abs(orders_px_grid - last_px)
    num_orders = orders_distance_grid.shape[0]
    order_idxs = np.zeros(num_orders, dtype=np.int32)
    for order_idx, idx in zip(range(num_orders),
                              np.argsort(orders_distance_grid)):
        order_idxs[idx] = order_idx
    utils.set_order_sequence(order_idxs)