Ejemplo n.º 1
0
    def compute_analytics(self, compute_dict):

        if not self.validate_compute_dict(compute_dict):
            return False

        self.num_datapoints = int(compute_dict['time'].shape[0])
        self.data['time'] = compute_dict['time']
        self.data['data'] = compute_dict['data']

        self.compute['tradeable'] = Analytics.market_hours(
            compute_dict['time'])

        for key, val in compute_dict.items():
            if key == 'sma':
                if isinstance(compute_dict[key], list):
                    self.compute[key] = []
                    for period in compute_dict[key]:
                        self.compute[key].append(
                            Analytics.moving_average(data=compute_dict['data'],
                                                     period=period))
                else:
                    continue

            elif key == 'derivative':
                if isinstance(compute_dict[key], list):
                    self.compute[key] = []
                    for sma_period, deriv_period in compute_dict[key]:
                        local_sma = Analytics.moving_average(
                            data=compute_dict['data'], period=sma_period)
                        self.compute[key].append(
                            Analytics.derivative(data=local_sma,
                                                 period=deriv_period))
                else:
                    continue

            elif key == 'Bollinger':
                if isinstance(compute_dict[key], list):
                    self.compute[key] = []
                    for anchor_period, oscillator_period, bollinger_period in compute_dict[
                            key]:
                        local_anchor = Analytics.moving_average(
                            data=compute_dict['data'], period=anchor_period)
                        local_oscillator = Analytics.moving_average(
                            data=compute_dict['data'],
                            period=oscillator_period)
                        self.compute[key].append(
                            Analytics.bollinger_bands(data=local_oscillator,
                                                      average=local_anchor,
                                                      period=bollinger_period))
                else:
                    continue

        self.analytics_up_to_date = True

        return True
