Skip to content

Commit

Permalink
Merge pull request #817 from yutiansut/master
Browse files Browse the repository at this point in the history
一些代码更新 DataStruct的generator考虑修改模式
  • Loading branch information
yutiansut authored Sep 24, 2018
2 parents 8707d6a + 9148d7b commit 7ff1516
Show file tree
Hide file tree
Showing 36 changed files with 112 additions and 11,544 deletions.
2 changes: 1 addition & 1 deletion Documents/backendapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ http://[ip]:[port]/marketdata/future/min?code=[]&start=[]&end=[]


```
http://[ip]:[port]/accounts
http://[ip]:[port]/accounts/all
```

#### 4.5.2 单个账户查询
Expand Down
16 changes: 12 additions & 4 deletions QUANTAXIS/QAARP/QAAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(self, strategy_name=None, user_cookie=None, portfolio_cookie=None,
self.frequence = frequence
self.running_environment = running_environment
########################################################################
self.market_data = None
self._market_data = None
self._currenttime = None
self.commission_coeff = commission_coeff
self.tax_coeff = tax_coeff
Expand Down Expand Up @@ -295,6 +295,10 @@ def end_date(self):
raise RuntimeWarning(
'QAACCOUNT: THIS ACCOUNT DOESNOT HAVE ANY TRADE')

@property
def market_data(self):
return self._market_data

@property
def trade_range(self):
return QA_util_get_trade_range(self.start_date, self.end_date)
Expand Down Expand Up @@ -930,13 +934,17 @@ def run(self, event):
"""update the market_data
1. update the inside market_data struct
2. tell the on_bar methods
# 这样有点慢
"""

self._currenttime = event.market_data.datetime[0]
if self.market_data is None:
self.market_data = event.market_data
if self._market_data is None:
self._market_data = event.market_data
else:
self.market_data = self.market_data + event.market_data
self._market_data = self._market_data + event.market_data
self.on_bar(event)

if event.callback:
Expand Down
2 changes: 1 addition & 1 deletion QUANTAXIS/QAAnalysis/QAAnalysis_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __repr__(self):
@lru_cache()
def market_data(self):
return QA_quotation(self.code, self.start, self.end, self.frequence,
market=MARKET_TYPE.STOCK_CN, source=DATASOURCE.MONGO, output=OUTPUT_FORMAT.DATASTRUCT)
market=MARKET_TYPE.STOCK_CN, source=DATASOURCE.MONGO, output=OUTPUT_FORMAT.DATASTRUCT).to_qfq()

