# ===策略参数 # n代表取平均线和标准差的参数 # m代表标准差的倍数 n = int(para[0]) m = para[1] ma_n = para[2] # MA指标的参数 # ===计算指标 # 计算均线 df['median'] = df['close'].rolling(n).mean() # 此处只计算最后几行的均线值,因为没有加min_period参数 median = df.iloc[-1]['median'] median2 = df.iloc[-2]['median'] # 计算标准差 df['std'] = df['close'].rolling(n).std(ddof=0) # ddof代表标准差自由度,只计算最后几行的均线值,因为没有加min_period参数 std = df.iloc[-1]['std'] std2 = df.iloc[-2]['std'] # 计算上轨、下轨道 upper = median + m * std lower = median - m * std upper2 = median2 + m * std2 lower2 = median2 - m * std2 # ===计算MA指标 df['MA'] = df['close'].rolling(ma_n).mean() ma = df.iloc[-1]['MA'] ma2 = df.iloc[-2]['MA'] # ===寻找交易信号 signal = None close = df.iloc[-1]['close'] close2 = df.iloc[-2]['close'] # 找出做多信号 if (close > upper) and (close2 <= upper2) and (close > ma) and (close2 <= ma2): signal = 1 # 找出做空信号 elif (close < lower) and (close2 >= lower2) and (close < ma) and (close2 >= ma2): signal = -1 # 找出做多平仓信号 elif (close < median) and (close2 >= median2): signal = 0 # 找出做空平仓信号 elif (close > median) and (close2 <= median2): signal = 0 return signal以上代码加入波动率来过滤信号
时间: 2023-12-10 11:42:04 浏览: 102
要加入波动率来过滤信号的话,可以在计算标准差时,将参数ddof设为1,这样计算得到的是无偏估计的标准差,再将上下轨道分别乘以一个波动率因子即可。具体实现如下:
```python
# 计算标准差,使用无偏估计
df['std'] = df['close'].rolling(n, min_periods=1).std(ddof=1)
# 计算上下轨道,加入波动率因子
volatility_factor = 2 # 波动率因子,可以根据实际情况调整
df['upper'] = df['median'] + volatility_factor * df['std']
df['lower'] = df['median'] - volatility_factor * df['std']
# 找出做多信号
if (close > upper) and (close2 <= upper2) and (close > ma) and (close2 <= ma2) and (std < volatility_factor * ma):
signal = 1
# 找出做空信号
elif (close < lower) and (close2 >= lower2) and (close < ma) and (close2 >= ma2) and (std < volatility_factor * ma):
signal = -1
```
这里假设波动率因子为2,如果标准差超过了2倍的移动平均值,就视为市场波动较大,此时不考虑交易信号。
相关问题
# ===策略参数 # n代表取平均线和标准差的参数 # m代表标准差的倍数 n = int(para[0]) m = para[1] # ===计算指标 # 计算均线 df['median'] = df['close'].rolling(n).mean() # 此处只计算最后几行的均线值,因为没有加min_period参数 median = df.iloc[-1]['median'] median2 = df.iloc[-2]['median'] # 计算标准差 df['std'] = df['close'].rolling(n).std(ddof=0) # ddof代表标准差自由度,只计算最后几行的均线值,因为没有加min_period参数 std = df.iloc[-1]['std'] std2 = df.iloc[-2]['std'] # 计算上轨、下轨道 upper = median + m * std lower = median - m * std upper2 = median2 + m * std2 lower2 = median2 - m * std2 # ===寻找交易信号 signal = None close = df.iloc[-1]['close'] close2 = df.iloc[-2]['close'] # 找出做多信号 if (close > upper) and (close2 <= upper2): signal = 1 # 找出做空信号 elif (close < lower) and (close2 >= lower2): signal = -1 # 找出做多平仓信号 elif (close < median) and (close2 >= median2): signal = 0 # 找出做空平仓信号 elif (close > median) and (close2 <= median2): signal = 0 return signal以上代码加入MA指标过滤交易信号
以下是加入MA指标过滤交易信号的代码:
```
# ===策略参数
# n代表取平均线和标准差的参数
# m代表标准差的倍数
n = int(para[0])
m = para[1]
ma_n = para[2] # MA指标的参数
# ===计算指标
# 计算均线
df['median'] = df['close'].rolling(n).mean() # 此处只计算最后几行的均线值,因为没有加min_period参数
median = df.iloc[-1]['median']
median2 = df.iloc[-2]['median']
# 计算标准差
df['std'] = df['close'].rolling(n).std(ddof=0) # ddof代表标准差自由度,只计算最后几行的均线值,因为没有加min_period参数
std = df.iloc[-1]['std']
std2 = df.iloc[-2]['std']
# 计算上轨、下轨道
upper = median + m * std
lower = median - m * std
upper2 = median2 + m * std2
lower2 = median2 - m * std2
# ===计算MA指标
df['MA'] = df['close'].rolling(ma_n).mean()
ma = df.iloc[-1]['MA']
ma2 = df.iloc[-2]['MA']
# ===寻找交易信号
signal = None
close = df.iloc[-1]['close']
close2 = df.iloc[-2]['close']
# 找出做多信号
if (close > upper) and (close2 <= upper2) and (close > ma) and (close2 <= ma2):
signal = 1
# 找出做空信号
elif (close < lower) and (close2 >= lower2) and (close < ma) and (close2 >= ma2):
signal = -1
# 找出做多平仓信号
elif (close < median) and (close2 >= median2):
signal = 0
# 找出做空平仓信号
elif (close > median) and (close2 <= median2):
signal = 0
return signal
```
这里加入了一个MA指标,通过比较收盘价和MA指标的关系,过滤掉一些无效的交易信号。具体来说,只有当收盘价大于上轨道、上一个收盘价小于上一个上轨道、收盘价大于MA指标、上一个收盘价小于上一个MA指标时,才会触发做多信号;只有当收盘价小于下轨道、上一个收盘价大于上一个下轨道、收盘价小于MA指标、上一个收盘价大于上一个MA指标时,才会触发做空信号。
def trading_strategy(df, para): # 策略参数 n = int(para[0]) # 取平均线和标准差的参数 m = para[1] # 标准差的倍数 ma_n = para[2] # MA指标的参数 volatility_factor = 2 # 波动率因子,可以根据实际情况调整 # 计算均线和标准差 close = df['close'].values ma = np.mean(close[-n:]) std = np.std(close[-n:], ddof=1) # 计算上下轨道 upper = ma + volatility_factor * std lower = ma - volatility_factor * std # 计算MA指标 ma_values = df['close'].rolling(ma_n).mean().values ma_current = ma_values[-1] ma_previous = ma_values[-2] # 寻找交易信号 signal = 0 close_current = close[-1] close_previous = close[-2] # 做多信号 if (close_current > upper) and (close_previous <= upper) and (close_current > ma_current) and (close_previous <= ma_previous) and (std < volatility_factor * ma_current): signal = 1 # 做空信号 elif (close_current < lower) and (close_previous >= lower) and (close_current < ma_current) and (close_previous >= ma_previous) and (std < volatility_factor * ma_current): signal = -1 # 平仓信号 elif ((close_current < ma) and (close_previous >= ma)) or ((close_current > ma) and (close_previous <= ma)): signal = 0 return signal将面的代码的n、m参数组合代码
这个策略的参数组合是一个包含三个参数的列表,分别是n、m、ma_n,其中n为取平均线和标准差的参数,m为标准差的倍数,ma_n为MA指标的参数。您可以通过定义不同的列表来进行参数组合,例如:
```
parameters = [[10, 2, 5], [20, 2, 10], [30, 3, 20]]
```
这里定义了三个不同的参数组合,分别是[10, 2, 5]、[20, 2, 10]和[30, 3, 20]。然后在程序中可以进行循环调用,例如:
```
for para in parameters:
signal = trading_strategy(df, para)
# do something with signal
```
这样就可以对同一份数据使用不同的参数组合进行策略测试了。
阅读全文