Exemple #1
0
def net_value_plot(strategy_net_value: pd.Series,
                   benchmark: pd.Series or None = None,
                   strategy_name='strategy',
                   fill=None):
    fig = go.Figure()
    fig.add_trace(
        net_value_line(strategy_net_value / strategy_net_value[0],
                       name=strategy_name,
                       fill=fill))
    if benchmark is not None:
        benchmark_copy = benchmark[
            (benchmark.index >= strategy_net_value.index[0])
            & (benchmark.index <= strategy_net_value.index[-1])]
        fig.add_trace(
            net_value_line(benchmark_copy / benchmark_copy[0],
                           color='#FFCC00',
                           name='benchmark'))

    x_axis = fig.data[0].x
    tick_value = [x_axis[i] for i in range(0, len(x_axis), len(x_axis) // 5)]
    tick_text = [
        x_axis[i][0:10] for i in range(0, len(x_axis),
                                       len(x_axis) // 5)
    ]
    fig.update_xaxes(ticktext=tick_text, tickvals=tick_value)

    return fig
Exemple #2
0
def maximum_drawdown_plot(drawdown_percent: pd.Series):
    fig = go.Figure()
    fig.add_trace(net_value_line(drawdown_percent, color='#73B839', name='underwater', fill='tozeroy'), )
    fig.update_layout(
        title="Underwater", )
    x_axis = fig.data[0].x
    tick_value = [x_axis[i] for i in range(0, len(x_axis), len(x_axis) // 5)]
    tick_text = [x_axis[i][0:10] for i in range(0, len(x_axis), len(x_axis) // 5)]
    fig.update_xaxes(ticktext=tick_text, tickvals=tick_value)
    return fig
Exemple #3
0
def entry_and_exit_plot(ohlc_df, traded: pd.DataFrame, symbol: str, ohlc_graph=True, price_key='close', ohlc_key=None,
                        entrust=False, ta_dict: None or dict = None):
    # traded columns:
    # code,order_time,order_price,order_qty,order_type,dealt_price,dealt_qty,order_direction,order_status,update_time,
    # exchange_order_id,order_id,cash_inflow
    subplot_num = 1
    if ta_dict is not None:
        subplot_num += len([i for i in ta_dict.values() if i is False])

    row_heights = [1] * subplot_num
    row_heights[0] = 2

    fig = make_subplots(
        rows=subplot_num, cols=1,
        shared_xaxes=True,
        vertical_spacing=0.03, row_heights=row_heights
    )
    # filter out unrelated trade
    traded = traded[traded['code'] == symbol]
    long = traded[traded['order_direction'] == 'LONG']
    short = traded[traded['order_direction'] == 'SHORT']

    if ohlc_graph:
        candles = candlestick(ohlc_df, ohlc_key=ohlc_key, symbol=symbol)
        fig.add_trace(candles, 1, 1)
    else:
        prices = net_value_line(ohlc_df[price_key], color='#FFA500', name=symbol)
        fig.add_trace(prices, 1, 1)

    long_dot = entry_exit_dot(long, True)
    short_dot = entry_exit_dot(short, False)
    fig.add_trace(long_dot, 1, 1)
    fig.add_trace(short_dot, 1, 1)
    if entrust:
        entrust_long = entrust_dot(long, True)
        entrust_short = entrust_dot(short, False)
        fig.add_trace(entrust_long, 1, 1)
        fig.add_trace(entrust_short, 1, 1)

    if ta_dict is not None:
        subplot_i = 2
        for ta, overlap in ta_dict.items():
            call_str = ta.replace('inputs', 'ohlc_df')
            ta_indicator = eval(call_str)
            ta_name = call_str.split('(')[0]
            if isinstance(ta_indicator, pd.Series):
                index = ta_indicator.index.strftime('%Y/%m/%d %H:%M:%S')
                if overlap is True:
                    if ta_name == 'SAR':
                        fig.add_trace(sar_graph(ta_indicator, ohlc_df['close']), 1, 1)
                    else:
                        fig.add_trace(go.Scatter(x=index, y=ta_indicator, mode='lines', name=call_str), 1, 1)
                else:
                    fig.add_trace(go.Scatter(x=index, y=ta_indicator, mode='lines', name=call_str), subplot_i, 1)
                    subplot_i += 1
            elif isinstance(ta_indicator, pd.DataFrame):
                index = ta_indicator.index.strftime('%Y/%m/%d %H:%M:%S')

                if overlap is True:
                    for col in ta_indicator.columns:
                        fig.add_trace(go.Scatter(x=index, y=ta_indicator[col], mode='lines', name=col), 1, 1)
                else:
                    if ta_name == 'MACD' or ta_name == 'MACDEXT' or ta_name == 'MACDFIX':
                        fig.add_traces(macd_graph(ta_indicator), [subplot_i] * 3, [1] * 3)
                    else:
                        for col in ta_indicator.columns:
                            fig.add_trace(go.Scatter(x=index, y=ta_indicator[col], mode='lines', name=col), subplot_i,
                                          1)
                    subplot_i += 1

    x_axis = fig.data[0].x
    tick_value = [x_axis[i] for i in range(0, len(x_axis), len(x_axis) // 5)]
    tick_text = [x_axis[i][0:10] for i in range(0, len(x_axis), len(x_axis) // 5)]
    fig.update_xaxes(ticktext=tick_text, tickvals=tick_value)
    # fig.update_layout(showlegend=True, xaxis_rangeslider_visible=False)
    fig.update_layout(showlegend=True, height=175 * len(fig.data),
                      yaxis1=dict(autorange=True, fixedrange=False),
                      yaxis2=dict(autorange=True, fixedrange=False),
                      xaxis_rangeslider_visible=False, hovermode='x unified'
                      )
    return fig