Exemplo n.º 1
0
    def get_analysis(self):

        headers = [
            'Total trades %s' % jhstats.get_count(self.total_values_arr), 'Absolute', 'Relative',
            'Profit trades %s' % jhstats.get_count(self.profit_values_arr), 'Absolute', 'Relative',
            'Loss trades %s' % jhstats.get_count(self.loss_values_arr), 'Absolute', 'Relative'
        ]

        table = [
            [
                'Total result', jhstats.get_absolute(self.total_values_arr), jhstats.get_relative(self.total_values_arr, self.balance_start),
                'Total result', jhstats.get_absolute(self.profit_values_arr), jhstats.get_relative(self.profit_values_arr, self.balance_start),
                'Total result', jhstats.get_absolute(self.loss_values_arr), jhstats.get_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Max result', jhstats.get_max_absolute(self.total_values_arr), jhstats.get_max_relative(self.total_values_arr, self.balance_start),
                'Max result', jhstats.get_max_absolute(self.profit_values_arr), jhstats.get_max_relative(self.profit_values_arr, self.balance_start),
                'Max result', jhstats.get_max_absolute(self.loss_values_arr), jhstats.get_max_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Min result', jhstats.get_min_absolute(self.total_values_arr), jhstats.get_min_relative(self.total_values_arr, self.balance_start),
                'Min result', jhstats.get_min_absolute(self.profit_values_arr), jhstats.get_min_relative(self.profit_values_arr, self.balance_start),
                'Min result', jhstats.get_min_absolute(self.loss_values_arr), jhstats.get_min_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Mean', jhstats.get_mean_absolute(self.total_values_arr), jhstats.get_mean_relative(self.total_values_arr, self.balance_start),
                'Mean', jhstats.get_mean_absolute(self.profit_values_arr), jhstats.get_mean_relative(self.profit_values_arr, self.balance_start),
                'Mean', jhstats.get_mean_absolute(self.loss_values_arr), jhstats.get_mean_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Median', jhstats.get_median_absolute(self.total_values_arr), jhstats.get_median_relative(self.total_values_arr, self.balance_start),
                'Median', jhstats.get_median_absolute(self.profit_values_arr), jhstats.get_median_relative(self.profit_values_arr, self.balance_start),
                'Median', jhstats.get_median_absolute(self.loss_values_arr), jhstats.get_median_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Variance', jhstats.get_variance_absolute(self.total_values_arr), jhstats.get_variance_relative(self.total_values_arr, self.balance_start),
                'Variance', jhstats.get_variance_absolute(self.profit_values_arr), jhstats.get_variance_relative(self.profit_values_arr, self.balance_start),
                'Variance', jhstats.get_variance_absolute(self.loss_values_arr), jhstats.get_variance_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Std Dev', jhstats.get_std_dev_absolute(self.total_values_arr), jhstats.get_std_dev_relative(self.total_values_arr, self.balance_start),
                'Std Dev', jhstats.get_std_dev_absolute(self.profit_values_arr), jhstats.get_std_dev_relative(self.profit_values_arr, self.balance_start),
                'Std Dev', jhstats.get_std_dev_absolute(self.loss_values_arr), jhstats.get_std_dev_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Skewness', jhstats.get_skewness_absolute(self.total_values_arr), jhstats.get_skewness_relative(self.total_values_arr, self.balance_start),
                'Skewness', jhstats.get_skewness_absolute(self.profit_values_arr), jhstats.get_skewness_relative(self.profit_values_arr, self.balance_start),
                'Skewness', jhstats.get_skewness_absolute(self.loss_values_arr), jhstats.get_skewness_relative(self.loss_values_arr, self.balance_start)
            ],
            [
                'Kurtosis', jhstats.get_kurtosis_absolute(self.total_values_arr), jhstats.get_kurtosis_relative(self.total_values_arr, self.balance_start),
                'Kurtosis', jhstats.get_kurtosis_absolute(self.profit_values_arr), jhstats.get_kurtosis_relative(self.profit_values_arr, self.balance_start),
                'Kurtosis', jhstats.get_kurtosis_absolute(self.loss_values_arr), jhstats.get_kurtosis_relative(self.loss_values_arr, self.balance_start)
            ]
        ]

        print tabulate(table, headers, tablefmt='grid', floatfmt='.4f')

        headers = ['Description', 'Value']

        table = [
            ['Bankruptcy date', self.get_bankruptcy_date()],
            ['Hitrate %', jhstats.get_hitrate(self.total_values_arr, self.profit_values_arr)],
            ['Profit Loss Ratio', jhstats.get_profit_loss_ratio(jhstats.get_mean_absolute(self.profit_values_arr), jhstats.get_mean_absolute(self.loss_values_arr))],
            ['Information Ratio', jhstats.get_information_ratio(self.total_values_arr, self.balance_start)],
            ['Sharpe Ratio', jhstats.get_sharpe_ratio(self.total_values_arr, self.balance_start, 1)],
            ['Value at Risk', jhstats.get_var(self.total_values_arr, self.balance_start)],
            ['Expected Value', jhstats.get_expected_value(jhstats.get_hitrate(self.total_values_arr, self.profit_values_arr), jhstats.get_mean_absolute(self.profit_values_arr), jhstats.get_mean_absolute(self.loss_values_arr))],
            ['Probability of Ruin (POR) (table of Lucas & LeBeau)', jhstats.get_por_lucas_and_lebeau(jhstats.get_hitrate(self.total_values_arr, self.profit_values_arr), jhstats.get_mean_absolute(self.profit_values_arr), jhstats.get_mean_absolute(self.loss_values_arr))],
            ['Total bars', self.get_bars_total()],
            ['Up bars', self.get_bar_up_count()],
            ['Down bars', self.get_bar_down_count()],
            ['In market bars', self.get_bars_in_market()],
            ['In market %', self.get_procent_in_market()],
            ['Result (buy & hold) absolute', self.get_result_buy_and_hold_value()],
            ['Result (buy & hold) relative', self.get_result_buy_and_hold_procent()]
        ]

        print tabulate(table, headers, tablefmt='grid', floatfmt='.4f')

        headers = ['Description', 'Value', 'Description', 'Value', 'Description', 'Value']

        table = [
            [
                'Consecutive profit count', self.get_max_consecutive_profit_count(),
                'Consecutive loss count', self.get_max_consecutive_loss_count(),
                'Max bar drawdown absolute', self.get_max_bar_drawdown_absolute()
            ],
            [
                'Consecutive profit absolute', self.get_max_consecutive_profit_absolute(),
                'Consecutive loss absolute', self.get_max_consecutive_loss_absolute(),
                'Max position drawdown absolute', self.get_max_position_drawdown_absolute()
            ]
        ]

        print tabulate(table, headers, tablefmt='grid', floatfmt='.4f')
