Skip to content

Commit

Permalink
add tradingview sectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ged0 committed Mar 12, 2021
1 parent 26cc347 commit f788fa6
Show file tree
Hide file tree
Showing 12 changed files with 4,987 additions and 82 deletions.
3 changes: 2 additions & 1 deletion a1chemy/data_source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .xueqiu import *
from .database import *
from .sina_finance import *
from .jisilu import *
from .jisilu import *
from .tradingview import *
3 changes: 2 additions & 1 deletion a1chemy/data_source/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .mongo_ticks import MongoTicks
from .mongo_tags import MongoTags
from .mongo_fund import MongoFund
from .mongo_fund import MongoFund
from .mongo_client_factory import ticks_client, tags_client, fund_client
19 changes: 19 additions & 0 deletions a1chemy/data_source/database/mongo_client_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pymongo
from .mongo_ticks import MongoTicks
from .mongo_tags import MongoTags
from .mongo_fund import MongoFund
default_mongo_client = pymongo.MongoClient(
"mongodb://localhost:27017/", username='a1chemy', password='1B2C9046-E3CC-447F-9961-E125759BA44F')


def mongo_client_factory():
return default_mongo_client

def tags_client():
return MongoTags(mongo_client=mongo_client_factory())

def fund_client():
return MongoFund(mongo_client=mongo_client_factory())

def ticks_client():
return MongoTicks(mongo_client=mongo_client_factory())
36 changes: 36 additions & 0 deletions a1chemy/data_source/tradingview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import a1chemy.data_source as data_source
from a1chemy.common import Tag

import pandas as pd


class TradingViewClient(object):

def __init__(self) -> None:
super().__init__()
self.mongo_ticks_client = data_source.ticks_client()
self.mongo_tag_client = data_source.tags_client()

def update_sectors(self, path):
sector_data_frame = pd.read_csv(path)
sectors_root_tag = Tag(id='tradingview_sectors',
parent=None, values={})
self.mongo_tag_client.delete_by_parent(parent=sectors_root_tag.id)
trading_view_tree = self.mongo_tag_client.tree(id=sectors_root_tag.id)
for sectors, stocks in trading_view_tree.root.children.items():
for key, value in stocks.children.items():
self.mongo_tag_client.delete_by_parent(key)
self.mongo_tag_client.insert(tag=sectors_root_tag)
for index, row in sector_data_frame.iterrows():
exchange = 'SZ' if row['Exchange'] == 'SZSE' else 'SH'
symbol = exchange + '{:06d}'.format(row['Ticker'])
sector = row['Sector']

sector_tag = Tag(id=sector, parent=sectors_root_tag.id, values={})
self.mongo_tag_client.insert(tag=sector_tag)

