1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| import streamlit as st import akshare as ak import pandas as pd import talib # 导入TA-Lib库 import os import requests from datetime import datetime
# 获取当前的环境变量 proxy_env_vars = {'HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy'} current_proxies = {k: v for k, v in os.environ.items() if k in proxy_env_vars}
# 清除环境变量中的代理设置 for var in proxy_env_vars: os.environ.pop(var, None)
# 获取所有主力连续合约的期货代码 futures_codes = ak.futures_display_main_sina() futures_list = futures_codes["symbol"].tolist()
# Streamlit App st.title("期货技术分析面板")
# 在侧边栏添加技术指标选择 with st.sidebar: st.header("技术指标配置")
# 添加技术指标复选框 st.subheader("选择技术指标") calc_macd = st.checkbox("MACD", value=True) calc_bollinger = st.checkbox("布林带", value=True) calc_rsi = st.checkbox("RSI", value=True)
# 指标参数设置 st.subheader("指标参数") rsi_period = st.slider("RSI周期", 5, 30, 14) macd_fast = st.slider("MACD快线周期", 5, 20, 12) macd_slow = st.slider("MACD慢线周期", 15, 40, 26) bollinger_period = st.slider("布林带周期", 10, 50, 20)
# 期货合约选择 selected_contract = st.sidebar.selectbox("选择期货合约代码", futures_list)
# 用户选择日期范围 start_date = st.sidebar.date_input("选择开始日期", datetime(2023, 1, 1)) end_date = st.sidebar.date_input("选择截止日期", datetime(2023, 12, 31))
# 从akshare获取期货行情数据 futures_data = ak.futures_main_sina(symbol=selected_contract, start_date=start_date.strftime("%Y%m%d"), end_date=end_date.strftime("%Y%m%d"))
# 重命名列以便处理(中文列名可能导致问题) futures_data = futures_data.rename(columns={ "日期": "date", "开盘价": "open", "最高价": "high", "最低价": "low", "收盘价": "close", "成交量": "volume" })
# 确保数据类型正确 futures_data['close'] = pd.to_numeric(futures_data['close'], errors='coerce') futures_data = futures_data.dropna(subset=['close'])
# 技术指标计算 if calc_macd: # 计算MACD指标[3,7](@ref) macd, macd_signal, macd_hist = talib.MACD( futures_data['close'], fastperiod=macd_fast, slowperiod=macd_slow, signalperiod=9 ) futures_data['MACD'] = macd futures_data['MACD_signal'] = macd_signal futures_data['MACD_hist'] = macd_hist
if calc_bollinger: # 计算布林带指标[6,7](@ref) upper, middle, lower = talib.BBANDS( futures_data['close'], timeperiod=bollinger_period, nbdevup=2, nbdevdn=2, matype=0 # 0表示简单移动平均 ) futures_data['BB_upper'] = upper futures_data['BB_middle'] = middle futures_data['BB_lower'] = lower
if calc_rsi: # 计算RSI指标[3,7](@ref) rsi = talib.RSI(futures_data['close'], timeperiod=rsi_period) futures_data['RSI'] = rsi
# 显示所选的期货合约代码和日期范围 st.write(f"您选择了期货合约代码: {selected_contract}") st.write(f"您选择的日期范围是从 {start_date} 到 {end_date}")
# 预览数据 st.subheader("期货行情数据与技术指标") st.dataframe(futures_data)
# 显示技术指标图表 st.subheader("技术指标可视化")
if calc_macd and 'MACD' in futures_data.columns: st.line_chart(futures_data[['MACD', 'MACD_signal']], use_container_width=True) st.bar_chart(futures_data['MACD_hist'], use_container_width=True)
if calc_bollinger and 'BB_upper' in futures_data.columns: st.line_chart(futures_data[['close', 'BB_upper', 'BB_middle', 'BB_lower']], use_container_width=True)
if calc_rsi and 'RSI' in futures_data.columns: st.line_chart(futures_data['RSI'], use_container_width=True) st.area_chart(futures_data['RSI'], use_container_width=True)
|