Exemple #1
0
def algo_night():
    DELTA_H, DELTA_M = config.get_config().ALGO_NIGHT_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:
        # specific bar

        BAR_4H_S_H, BAR_4H_S_M = config.get_config().ALGO_NIGHT_4H_BAR

        last_bar = utils.get_last_bar(fut_code)
        if last_bar.DateTime.hour != BAR_4H_S_H:
            exit(1)

        last_px = last_bar.Close
        close_px = last_bar.Close

        if config.get_config().UPTREND:
            buy_stps = config.get_config().ALGO_NIGHT_UPTREND_BUY_STP
            sell_stps = config.get_config().ALGO_NIGHT_UPTREND_SELL_STP
        else:
            buy_stps = config.get_config().ALGO_NIGHT_DOWNTREND_BUY_STP
            sell_stps = config.get_config().ALGO_NIGHT_DOWNTREND_SELL_STP

        main_levels_algo(fut_code, close_px, close_px, DELTA_TIME, last_px,
                         buy_stps, sell_stps)
Exemple #2
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 #3
0
def algo_stat():
    # shift time
    DELTA_H, DELTA_M = config.get_config().ALGO_STAT_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:
        M5_S_H, M5_S_M, M5_E_H, M5_E_M = config.get_config().ALGO_STAT_5M_RANGE

        bars_5_min = utils.get_bars_range(
            fut_code,
            start_hour=M5_S_H,
            start_minute=M5_S_M,
            end_hour=M5_E_H,
            end_minute=M5_E_M,
            duration='5 mins',
        )
        bar_open = bars_5_min.iloc[0].Open
        bar_close = bars_5_min.iloc[-1].Close
        bar_high = bar_open
        bar_low = bar_open
        for _, bar in bars_5_min.iterrows():
            bar_high = max(bar_high, bar.High)
        for _, bar in bars_5_min.iterrows():
            bar_low = min(bar_low, bar.Low)

        if bar_close > bar_open:
            high = bar_high
            low = bar_close
        elif bar_close < bar_open:
            high = bar_close
            low = bar_low
        else:
            high = bar_high
            low = bar_low

        last_bar = utils.get_last_bar(fut_code)
        last_px = last_bar.Close

        buy_stps = config.get_config().ALGO_STAT_BUY_STP
        sell_stps = config.get_config().ALGO_STAT_SELL_STP

        main_levels_algo(fut_code, high, low, DELTA_TIME, last_px, buy_stps,
                         sell_stps)
Exemple #4
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 #5
0
def algo_safe():
    DELTA_TIME = config.get_config().RUN_TIME

    # find all futures
    positions_df = config.get_config().positions_df
    fut_codes = utils.get_fut_codes(positions_df)

    for fut_code in fut_codes:
        last_bar = utils.get_last_bar(fut_code)
        last_px = last_bar.Close

        high = last_px
        low = last_px

        buy_stps = config.get_config().ALGO_SAFE_BUY_STP
        sell_stps = config.get_config().ALGO_SAFE_SELL_STP

        main_levels_algo(fut_code, high, low, DELTA_TIME, last_px, buy_stps,
                         sell_stps)
Exemple #6
0
def algo_07_00():
    # shift time
    DELTA_H, DELTA_M = config.get_config().ALGO_07_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:
        FIRST_BAR_4H_S_H, FIRST_BAR_4H_S_M = config.get_config(
        ).ALGO_07_00_4H_FIRST_BAR
        SECOND_BAR_4H_S_H, SECOND_BAR_4H_S_M = config.get_config(
        ).ALGO_07_00_4H_SECOND_BAR

        first_bar = utils.get_specific_bar(fut_code,
                                           hour=FIRST_BAR_4H_S_H,
                                           minute=FIRST_BAR_4H_S_M)
        second_bar = utils.get_specific_bar(fut_code,
                                            hour=SECOND_BAR_4H_S_H,
                                            minute=SECOND_BAR_4H_S_M)

        high = max(first_bar.High, second_bar.High)
        low = min(first_bar.Low, second_bar.Low)

        last_bar = utils.get_last_bar(fut_code)
        last_px = last_bar.Close

        buy_stps = config.get_config().ALGO_07_00_BUY_STP
        sell_stps = config.get_config().ALGO_07_00_SELL_STP

        main_levels_algo(fut_code, high, low, DELTA_TIME, last_px, buy_stps,
                         sell_stps)
