Skip to content

Commit

Permalink
refactor,more abstract,a lot improvement
Browse files Browse the repository at this point in the history
Former-commit-id: bce9c47
  • Loading branch information
foolcage committed Jun 23, 2020
1 parent f10954e commit bb0f4e3
Show file tree
Hide file tree
Showing 177 changed files with 2,746 additions and 2,907 deletions.
2 changes: 1 addition & 1 deletion README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ wechat Public number (some tutorials would be here):
<img src="./docs/imgs/gongzhonghao.jpg" width="25%" alt="Wechat">

## Thanks
<p><a href=https://www.jetbrains.com/?from=zvt><img src="https://raw.githubusercontent.com/zvtvz/zvt/master/docs/imgs/jetbrains.png" width="50%" alt="jetbrains"></a></p>
<p><a href=https://www.jetbrains.com/?from=zvt><img src="https://raw.githubusercontent.com/zvtvz/zvt/master/docs/imgs/jetbrains.png" width="25%" alt="jetbrains"></a></p>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,4 @@ pycharm导入工程(推荐,你也可以使用其他ide),然后pytest跑测试
https://zhuanlan.zhihu.com/automoney

## Thanks
<p><a href=https://www.jetbrains.com/?from=zvt><img src="https://raw.githubusercontent.com/zvtvz/zvt/master/docs/imgs/jetbrains.png" width="50%" alt="jetbrains"></a></p>
<p><a href=https://www.jetbrains.com/?from=zvt><img src="https://raw.githubusercontent.com/zvtvz/zvt/master/docs/imgs/jetbrains.png" width="25%" alt="jetbrains"></a></p>
2 changes: 0 additions & 2 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# -*- coding: utf-8 -*-
from zvt.recorders.eastmoney.finance.china_stock_balance_sheet_recorder import *
ChinaStockBalanceSheetRecorder
2 changes: 1 addition & 1 deletion examples/factors/block_selector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from zvt.core import IntervalLevel
from zvt.contract import IntervalLevel
from zvt.domain import Block
from zvt.factors.money_flow_factor import BlockMoneyFlowFactor
from zvt.factors.target_selector import TargetSelector
Expand Down
2 changes: 1 addition & 1 deletion examples/factors/composite_selector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from examples.factors.technical_selector import TechnicalSelector
from zvt.core import IntervalLevel
from zvt.contract import IntervalLevel
from zvt.domain import Block
from zvt.factors.money_flow_factor import BlockMoneyFlowFactor
from zvt.factors.target_selector import TargetSelector
Expand Down
2 changes: 1 addition & 1 deletion examples/factors/technical_selector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from zvt.core import IntervalLevel
from zvt.contract import IntervalLevel
from zvt.domain import Stock
from zvt.factors.target_selector import TargetSelector
from zvt.factors.technical_factor import BullFactor
Expand Down
15 changes: 8 additions & 7 deletions examples/recorders/joinquant_data_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from apscheduler.schedulers.background import BackgroundScheduler

from zvt import init_log
from zvt.domain import *
from zvt.informer.informer import EmailInformer
from zvt.domain import *

logger = logging.getLogger(__name__)

Expand All @@ -22,19 +22,20 @@ def record_kdata():
Stock.record_data(provider='joinquant', sleeping_time=1)
StockTradeDay.record_data(provider='joinquant', sleeping_time=1)
Stock1dKdata.record_data(provider='joinquant', sleeping_time=1)
Stock1dHfqKdata.record_data(provider='joinquant', sleeping_time=1)
StockValuation.record_data(provider='joinquant', sleeping_time=1)

email_action.send_message("[email protected]", 'joinquant record kdata finished', '')
break
except Exception as e:
msg = f'joinquant runner error:{e}'
msg = f'joinquant record kdata:{e}'
logger.exception(msg)

email_action.send_message("[email protected]", 'joinquant runner error', msg)
email_action.send_message("[email protected]", 'joinquant record kdata error', msg)
time.sleep(60)


