 
等级: 超级版主
- 注册:
- 2021-5-18
- 曾用名:
|
帮我修改一下这个大盘情绪的定义
# 金字塔(Python)版本的大盘情绪判定
import numpy as np
import pandas as pd
def check_market_sentiment_pyramid():
"""
金字塔版本的大盘情绪判定
基于上证指数000001.SH的TYP指标
条件:TYP[-1]>TYP[-2] and TYP[-1]>TYPMA1[-1] and TYPMA1[-1]>TYPMA2[-1]
"""
try:
# 获取上证指数数据
market_index = '000001'
# 金字塔获取数据方式
# 获取40个交易日的收盘价、最高价、最低价
count = 40
close_data = history_bars(market_index, count, '1d', ['close'])
high_data = history_bars(market_index, count, '1d', ['high'])
low_data = history_bars(market_index, count, '1d', ['low'])
if len(close_data) < 36:
print("数据不足,无法计算TYP指标")
return True
# 计算TYP指标
typ, typma1, typma2 = get_typ_indicators_pyramid(close_data, high_data, low_data, 5, 34)
# 检查条件
if len(typ) >= 2 and len(typma1) >= 1 and len(typma2) >= 1:
condition1 = typ[-1] > typ[-2] # TYP[-1] > TYP[-2]
condition2 = typ[-1] > typma1[-1] # TYP[-1] > TYPMA1[-1]
condition3 = typma1[-1] > typma2[-1] # TYPMA1[-1] > TYPMA2[-1]
market_ok = condition1 and condition2 and condition3
# 打印调试信息
print(f"大盘情绪判定: TYP[-1]={typ[-1]:.2f}, TYP[-2]={typ[-2]:.2f}, TYPMA1[-1]={typma1[-1]:.2f}, TYPMA2[-1]={typma2[-1]:.2f}")
print(f"条件1(TYP上升): {condition1}, 条件2(突破短期): {condition2}, 条件3(多头排列): {condition3}")
print(f"综合判定: {'情绪良好,允许开仓' if market_ok else '情绪不佳,限制开仓'}")
return market_ok
else:
print("TYP指标计算数据不足")
return True
except Exception as e:
print(f"大盘情绪判定出错: {e}")
return True
def get_typ_indicators_pyramid(close_list, high_list, low_list, n1, n2):
"""
金字塔版本的计算TYP相关指标
TYP:(CLOSE+HIGH+LOW)/3;
TYPMA1:EMA(TYP,5); # 短期EMA周期固定为5
TYPMA2:EMA(TYP,34); # 长期EMA周期固定为34
"""
# 计算TYP典型价格
typ = (close_list + high_list + low_list) / 3
# 计算TYPMA1和TYPMA2 (EMA)
typma1 = get_ema_pyramid(typ, n1)
typma2 = get_ema_pyramid(typ, n2)
return typ, typma1, typma2
def get_ema_pyramid(data, period):
"""
金字塔版本的EMA计算
"""
# 使用pandas计算EMA
series = pd.Series(data)
ema = series.ewm(span=period, adjust=False).mean()
return ema.values
# 在金字塔策略中的使用示例
def main_strategy(context):
"""
主策略函数示例
"""
# 检查大盘情绪
sentiment_ok = check_market_sentiment_pyramid()
if sentiment_ok:
# 大盘情绪良好,执行正常交易逻辑
print("大盘情绪良好,执行交易")
# 这里添加你的买入逻辑
# buy_stocks(context)
else:
# 大盘情绪不佳,限制交易或只执行卖出
print("大盘情绪不佳,限制开仓")
# 这里可以只执行卖出逻辑或空仓等待
# sell_stocks_only(context)
# 简化版本(如果不需要详细打印信息)
def check_market_sentiment_simple():
"""
简化版本的大盘情绪判定
"""
try:
market_index = '000001'
count = 40
close_data = history_bars(market_index, count, '1d', ['close'])
high_data = history_bars(market_index, count, '1d', ['high'])
low_data = history_bars(market_index, count, '1d', ['low'])
if len(close_data) < 36:
return True
# 计算TYP指标
typ = (close_data + high_data + low_data) / 3
# 计算EMA
typma1 = pd.Series(typ).ewm(span=5, adjust=False).mean().values
typma2 = pd.Series(typ).ewm(span=34, adjust=False).mean().values
# 检查条件
if len(typ) >= 2 and len(typma1) >= 1 and len(typma2) >= 1:
return typ[-1] > typ[-2] and typ[-1] > typma1[-1] and typma1[-1] > typma2[-1]
else:
return True
except Exception as e:
print(f"大盘情绪判定出错: {e}")
return True
# 在金字塔中定时调用示例
def on_bar(context):
"""
K线回调函数
"""
# 只在开盘后一段时间内检查(避免开盘波动)
current_time = context.current_dt.strftime('%H%M%S')
if '093000' <= current_time <= '150000':
sentiment_ok = check_market_sentiment_pyramid()
context.market_sentiment = sentiment_ok # 保存状态供其他函数使用
|
|