龙空技术网

如何用Python实现基于技术分析的股票交易策略?

紫夜星辰zsabin 385

前言:

现时大家对“如何用python炒股”大体比较关注,我们都想要学习一些“如何用python炒股”的相关文章。那么小编在网摘上汇集了一些对于“如何用python炒股””的相关内容,希望看官们能喜欢,咱们快快来了解一下吧!

制定以下的交易策略:

当日的收盘价高于前一日的收盘价,且满足以下条件时,买入该股票:

5日均线上穿24日均线;5日均量线上穿60日均量线;MACD指标在0轴上方;当日盘中出现突破平台的情况。

代码示例:

import talibimport tushare as ts# 设置 API 授权信息ts.set_token('YOUR_TOKEN')# 初始化 tusharepro = ts.pro_api()# 获取交易数据df = pro.daily(ts_code='000001.SZ', start_date='20210101', end_date='20220625')# 计算均线和成交量均线df['ma5'] = talib.MA(df['close'], timeperiod=5)df['ma24'] = talib.MA(df['close'], timeperiod=24)df['ma5_vol'] = talib.MA(df['vol'], timeperiod=5)df['ma60_vol'] = talib.MA(df['vol'], timeperiod=60)# 计算 MACD 指标df['macd'], df['signal'], df['hist'] = talib.MACD(df['close'], fastperiod=12, slowperiod=26, signalperiod=9)# 确定买入点buy_signal = (df['close'] > df['close'].shift(1)) \                & (df['ma5'] > df['ma24']) \                & (df['ma5_vol'] > df['ma60_vol']) \                & (df['macd'] > 0) \                & (df['high'] > talib.MAX(df['high'].rolling(window=20).max().shift(1), 20))# 买入信号的日期buy_dates = df.loc[buy_signal, 'trade_date']print(buy_dates)

标签: #如何用python炒股