Skip to content

Commit

Permalink
improve rest
Browse files Browse the repository at this point in the history
  • Loading branch information
foolcage committed Jul 26, 2018
1 parent d6ba4c3 commit bbfa8d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions fooltrader/api/esapi/esapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def es_get_kdata(security_item, exchange=None, the_date=None, start_date=None, end_date=None, level='day', fields=None,
from_idx=0, size=10):
from_idx=0, size=10, csv=False):
"""
get kdata.
Expand Down Expand Up @@ -70,7 +70,7 @@ def es_get_kdata(security_item, exchange=None, the_date=None, start_date=None, e

resp = s[from_idx:from_idx + size].execute()

return es_resp_to_payload(resp)
return es_resp_to_payload(resp, csv)


if __name__ == '__main__':
Expand Down
8 changes: 6 additions & 2 deletions fooltrader/rest/tech.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ def get_kdata(securityid):
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
level = request.args.get('level', 'day')

fields = request.args.get('fields')
if not fields:
fields = ['open', 'high', 'low', 'close', 'volume']

from_idx = request.args.get('from_idx', 0)
size = request.args.get('size', 10)

result = esapi.es_get_kdata(security_item=securityid, the_date=the_date, start_date=start_date,
end_date=end_date,
level=level, fields=fields, from_idx=int(from_idx), size=int(size))
end_date=end_date, fields=fields, csv=True,
level=level, from_idx=int(from_idx), size=int(size))

return success(result)
4 changes: 3 additions & 1 deletion fooltrader/utils/es_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ def es_index_mapping(index_name, doc_type, force=False):
index.upgrade()


def es_resp_to_payload(resp):
def es_resp_to_payload(resp, csv):
datas = [hit['_source'].to_dict() for hit in resp['hits']['hits']]
if csv:
datas = [[data['open'], data['high'], data['low'], data['close'], data['volume']] for data in datas]
return {
'total': resp['hits']['total'],
'data': datas
Expand Down

0 comments on commit bbfa8d2

Please sign in to comment.