Ejemplo n.º 2
0
    else:
        print('Data file does not exist!')

    group_choice = np.random.choice(list(datafile.keys()))

    time = datafile[group_choice]['datetime'][...]
    data_open = datafile[group_choice]['open'][...]
    data_high = datafile[group_choice]['high'][...]
    data_low = datafile[group_choice]['low'][...]
    datafile.close()

    data = Analytics.candle_avg(open=data_open, high=data_high, low=data_low)
    candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands(
        open=data_open, high=data_high, low=data_low, average=data, period=30)

    sma = Analytics.moving_average(data=data, period=20)
    sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands(
        data=data, average=sma)

    call_buy_locs, call_buy_price, \
    call_sell_locs, call_sell_price, \
    put_buy_locs, put_buy_price, \
    put_sell_locs, put_sell_price, \
    local_minimums, local_minimums_loc, \
    local_maximums, local_maximums_loc = SMA_strat(sma=sma,
                                                   bollinger_down=sma_low_bollinger,
                                                   bollinger_up=sma_high_bollinger,
                                                   candle=data,
                                                   candle_down=candle_low_bollinger,
                                                   candle_up=candle_high_bollinger)
    plt.suptitle('volume vs volume first mode')
    plt.hist2d(x=data_vol[data_vol < 1e5][1:], y=data_vol[data_vol < 1e5][:-1], range=[[0, 1e5], [0, 1e5]], bins=50)
    '''

    plt.figure(figsize=(10, 10))
    plt.suptitle('volume vs volume')
    plt.hist2d(x=data_vol[1:],
               y=data_vol[:-1],
               range=[[1e5, 1e6], [1e5, 1e6]],
               bins=50)

    ####################################################################################################################
    periods = np.arange(start=1, stop=10, step=2)
    fig, axs = plt.subplots(nrows=5, ncols=4, figsize=(20, 20))
    for period, ax_row in zip(periods, np.arange(periods.shape[0])):
        sma_candle_range = Analytics.moving_average(data=candle_range,
                                                    period=period)
        sma_vol = Analytics.moving_average(data=data_vol, period=period)

        axs[ax_row, 0].set_title('sma volume')
        axs[ax_row, 0].hist(sma_vol, range=[0, 1e6], bins=100)

        axs[ax_row, 1].set_title('sma candle range')
        axs[ax_row, 1].hist(sma_candle_range, range=[0, 2.], bins=100)

        axs[ax_row, 2].set_title('sma volume vs sma candle range')
        axs[ax_row, 2].hist2d(x=sma_vol,
                              y=sma_candle_range,
                              range=[[1.5e5, 1e6], [0, 1.5]],
                              bins=50)

        axs[ax_row, 3].set_title('sma candle range vs sma candle range')
    def compute_analytics(self, data):

        # if not self.validate_compute_dict(self.compute_dict):
        #    return False

        self.data = data
        self.compute[enums.ComputeKeys.datetime] = np.array(
            data['datetime'].tolist())
        self.num_datapoints = int(
            self.compute[enums.ComputeKeys.datetime].shape[0])

        self.compute[enums.ComputeKeys.candle] = Analytics.candle_avg(
            open=np.array(data['open'].tolist()),
            high=np.array(data['high'].tolist()),
            low=np.array(data['low'].tolist()))

        self.compute[enums.ComputeKeys.tradeable] = Analytics.market_hours(
            self.compute[enums.ComputeKeys.datetime])

        for key, val in self.compute_dict.items():
            if key is enums.ComputeKeys.sma:
                if isinstance(val, list):
                    self.compute[key] = []
                    for period in val:
                        self.compute[key].append(
                            Analytics.moving_average(
                                data=self.compute[enums.ComputeKeys.candle],
                                period=period))
                else:
                    continue

            if key is enums.ComputeKeys.derivative:
                if isinstance(self.compute_dict[key], list):
                    self.compute[key] = []
                    for sma_period, deriv_period in val:
                        local_sma = Analytics.moving_average(
                            data=self.compute[enums.ComputeKeys.candle],
                            period=sma_period)
                        self.compute[key].append(
                            Analytics.derivative(data=local_sma,
                                                 period=deriv_period))
                else:
                    continue

            if key is enums.ComputeKeys.Bollinger:
                if isinstance(val, list):
                    self.compute[key] = []
                    for anchor_period, oscillator_period, Bollinger_period in val:
                        local_anchor = Analytics.moving_average(
                            data=self.compute[enums.ComputeKeys.candle],
                            period=anchor_period)
                        local_oscillator = Analytics.moving_average(
                            data=self.compute[enums.ComputeKeys.candle],
                            period=oscillator_period)
                        self.compute[key].append(
                            Analytics.bollinger_bands(data=local_oscillator,
                                                      average=local_anchor,
                                                      period=Bollinger_period))

        self.analytics_up_to_date = True

        return True
def slinger(ax, datafile, ticker, put_parameters, call_parameters):
    time = datafile[ticker]['datetime'][...]
    data_open = datafile[ticker]['open'][...]
    data_high = datafile[ticker]['high'][...]
    data_low = datafile[ticker]['low'][...]
    data_volume = datafile[ticker]['volume'][...]
    datafile.close()

    tradeable = Analytics.market_hours(t=time)
    print(np.sum(tradeable))
    candle = Analytics.candle_avg(open=data_open, high=data_high, low=data_low)
    candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands(
        open=data_open,
        high=data_high,
        low=data_low,
        average=candle,
        period=30)
    period = 30
    sma = Analytics.moving_average(data=candle, period=period)
    sma_short = Analytics.exp_moving_average(data=candle,
                                             alpha=.1,
                                             period=period // 3)
    sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands(
        data=sma_short, average=sma)
    sma_d = Analytics.derivative(sma, period=period // 6)
    # sma_d = Analytics.moving_average(sma_d, period=period // 6)
    sma_dd = Analytics.second_derivative(sma, period=period)

    results_list = SMA_chaser.put_chaser_strat(
        time=time,
        sma=sma,
        bollinger_up=sma_high_bollinger,
        bollinger_down=sma_low_bollinger,
        sma_d=sma_d,
        candle=candle,
        candle_high=candle_high_bollinger,
        candle_low=candle_low_bollinger,
        parameters=put_parameters)

    put_buy_locs = results_list[0]
    put_buy_price = results_list[1]
    # print(put_buy_price)
    put_buy_option_price = results_list[2]
    print(put_buy_option_price)

    put_sell_locs = results_list[3]
    put_sell_price = results_list[4]
    # print(put_sell_price)
    put_sell_option_price = results_list[5]
    print(put_sell_option_price)

    put_profits = (put_sell_option_price - put_buy_option_price)

    put_percent = (put_sell_option_price -
                   put_buy_option_price) / put_buy_option_price
    print(put_percent)
    put_percent_avg = np.sum(put_percent) / put_percent.shape[0]
    print(put_percent_avg)

    # return_list.append(put_percent)
    # return_list.append(put_percent_avg)

    focus_top = time.shape[0] - 60 * 48
    focus_bot = time.shape[0] + 1
    focus_top = 0
    focus_bot = time.shape[0] + 1

    candle_rescaled = candle - np.sum(candle) / sma.shape[0]
    candle_rescaled = candle_rescaled / np.abs(candle_rescaled).max()
    sma_rescaled = sma - np.sum(sma) / sma.shape[0]
    sma_rescaled = sma_rescaled / np.abs(sma_rescaled).max()
    Bollinger_oscillator = 2 * (
        sma_short - sma) / np.absolute(sma_high_bollinger - sma_low_bollinger)
    '''
    ax[0].plot(time[focus_top:focus_bot], candle_rescaled, label='candle')
    ax[0].plot(time[focus_top:focus_bot], sma_rescaled, label='sma')
    ax[0].plot(time[focus_top:focus_bot], sma_d[focus_top:focus_bot] / np.abs(sma_d).max(), label='sma_d')
    ax[0].plot(time[focus_top:focus_bot], sma_dd[focus_top:focus_bot] / np.abs(sma_dd).max(), label='sma_dd')
    ax[0].legend()
    '''

    #################################################################################
    # plt.figure(figsize=(20, 10))
    # plt.suptitle('profitable trades')
    # ax[0].plot(time[tradeable], data_volume[tradeable], '.')

    ax_twin = ax[0].twinx()
    ax_twin.plot(time[tradeable], sma_d[tradeable])
    ax[0].plot(time[tradeable],
               candle[tradeable],
               '.',
               label=str(put_percent_avg))
    ax[0].plot(time[tradeable], sma[tradeable])
    ax[0].plot(time[tradeable], sma_low_bollinger[tradeable])
    ax[0].plot(time[tradeable], sma_high_bollinger[tradeable])
    ax[0].plot(time[tradeable], candle_low_bollinger[tradeable])
    ax[0].plot(time[tradeable], candle_high_bollinger[tradeable])

    profit_put_buy_locs = put_buy_locs[put_profits >= 0]
    put_cut = profit_put_buy_locs[profit_put_buy_locs > focus_top]
    ax[0].plot(time[put_cut], candle[put_cut], '>', color='r')

    profit_put_sell_locs = put_sell_locs[put_profits >= 0]
    put_cut = profit_put_sell_locs[profit_put_sell_locs > focus_top]
    ax[0].plot(time[put_cut], candle[put_cut], '<', color='g')

    ax[0].legend()

    #################################################################################
    # plt.figure(figsize=(20, 10))
    # plt.suptitle('loss trades')
    # ax[1].plot(time, data_volume, '.')
    ax[1].plot(time[tradeable],
               candle[tradeable],
               '.',
               label=str(put_percent_avg))
    ax[1].plot(time[tradeable], sma[tradeable])
    ax[1].plot(time[tradeable], sma_low_bollinger[tradeable])
    ax[1].plot(time[tradeable], sma_high_bollinger[tradeable])
    ax[1].plot(time[tradeable], candle_low_bollinger[tradeable])
    ax[1].plot(time[tradeable], candle_high_bollinger[tradeable])

    loss_put_buy_locs = put_buy_locs[put_profits < 0]
    put_cut = loss_put_buy_locs[loss_put_buy_locs > focus_top]
    ax[1].plot(time[put_cut], candle[put_cut], '>', color='r')

    loss_put_sell_locs = put_sell_locs[put_profits < 0]
    put_cut = loss_put_sell_locs[loss_put_sell_locs > focus_top]
    ax[1].plot(time[put_cut], candle[put_cut], '<', color='g')

    ax[1].legend()

    results_list = SMA_chaser.call_chaser_strat(
        time=time,
        sma=sma,
        bollinger_up=sma_high_bollinger,
        bollinger_down=sma_low_bollinger,
        sma_d=sma_d,
        candle=candle,
        candle_high=candle_high_bollinger,
        candle_low=candle_low_bollinger,
        parameters=call_parameters)

    call_buy_option_price = results_list[2]
    print(put_buy_option_price)

    call_sell_option_price = results_list[5]
    print(put_sell_option_price)

    call_percent = (call_sell_option_price -
                    call_buy_option_price) / call_buy_option_price
    print(call_percent)
    call_percent_avg = np.sum(call_percent) / call_percent.shape[0]
    print(call_percent_avg)

    percent_profits = np.concatenate((put_percent, call_percent))
    print(percent_profits)
    average_profit = np.sum(percent_profits) / percent_profits.shape[0]
    print(average_profit)

    return call_percent_avg
def slinger(ax, datafile, ticker, parameters):
    time = datafile[ticker]['datetime'][...]
    data_open = datafile[ticker]['open'][...]
    data_high = datafile[ticker]['high'][...]
    data_low = datafile[ticker]['low'][...]
    data_volume = datafile[ticker]['volume'][...]
    datafile.close()

    tradeable = Analytics.market_hours(t=time)
    print(np.sum(tradeable))
    candle = Analytics.candle_avg(open=data_open, high=data_high, low=data_low)
    candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands(
        open=data_open,
        high=data_high,
        low=data_low,
        average=candle,
        period=30)
    period = 30
    sma = Analytics.moving_average(data=candle, period=period)
    sma_short = Analytics.moving_average(data=candle, period=period // 3)
    sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands(
        data=sma_short, average=sma)
    sma_d = Analytics.derivative(sma, period=period // 6)
    # sma_d = Analytics.moving_average(sma_d, period=period // 6)
    sma_dd = Analytics.second_derivative(sma, period=period)
    day_volatility = Analytics.day_volatility(data=candle, tradeable=tradeable)
    print('day volatility: {}'.format(day_volatility))

    # find the best strategy of given strategies:

    performance_list = []
    # parameters['option_type'] = position_class.OptionType.PUT
    for parameter in parameters:
        parameter['VIX'] = day_volatility
        results_list = PutSlingerBollinger.Bollinger_strat(
            time=time,
            sma=sma,
            sma_short=sma_short,
            bollinger_up=sma_high_bollinger,
            bollinger_down=sma_low_bollinger,
            sma_d=sma_d,
            candle=candle,
            candle_high=candle_high_bollinger,
            candle_low=candle_low_bollinger,
            parameters=parameter)

        put_buy_option_price = results_list[2]

        put_sell_option_price = results_list[5]

        put_percent = (put_sell_option_price -
                       put_buy_option_price) / put_buy_option_price

        put_percent[
            put_percent > 5] = 5  # put an upper bound on the option returns.

        put_percent_avg = np.sum(put_percent) / put_percent.shape[0]

        performance_list.append(put_percent_avg)

    performance_array = np.array(performance_list)
    print('performance_array: {}'.format(performance_array))
    best_perf_loc = np.where(
        performance_array == performance_array.max())[0][0]

    # hack the parameters to be the best choice for the rest of the function, this keeps recoding to a minimum:
    parameter = parameters[best_perf_loc]
    parameter = parameters[0]

    results_list = PutSlingerBollinger.Bollinger_strat(
        time=time,
        sma=sma,
        sma_short=sma_short,
        bollinger_up=sma_high_bollinger,
        bollinger_down=sma_low_bollinger,
        sma_d=sma_d,
        candle=candle,
        candle_high=candle_high_bollinger,
        candle_low=candle_low_bollinger,
        parameters=parameter)

    put_buy_locs = results_list[0]
    put_buy_price = results_list[1]
    put_buy_option_price = results_list[2]

    put_sell_locs = results_list[3]
    put_sell_price = results_list[4]
    put_sell_option_price = results_list[5]

    position_value = results_list[7]

    print('stock price at open: {}'.format(put_buy_price))
    print('strike price: {}'.format(results_list[6]))
    print('stock price at close: {}'.format(put_sell_price))

    print('option cost at open: {}'.format(put_buy_option_price))
    print('option cost at close: {}'.format(put_sell_option_price))

    put_profits = (put_sell_option_price - put_buy_option_price)

    put_percent = (put_sell_option_price -
                   put_buy_option_price) / put_buy_option_price
    print('option % gain: {}'.format(put_percent))

    put_percent[
        put_percent > 5] = 5  # put an upper bound on the option returns.

    print('position values: {}'.format(position_value))
    account_value = np.sum(position_value)
    print('account value ate EOD: {}'.format(account_value))

    # put_percent_avg = np.sum(put_percent) / put_percent.shape[0]

    put_percent_avg = np.sum(account_value) - 1
    print('average option % gain: {}'.format(put_percent_avg))

    results_list.append(put_percent)
    results_list.append(put_percent_avg)

    focus_top = time.shape[0] - 60 * 48
    focus_bot = time.shape[0] + 1
    focus_top = 0
    focus_bot = time.shape[0] + 1

    candle_rescaled = candle - np.sum(candle) / sma.shape[0]
    candle_rescaled = candle_rescaled / np.abs(candle_rescaled).max()
    sma_rescaled = sma - np.sum(sma) / sma.shape[0]
    sma_rescaled = sma_rescaled / np.abs(sma_rescaled).max()
    Bollinger_oscillator = 2 * (
        sma_short - sma) / np.absolute(sma_high_bollinger - sma_low_bollinger)

    minute_time = Analytics.minute_time(time)
    # print(minute_time)
    '''
    ax[0].plot(time[focus_top:focus_bot], candle_rescaled, label='candle')
    ax[0].plot(time[focus_top:focus_bot], sma_rescaled, label='sma')
    ax[0].plot(time[focus_top:focus_bot], sma_d[focus_top:focus_bot] / np.abs(sma_d).max(), label='sma_d')
    ax[0].plot(time[focus_top:focus_bot], sma_dd[focus_top:focus_bot] / np.abs(sma_dd).max(), label='sma_dd')
    ax[0].legend()
    '''

    #################################################################################
    # plt.figure(figsize=(20, 10))
    # plt.suptitle('profitable trades')
    # ax[0].plot(time[tradeable], data_volume[tradeable], '.')

    ax_twin = ax[0].twinx()
    ax_twin.plot(minute_time[tradeable], Bollinger_oscillator[tradeable])
    ax_twin.plot(minute_time[tradeable],
                 np.ones_like(minute_time[tradeable]) *
                 parameter['Bollinger_top'],
                 color='k')
    ax_twin.plot(minute_time[tradeable],
                 np.ones_like(minute_time[tradeable]) *
                 parameter['Bollinger_bot'],
                 color='k')

    ax[0].plot(minute_time[tradeable],
               candle[tradeable],
               '.',
               label=str(put_percent_avg))
    ax[0].plot(minute_time[tradeable], sma[tradeable])
    ax[0].plot(minute_time[tradeable], sma_low_bollinger[tradeable])
    ax[0].plot(minute_time[tradeable], sma_high_bollinger[tradeable])
    ax[0].plot(minute_time[tradeable], candle_low_bollinger[tradeable])
    ax[0].plot(minute_time[tradeable], candle_high_bollinger[tradeable])

    profit_put_buy_locs = put_buy_locs[put_profits >= 0]
    put_cut = profit_put_buy_locs[profit_put_buy_locs > focus_top]
    ax[0].plot(minute_time[put_cut], candle[put_cut], '>', color='k')

    profit_put_sell_locs = put_sell_locs[put_profits >= 0]
    put_cut = profit_put_sell_locs[profit_put_sell_locs > focus_top]
    ax[0].plot(minute_time[put_cut], candle[put_cut], '<', color='k')

    ax[0].legend()

    #################################################################################
    # plt.figure(figsize=(20, 10))
    # plt.suptitle('loss trades')
    # ax[1].plot(minute_time, data_volume, '.')

    ax_twin = ax[1].twinx()
    ax_twin.plot(minute_time[tradeable], Bollinger_oscillator[tradeable])
    ax_twin.plot(minute_time[tradeable],
                 np.ones_like(minute_time[tradeable]) *
                 parameter['Bollinger_top'],
                 color='k')
    ax_twin.plot(minute_time[tradeable],
                 np.ones_like(minute_time[tradeable]) *
                 parameter['Bollinger_bot'],
                 color='k')

    ax[1].plot(minute_time[tradeable],
               candle[tradeable],
               '.',
               label=str(put_percent_avg))
    ax[1].plot(minute_time[tradeable], sma[tradeable])
    ax[1].plot(minute_time[tradeable], sma_low_bollinger[tradeable])
    ax[1].plot(minute_time[tradeable], sma_high_bollinger[tradeable])
    ax[1].plot(minute_time[tradeable], candle_low_bollinger[tradeable])
    ax[1].plot(minute_time[tradeable], candle_high_bollinger[tradeable])

    loss_put_buy_locs = put_buy_locs[put_profits < 0]
    put_cut = loss_put_buy_locs[loss_put_buy_locs > focus_top]
    ax[1].plot(minute_time[put_cut], candle[put_cut], '>', color='k')

    loss_put_sell_locs = put_sell_locs[put_profits < 0]
    put_cut = loss_put_sell_locs[loss_put_sell_locs > focus_top]
    ax[1].plot(minute_time[put_cut], candle[put_cut], '<', color='k')

    ax[1].legend()

    return put_percent_avg, performance_array
Ejemplo n.º 7
0
    time = datafile[group_choice]['datetime'][...]
    data_open = datafile[group_choice]['open'][...]
    data_high = datafile[group_choice]['high'][...]
    data_low = datafile[group_choice]['low'][...]
    datafile.close()

    tradeable = Analytics.market_hours(t=time)
    print(np.sum(tradeable))
    candle = Analytics.candle_avg(open=data_open, high=data_high, low=data_low)
    candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands(open=data_open,
                                                                                   high=data_high,
                                                                                   low=data_low,
                                                                                   average=candle,
                                                                                   period=30)
    period = 30
    sma = Analytics.moving_average(data=candle, period=period)
    sma_short = Analytics.moving_average(data=candle, period=period // 3)
    sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands(data=sma_short, average=sma)
    sma_d = Analytics.derivative(sma, period=period // 6)
    # sma_d = Analytics.moving_average(sma_d, period=period // 6)
    sma_dd = Analytics.second_derivative(sma, period=period)

    parameter = {'Bollinger_top': .0,
                  'Bollinger_bot': -2.0,
                  'stop_loss': .8,
                  'profit': .8,
                  'flip': 1,
                  'option_type': position_class.OptionType.CALL}

    results_list = Bollinger_strat(time=time,
                                   sma=sma,
Ejemplo n.º 8
0
        print('Data file does not exist!')

    # group_choice = np.random.choice(list(datafile.keys()))
    group_choice = 'SPY'

    time = datafile[group_choice]['datetime'][...]
    data_open = datafile[group_choice]['open'][...]
    data_high = datafile[group_choice]['high'][...]
    data_low = datafile[group_choice]['low'][...]
    datafile.close()

    data = Analytics.candle_avg(open=data_open, high=data_high, low=data_low)
    candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands(
        open=data_open, high=data_high, low=data_low, average=data, period=30)

    sma = Analytics.moving_average(data=data, period=30)
    sma_short = Analytics.moving_average(data=data, period=15)
    # sma = Analytics.exp_moving_average(data=data, alpha=.1, period=30)
    sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands(
        data=data, average=sma)

    results_list = bollinger_strat(time=time,
                                   sma=sma,
                                   sma_short=sma_short,
                                   bollinger_down=sma_low_bollinger,
                                   bollinger_up=sma_high_bollinger,
                                   candle=data,
                                   candle_down=candle_low_bollinger,
                                   candle_up=candle_high_bollinger)
    call_buy_locs = results_list[0]
    call_buy_price = results_list[1]
Ejemplo n.º 9
0
def DataAnalysis(filedirectory='../StockData/'):
    result_list = []
    meta_result_list = []

    for direntry in os.scandir(filedirectory):
        result_dict = {}
        meta_result_dict = {}

        if direntry.is_dir():
            continue

        if direntry.is_file():
            filepath = direntry.path
            print(filepath)
            try:
                datafile = h5py.File(filepath)
            except:
                print('could not open file: {}'.format(filepath))
                continue

        time_data = datafile['SPY']['datetime'][...]
        if time_data.shape[0] == 0:
            print('no \'SPY\' data in file: {}'.format(filepath))
            continue

        clock_list = []
        market_hours_list = []
        for i, t in enumerate(time_data):
            # gm_time = tm.gmtime(t * 1e-3)
            utc_time = arrow.get(t * 1e-3).to('utc')
            utc_time = utc_time.shift(
                hours=-4)  # must explicitely shift time for numpy to recognize
            market_open = utc_time.replace(hour=9,
                                           minute=30,
                                           second=0,
                                           microsecond=0)
            market_close = utc_time.replace(hour=16,
                                            minute=0,
                                            second=0,
                                            microsecond=0)
            open_delta = utc_time - market_open
            close_delta = market_close - utc_time
            # nyt_time = utc_time.to('America/New_York')

            clock_list.append(open_delta.total_seconds())

            if open_delta.total_seconds() >= 0 and close_delta.total_seconds(
            ) >= 0:
                market_hours_list.append(True)
            else:
                market_hours_list.append(False)

            if i == time_data.shape[0] // 2:
                meta_result_dict['date_list'] = utc_time.date()

            # print(clock_list)
        meta_result_dict['clocktime'] = np.array(clock_list)
        meta_result_dict['tradeable'] = np.array(market_hours_list,
                                                 dtype=np.bool)

        open_data = datafile['SPY']['open'][...]
        high_data = datafile['SPY']['high'][...]
        low_data = datafile['SPY']['low'][...]

        period = 5
        result_dict['candle_average'] = Analytics.candle_avg(open=open_data,
                                                             high=high_data,
                                                             low=low_data)
        result_dict['moving_average'] = Analytics.moving_average(
            data=result_dict['candle_average'], period=period)
        result_dict['mvg_avg_d'] = Analytics.derivative(
            data=result_dict['moving_average'], period=period)
        result_dict['mvg_avg_dd'] = Analytics.second_derivative(
            data=result_dict['moving_average'], period=period)

        meta_result_list.append(meta_result_dict)
        result_list.append(result_dict)

        datafile.close()

    print('number of \'SPY\' days found: {}'.format(len(result_list)))

    # print(str(date_list[0]))

    fig, axs = plt.subplots(nrows=len(result_list),
                            ncols=len(result_list[0]),
                            figsize=(20, 40))
    # print(axs.shape)
    for i, meta_result_dict, result_dict in zip(np.arange(len(result_list)),
                                                meta_result_list, result_list):
        # print(result_dict.keys())
        for j, key in enumerate(result_dict.keys()):
            # print(result_dict[key])
            axs[i, j].hist(result_dict[key], bins=30)
            # axs[i, j].legend()

            if i == 0:
                axs[i, j].set_title(key)
            if j == 0:
                axs[i, j].set_ylabel(str(meta_result_dict['date_list']),
                                     rotation=90,
                                     size='large')

            # axs[i].set_xlim(left=(9 * 3600 + 30 * 60), right=(16 * 3600))
            # axs[i, j].set_xlim(left=(-30 * 60), right=(6 * 3600 + 30 * 60))
            # axs[i].set_xticks(np.arange(8)*3600)
            # axs[i].set_xticklabels(map(str, np.arange(8)+9))

    plt.tight_layout()
    # plt.figure(figsize=(10, 10))
    # for i, clocktime, candle_average in zip(np.arange(len(clocktime_list)), clocktime_list, candle_average_list):
    #    plt.plot(clocktime, candle_average / np.max(np.abs(candle_average)))

    return
Ejemplo n.º 10
0
            print(dataset.name)
            print(dataset.shape)
    '''

    group_choice = np.random.choice(list(datafile.keys()))
    group_choice = 'SPY'

    time = datafile[group_choice]['datetime'][...]
    data_open = datafile[group_choice]['open'][...]
    data_high = datafile[group_choice]['high'][...]
    data_low = datafile[group_choice]['low'][...]
    data_volume = datafile[group_choice]['volume'][...]
    datafile.close()

    candle_data = Analytics.candle_avg(open=data_open,
                                       high=data_high,
                                       low=data_low)
    candle_sma = Analytics.moving_average(data=candle_data, period=20)
    candle_rail, _, candle_volume = trace_oscillations(data=candle_data -
                                                       candle_sma,
                                                       volume=data_volume,
                                                       threshold_down=0,
                                                       threshold_up=0)

    plt.scatter(candle_rail, candle_volume)

    plt.figure()
    plt.hist(candle_rail, bins=100)
    plt.figure()
    plt.hist(candle_volume, bins=100)