Exemple #7
0
def algo_news():
    # shift time
    DELTA_H, DELTA_M = config.get_config().ALGO_NEWS_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:
        BAR_4H_S_H, BAR_4H_S_M = config.get_config().ALGO_NEWS_4H_BAR
        bar_4h = utils.get_specific_bar(fut_code, hour=BAR_4H_S_H, minute=BAR_4H_S_M)

        M5_S_H, M5_S_M, M5_E_H, M5_E_M = config.get_config().ALGO_NEWS_5M_RANGE
        bars_5_min = utils.get_bars_range(fut_code,
                                          start_hour=M5_S_H,
                                          start_minute=M5_S_M,
                                          end_hour=M5_E_H,
                                          end_minute=M5_E_M,
                                          duration='5 mins',
                                          )
        M1_S_H, M1_S_M, M1_E_H, M1_E_M = config.get_config().ALGO_NEWS_1M_RANGE
        bars_1_min = utils.get_bars_range(fut_code,
                                          start_hour=M1_S_H,
                                          start_minute=M1_S_M,
                                          end_hour=M1_E_H,
                                          end_minute=M1_E_M,
                                          duration='1 min',
                                          )

        second_bar_open = bars_5_min.iloc[0].Open
        if bars_1_min.shape[0] == 0:
            second_bar_close = bars_5_min.iloc[-1].Close
        else:
            second_bar_close = bars_1_min.iloc[-1].Close
        second_bar_high = second_bar_open
        second_bar_low = second_bar_open
        for _, bar in bars_5_min.iterrows():
            second_bar_high = max(second_bar_high, bar.High)
        for _, bar in bars_1_min.iterrows():
            second_bar_high = max(second_bar_high, bar.High)

        for _, bar in bars_5_min.iterrows():
            second_bar_low = min(second_bar_low, bar.Low)
        for _, bar in bars_1_min.iterrows():
            second_bar_low = min(second_bar_low, bar.Low)

        hl_dist = (bar_4h.High - bar_4h.Low) / 2
        h_px = bar_4h.Close + hl_dist
        l_px = bar_4h.Close - hl_dist

        if bar_4h.Close > bar_4h.Open:
            high = max(bar_4h.High, second_bar_high)
            low = min(bar_4h.Open, second_bar_low)
        elif bar_4h.Close < bar_4h.Open:
            high = max(bar_4h.Open, second_bar_high)
            low = min(bar_4h.Low, second_bar_low)
        else:
            high = max(bar_4h.High, second_bar_high)
            low = min(bar_4h.Low, second_bar_low)

        high = min(high, h_px)
        low = max(low, l_px)

        last_bar = utils.get_last_bar(fut_code)
        last_px = last_bar.Close

        buy_stps = config.get_config().ALGO_NEWS_BUY_STP
        sell_stps = config.get_config().ALGO_NEWS_SELL_STP

        # condition = False
        # if condition:
        #     config.get_config().ALGO_NEWS_BUY_STP = [
        #         (5, 1, 45, 45),
        #         (0, 2, 15, 15),
        #         (0, 3, 65, 0)
        #     ]

        main_levels_algo(fut_code, high, low, DELTA_TIME, last_px, buy_stps, sell_stps)
Exemple #8
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)
Exemple #9
0
utils.parse_input()

# 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

    lower_px = max(under_px - config.get_config().GRID_PX_STEP * config.get_config().PROFILE_STEPS, 0)
    upper_px = under_px + config.get_config().GRID_PX_STEP * config.get_config().PROFILE_STEPS
    px_grid =np.linspace(lower_px, upper_px, num=(upper_px - lower_px) // config.get_config().GRID_PX_STEP + 1)

    delta_grid, porfolio_px_grid, porfolio_px_expiry_grid = utils.get_portfolio_params(fut_code, px_grid)

    # plot portfolio profile
    min_px = min(np.min(porfolio_px_grid), np.min(porfolio_px_expiry_grid))
    max_px = max(np.max(porfolio_px_grid), np.max(porfolio_px_expiry_grid))
    curr_px_line_x = np.array([under_px, under_px])
    curr_px_line_y = np.array([min_px, max_px])

    fig = plt.figure()