pythonで仮想通貨シストレをやりたいと思い、勉強中です。
https://qiita.com/ti-ginkgo/items/7e15bdac6618c07534be
こちらの記事を参考に、pythonでコインチェックのapiを叩くテストクラスを作成しました。
クラスでまとめて、それぞれのapiをメソッド化してあります。また、一部apiのリクエストパラメーターが変わっていたようなので修正しています。
python3系が実行できる環境であれば、コピペですぐにコマンド実行できます。
※requestsは外部ライブラリなので、インストールしていない方はpip install requests を実行してください。
ソースコード
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 |
"""This is a test program.""" # -*- coding: utf-8 -*- import requests class CoinCheck: """ 参考ページ https://qiita.com/ti-ginkgo/items/7e15bdac6618c07534be """ def ticker(self): """ ティッカー [GET] """ print("\nティッカー [GET]") url = 'https://coincheck.com/api/ticker' coincheck = requests.get(url).json() for key, item in coincheck.items(): print("%-9s : %-10.9s " % (key, item)) def trades(self): """ 全取取引履歴 [GET] """ print("\n全取取引履歴 [GET]") url = 'https://coincheck.com/api/trades' coincheck = requests.get(url, params={"pair": "btc_jpy"}).json() print(coincheck) def order_books(self): """ 板情報 [GET] """ print("\n板情報 [GET]") url = 'https://coincheck.com/api/order_books' coincheck = requests.get(url).json() for key in coincheck.keys(): print(key, ":") for value in coincheck[key]: print(value) print() def rate(self): """ レート [GET] """ print("\nレート [GET]") url = 'https://coincheck.com/api/exchange/orders/rate' params = {'order_type': 'sell', 'pair': 'btc_jpy', 'amount': 0.1} coincheck = requests.get(url, params=params).json() print(coincheck) params = {'order_type': 'buy', 'pair': 'btc_jpy', 'price': 280000} coincheck = requests.get(url, params=params).json() print(coincheck) def sale_rate(self): """ 販売レート [GET] """ print("\n販売レート [GET]") coins = {'BTC': 'btc_jpy', 'ETH': 'eth_jpy', 'XEM': 'xem_jpy', 'BCH': 'bch_jpy'} url = 'https://coincheck.com/api/rate/' for key, item in coins.items(): coincheck = requests.get(url+item).json() print("%-4s : %-10s" % (key, coincheck['rate'])) if __name__ == '__main__': CC = CoinCheck() CC.ticker() CC.trades() CC.order_books() CC.rate() CC.sale_rate() |
実行結果
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
% python coincheck_api_test.py [11:59:22] ティッカー [GET] last : 1190190.0 timestamp : 151728116 low : 1179000.0 high : 1254777.0 bid : 1190311.0 ask : 1190996.0 volume : 11548.747 全取取引履歴 [GET] {'data': [{'created_at': '2018-01-30T02:59:29.000Z', 'amount': '0.008', 'order_type': 'sell', 'id': 86928135, 'rate': 1191000, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:29.000Z', 'amount': '0.2', 'order_type': 'sell', 'id': 86928134, 'rate': 1191000, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:29.000Z', 'amount': '0.008', 'order_type': 'sell', 'id': 86928133, 'rate': 1191000, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:29.000Z', 'amount': '0.006', 'order_type': 'sell', 'id': 86928132, 'rate': 1191000, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:29.000Z', 'amount': '0.317', 'order_type': 'buy', 'id': 86928131, 'rate': 1190490, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:28.000Z', 'amount': '0.022113', 'order_type': 'buy', 'id': 86928130, 'rate': 1190061, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:28.000Z', 'amount': '0.2', 'order_type': 'sell', 'id': 86928129, 'rate': 1190250, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:28.000Z', 'amount': '0.277887', 'order_type': 'sell', 'id': 86928128, 'rate': 1190312, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:28.000Z', 'amount': '0.004413', 'order_type': 'buy', 'id': 86928127, 'rate': 1190311, 'pair': 'btc_jpy'}, {'created_at': '2018-01-30T02:59:28.000Z', 'amount': '0.015587', 'order_type': 'sell', 'id': 86928126, 'rate': 1190311, 'pair': 'btc_jpy'}], 'success': True, 'pagination': {'starting_after': None, 'order': 'desc', 'limit': 10, 'ending_before': None}} 板情報 [GET] bids : ['1191005.0', '0.008'] ['1191000.0', '0.0272'] ['1190761.0', '0.0415'] ['1190068.0', '0.006'] ['1190057.0', '0.005'] ['1190056.0', '0.005'] ['1190052.0', '0.005'] ['1190045.0', '0.0354'] ['1190041.0', '0.011'] ['1190030.0', '0.005'] ['1190001.0', '0.059'] ['1190000.0', '1.49134'] ['1189980.0', '0.01675467'] ['1189847.0', '0.05'] ['1189795.0', '0.11379137'] ['1189532.0', '0.08006283'] ['1189499.0', '0.43'] ['1189160.0', '0.2'] ['1189124.0', '0.33638208'] ['1189120.0', '0.0343'] ['1189050.0', '0.0523'] ['1189039.0', '0.4'] ['1189029.0', '0.005'] ['1189001.0', '0.059'] ['1189000.0', '0.04'] ['1188625.0', '0.016356'] ['1188430.0', '0.0354'] ['1188100.0', '0.20092769'] ['1188067.0', '0.014'] ['1188010.0', '0.005'] ['1188001.0', '0.06462651'] ['1188000.0', '0.046'] ['1187804.0', '0.005'] ['1187794.0', '0.014'] ['1187784.0', '0.01'] ['1187730.0', '0.005'] ['1187694.0', '0.04026946'] ['1187686.0', '0.015676'] ['1187669.0', '0.081'] ['1187668.0', '0.075'] ['1187667.0', '0.075'] ['1187666.0', '0.075'] ['1187665.0', '0.075'] ['1187663.0', '0.0587'] ['1187660.0', '0.1'] ['1187658.0', '1.1'] ['1187657.0', '2.25'] ['1187649.0', '0.014'] ['1187628.0', '0.014'] ['1187615.0', '0.014'] ['1187518.0', '0.014'] ['1187479.0', '0.014'] ['1187348.0', '0.014'] ['1187235.0', '0.014'] ['1187217.0', '0.014'] ['1187175.0', '0.005'] ['1187014.0', '0.014'] ['1187001.0', '0.059'] ['1187000.0', '0.04'] ['1186861.0', '0.65'] ['1186818.0', '0.1820309'] ['1186769.0', '0.014'] ['1186752.0', '0.014'] ['1186741.0', '0.0147'] ['1186001.0', '0.06044065'] ['1186000.0', '0.04'] ['1185891.0', '0.014'] ['1185767.0', '0.014'] ['1185760.0', '0.014'] ['1185695.0', '0.0354'] ['1185623.0', '0.014'] ['1185482.0', '0.8'] ['1185481.0', '2.77'] ['1185001.0', '0.059'] ['1185000.0', '0.138'] ['1184718.0', '0.0540262'] ['1184001.0', '0.059'] ['1184000.0', '0.046'] ['1183842.0', '0.79956359'] ['1183835.0', '0.0354'] ['1183727.0', '0.01'] ['1183549.0', '0.014'] ['1183356.0', '1.0'] ['1183355.0', '2.92'] ['1183107.0', '0.014'] ['1183001.0', '0.059'] ['1183000.0', '0.05'] ['1182976.0', '0.014'] ['1182345.0', '0.01'] ['1182149.0', '1.0'] ['1182037.0', '0.497'] ['1182035.0', '1.5'] ['1182034.0', '1.826'] ['1182001.0', '0.059'] ['1182000.0', '0.07'] ['1181995.0', '0.044836'] ['1181941.0', '0.42732928'] ['1181940.0', '18.237'] ['1181782.0', '4.34'] ['1181741.0', '0.06832541'] ['1181001.0', '0.05'] ['1181000.0', '0.1058425'] ['1180698.0', '10.066'] ['1180001.0', '0.059'] ['1180000.0', '1.251'] ['1179001.0', '0.059'] ['1179000.0', '4.01263443'] ['1178200.0', '0.03'] ['1178001.0', '0.059'] ['1178000.0', '0.02'] ['1177777.0', '0.042'] ['1177642.0', '0.01'] ['1177001.0', '0.059'] ['1176001.0', '0.059'] ['1176000.0', '0.006'] ['1175757.0', '0.005'] ['1175700.0', '0.07'] ['1175001.0', '0.059'] ['1175000.0', '0.14255319'] ['1174761.0', '0.01'] ['1174500.0', '0.02'] ['1174001.0', '0.059'] ['1174000.0', '0.08517887'] ['1173880.0', '10.0'] ['1173300.0', '0.012'] ['1173001.0', '0.059'] ['1172194.0', '1.51'] ['1172093.0', '1.51'] ['1172090.0', '1.51'] ['1172001.0', '0.059'] ['1172000.0', '10.006'] ['1171900.0', '0.24'] ['1171113.0', '0.025'] ['1171001.0', '0.059'] ['1171000.0', '0.35321946'] ['1170880.0', '0.01'] ['1170700.0', '0.2'] ['1170500.0', '0.4'] ['1170010.0', '0.035'] ['1170001.0', '0.059'] ['1170000.0', '7.52966118'] ['1169200.0', '0.05'] ['1169029.0', '0.2'] ['1169001.0', '0.059'] ['1168001.0', '0.059'] ['1168000.0', '0.056'] ['1167627.0', '0.15'] ['1167001.0', '0.059'] ['1167000.0', '0.0075'] ['1166889.0', '0.01415653'] ['1166220.0', '0.05'] ['1166001.0', '0.059'] ['1165757.0', '0.005'] ['1165700.0', '0.07'] ['1165010.0', '0.035'] ['1165005.0', '0.4291827'] ['1165001.0', '0.079'] ['1165000.0', '0.095'] ['1164001.0', '0.059'] ['1164000.0', '0.126'] ['1163989.0', '0.5'] ['1163001.0', '0.09101108'] ['1162001.0', '0.059'] ['1162000.0', '10.0'] ['1161823.0', '0.224182'] ['1161300.0', '0.005'] ['1161111.0', '0.3'] ['1161001.0', '0.059'] ['1161000.0', '0.59938845'] ['1160880.0', '0.01'] ['1160789.0', '0.25'] ['1160700.0', '0.2'] ['1160010.0', '0.01'] ['1160001.0', '0.307'] ['1160000.0', '11.00331172'] ['1159780.0', '0.06565'] ['1159200.0', '0.1'] ['1159029.0', '0.2'] ['1159001.0', '0.059'] ['1159000.0', '0.0710575'] ['1158001.0', '0.059'] ['1158000.0', '0.02'] ['1157891.0', '0.30155'] ['1157750.0', '0.02'] ['1157736.0', '0.01'] ['1157500.0', '0.1'] ['1157464.0', '11.64'] ['1157001.0', '0.059'] ['1156537.0', '0.02'] ['1156001.0', '0.059'] ['1156000.0', '0.006'] ['1155700.0', '0.07'] ['1155500.0', '0.57673647'] ['1155096.0', '0.1'] ['1155040.0', '0.15'] ['1155001.0', '0.059'] ['1155000.0', '0.16721302'] ['1154500.0', '0.02'] ['1154460.0', '0.0101'] ['1154412.0', '10.0'] asks : ['1191145.0', '0.0354'] ['1191223.0', '0.07965396'] ['1191461.0', '0.0379'] ['1191558.0', '0.4'] ['1191598.0', '0.05'] ['1191599.0', '0.235'] ['1191600.0', '0.63'] ['1191680.0', '0.0354'] ['1191681.0', '0.32'] ['1191719.0', '0.4'] ['1191849.0', '0.08'] ['1191985.0', '0.005'] ['1191998.0', '0.04'] ['1191999.0', '0.659'] ['1192000.0', '1.04'] ['1192177.0', '0.32'] ['1192187.0', '0.0343'] ['1192205.0', '0.035'] ['1192248.0', '0.04'] ['1192250.0', '0.005'] ['1192285.0', '0.04'] ['1192380.0', '0.0404'] ['1192457.0', '0.0803542'] ['1192584.0', '0.006'] ['1192595.0', '0.0003'] ['1192694.0', '0.64'] ['1192820.0', '0.006'] ['1192870.0', '0.8'] ['1192871.0', '8.7809'] ['1192872.0', '0.068'] ['1192873.0', '0.068'] ['1192874.0', '0.068'] ['1192875.0', '0.068'] ['1192876.0', '0.068'] ['1192877.0', '0.068'] ['1192878.0', '0.068'] ['1192879.0', '0.068'] ['1192880.0', '0.068'] ['1192881.0', '0.068'] ['1192882.0', '0.068'] ['1192883.0', '0.068'] ['1192885.0', '0.0538'] ['1192888.0', '4.8'] ['1192915.0', '0.014'] ['1192939.0', '0.00525133'] ['1192997.0', '0.014'] ['1192999.0', '0.059'] ['1193000.0', '0.04'] ['1193026.0', '0.0254'] ['1193037.0', '0.014'] ['1193109.0', '0.0018'] ['1193128.0', '0.014'] ['1193158.0', '0.01565'] ['1193515.0', '0.014'] ['1193664.0', '0.02'] ['1193781.0', '0.005'] ['1193784.0', '0.68293582'] ['1193800.0', '0.03'] ['1193805.0', '0.0354'] ['1193850.0', '0.005'] ['1193871.0', '0.014'] ['1193999.0', '0.059'] ['1194000.0', '0.05'] ['1194002.0', '0.014'] ['1194027.0', '0.014'] ['1194040.0', '0.0356'] ['1194200.0', '0.27698172'] ['1194221.0', '0.00913621'] ['1194286.0', '0.006'] ['1194571.0', '0.0147'] ['1194636.0', '0.014'] ['1194850.0', '0.0406'] ['1194872.0', '0.02'] ['1194951.0', '0.005'] ['1194952.0', '0.005'] ['1194999.0', '0.059'] ['1195000.0', '0.43886355'] ['1195015.0', '0.0354'] ['1195029.0', '0.014'] ['1195326.0', '0.014'] ['1195431.0', '0.006'] ['1195515.0', '0.0075'] ['1195671.0', '0.014'] ['1195700.0', '0.07'] ['1195797.0', '0.014'] ['1195928.0', '0.014'] ['1195999.0', '0.059'] ['1196000.0', '0.366'] ['1196059.0', '0.014'] ['1196118.0', '0.014'] ['1196305.0', '0.014'] ['1196380.0', '0.014'] ['1196436.0', '0.014'] ['1196500.0', '0.161'] ['1196561.0', '0.014'] ['1196684.0', '0.02'] ['1196991.0', '0.22'] ['1196999.0', '0.06498693'] ['1197000.0', '0.05'] ['1197085.0', '0.0352'] ['1197177.0', '0.47609266'] ['1197999.0', '1.059'] ['1198000.0', '3.95'] ['1198500.0', '0.1'] ['1198696.0', '2.75'] ['1198999.0', '0.059'] ['1199000.0', '0.15'] ['1199100.0', '0.015'] ['1199999.0', '0.069'] ['1200000.0', '1.03583235'] ['1200546.0', '0.5'] ['1200781.0', '2.92'] ['1200999.0', '0.059'] ['1201000.0', '2.05'] ['1201539.0', '4.32'] ['1201999.0', '0.059'] ['1202000.0', '0.05'] ['1202273.0', '1.0'] ['1202999.0', '0.059'] ['1203000.0', '0.05'] ['1203017.0', '2.45'] ['1203999.0', '0.059'] ['1204000.0', '0.586'] ['1204500.0', '0.47572216'] ['1204999.0', '0.069'] ['1205000.0', '1.067'] ['1205700.0', '0.07'] ['1205999.0', '0.059'] ['1206000.0', '0.05'] ['1206999.0', '0.059'] ['1207000.0', '0.05'] ['1207999.0', '0.059'] ['1208000.0', '0.058'] ['1208949.0', '10.0'] ['1208999.0', '0.059'] ['1209000.0', '0.15'] ['1209800.0', '0.01630272'] ['1209920.0', '0.0085138'] ['1209999.0', '0.069'] ['1210000.0', '0.125'] ['1210999.0', '0.059'] ['1211000.0', '0.05'] ['1211999.0', '0.059'] ['1212000.0', '0.058'] ['1212999.0', '0.059'] ['1213000.0', '0.05'] ['1213999.0', '0.059'] ['1214000.0', '0.05'] ['1214999.0', '0.069'] ['1215000.0', '0.35'] ['1215300.0', '0.155'] ['1215500.0', '0.05'] ['1215700.0', '0.07'] ['1215999.0', '0.059'] ['1216000.0', '0.058'] ['1216999.0', '0.059'] ['1217000.0', '0.05'] ['1217999.0', '0.059'] ['1218000.0', '0.07597463'] ['1218010.0', '0.0354'] ['1218495.0', '0.0354'] ['1218999.0', '0.059'] ['1219000.0', '0.15'] ['1219500.0', '0.46083336'] ['1219790.0', '0.0354'] ['1219999.0', '0.069'] ['1220000.0', '3.10224049'] ['1220405.0', '0.0355'] ['1220500.0', '0.01405'] ['1220999.0', '0.059'] ['1221000.0', '1.94'] ['1221999.0', '0.059'] ['1222000.0', '0.064'] ['1222345.0', '0.0355'] ['1222814.0', '0.224182'] ['1222999.0', '0.059'] ['1223000.0', '0.00501588'] ['1223100.0', '0.05'] ['1223245.0', '0.0187'] ['1223415.0', '0.0355'] ['1223500.0', '0.005'] ['1223735.0', '0.01'] ['1223999.0', '0.059'] ['1224000.0', '0.163'] ['1224800.0', '0.121'] ['1224999.0', '0.069'] ['1225000.0', '3.295'] ['1225071.0', '0.01'] ['1225260.0', '0.0354'] ['1225295.0', '0.0355'] ['1225700.0', '0.07'] ['1225860.0', '0.0095'] ['1225999.0', '0.059'] ['1226000.0', '0.1'] ['1226685.0', '0.02756923'] ['1226718.0', '0.05'] ['1226760.0', '0.0354'] ['1226999.0', '0.0879'] ['1227000.0', '0.91557621'] ['1227150.0', '0.0509'] レート [GET] {'price': '119004.867', 'success': True, 'rate': '1190048.67', 'amount': '0.1'} {'price': '280000.0', 'success': True, 'rate': '1190375.01872', 'amount': '0.23521999'} 販売レート [GET] XEM : 95.21045856 ETH : 123704.05223664 BTC : 1190219.0 BCH : 174466.87146096 |