我真的不知道该如何称呼,但是我的问题如下,我正在使用基本的SMA交叉交易算法。代码如下:
import pandas as pd
import pandas_datareader as data
import datetime as dt
import numpy as np
start = dt.datetime(2017, 1, 1)
end = dt.datetime(2020, 1, 20)
d = data.get_data_yahoo('URI', start, end)
d['sma50'] = np.round(d['Close'].rolling(window=2).mean())
d['sma200'] = np.round(d['Close'].rolling(window=14).mean(), decimals = 2)
d['200-50'] = d['sma200'] - d['sma50']
_buy = -2
d['Crossover_Long'] = np.where(d['200-50'] < _buy, 1, 0)
d['buy'] = np.where(d['Crossover_Long']==1, 'buy', 'sell')
pd.set_option('display.max_rows', 400)
d.drop(['High', 'Low', 'Volume', 'Adj Close', 'Open'], axis=1, inplace=True)
d.dropna(inplace=True)
d.head()
因此,前5行是:
Close sma50 sma200 200-50 Crossover_Long buy
Date
2017-01-23 110.110001 111.0 109.04 -1.96 0 sell
2017-01-24 113.610001 112.0 109.35 -2.65 1 buy
2017-01-25 114.260002 114.0 109.67 -4.33 1 buy
2017-01-26 127.059998 121.0 110.87 -10.13 1 buy
2017-01-27 128.259995 128.0 112.22 -15.78 1 buy
如果有1,则应该买入;如果有0,则应该卖出。现在的问题是一种解决方法,即当sma2高于sma14时,而不是继续写sma2时,仅在出现交叉时才打印1。然后介于0之间,直到下一个分频。有任何想法吗?谢谢!