如何获得交替交易信号?
How to get alternating trading signals?
我正在尝试使用此代码开发一个简单的 RSI 策略。正如您通过绘制代码可以看到的那样,有太多连续的卖出信号。我希望有一个买入信号,然后是一个单一的卖出信号,然后是另一个买入信号,以便在最后有类似买、卖、买、卖的东西……而不是像买、卖这样的东西, 卖, 卖, 卖, 买, 卖, 卖...这是代码:
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
import ta
start = dt.datetime(2020, 1, 1)
df = yf.download("^GSPC", start)
portfolio = 10000
df['rsi'] = ta.momentum.rsi(df.Close, window=10)
df['200ma'] = df.Close.rolling(200).mean()
df.dropna(inplace=True)
buy, sell = [], []
for i in range(1, len(df)):
if df.Close.iloc[i] > df['200ma'].iloc[i]:
if df['rsi'].iloc[i] < 30:
buy.append(i)
elif df['rsi'].iloc[i] > 40 or ((df['rsi'].iloc[i] - df.Close.iloc[i]) > 10):
sell.append(i)
realbuys = [i+1 for i in buy]
realsells = [i+1 for i in sell]
buyprices = df.Open.iloc[realbuys]
sellprices = df.Open.iloc[realsells]
# plt.figure(figsize = (12, 6))
# plt.scatter(df.iloc[buy].index, df.iloc[buy].Close, marker = "^", color = "green" )
# plt.scatter(df.iloc[sell].index, df.iloc[sell].Close, marker = "v", color = "red" )
# plt.plot(df.Close, label = "S&P", color='k')
# plt.legend()
# plt.show()
pnl = []
for i in range(len(sellprices)):
pnl.append(((sellprices[i] - buyprices[i])/buyprices[i]))
portfolio = portfolio + (portfolio * pnl[i])
averagepnl = sum(pnl) / len(pnl)
print(portfolio, averagepnl)
所以。创建了一个变量'order',我们在其中记录当前交易。正如您将注意到的,进入交易只发生在相反的方向。例如,如果您现在购买了它,那么下一次交易将只是一次销售。或任何交易,如果它是第一笔交易。
在投资组合中,我有意将周期减少了一个元素。由于买卖交易是不一样的。也就是当前的sale没有关闭。
import yfinance as yf
import matplotlib.pyplot as plt
import datetime as dt
import ta
start = dt.datetime(2020, 1, 1)
df = yf.download("^GSPC", start)
portfolio = 10000
df['rsi'] = ta.momentum.rsi(df.Close, window=10)
df['200ma'] = df.Close.rolling(200).mean()
df.dropna(inplace=True)
buy, sell = [], []
order = ''
for i in range(1, len(df)):
if df.Close.iloc[i] > df['200ma'].iloc[i]:
if df['rsi'].iloc[i] < 30:
if order == 'sell' or order == '':
buy.append(i)
order = 'buy'
elif df['rsi'].iloc[i] > 40 or ((df['rsi'].iloc[i] - df.Close.iloc[i]) > 10):
if order == 'buy' or order == '':
sell.append(i)
order = 'sell'
realbuys = [i + 1 for i in buy]
realsells = [i + 1 for i in sell]
buyprices = df.Open.iloc[realbuys]
sellprices = df.Open.iloc[realsells]
plt.figure(figsize = (12, 6))
plt.scatter(df.iloc[buy].index, df.iloc[buy].Close, marker = "^", color = "green" )
plt.scatter(df.iloc[sell].index, df.iloc[sell].Close, marker = "v", color = "red" )
plt.plot(df.Close, label = "S&P", color='k')
plt.legend()
plt.show()
pnl = []
for i in range(len(sellprices)-1):
pnl.append(((sellprices[i] - buyprices[i]) / buyprices[i]))
portfolio = portfolio + (portfolio * pnl[i])
averagepnl = sum(pnl) / len(pnl)
print(portfolio, averagepnl)
我正在尝试使用此代码开发一个简单的 RSI 策略。正如您通过绘制代码可以看到的那样,有太多连续的卖出信号。我希望有一个买入信号,然后是一个单一的卖出信号,然后是另一个买入信号,以便在最后有类似买、卖、买、卖的东西……而不是像买、卖这样的东西, 卖, 卖, 卖, 买, 卖, 卖...这是代码:
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
import ta
start = dt.datetime(2020, 1, 1)
df = yf.download("^GSPC", start)
portfolio = 10000
df['rsi'] = ta.momentum.rsi(df.Close, window=10)
df['200ma'] = df.Close.rolling(200).mean()
df.dropna(inplace=True)
buy, sell = [], []
for i in range(1, len(df)):
if df.Close.iloc[i] > df['200ma'].iloc[i]:
if df['rsi'].iloc[i] < 30:
buy.append(i)
elif df['rsi'].iloc[i] > 40 or ((df['rsi'].iloc[i] - df.Close.iloc[i]) > 10):
sell.append(i)
realbuys = [i+1 for i in buy]
realsells = [i+1 for i in sell]
buyprices = df.Open.iloc[realbuys]
sellprices = df.Open.iloc[realsells]
# plt.figure(figsize = (12, 6))
# plt.scatter(df.iloc[buy].index, df.iloc[buy].Close, marker = "^", color = "green" )
# plt.scatter(df.iloc[sell].index, df.iloc[sell].Close, marker = "v", color = "red" )
# plt.plot(df.Close, label = "S&P", color='k')
# plt.legend()
# plt.show()
pnl = []
for i in range(len(sellprices)):
pnl.append(((sellprices[i] - buyprices[i])/buyprices[i]))
portfolio = portfolio + (portfolio * pnl[i])
averagepnl = sum(pnl) / len(pnl)
print(portfolio, averagepnl)
所以。创建了一个变量'order',我们在其中记录当前交易。正如您将注意到的,进入交易只发生在相反的方向。例如,如果您现在购买了它,那么下一次交易将只是一次销售。或任何交易,如果它是第一笔交易。
在投资组合中,我有意将周期减少了一个元素。由于买卖交易是不一样的。也就是当前的sale没有关闭。
import yfinance as yf
import matplotlib.pyplot as plt
import datetime as dt
import ta
start = dt.datetime(2020, 1, 1)
df = yf.download("^GSPC", start)
portfolio = 10000
df['rsi'] = ta.momentum.rsi(df.Close, window=10)
df['200ma'] = df.Close.rolling(200).mean()
df.dropna(inplace=True)
buy, sell = [], []
order = ''
for i in range(1, len(df)):
if df.Close.iloc[i] > df['200ma'].iloc[i]:
if df['rsi'].iloc[i] < 30:
if order == 'sell' or order == '':
buy.append(i)
order = 'buy'
elif df['rsi'].iloc[i] > 40 or ((df['rsi'].iloc[i] - df.Close.iloc[i]) > 10):
if order == 'buy' or order == '':
sell.append(i)
order = 'sell'
realbuys = [i + 1 for i in buy]
realsells = [i + 1 for i in sell]
buyprices = df.Open.iloc[realbuys]
sellprices = df.Open.iloc[realsells]
plt.figure(figsize = (12, 6))
plt.scatter(df.iloc[buy].index, df.iloc[buy].Close, marker = "^", color = "green" )
plt.scatter(df.iloc[sell].index, df.iloc[sell].Close, marker = "v", color = "red" )
plt.plot(df.Close, label = "S&P", color='k')
plt.legend()
plt.show()
pnl = []
for i in range(len(sellprices)-1):
pnl.append(((sellprices[i] - buyprices[i]) / buyprices[i]))
portfolio = portfolio + (portfolio * pnl[i])
averagepnl = sum(pnl) / len(pnl)
print(portfolio, averagepnl)