Пример #1
0
def _subplot_indicator(subplot_technical_index, sub_plot_param, data, fig, row):
    if subplot_technical_index:
        for n, ind in enumerate(subplot_technical_index):
            if sub_plot_param is not None:
                try:
                    df = statistic.indicator(data, ind, sub_plot_param[ind])
                except:
                    df = statistic.indicator(data, ind)

            else:
                df = statistic.indicator(data, ind)

            add_trace(fig, df, row=row + n)

    else:
        pass
Пример #2
0
 def strategy(df):
     kd = indicator(df, 'STOCH')
     condition = (kd['slowk'] > kd['slowd']) & (kd['slowk'].shift() <
                                                kd['slowd'].shift())
     return condition
Пример #3
0
    def indicator(self, name, timeperiod=None):

        return statistic.indicator(self.data, name, timeperiod)
Пример #4
0
def get_plotly(data,
               subplot_technical_index: list,
               overlap=None,
               sub_plot_param=None,
               overlap_param=None,
               log=None,
               callback: list = None):
    data = data.copy()
    data['diag'] = np.empty(len(data))
    data.diag[data.close > data.close.shift()] = '#f6416c'
    data.diag[data.close <= data.close.shift()] = '#7bc0a3'
    data.index = data.index.strftime("%Y/%m/%d")

    if log is not None:
        if subplot_technical_index:
            subplot_titles = ["收盤價", "累積報酬率", 'MDD(%)'
                              ] + [x for x in subplot_technical_index]
            length_subplot_technical_index = len(subplot_technical_index)
        else:
            subplot_titles = ["收盤價", "累積報酬率", 'MDD(%)']
            length_subplot_technical_index = 0
        row_heights = [450] + [300] * (2 + length_subplot_technical_index)
        specs = [[{
            "secondary_y": True
        }]] * (3 + length_subplot_technical_index)

        fig = make_subplots(rows=3 + length_subplot_technical_index,
                            cols=1,
                            row_heights=row_heights,
                            shared_xaxes=True,
                            subplot_titles=subplot_titles,
                            specs=specs)
        _update_layout(fig)
        _main_fig(data=data, fig=fig, callback=callback)
        date = pd.Index(np.where(log.KeepDay > 0, log.SellDate, log.BuyDate))
        empty_series = pd.Series(index=data.index)
        buy = pd.Series(
            log['BuyPrice'].values,
            index=log['BuyDate']).drop_duplicates().rename('BuyPrice')
        sell = pd.Series(
            log['SellPrice'].values,
            index=log['SellDate']).drop_duplicates().rename('SellPrice')
        _log = pd.concat([buy, sell, empty_series], 1)
        index_return = statistic.index_accumulate_return(
            str(log['BuyDate'][0].year),
            str(log['BuyDate'].iloc[-1].year),
            index='^GSPC')
        fig.add_trace(go.Scatter(
            x=date,
            y=log['累積報酬率(%)'],
            mode='lines',
            name='累積報酬率(%)',
        ),
                      row=2,
                      col=1)
        fig.add_trace(go.Scatter(
            x=date,
            y=index_return[log['SellDate']],
            mode='lines',
            name='大盤累積報酬率(%)',
        ),
                      row=2,
                      col=1)

        fig.add_trace(go.Scatter(x=data.index,
                                 y=_log['BuyPrice'],
                                 mode='markers',
                                 name='Buydate'),
                      row=1,
                      col=1)

        fig.add_trace(go.Scatter(x=data.index,
                                 y=_log['SellPrice'],
                                 mode='markers',
                                 name='Selldate'),
                      row=1,
                      col=1)

        fig.add_trace(go.Scatter(x=date,
                                 y=log['MDD(%)'] * -1,
                                 fill='tozeroy',
                                 name='MDD'),
                      row=3,
                      col=1)
        _subplot_indicator(subplot_technical_index,
                           sub_plot_param,
                           data,
                           fig,
                           row=4)
    else:
        if subplot_technical_index:
            subplot_titles = ["收盤價"] + [x for x in subplot_technical_index]
            length_subplot_technical_index = len(subplot_technical_index)
        else:
            subplot_titles = ["收盤價"]
            length_subplot_technical_index = 0

        row_heights = [450] + [300] * length_subplot_technical_index
        specs = [[{
            "secondary_y": True
        }]] * (1 + length_subplot_technical_index)
        fig = make_subplots(rows=1 + length_subplot_technical_index,
                            cols=1,
                            row_heights=row_heights,
                            shared_xaxes=True,
                            subplot_titles=subplot_titles,
                            specs=specs)
        _update_layout(fig)
        _main_fig(data=data, fig=fig, callback=callback)
        _subplot_indicator(subplot_technical_index,
                           sub_plot_param,
                           data,
                           fig,
                           row=2)

    if overlap is not None:
        for ind in overlap:
            if overlap_param is not None:
                # overlappara={'MA': [5, 10, 20]}
                add_trace(fig,
                          statistic.indicator(data, ind, overlap_param[ind]),
                          row=1)
            else:

                add_trace(fig, statistic.indicator(data, ind), row=1)

    fig.show()