为外汇d绘制WADL时出现值错误

2024-09-27 04:23:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循的是一系列教程,我完全遵循他的做法,公式如下:

# Williams Accumulation Distribution Function

def wadl(prices, periods):

    """
    :param prices: dataframe of OHLC prices
    :param periods: (list) periods which to 
    calculate the function
    :return: Williams Accumulation Distribution 
    Lines for each periods
    """

    results = holder()
    dict = {}

    for i in range(0, len(periods)):

        WAD = []

        for j in range(periods[i], len(prices)- 
periods[i]):

            TRH = np.array([prices.high.iloc[j], 
prices.close.iloc[j-1]]).max()

            TRL = np.array([prices.low.iloc[j], 
prices.close.iloc[j-1]]).min()

            if prices.close.iloc[j] > 
prices.close.iloc[j-1]:

                PM = prices.close.iloc[j] - TRL

            elif prices.close.iloc[j] < 
prices.close.iloc[j-1]:

                PM = prices.close.iloc[j] - TRH

            elif prices.close.iloc[j] == 
prices.close.iloc[j-1]:

                PM = 0

            else:

                print('Unknown error occur!')

            AD = PM * prices.volume.iloc[j]

            WAD = np.append(WAD, AD)

        WAD = WAD.cumsum()

        WAD = pd.DataFrame(WAD, index=prices.iloc[periods[i]:-periods[i]].index)

        WAD.columns = [['close']]

        dict[periods[i]] = WAD

    results.wadl = dict

    return results

代码存储在名为feature\u functions的文件中,并导入到我的福莱赛.py具体如下:

from feature_functions import *
import pandas as pd
import plotly as py
from plotly import tools
import plotly.graph_objs as go

# 1 Load data and create moving average


df = pd.read_csv('EURUSDhour.csv')
df.columns = ['date', 'open', 'high', 'low', 
'close', 'volume']
df.date = pd.to_datetime(df.date, format='%d.%m.%Y %H:%M:%S.%f')
df = df.set_index('date')
df = df.drop_duplicates(keep=False)
df = df.iloc[:200]

ma = df.close.rolling(center=False, window=30).mean()


# 2 Get function data from selected function


results = wadl(df, [15])
line = results.wadl[15]
print(line)

# 3 Plot

trace0 = go.Ohlc(x=df.index, open=df.open, 
high=df.high, low=df.low, close=df.close, 
name='Currency Qoute')
trace1 = go.Scatter(x=df.index, y=ma)
trace2 = go.Scatter(x=line.index, y=line.close)

data = [trace0, trace1, trace2]

fig = tools.make_subplots(rows=2, cols=1, 
shared_xaxes=True)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)


py.offline.plot(fig, filename='tutorial.html')

问题是,它并没有通过说:

C:/ForexAi/ForexAi.py
C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
   import imp
                          close
date                           
2018-01-02 13:00:00  -11.532949
2018-01-02 14:00:00  -38.481924
2018-01-02 15:00:00  -12.526650
2018-01-02 16:00:00   -5.530906
2018-01-02 17:00:00  -13.941189
2018-01-02 18:00:00  -17.494994
2018-01-02 19:00:00  -10.528929
2018-01-02 20:00:00   -5.539890
2018-01-02 21:00:00   -8.898629
2018-01-02 22:00:00   -9.391708
2018-01-02 23:00:00   -7.718959
2018-01-03 00:00:00  -10.432568
2018-01-03 01:00:00  -17.408881
2018-01-03 02:00:00  -23.787804
2018-01-03 03:00:00  -21.689266
2018-01-03 04:00:00  -20.054626
2018-01-03 05:00:00  -16.299942
2018-01-03 06:00:00  -25.720982
2018-01-03 07:00:00  -37.373113
2018-01-03 08:00:00  -24.459387
2018-01-03 09:00:00  -28.984391
2018-01-03 10:00:00  -55.622087
2018-01-03 11:00:00  -60.688374
2018-01-03 12:00:00  -47.191124
2018-01-03 13:00:00  -31.883299
2018-01-03 14:00:00  -49.793659
2018-01-03 15:00:00  -20.739731
2018-01-03 16:00:00   -8.963290
2018-01-03 17:00:00  -12.381072
2018-01-03 18:00:00   -7.606502
...                         ...
2018-01-10 09:00:00 -163.327093
2018-01-10 10:00:00  -42.003755
2018-01-10 11:00:00   -6.183550
2018-01-10 12:00:00  -39.478537
2018-01-10 13:00:00  -50.752446
2018-01-10 14:00:00  -87.650388
2018-01-10 15:00:00 -110.091646
2018-01-10 16:00:00 -149.885501
2018-01-10 17:00:00 -139.029629
2018-01-10 18:00:00 -148.369751
2018-01-10 19:00:00 -151.054451
2018-01-10 20:00:00 -159.866768
2018-01-10 21:00:00 -164.778860
2018-01-10 22:00:00 -164.041395
2018-01-10 23:00:00 -161.887204
2018-01-11 00:00:00 -152.415646
2018-01-11 01:00:00 -155.230264
2018-01-11 02:00:00 -149.145067
2018-01-11 03:00:00 -163.367665
2018-01-11 04:00:00 -169.028866
2018-01-11 05:00:00 -161.216732
2018-01-11 06:00:00 -169.357748
2018-01-11 07:00:00 -181.240728
2018-01-11 08:00:00 -186.064724
2018-01-11 09:00:00 -169.987864
2018-01-11 10:00:00 -175.696136
2018-01-11 11:00:00 -182.739549
2018-01-11 12:00:00  -48.243193
2018-01-11 13:00:00   18.656411
2018-01-11 14:00:00   48.336694