@property
@lru_cache()
Expand Down
2 changes: 0 additions & 2 deletions QUANTAXIS/QAData/QADataStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
from QUANTAXIS.QAData.base_datastruct import _quotation_base
from QUANTAXIS.QAData.data_fq import QA_data_stock_to_fq
from QUANTAXIS.QAData.data_resample import QA_data_tick_resample, QA_data_day_resample, QA_data_min_resample
from QUANTAXIS.QAData.proto import stock_day_pb2 # protobuf import
from QUANTAXIS.QAData.proto import stock_min_pb2
from QUANTAXIS.QAIndicator import EMA, HHV, LLV, SMA
from QUANTAXIS.QAUtil import (DATABASE, QA_util_log_info,
QA_util_random_with_topic,
Expand Down
73 changes: 68 additions & 5 deletions QUANTAXIS/QAData/dsmethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
"""DataStruct的方法
"""
import pandas as pd

import numpy as np
from QUANTAXIS.QAData.QADataStruct import (QA_DataStruct_Index_day,
QA_DataStruct_Index_min,
QA_DataStruct_Future_day,
QA_DataStruct_Future_min,
QA_DataStruct_Stock_day,
QA_DataStruct_Stock_min)
from QUANTAXIS.QAUtil.QAParameter import FREQUENCE, MARKET_TYPE


def concat(lists):
Expand All @@ -48,6 +51,66 @@ def concat(lists):
return lists[0].new(pd.concat([lists.data for lists in lists]).drop_duplicates())


def datastruct_formater(data, frequence=FREQUENCE.DAY, market_type=MARKET_TYPE.STOCK_CN, default_header=[]):
"""一个任意格式转化为DataStruct的方法
Arguments:
data {[type]} -- [description]
Keyword Arguments:
frequence {[type]} -- [description] (default: {FREQUENCE.DAY})
market_type {[type]} -- [description] (default: {MARKET_TYPE.STOCK_CN})
default_header {list} -- [description] (default: {[]})
Returns:
[type] -- [description]
"""

if isinstance(data, list):
try:
res = pd.DataFrame(data, columns=default_header)
if frequence is FREQUENCE.DAY:
if market_type is MARKET_TYPE.STOCK_CN:
return QA_DataStruct_Stock_day(
res.assign(date=pd.to_datetime(res.date)).set_index(
['date', 'code'], drop=False),
dtype='stock_day')
elif frequence in [FREQUENCE.ONE_MIN, FREQUENCE.FIVE_MIN, FREQUENCE.FIFTEEN_MIN, FREQUENCE.THIRTY_MIN, FREQUENCE.SIXTY_MIN]:
if market_type is MARKET_TYPE.STOCK_CN:
return QA_DataStruct_Stock_min(
res.assign(datetime=pd.to_datetime(res.datetime)).set_index(
['datetime', 'code'], drop=False),
dtype='stock_min')
except:
pass
elif isinstance(data, np.ndarray):
try:
res = pd.DataFrame(data, columns=default_header)
if frequence is FREQUENCE.DAY:
if market_type is MARKET_TYPE.STOCK_CN:
return QA_DataStruct_Stock_day(
res.assign(date=pd.to_datetime(res.date)).set_index(
['date', 'code'], drop=False),
dtype='stock_day')
elif frequence in [FREQUENCE.ONE_MIN, FREQUENCE.FIVE_MIN, FREQUENCE.FIFTEEN_MIN, FREQUENCE.THIRTY_MIN, FREQUENCE.SIXTY_MIN]:
if market_type is MARKET_TYPE.STOCK_CN:
return QA_DataStruct_Stock_min(
res.assign(datetime=pd.to_datetime(res.datetime)).set_index(
['datetime', 'code'], drop=False),
dtype='stock_min')
except:
pass

elif isinstance(data, pd.DataFrame):
index = data.index
if isinstance(index, pd.MultiIndex):
pass
elif isinstance(index, pd.DatetimeIndex):
pass
elif isinstance(index, pd.Index):
pass


def from_tushare(dataframe, dtype='day'):
"""dataframe from tushare
Expand All @@ -71,7 +134,6 @@ def QDS_StockDayWarpper(func):
def warpper(*args, **kwargs):
data = func(*args, **kwargs)


if isinstance(data.index, pd.MultiIndex):

return QA_DataStruct_Stock_day(data)
Expand All @@ -94,6 +156,7 @@ def warpper(*args, **kwargs):
return QA_DataStruct_Stock_min(data.assign(datetime=pd.to_datetime(data.datetime)).set_index(['datetime', 'code'], drop=False), dtype='stock_min')
return warpper


def QDS_IndexDayWarpper(func, *args, **kwargs):
"""
指数日线QDS装饰器
Expand Down Expand Up @@ -124,9 +187,9 @@ def warpper(*args, **kwargs):
return warpper


if __name__ =='__main__':
if __name__ == '__main__':
"""演示QDS装饰器
Returns:
[type] -- [description]
"""
Expand All @@ -142,7 +205,7 @@ def warpper(*args, **kwargs):
"""

import tushare as ts
print(from_tushare(ts.get_k_data('000001','2018-01-01','2018-06-26')))
print(from_tushare(ts.get_k_data('000001', '2018-01-01', '2018-06-26')))

"""[summary]
"""
Loading

0 comments on commit 7ff1516

Please sign in to comment.