@sched.scheduled_job('cron', hour=18, minute=30)
@sched.scheduled_job('cron', hour=19, minute=00)
def record_others():
while True:
email_action = EmailInformer()
Expand All @@ -43,13 +44,13 @@ def record_others():
Etf.record_data(provider='joinquant', sleeping_time=1)
EtfStock.record_data(provider='joinquant', sleeping_time=1)

email_action.send_message("[email protected]", 'joinquant runner finished', '')
email_action.send_message("[email protected]", 'joinquant record etf finished', '')
break
except Exception as e:
msg = f'joinquant runner error:{e}'
msg = f'joinquant record etf error:{e}'
logger.exception(msg)

email_action.send_message("[email protected]", 'joinquant runner error', msg)
email_action.send_message("[email protected]", 'joinquant record etf error', msg)
time.sleep(60)


Expand Down
2 changes: 1 addition & 1 deletion examples/reports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def risky_company(the_date=to_pd_timestamp(now_time_str()), income_yoy=-0.1, pro

# 高应收,高存货,高商誉
balance_filter = (BalanceSheet.accounts_receivable + BalanceSheet.inventories + BalanceSheet.goodwill) \
> BalanceSheet.total_equity / 2
> BalanceSheet.total_equity
df = BalanceSheet.query_data(entity_ids=entity_ids, start_timestamp=start_timestamp, filters=[balance_filter],
columns=['code'])
if pd_is_not_null(df):
Expand Down
2 changes: 1 addition & 1 deletion examples/reports/report_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from apscheduler.schedulers.background import BackgroundScheduler

from zvt.core import IntervalLevel
from zvt.contract import IntervalLevel
from zvt import init_log
from zvt.domain import Block, BlockMoneyFlow, BlockCategory
from zvt.factors import TargetSelector
Expand Down
2 changes: 1 addition & 1 deletion examples/reports/report_core_compay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from examples.factors.fundamental_selector import FundamentalSelector
from examples.reports import get_subscriber_emails
from zvt.core.api import get_entities
from zvt.contract.api import get_entities
from zvt.utils.time_utils import now_pd_timestamp, to_time_str
from zvt import init_log
from zvt.domain import Stock
Expand Down
2 changes: 1 addition & 1 deletion examples/reports/report_cross_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from apscheduler.schedulers.background import BackgroundScheduler

from zvt.core.api import get_entities
from zvt.contract.api import get_entities
from zvt.utils.time_utils import now_pd_timestamp
from zvt import init_log
from zvt.domain import Stock, StockTradeDay
Expand Down
2 changes: 1 addition & 1 deletion examples/reports/report_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from apscheduler.schedulers.background import BackgroundScheduler

from examples.factors.block_selector import BlockSelector
from zvt.core.api import get_entities
from zvt.contract.api import get_entities
from zvt import init_log
from zvt.domain import Stock1dKdata, BlockStock, Block, StockValuation, Stock
from zvt.factors import TargetSelector
Expand Down
64 changes: 29 additions & 35 deletions examples/reports/report_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from apscheduler.schedulers.background import BackgroundScheduler

from examples.reports import risky_company
from zvt.core.api import get_entities, get_entity_code
from zvt import init_log
from zvt.domain import Stock1dKdata, StockValuation, Stock
from zvt.contract.api import get_entities, get_entity_code
from zvt.domain import Stock1dKdata, Stock
from zvt.factors import TargetSelector
from zvt.factors.ma.ma_factor import VolumeUpMa250Factor
from zvt.factors.ma.ma_factor import ImprovedMaFactor
from zvt.factors.ma.ma_stats import MaStateStatsFactor
from zvt.informer.informer import EmailInformer

Expand All @@ -35,58 +35,52 @@ def report_state():
# 计算均线
my_selector = TargetSelector(start_timestamp='2018-01-01', end_timestamp=target_date)
# add the factors
factor1 = VolumeUpMa250Factor(start_timestamp='2018-01-01', end_timestamp=target_date)
factor1 = ImprovedMaFactor(start_timestamp='2018-01-01', end_timestamp=target_date)

my_selector.add_filter_factor(factor1)

my_selector.run()

long_stocks = my_selector.get_open_long_targets(timestamp=target_date)
stock_map_slope = {}

msg = 'no targets'
# 过滤亏损股
# check StockValuation data
pe_date = target_date - datetime.timedelta(10)
if StockValuation.query_data(start_timestamp=pe_date, limit=1, return_type='domain'):
positive_df = StockValuation.query_data(provider='joinquant', entity_ids=long_stocks,
start_timestamp=pe_date,
filters=[StockValuation.pe > 0],
columns=['entity_id'])
bad_stocks = set(long_stocks) - set(positive_df['entity_id'].tolist())
if bad_stocks:
stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=bad_stocks,
return_type='domain')
info = [f'{stock.name}({stock.code})' for stock in stocks]
msg = '亏损股:' + ' '.join(info) + '\n'
logger.info(long_stocks)