id = exchange + '_' + symbol
parent = sector_tag.id
values = {'exchange': exchange, 'symbol': symbol}
stock_tag = Tag(id=id, parent=parent, values=values)
self.mongo_tag_client.insert(tag=stock_tag)
21 changes: 20 additions & 1 deletion notebook/china_stock_market_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,26 @@
"parse_sci_to_mongo(url = 'http://www.csindex.com.cn/zh-CN/indices/index-detail/000906',\n",
" xls_path='/Users/line/workspace/freedom/doc/000906cons.xls',\n",
" json_file_path = '/Users/line/workspace/freedom/A1chemy/data/csi_800.json',\n",
" index_tag_id = 'csi800')"
" index_tag_id = 'csi800')\n",
"\n",
"#http://www.csindex.com.cn/zh-CN/indices/index-detail/000016\n",
"parse_sci_to_mongo(url = 'http://www.csindex.com.cn/zh-CN/indices/index-detail/000016',\n",
" xls_path='/Users/line/workspace/freedom/doc/000016cons.xls',\n",
" json_file_path = '/Users/line/workspace/freedom/A1chemy/data/sse_50.json',\n",
" index_tag_id = 'sse50')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### tradingview上的行业分类\n",
"import a1chemy.data_source as data_source\n",
"\n",
"tradingViewClient = data_source.TradingViewClient()\n",
"tradingViewClient.update_sectors(path='/Users/line/Downloads/china_2021-03-12.csv')"
]
}
],
Expand Down
196 changes: 166 additions & 30 deletions notebook/hunter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,163 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e7f0c0e29e7d4a54a343ef5ab68ca90c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=16.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2f4601d6faf44b448e15f1b49559482e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=38.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "eddff3e2a7474cd29da563d029004374",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=42.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"get SH517000 shares failed\n",
"get SZ160222 shares failed\n",
"get SZ163402 shares failed\n",
"get SZ163406 shares failed\n",
"get SZ161005 shares failed\n",
"get SZ162703 shares failed\n",
"get SZ161725 shares failed\n",
"get SZ163415 shares failed\n",
"get SZ162605 shares failed\n",
"get SZ163417 shares failed\n",
"get SH513550 shares failed\n",
"get SH513050 shares failed\n",
"get SH501050 shares failed\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c7b0d329e14441acba263b54655e5e58",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=46.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "294f92109a464ac491dfde28e878bd8f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=300.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0987bdc25092413aafb9e2fd378f106a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=50.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f95ea2be177a4e06a3dfa9ac1ec5b401",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Tab(children=(Output(), Output(), Output(), Output(), Output(), Output()), _titles={'0': 'CashCow', '1': 'TOP'…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import a1chemy.data_source as data_source\n",
"import a1chemy.indicators as indicators\n",
Expand All @@ -21,7 +175,7 @@
"import pandas as pd\n",
"from tqdm.notebook import tqdm\n",
"import base64\n",
"\n",
"import ipywidgets as widgets\n",
"# data \n",
"def return_rate(t, day1, day2):\n",
" length = t.raw_data.shape[0]\n",
Expand Down Expand Up @@ -97,40 +251,22 @@
"\n",
" # df.style\n",
" percent_list = list(df)[3:-2]\n",
" return df.style.format(\"{:.2f}\", subset=percent_list).applymap(percent_color, subset=percent_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"show('CASH_COW')\n",
"show('TOP')\n",
"show('ZH_ETF', extend={'shares': shares_image_html})\n",
"show('ZH_STOCK')\n",
"show('csi300')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
" return df.style.format(\"{:.2f}\", subset=percent_list).applymap(percent_color, subset=percent_list)\n",
"\n",
"# show('CASH_COW')\n",
"# show('TOP')\n",
"# show('ZH_ETF', extend={'shares': shares_image_html})\n",
"# show('ZH_STOCK')\n",
"# show('csi300')\n",
"\n",
"def setup_ui(df):\n",
" out = widgets.Output()\n",
" with out:\n",
" display(df)\n",
" return out\n",
"\n",
"titles = ['CashCow', 'TOP', 'ZH_ETF', 'ZH_STOCK', '沪深300']\n",
"dataframes = [show('CASH_COW'), show('TOP'), show('ZH_ETF', extend={'shares': shares_image_html}), show('ZH_STOCK'), show('csi300')]\n",
"titles = ['CashCow', 'TOP', 'ZH_ETF', 'ZH_STOCK', '沪深300', '上证50']\n",
"dataframes = [show('CASH_COW'), show('TOP'), show('ZH_ETF', extend={'shares': shares_image_html}), show('ZH_STOCK'), show('csi300'), show('sse50')]\n",
"outputs = [setup_ui(df) for df in dataframes]\n",
"tab = widgets.Tab()\n",
"tab.children = outputs\n",
Expand Down
17 changes: 14 additions & 3 deletions notebook/market_breath.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@
"import pymongo\n",
"from IPython.core.display import display, HTML\n",
"display(HTML(\"<style>.container { width:100% !important; }</style>\"))\n",
"def magnify():\n",
" width = '80px'\n",
" return [dict(selector=\"th\",\n",
" props=[('max-width', width), ('width', width), ('word-wrap', 'break-word'), ('text-align', 'center')]),\n",
" dict(selector=\"td\",\n",
" props=[('max-width', width), ('width', width), ('text-align', 'center')]),\n",
" dict(selector=\"tr\",\n",
" props=[('max-width', width), ('width', width), ('text-align', 'center')])\n",
"]\n",
"\n",
"mongo_client = pymongo.MongoClient(\"mongodb://localhost:27017/\", username='a1chemy', password='1B2C9046-E3CC-447F-9961-E125759BA44F')\n",
"mongo_ticks_client = data_source.MongoTicks(mongo_client)\n",
"mongo_tags_client = data_source.MongoTags(mongo_client=mongo_client)\n",
"\n",
"# sectors data\n",
"sectors_list = a1.read_json_from_file('/Users/line/workspace/freedom/A1chemy/data/sw_sector.json')\n",
"swsindex_tag_tree = mongo_tags_client.tree(id='swsindex')\n",
"swsindex_tag_tree = mongo_tags_client.tree(id='tradingview_sectors')\n",
"print('swsindex_tag_tree.len({})'.format(len(swsindex_tag_tree.index_map)))\n",
"stock_sector_map = {}\n",
"sectors = set()\n",
"\n",
Expand All @@ -36,7 +46,7 @@
" sectors.add(key)\n",
" for id, stock in value.children.items():\n",
" stock_sector_map[stock.values['symbol']] = key\n",
"\n",
"print('stock_sector_map len({})'.format(len(stock_sector_map)))\n",
"\n",
"stocks_tree = mongo_tags_client.tree(id='csi300')\n",
"sci_300_sector_map = {}\n",
Expand Down Expand Up @@ -136,7 +146,8 @@
"market_breath_column_name_list.append(\"ALL\")\n",
"market_breath_data_frame=pd.DataFrame(market_breath_show_data, columns=market_breath_column_name_list)\n",
"\n",
"market_breath_data_frame.style.applymap(market_breath_color, subset=market_breath_column_name_list[1:])\n",
"\n",
"market_breath_data_frame.style.applymap(market_breath_color, subset=market_breath_column_name_list[1:]).set_table_styles(magnify())\n",
" "
]
}
Expand Down
Loading

0 comments on commit f788fa6

Please sign in to comment.