尝试在python中使用Oanda

时间:2019-01-17 11:00:35

标签: python pandas dataframe

我正在关注这个家伙教程,并且遇到了一些问题。 https://www.oreilly.com/learning/algorithmic-trading-in-less-than-100-lines-of-python-code

当我运行代码时,它给了我这个问题,我可以找出问题出在哪里,因为它和那个家伙的代码一样。

 RuntimeWarning: invalid value encountered in sign df[col] = np.sign(df['returns'].rolling(momentum).mean())

它也没有绘制数据框。我对数据帧的了解并不多,但据我所知,它看起来是正确的。

这是我所拥有的,这是男生教程的第一部分。

import configparser
import oandapy as opy
import pandas as pd
import numpy as np
import matplotlib

config = configparser.ConfigParser()
config.read('Config/oanda.cfg')

oanda = opy.API(environment='practice',
            access_token=config['oanda']['access_token'])

# Read in the data
data = oanda.get_history(instrument='EUR_USD',  # our instrument
                     start='2016-12-08',  # start data
                     end='2016-12-10',  # end date
                     granularity='M1')  # minute bars

df = pd.DataFrame(data['candles']).set_index('time')

df.index = pd.DatetimeIndex(df.index)

# df.info()

df['returns'] = np.log(df['closeAsk'] / df['closeAsk'].shift(1))

cols = []

for momentum in [15, 30, 60, 120]:
    col = 'position_%s' % momentum
    df[col] = np.sign(df['returns'].rolling(momentum).mean())
    cols.append(col)

import seaborn as sns; sns.set()
strats = ['returns']

for col in cols:
    strat = 'strategy_%s' % col.split('_')[1]
    df[strat] = df[col].shift(1) * df['returns']
    strats.append(strat)

df[strats].dropna().cumsum().apply(np.exp).plot()

0 个答案:

没有答案