Skip to content

Commit 26e7f04

Browse files
committed
add backtesting
1 parent 1976156 commit 26e7f04

2 files changed

Lines changed: 157 additions & 1 deletion

File tree

Python/xapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def get_api_version(self):
6464
func.restype = c_char_p
6565
func.argtypes = [c_void_p]
6666
ptr = func(self.p_fun)
67-
return ptr.value
67+
return ptr
6868

6969
def get_api_name(self):
7070
self.invoke_log('on_invoke_get_api_name')

Python/xapi_backtesting.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
from base.abstract_xapi import AbstractXApi
4+
from base.comm import *
5+
6+
__author__ = 'Chunyou<[email protected]>'
7+
8+
os.environ['PATH'] = ';'.join([os.path.dirname(__file__) + "\\include",
9+
os.path.dirname(__file__) + "\\include\\LTS\\win32",
10+
os.environ['PATH']])
11+
12+
13+
class XApiBacktesting(AbstractXApi):
14+
def __init__(self, lib_path, is_market=True):
15+
super(XApiBacktesting, self).__init__("QuantBox_XAPI", lib_path, is_market)
16+
17+
def init(self):
18+
"""
19+
load the lib
20+
:return:
21+
"""
22+
self.invoke_log('on_invoke_init')
23+
return True
24+
25+
def get_last_error(self):
26+
self.invoke_log('on_invoke_get_last_error')
27+
28+
def get_api_type(self):
29+
self.invoke_log('on_invoke_get_api_type')
30+
return MarketData
31+
32+
def get_api_version(self):
33+
self.invoke_log('on_invoke_get_api_version')
34+
return "0.1.0"
35+
36+
def get_api_name(self):
37+
self.invoke_log('on_invoke_get_api_name')
38+
return "XApi Backtesing"
39+
40+
def connect(self, path, server_info, user_info, count=0):
41+
"""
42+
Connect to the server.
43+
:param path:
44+
:param server_info:
45+
:param user_info:
46+
:param count:
47+
:return:
48+
"""
49+
self.invoke_log('on_invoke_connect', path=path, server_info=server_info, user_info=user_info, count=count)
50+
user_login_field = RspUserLoginField()
51+
user_login_field.TradingDay = '20150120'
52+
user_login_field.SessionID = '565656565'
53+
user_login_field.LoginTime = '07:22:07'
54+
self.make_response(OnConnectionStatus, p_api2=self, double1=ord(Logined.value), ptr1=user_login_field)
55+
self.make_response(OnConnectionStatus, p_api2=self, double1=ord(Done.value))
56+
57+
def disconnect(self):
58+
self.invoke_log('on_invoke_disconnect')
59+
60+
def subscribe(self, instrument_ids, exchange_id=b''):
61+
"""
62+
Subscribe market data for the given instruments.
63+
:param instrument_ids: [string], list of instrument ids.
64+
:param exchange_id: string, exchange id
65+
"""
66+
self.invoke_log('on_invoke_subscribe', instrument_ids=instrument_ids, exchange_id=exchange_id)
67+
68+
def unsubscribe(self, instrument_ids, exchange_id=b''):
69+
"""
70+
Un-subscribe market data for the given instruments.
71+
:param instrument_ids: [string], list of instrument ids.
72+
:param exchange_id: string, exchange id
73+
"""
74+
self.invoke_log('on_invoke_unsubscribe', instrument_ids=instrument_ids, exchange_id=exchange_id)
75+
76+
def subscribe_quote(self, instrument_ids, exchange_id=b''):
77+
"""
78+
Subscribe market data for the given instruments.
79+
:param instrument_ids: [string], list of instrument ids.
80+
:param exchange_id: string, exchange id
81+
"""
82+
self.invoke_log('on_invoke_subscribe_quote', instrument_ids=instrument_ids, exchange_id=exchange_id)
83+
84+
def unsubscribe_quote(self, instrument_ids, exchange_id=b''):
85+
"""
86+
Un-subscribe market data for the given instruments.
87+
:param instrument_ids: [string], list of instrument ids.
88+
:param exchange_id: string, exchange id
89+
"""
90+
self.invoke_log('on_invoke_unsubscribe_quote', instrument_ids=instrument_ids, exchange_id=exchange_id)
91+
92+
def req_qry_instrument(self, instrument_id, exchange_id):
93+
"""
94+
get instrument info
95+
:param instrument_id: string
96+
:param exchange_id: string, exchange id
97+
:return:
98+
"""
99+
self.invoke_log('on_invoke_req_qry_instrument', instrument_id=instrument_id, exchange_id=exchange_id)
100+
101+
def req_qry_investor_position(self, instrument_id, exchange_id):
102+
"""
103+
get investor position
104+
:param instrument_id: string
105+
:param exchange_id: string
106+
:return:
107+
"""
108+
self.invoke_log('on_invoke_req_qry_investor_position', instrument_id=instrument_id, exchange_id=exchange_id)
109+
110+
def req_qry_trading_account(self):
111+
"""
112+
get trading account info
113+
:return:
114+
"""
115+
self.invoke_log('on_invoke_req_qry_trading_account')
116+
117+
def send_order(self, order, in_out, count):
118+
"""
119+
create a new order
120+
:param order:
121+
:param in_out:
122+
:param count:
123+
:return:
124+
"""
125+
self.invoke_log('on_invoke_send_order', order=order, in_out=in_out, count=count)
126+
127+
def cancel_order(self, p_in, p_out, count):
128+
"""
129+
cancel an order
130+
:param p_in:
131+
:param p_out:
132+
:param count:
133+
:return:
134+
"""
135+
self.invoke_log('on_invoke_cancel_order', p_in=p_in, p_out=p_out, count=count)
136+
137+
def send_quote(self, quote, ask_out, bid_out, count):
138+
"""
139+
create a new quote
140+
:param quote:
141+
:param ask_out:
142+
:param bid_out:
143+
:param count:
144+
:return:
145+
"""
146+
self.invoke_log('on_invoke_send_quote', quote=quote, ask_out=ask_out, bid_out=bid_out, count=count)
147+
148+
def cancel_quote(self, p_in, p_out, count):
149+
"""
150+
cancel a quote
151+
:param p_in:
152+
:param p_out:
153+
:param count:
154+
:return:
155+
"""
156+
self.invoke_log('on_invoke_cancel_quote', p_in=p_in, p_out=p_out, count=count)

0 commit comments

Comments
 (0)