/
sample3.py
209 lines (189 loc) · 6.96 KB
/
sample3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import urllib.request
import pprint
import json
host_ = '18080'
pass_ = '*****'
def generate_token():
obj = {'APIPassword': pass_}
json_data = json.dumps(obj).encode('utf8')
url = f'http://localhost:{host_}/kabusapi/token'
req = urllib.request.Request(url, json_data, method='POST')
req.add_header('Content-Type', 'application/json')
try:
with urllib.request.urlopen(req) as res:
content = json.loads(res.read())
token_value = content.get('Token')
except urllib.error.HTTPError as e:
print(e)
return token_value
token = generate_token()
# 現物注文を出す関数
def send_cashorder(token, pass_, symbol, exchange, side, ordertype, qty, price, expire):
if side == 'buy':
side = '2'
dtype = 2
ftype = 'AA'
elif side == 'sell':
side = '1'
dtype = 0
ftype = ' '
# 成行注文のときのオプション
if str(ordertype) == '10':
price = 0
obj = { 'Password': pass_,
'Symbol': str(symbol), # 銘柄コード
'Exchange': int(exchange), #1が「東証」
'SecurityType': 1, # 1が「株式」
'Side': side, # 1が「売り」、2が「買い」
'CashMargin': 1, #1が「現物」 2が「信用新規」 3が「信用返済」
'DelivType': dtype, # 1が「制度信用」 2が「長期」 3が「デイトレ」
'AccountType': 2, # 口座の種類 2が「一般」
'Qty': int(qty),
'FrontOrderType': int(ordertype),
'FundType':ftype,
'Price': price,
'ExpireDay': expire
}
#print(obj)
json_data = json.dumps(obj).encode('utf-8')
url = 'http://localhost:18080/kabusapi/sendorder'
req = urllib.request.Request(url, json_data, method='POST')
req.add_header('Content-Type', 'application/json')
req.add_header('X-API-KEY', token)
try:
with urllib.request.urlopen(req) as res:
#print(res.status, res.reason)
for header in res.getheaders():
print(header)
content = json.loads(res.read())
return content
except urllib.error.HTTPError as e:
print(e)
content = json.loads(e.read())
return content
except Exception as e:
return e
# 注文を取り消す関数
def cancel_order(token, pass_, id_):
url = 'http://localhost:18080/kabusapi/cancelorder'
obj = {
'Password': pass_,
'OrderId': id_
}
json_data = json.dumps(obj).encode('utf-8')
req = urllib.request.Request(url, json_data, method='PUT')
req.add_header('Content-Type', 'application/json')
req.add_header('X-API-KEY', token)
try:
with urllib.request.urlopen(req) as res:
print(res.status, res.reason)
for header in res.getheaders():
print(header)
content = json.loads(res.read())
# pprint.pprint(content)
return content
except urllib.error.HTTPError as e:
print(e)
content = json.loads(e.read())
# pprint.pprint(content)
return content
except Exception as e:
# print(e)
return e
# 信用取引の注文を出す関数
def send_marginorder(token, pass_, symbol, exchange, side, margintype, ordertype,qty, price, expire):
if side == 'buy':
side = '2'
elif side == 'sell':
side = '1'
if int(ordertype) == 10:
price = 0
obj = { 'Password': pass_,
'Symbol': str(symbol), # 銘柄コード
'Exchange': int(exchange), #1が「東証」
'SecurityType': 1, # 1が「株式」
'Side': side, # 1が「売り」、2が「買い」
'CashMargin': 2, #1が「現物」 2が「信用新規」 3が「信用返済」
'MarginTradeType': margintype,
'DelivType': 0,
'AccountType': 2, # 口座の種類 2が「一般」
'Qty': int(qty),
'FrontOrderType': ordertype,
'Price': price,
'ExpireDay': expire
}
print(obj)
json_data = json.dumps(obj).encode('utf-8')
url = 'http://localhost:18080/kabusapi/sendorder'
req = urllib.request.Request(url, json_data, method='POST')
req.add_header('Content-Type', 'application/json')
req.add_header('X-API-KEY', token)
try:
with urllib.request.urlopen(req) as res:
print(res.status, res.reason)
for header in res.getheaders():
print(header)
content = json.loads(res.read())
return content
except urllib.error.HTTPError as e:
print(e)
content = json.loads(e.read())
# pprint.pprint(content)
return content
except Exception as e:
# print(e)
return e
# ポジションを決済する関数
def send_margin_closeorder(token, pass_, exchange, order_id, order_type, price, expire):
position_data = get_position(token,2)
posi_data = position_data[position_data['ポジションID']==order_id]
qty = posi_data['注文数'][0]
# print('ggu',side)
if order_type == 10:
price = 0
if posi_data['売買'][0]=='買':
print(1)
side = '1'
elif posi_data['売買'][0]=='売':
print(2)
side = '2'
margintype = posi_data['信用注文タイプ'][0]
symbol = posi_data['コード'][0]
obj = { 'Password': pass_,
'Symbol': str(symbol), # 銘柄コード
'Exchange': int(exchange), #1が「東証」
'SecurityType': 1, # 1が「株式」
'FrontOrderType': order_type,
'TimeInForce': 0,
'Side': side, # 1が「売り」、2が「買い」
'CashMargin': 3, #1が「現物」 2が「信用新規」 3が「信用返済」
'MarginTradeType': int(margintype),
'DelivType': 2,
'FundType': 11,
'AccountType': 2, # 口座の種類 2が「一般」
'Qty': int(qty),
'Price': int(price),
'ExpireDay': expire,
'ClosePositions': [{'HoldID':order_id ,'Qty':int(qty)}]
}
print(obj)
json_data = json.dumps(obj).encode('utf-8')
url = 'http://localhost:18080/kabusapi/sendorder'
req = urllib.request.Request(url, json_data, method='POST')
req.add_header('Content-Type', 'application/json')
req.add_header('X-API-KEY', token)
try:
with urllib.request.urlopen(req) as res:
print(res.status, res.reason)
for header in res.getheaders():
print(header)
content = json.loads(res.read())
return content
except urllib.error.HTTPError as e:
print(e)
content = json.loads(e.read())
# pprint.pprint(content)
return content
except Exception as e:
# print(e)
return e