long_stocks = set(positive_df['entity_id'].tolist())
msg = 'no targets'

if long_stocks:
pre_date = target_date - datetime.timedelta(3 * 365)
pre_date = target_date - datetime.timedelta(2 * 365)
ma_state = MaStateStatsFactor(entity_ids=long_stocks, start_timestamp=pre_date,
end_timestamp=target_date, persist_factor=False)
bad_stocks = []
end_timestamp=target_date, need_persist=False)

ma_state.factor_df['slope'] = 100 * ma_state.factor_df['current_pct'] / ma_state.factor_df[
'current_count']

high_stocks = []
for entity_id, df in ma_state.factor_df.groupby(level=0):
if df['current_pct'].max() >= 0.35:
bad_stocks.append(entity_id)
long_stocks.remove(entity_id)
if bad_stocks:
stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=bad_stocks,
if df['current_pct'].max() >= 0.7:
high_stocks.append(entity_id)

stock_map_slope[entity_id] = round(df['slope'].iat[-1], 2)

if high_stocks:
stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=high_stocks,
return_type='domain')
info = [f'{stock.name}({stock.code})' for stock in stocks]
msg = msg + '3年内高潮过:' + ' '.join(info) + '\n'
info = [f'{stock.name}({stock.code})[{stock_map_slope.get(stock.entity_id)}]' for stock in stocks]
msg = msg + '2年内高潮过:' + ' '.join(info) + '\n'

# 过滤风险股
if long_stocks:
risky_codes = risky_company(the_date=target_date, entity_ids=long_stocks)
risky_codes = risky_company(the_date=target_date, entity_ids=long_stocks, income_yoy=-0.8,
profit_yoy=-0.8)

if risky_codes:
long_stocks = [entity_id for entity_id in long_stocks if
get_entity_code(entity_id) not in risky_codes]

stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=risky_codes,
stocks = get_entities(provider='joinquant', entity_schema=Stock, codes=risky_codes,
return_type='domain')
info = [f'{stock.name}({stock.code})' for stock in stocks]
info = [f'{stock.name}({stock.code})[{stock_map_slope.get(stock.entity_id)}]' for stock in stocks]
msg = msg + '风险股:' + ' '.join(info) + '\n'
if long_stocks:
stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=long_stocks,
Expand All @@ -104,8 +98,8 @@ def report_state():
email_action.send_message("[email protected]", f'report state error',
'report state error:{}'.format(e))

info = [f'{stock.name}({stock.code})' for stock in stocks]
msg = msg + '盈利股:' + ' '.join(info) + '\n'
info = [f'{stock.name}({stock.code})[{stock_map_slope.get(stock.entity_id)}]' for stock in stocks]
msg = msg + '选中:' + ' '.join(info) + '\n'

logger.info(msg)
email_action.send_message('[email protected]', f'{target_date} 放量突破年线state选股结果', msg)
Expand Down
2 changes: 1 addition & 1 deletion examples/reports/report_vol_up250.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from apscheduler.schedulers.background import BackgroundScheduler