Exemplo n.º 2
0
def main():
    # set time and date:
    ts = int(time.time())
    ts_start = ts - (60*60*24*365.25)  # a year
    ts_end = ts - (60*60*24*0)  # current day
    date_start = dt.fromtimestamp(ts_start).strftime('%Y-%m-%d')
    date_end = dt.fromtimestamp(ts_end).strftime('%Y-%m-%d')
    end_date_time = dt.fromtimestamp(ts_end).strftime('%Y%m%d %H:%M:%S UTC')

    # set values:
    balance_start = 10000
    bankruptcy_at = 8000
    multiplier = 200
    transaction_costs = 2.9
    slippage = 0

    # get data:
    data = Data()
    data.set_contract_ib('EOE', 'IND', 'FTA', 'EUR', 200, '201510')
    bars = data.get_data_ib(1, end_date_time, '1 Y', '1 day', 'TRADES', 0, 1)

    # get best timeperiod:
    best_result = -1000000
    best_timeperiod = 0
    result_arr = []
    timeperiod_arr = []
    for timeperiod in range(2, 200):
        strat_exam = StrategyMy(
            bars,
            balance_start,
            bankruptcy_at,
            multiplier,
            transaction_costs,
            slippage,
            timeperiod
        )
        strat_exam.run()

        result = jhstats.get_absolute(strat_exam.total_values_arr)
#        result = jhstats.get_mean_absolute(strat_exam.total_values_arr)
#        result = jhstats.get_information_ratio(strat_exam.total_values_arr, strat_exam.balance_start)
#        result = jhstats.get_hitrate(strat_exam.total_values_arr, strat_exam.profit_values_arr)
#        result = jhstats.get_count(strat_exam.profit_values_arr)

        if result > best_result:
            best_result = result
            best_timeperiod = timeperiod

        result_arr.append(result)
        timeperiod_arr.append(timeperiod)

    print best_result
    print best_timeperiod

    # get analysis with best timeperiod:
    timeperiod = best_timeperiod
    strat_exam = StrategyMy(
        bars,
        balance_start,
        bankruptcy_at,
        multiplier,
        transaction_costs,
        slippage,
        timeperiod
    )
    strat_exam.run()
    print (('############################################################'))
    print (('date start: %s' % date_start))
    print (('date end: %s' % date_end))
    print (('balance start: %s' % balance_start))
    print (('bankruptcy at: %s' % bankruptcy_at))
    print (('multiplier: %s' % multiplier))
    print (('transaction costs: %s' % transaction_costs))
    print (('slippage: %s' % slippage))
    print (('best timeperiod: %s' % timeperiod))
    print (('############################################################'))
#    print (('\a'))
    print (('Current time: %s' % end_date_time))
    strat_exam.get_analysis()

    # equity curve:
#    jhgraph.get_equity_curve(strat_exam.bars_pd['datetime'], strat_exam.bars_pd['Close'], strat_exam.equity_curve_arr)
    jhgraph.get_equity_curve2(strat_exam.bars_pd['datetime'], strat_exam.bars_pd['Close'], strat_exam.equity_curve_arr, strat_exam.open_equity_curve_arr)

    # optimization curve:
    jhgraph.get_optimization_curve(timeperiod_arr, result_arr)