为了做量化分析需要把获取的数据存入mysql,这样方便处理数据:
直接给上代码,下面看部分代码分享:
计算出一字板存入:
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine
import pymysql
ts.set_token('youtoken')
pro = ts.pro_api()
day = input('请输入要查询的日期(例如20220101):')
tatle = '每日一字涨停'
name = '一字'+ day
df = pro.bak_daily(trade_date=day, fields='trade_date,ts_code,name,industry,pct_change,close,open,avg_price,vol_ratio,turn_over,vol,selling,buying,total_share,float_share,total_mv,high,low,pre_close')
df.columns = ('股票代码','交易日期','股票名称','涨跌幅','收盘价','开盘价','最高价','最低价','昨日收盘价','量比','换手率','成交量','内盘(主动卖,手)','外盘(主动买,手)','总股本(亿)','流通股本(亿)','所属行业','总市值','平均价')
def sum(a,b): # 条件函数
sums = a-b
return sums
df['开收盘价差'] = df.apply(lambda row:sum(row['收盘价'],row['开盘价']),axis=1)
df['高低价差'] = df.apply(lambda row:sum(row['最高价'],row['最低价']),axis=1)
df = df[df['开收盘价差']==0]
df = df[df['高低价差']==0]
df = df[df['涨跌幅']>9]
# df = df.set_index('股票代码')
engine = create_engine(f"mysql+pymysql://root:pi@192.168.2.100:3306/