from examples.reports import get_subscriber_emails
from zvt.core.api import get_entities
from zvt.contract.api import get_entities
from zvt import init_log
from zvt.domain import Stock, Stock1dKdata, StockValuation
from zvt.factors.ma.ma_factor import VolumeUpMa250Factor
Expand Down
31 changes: 0 additions & 31 deletions examples/trader/coin_traders.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/trader/stock_traders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from zvt.core import IntervalLevel
from zvt.contract import IntervalLevel
from zvt.factors.ma.ma_factor import CrossMaFactor
from zvt.factors.target_selector import TargetSelector
from zvt.factors.technical_factor import BullFactor
Expand All @@ -16,7 +16,7 @@ def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_time
myselector.add_filter_factor(
CrossMaFactor(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges,
codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp,
windows=[5, 10], persist_factor=False))
windows=[5, 10], need_persist=False))

self.selectors.append(myselector)

Expand Down
2 changes: 1 addition & 1 deletion init_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if ! which virtualenv > /dev/null; then
fi

if [ ! -d "$BASEDIR/ve" ]; then
virtualenv -p python3 $BASEDIR/ve
virtualenv -p python3 $BASEDIR/ve --no-download
echo "Virtualenv created."
fi

Expand Down
13 changes: 7 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
requests == 2.20.1
SQLAlchemy == 1.2.14
pandas == 0.24.2
pandas == 0.25.3
arrow == 0.11.0
tzlocal == 1.5.1
xlrd == 1.1.0
apscheduler == 3.4.0
# TA-Lib
jqdatasdk == 1.7.8
jqdatasdk == 1.8.1
demjson == 2.2.4
marshmallow-sqlalchemy == 0.19.0
marshmallow-sqlalchemy == 0.23.1
marshmallow == 3.2.2
plotly==4.1.0
plotly==4.7.1
dash==1.1.1
simplejson==3.16.0
html5lib == 1.0.1
schedule == 0.6.0
schedule == 0.6.0
pluggy
15 changes: 10 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
# Always prefer setuptools over distutils
from setuptools import setup, find_packages

try:
# for pip >= 10
from pip._internal.req import parse_requirements
except ImportError:
# for pip <= 9.0.3
from pip.req import parse_requirements

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
Expand All @@ -16,6 +23,8 @@
# Arguments marked as "Required" below must be included for upload to PyPI.
# Fields marked as "Optional" may be commented out.

requirements = [str(ir.req) for ir in parse_requirements("requirements.txt", session=False)]

setup(
# This is the name of your project. The first time you publish this
# package, this name will be registered for you. It will determine how
Expand Down Expand Up @@ -119,11 +128,7 @@
# For an analysis of "install_requires" vs pip's requirements files see:
# https://packaging.python.org/en/latest/requirements.html

install_requires=['zvdata>=1.2.3', 'requests>=2.20.1', 'SQLAlchemy>=1.2.14', 'pandas>=0.24.2',
'arrow>=0.11.0', 'marshmallow >= 3.2.2', 'tzlocal>=1.5.1', 'xlrd>=1.1.0', 'apscheduler>=3.4.0',
'jqdatasdk', 'demjson>=2.2.4', 'marshmallow-sqlalchemy>=0.19.0', 'ccxt>=1.17.191',
'plotly>=4.1.0', 'simplejson>=3.16.0',
'html5lib>=1.0.1', 'schedule>=0.6.0'], # Optional
install_requires=requirements, # Optional

# List additional groups of dependencies here (e.g. development
# dependencies). Users will be able to install these using the "extras"
Expand Down
6 changes: 3 additions & 3 deletions tests/api/test_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from zvt.core import IntervalLevel
from zvt.api import get_kdata
from zvt.api.common import to_high_level_kdata, get_recent_report_date
from zvt.contract import IntervalLevel
from zvt.api.quote import get_kdata
from zvt.api.quote import to_high_level_kdata, get_recent_report_date
from ..context import init_test_context

init_test_context()
Expand Down
Loading

0 comments on commit bb0f4e3

Please sign in to comment.