[170 rows x 1 columns]
Traceback (most recent call last):
  File "C:/ForexAi/ForexAi.py", line 34, in <module>
    trace2 = go.Scatter(x=line.index, y=line.close)
  File "C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\plotly\graph_objs\_scatter.py",     line 2382, in __init__
    self['y'] = y if y is not None else _v
  File "C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\plotly\basedatatypes.py", line 2799, in __setitem__
    self._set_prop(prop, value)
  File "C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\plotly\basedatatypes.py", line 3035, in _set_prop
    raise err
  File "C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\plotly\basedatatypes.py", line     3030, in _set_prop
    val = validator.validate_coerce(val)
  File "C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\_plotly_utils\basevalidators.py", line 353, in validate_coerce
    self.raise_invalid_val(v)
  File "C:\Users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\_plotly_utils\basevalidators.py", line 254, in raise_invalid_val
    valid_clr_desc=self.description()))
ValueError: 
    Invalid value of type 'pandas.core.frame.DataFrame' received for the 'y' property of scatter
        Received value:                                   
                   close
date                           
2018-01-02 13:00:00  -11.532949
2018-01-02 14:00:00  -38.481924
2018-01-02 15:00:00  -12.526650
2018-01-02 16:00:00   -5.530906
2018-01-02 17:00:00  -13.941189
2018-01-02 18:00:00  -17.494994
2018-01-02 19:00:00  -10.528929
2018-01-02 20:00:00   -5.539890
2018-01-02 21:00:00   -8.898629
2018-01-02 22:00:00   -9.391708
2018-01-02 23:00:00   -7.718959
2018-01-03 00:00:00  -10.432568
2018-01-03 01:00:00  -17.408881
2018-01-03 02:00:00  -23.787804
2018-01-03 03:00:00  -21.689266
2018-01-03 04:00:00  -20.054626
2018-01-03 05:00:00  -16.299942
2018-01-03 06:00:00  -25.720982
2018-01-03 07:00:00  -37.373113
2018-01-03 08:00:00  -24.459387
2018-01-03 09:00:00  -28.984391
2018-01-03 10:00:00  -55.622087
2018-01-03 11:00:00  -60.688374
2018-01-03 12:00:00  -47.191124
2018-01-03 13:00:00  -31.883299
2018-01-03 14:00:00  -49.793659
2018-01-03 15:00:00  -20.739731
2018-01-03 16:00:00   -8.963290
2018-01-03 17:00:00  -12.381072
2018-01-03 18:00:00   -7.606502
...                         ...
2018-01-10 09:00:00 -163.327093
2018-01-10 10:00:00  -42.003755
2018-01-10 11:00:00   -6.183550
2018-01-10 12:00:00  -39.478537
2018-01-10 13:00:00  -50.752446
2018-01-10 14:00:00  -87.650388
2018-01-10 15:00:00 -110.091646
2018-01-10 16:00:00 -149.885501
2018-01-10 17:00:00 -139.029629
2018-01-10 18:00:00 -148.369751
2018-01-10 19:00:00 -151.054451
2018-01-10 20:00:00 -159.866768
2018-01-10 21:00:00 -164.778860
2018-01-10 22:00:00 -164.041395
2018-01-10 23:00:00 -161.887204
2018-01-11 00:00:00 -152.415646
2018-01-11 01:00:00 -155.230264
2018-01-11 02:00:00 -149.145067
2018-01-11 03:00:00 -163.367665
2018-01-11 04:00:00 -169.028866
2018-01-11 05:00:00 -161.216732
2018-01-11 06:00:00 -169.357748
2018-01-11 07:00:00 -181.240728
2018-01-11 08:00:00 -186.064724
2018-01-11 09:00:00 -169.987864
2018-01-11 10:00:00 -175.696136
2018-01-11 11:00:00 -182.739549
2018-01-11 12:00:00  -48.243193
2018-01-11 13:00:00   18.656411
2018-01-11 14:00:00   48.336694

[170 rows x 1 columns]

    The 'y' property is an array that may be specified as a tuple,
    list, numpy array, or pandas Series

Process finished with exit code 1

我不知道哪里出了问题。我在跑步: Python 3.6.3版 熊猫0.23.4 图3.4 matplotlib 3.0.1版 有什么帮助吗?非常感谢!你知道吗


Tags: inpydfcloseindexlineplotlyusers
1条回答
网友
1楼 · 发布于 2024-09-27 04:23:11

我在这里发现了错误:

fig = tools.make_subplots(rows=2, cols=1, 
shared_xaxes=True)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)

你从来没见过trace0,因为代他trace1。你知道吗

如果要绘制所有三个绘图,只需将行更改为3,并按如下方式分配:

fig = tools.make_subplots(rows=3, cols=1, 
shared_xaxes=True, subplot_titles=('Plot 1', 'Plot 2', 'Plot 3'))
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 3, 1)

相关问题 更多 >

    热门问题