原创,如有转载,请注明出处!!!
A股市场上,一轮大行情的开启,往往由主力资金来主导,从实时盘口来说,最直接的指标则是个股异动次数,然而单指标并不能直接反馈出主力资金的流动方向,因此需要结合:主力资金净额来分析。
盘口异动数据来源:http://quotation.eastmoney.com/changes【东方财富盘口异动】- 爬虫获取
主力资金数据来源:https://tushare.pro/【Tushare金融大数据社区】- 接口调用
爬虫获取东方财富盘口异动:
# -*- coding: utf-8 -*- import requests import json import pymysql import time import datetime import warnings # 东方财富:个股盘口异动数据 def eastmoney_yd_spider(current): print('东财异动正在获取%s数据' % current) # 1、构造param param_list = [] for page in range(0, 600): param = {"pageindex": page, "pagesize": '64', "ut": '7eea3edcaed734bea9cbfc24409ed989', "dpt": 'wzchanges'} param_list.append(param) # 2、抓取网页数据 content_list = [] header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3676.400 QQBrowser/10.5.3738.400"} url = "http://push2ex.eastmoney.com/getAllStockChanges?type=8201,8202,8193,4,32,64,8207,8209,8211,8213,8215,8204,8203,8194,8,16,128,8208,8210,8212,8214,8216" for param in param_list: content = json.loads(session.get(url=url, params=param, headers=header).text) print(content) if content['data'] is None: break else: content_list.append(content) # 3、网页数据解析 content_list_data = [] for content in content_list: allstock = content['data']['allstock'] for stock in allstock: stk_code = stock['c'] # 股票代码 stk_name = stock['n'] # 股票名称 chg_time = stock['tm'] # 异动时间 chg_type = stock['t'] # 异动类型 if chg_type in ('8201','8202','8207','8209','8211','8215','8204','8203','8208','8210','8212','8216'): chg_value = stock['i'] * 100 # 异动值 elif chg_type in ('8193','8194','128','64'): chg_value = stock['i'] / 10000 # 异动值 else: chg_value = stock['i'] # 异动值 content_list_data.append((current, stk_code, stk_name, chg_time, chg_type, chg_value)) # 4、网页数据存储 conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='quant', charset='utf8mb4') cursor = conn.cursor() try: content_list_sql = '''insert ignore into yd_detail(trade_date,stk_code,stk_name,chg_time,chg_type,chg_value) values(%s,%s,%s,%s,%s,%s)''' cursor.executemany(content_list_sql, content_list_data) except Exception as error: print(error) else: print('数据写入成功') conn.commit() cursor.close() conn.close() if __name__ == '__main__': print('程序开始执行') print('------------------------------------------------------------------------------------------') t1 = time.time() current = time.strftime("%Y%m%d", time.localtime()) session = requests.Session() eastmoney_yd_spider(current) # 东财盘口异动 t2 = time.time() print('本次共耗费%.2f' % (t2 - t1)) print('程序执行完毕') print('------------------------------------------------------------------------------------------')
获取主力资金动向:
# -*- coding: utf-8 -*- import pandas as pd import tushare as ts import pymysql import time import datetime from retry import retry import warnings warnings.filterwarnings("ignore") # ------------------------------------------------------------------------------------------------------------- # tushare:大单资金 @retry(tries=-1, delay=5) def big_moneyflow(current): print('%s大单资金数据正在抓取' % current) # 1、接口数据请求 big_money = pd.DataFrame() for offset in range(0, 6000, 3000): data_1 = pro.moneyflow(trade_date=current, fields='ts_code,trade_date,buy_lg_amount,sell_lg_amount,buy_elg_amount,sell_elg_amount', offset=offset, limit=3000) big_money = big_money.append(data_1, ignore_index=True) big_money = big_money.rename(columns={'ts_code': 'stk_code'}) big_money['stk_code'] = big_money['stk_code'].apply(lambda x: x[:6]) print(big_money) # 2、接口数据解析 big_money_data = [] for index, row in big_money.iterrows(): big_money_data.append((row['trade_date'], row['stk_code'], row['buy_lg_amount'], row['sell_lg_amount'], row['buy_elg_amount'], row['sell_elg_amount'])) # 3、接口数据mysql存储 conn = conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='quant', charset='utf8mb4') cursor = conn.cursor() try: big_money_sql = '''insert ignore into big_moneyflow(trade_date,stk_code,buy_lg_amount,sell_lg_amount,buy_elg_amount,sell_elg_amount) values(%s,%s,%s,%s,%s,%s)''' cursor.executemany(big_money_sql, big_money_data) except Exception as error: print(error) else: print('数据写入成功') conn.commit() cursor.close() conn.close() if __name__ == '__main__': print('程序开始执行') print('------------------------------------------------------------------------------------------') t1 = time.time() pro = ts.pro_api('set your token') current = time.strftime("%Y%m%d", time.localtime()) big_moneyflow(current) # 大资金流向 t2 = time.time() print('本次共耗费%.2f' % (t2 - t1)) print('程序执行完毕') print('------------------------------------------------------------------------------------------')
Tableau构建可视化看板:
从下图,我们很容易看出,主力资金主要扎堆在大消费板块,资金成交最为活跃板块是:华为概念、芯片和5G。