├── .idea ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── ok_huobi.iml └── vcs.xml ├── Dockerfile ├── README.md ├── app ├── OK_Services.py ├── OK_Utils.py ├── ok_trade.py └── settings.py ├── base.txt └── requirements.txt /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/ok_huobi.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM daocloud.io/python:3-onbuild 2 | 3 | MAINTAINER Robin 4 | 5 | ENV LANG C.UTF-8 6 | 7 | RUN mkdir -p /app 8 | 9 | WORKDIR /app 10 | 11 | COPY /app /app 12 | COPY base.txt /app 13 | COPY requirements.txt /app 14 | 15 | #安装Python程序运行的依赖库 16 | RUN cd /app && pip install -r base.txt 17 | RUN cd /app && pip install -r requirements.txt 18 | 19 | 20 | EXPOSE 80 21 | 22 | 23 | ENTRYPOINT ["python", "/app/ok_trade.py"] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #OKex自动化交易 2 | -------------------------------------------------------------------------------- /app/OK_Services.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | try: 6 | from app.OK_Utils import * 7 | except Exception as e: 8 | from OK_Utils import * 9 | 10 | 11 | class Ok_Services: 12 | 13 | def __init__(self): 14 | 15 | #为了账户的安全性,API_KEY和SECRET_KEY取环境变量的值 16 | if 'API_KEY' in os.environ: 17 | OK_PARAMS['api_key'] = os.environ['API_KEY'] 18 | if 'SECRET_KEY' in os.environ: 19 | OK_PARAMS['SECRET_KEY'] = os.environ['SECRET_KEY'] 20 | self.utils = OK_Utils(OK_PARAMS) 21 | print(self.utils.params) 22 | 23 | 24 | def get_accounts(self): 25 | pass 26 | 27 | def get_balance(self): 28 | pass 29 | 30 | def get_depth(self,symbol): 31 | ''' 32 | 获取OKEX币币市场深度 33 | :param symbol: 34 | :return: 35 | ''' 36 | path = OK_API['DEPTH'][0] 37 | params = {'symbol': symbol} 38 | 39 | return self.utils.api_key_get(params, path) 40 | 41 | def get_ticker(self,symbol): 42 | ''' 43 | #获取OKex币币市场行情 44 | :param symbol: 45 | :return: 46 | ''' 47 | path = OK_API['TICKER'][0] 48 | params = {'symbol': symbol} 49 | 50 | return self.utils.api_key_get(params,path) 51 | 52 | def get_trade_detail(self,symbol,start_date='',end_date=''): 53 | ''' 54 | 获取OKex币币交易信息(600条) 55 | :param symbol: 56 | :param start_date: 57 | :param end_date: 58 | :return: 59 | ''' 60 | path = OK_API['TRADES'][0] 61 | params = {'symbol': symbol} 62 | 63 | return self.utils.api_key_get(params,path) 64 | 65 | def get_kline(self,symbol,period,size = 60): 66 | ''' 67 | 获取OKex币币K线数据 每个周期条数2000左右 68 | :param symbol: 69 | :param period: 可选值:{1min, 5min, 15min, 30min, 60min, 1day, 1mon, 1week, 1year } 70 | :param size: 71 | :return: 72 | ''' 73 | path = OK_API['KLINE'][0] 74 | params = {'symbol': symbol, 75 | 'type':period 76 | } 77 | return self.utils.api_key_get(params,path) 78 | #----------------------------合约行情信息---------------------# 79 | 80 | def get_future_ticker(self,symbol,contract_type='this_week'): 81 | ''' 82 | #获取Okex合约账户信息(全仓) 83 | :param symbol: 84 | :param contract_type: 85 | :return: 86 | ''' 87 | path = OK_API['F_TICKER'][0] 88 | params = {'symbol': symbol, 89 | 'contract_type': contract_type 90 | } 91 | return self.utils.api_key_get(params,path) 92 | 93 | def get_future_depth(self,symbol,contract_type='this_week'): 94 | ''' 95 | #获取OKex 合约深度信息 96 | :param symbol: 97 | :param contract_type: 98 | :return: 99 | ''' 100 | path = OK_API['F_DEPTH'][0] 101 | params = {'symbol': symbol, 102 | 'contract_type': contract_type 103 | } 104 | return self.utils.api_key_get(params, path) 105 | 106 | def get_future_trades(self,symbol,contract_type='this_week'): 107 | """ 108 | 获取OKex合约成交信息 109 | :param symbol: 110 | :param contract_type: 111 | :return: 112 | """ 113 | path = OK_API['F_TRADES'][0] 114 | params = {'symbol': symbol, 115 | 'contract_type': contract_type 116 | } 117 | return self.utils.api_key_get(params, path) 118 | 119 | def get_future_index(self,symbol): 120 | ''' 121 | #获取OKEX合约指数信息 122 | :param symbol: 123 | :return: 124 | ''' 125 | path = OK_API['F_INDEX'][0] 126 | params = {'symbol': symbol} 127 | return self.utils.api_key_get(params, path) 128 | 129 | def get_exchange_usdcnyrate(self): 130 | ''' 131 | #获取美元人民币汇率 132 | :return: 133 | ''' 134 | path = OK_API['EXCHANGE_RATE'][0] 135 | params = {} 136 | return self.utils.api_key_get(params, path) 137 | 138 | def get_future_estimated_price(self,symbol): 139 | ''' 140 | #获取交割预估价 141 | :param symbol: 142 | :return: 143 | ''' 144 | path = OK_API['F_EST_PRICE'][0] 145 | params = {'symbol':symbol} 146 | return self.utils.api_key_get(params, path) 147 | 148 | def get_future_kline(self,symbol,period,size=60,contract_type='this_week'): 149 | ''' 150 | #获取虚拟合约的K线数据 151 | :param symbol: 152 | :param period: 153 | 1min : 1分钟, 3min : 3分钟, 5min : 5分钟,15min : 15分钟,30min : 30分钟 154 | 1day : 1日,3day : 3日,1week : 1周 155 | 1hour : 1小时,2hour : 2小时,4hour : 4小时,6hour : 6小时,12hour : 12小时 156 | :param contract_type: 157 | :return: 158 | ''' 159 | path = OK_API['F_KLINE'][0] 160 | params = {'symbol': symbol, 161 | 'type': period, 162 | 'size':size, 163 | 'contract_type': contract_type 164 | } 165 | return self.utils.api_key_get(params, path) 166 | 167 | def get_future_hold_amount(self,symbol,contract_type='this_week'): 168 | ''' 169 | #获取当前可用合约总持仓量 170 | :param symbol: 171 | :param contract_type: 172 | :return: 173 | ''' 174 | path = OK_API['F_H_AMOUNT'][0] 175 | params = {'symbol': symbol, 176 | 'contract_type': contract_type 177 | } 178 | return self.utils.api_key_get(params, path) 179 | 180 | def get_future_price_limit(self,symbol,contract_type='this_week'): 181 | ''' 182 | #获取合约最高限价和最低限价 183 | :param symbol: 184 | :param contract_type: 185 | :return: 186 | ''' 187 | path = OK_API['F_PRICE_LIMIT'][0] 188 | params = {'symbol': symbol, 189 | 'contract_type': contract_type 190 | } 191 | return self.utils.api_key_get(params, path) 192 | 193 | #------------------------------币币交易部分------------------------# 194 | 195 | def get_userinfo(self): 196 | ''' 197 | #获取用户信息 198 | :return: 199 | ''' 200 | path = OK_API['USERINFO'][0] 201 | params = {} 202 | 203 | return self.utils.api_key_post(params,path) 204 | 205 | def send_order(self,symbol,type,amount,price): 206 | ''' 207 | #发送订单 208 | :param symbol: 209 | :param type:买卖类型: 限价单(buy/sell) 市价单(buy_market/sell_market) 210 | :param amount: 211 | :param price: 212 | :return: 213 | ''' 214 | path = OK_API['TRADE'][0] 215 | params = {'symbol':symbol, 216 | 'type':type, 217 | 'price':price, 218 | 'amount':amount 219 | } 220 | return self.utils.api_key_post(params, path) 221 | 222 | def send_batch_orders(self,symbol,type,order_data): 223 | ''' 224 | #币币交易批量下单 225 | :param symbol: 226 | :param type:买卖类型: 限价单(buy/sell) 227 | :param order_data:最大下单量为5, price和amount参数参考trade接口中的说明,最终买卖类型由orders_data 中type 为准,如orders_data不设定type 则由上面type设置为准。 228 | :return: 229 | ''' 230 | 231 | path = OK_API['BATCH_TRADE'][0] 232 | params = {'symbol': symbol, 233 | 'type': type, 234 | 'order_data': order_data 235 | } 236 | return self.utils.api_key_post(params, path) 237 | 238 | def cancel_order(self,symbol,order_id): 239 | ''' 240 | #撤销订单 241 | :param symbol: 242 | :param order_id:订单ID(多个订单ID中间以","分隔,一次最多允许撤消3个订单) 243 | :return: 244 | ''' 245 | path = OK_API['CANCEL_ORDER'][0] 246 | params = {'symbol': symbol, 247 | 'order_id': order_id 248 | } 249 | return self.utils.api_key_post(params, path) 250 | 251 | def get_order_info(self,symbol,order_id): 252 | ''' 253 | #获取用户的订单信息 254 | :param symbol: 255 | :param order_id: 256 | :return: 257 | ''' 258 | path = OK_API['ORDER_INFO'][0] 259 | params = {'symbol': symbol, 260 | 'order_id': order_id 261 | } 262 | return self.utils.api_key_post(params, path) 263 | 264 | def get_order_list(self,symbol,type,order_id): 265 | ''' 266 | 批量获取用户订单 267 | :param symbol: 268 | :param type:查询类型 0:未完成的订单 1:已经完成的订单 269 | :param order_id:订单ID(多个订单ID中间以","分隔,一次最多允许查询50个订单) 270 | :return: 271 | ''' 272 | path = OK_API['ORDERS_INFO'][0] 273 | params = {'symbol': symbol, 274 | 'type':type, 275 | 'order_id': order_id 276 | } 277 | return self.utils.api_key_post(params, path) 278 | 279 | def get_order_history(self,symbol,status=0,current_page=1,page_length=100): 280 | ''' 281 | #获取历史订单信息,只返回最近两天的信息 282 | :param symbol: 283 | :param status:查询状态 0:未完成的订单 1:已经完成的订单 (最近两天的数据) 284 | :param current_page:当前页数 285 | :param page_length:每页数据条数,最多不超过200 286 | :return: 287 | ''' 288 | path = OK_API['ORDER_HISTORY'][0] 289 | params = {'symbol': symbol, 290 | 'status': status, 291 | 'current_page': current_page, 292 | 'page_length':page_length 293 | } 294 | return self.utils.api_key_post(params, path) 295 | 296 | def withdraw(self,symbol,withdraw_address,withdraw_amount,trade_pwd,chargefee,target): 297 | ''' 298 | 提币BTC/LTC/ETH/ETC/BCH 299 | :param symbol:btc_usd:比特币 ltc_usd :莱特币 eth_usd :以太坊 etc_usd :以太经典 bch_usd :比特现金 300 | :param withdraw_address:认证的地址、邮箱 或手机号码 301 | :param withdraw_amount:提币数量 BTC>=0.01 LTC>=0.1 ETH>=0.1 ETC>=0.1 BCH>=0.1 302 | :param trade_pwd:交易密码 303 | :param chargefee:网络手续费 >=0 304 | BTC范围 [0.002,0.005] 305 | LTC范围 [0.001,0.2] 306 | ETH范围 [0.01] 307 | ETC范围 [0.0001,0.2] 308 | BCH范围 [0.0005,0.002] 309 | 手续费越高,网络确认越快,OKCoin内部提币设置0 310 | :param target:地址类型 okcn:国内站 okcom:国际站 okex:OKEX address:外部地址 311 | :return: 312 | ''' 313 | path = OK_API['WITHDRAW'][0] 314 | params = {'symbol': symbol, 315 | 'withdraw_address': withdraw_address, 316 | 'withdraw_amount': withdraw_amount, 317 | 'trade_pwd': trade_pwd, 318 | 'chargefee':chargefee, 319 | 'target':target 320 | } 321 | return self.utils.api_key_post(params, path) 322 | 323 | def cancel_withdraw(self,symbol,withdraw_id): 324 | ''' 325 | 取消提币BTC/LTC/ETH/ETC/BCH 326 | :param symbol:btc_usd:比特币 ltc_usd :莱特币 eth_usd :以太坊 etc_usd :以太经典 bch_usd :比特现金 327 | :param withdraw:提币申请Id 328 | :return: 329 | ''' 330 | path = OK_API['CANCEL_WITHDRAW'][0] 331 | params = {'symbol': symbol, 332 | 'withdraw_id': withdraw_id 333 | } 334 | return self.utils.api_key_post(params, path) 335 | 336 | def get_withdraw_info(self,symbol,withdraw_id): 337 | ''' 338 | 339 | :param symbol:查询提币BTC/LTC/ETH/ETC/BCH信息 340 | :param withdraw_id:提币申请Id 341 | :return: 342 | result:true表示请求成功 343 | address:提现地址 344 | amount:提现金额 345 | created_date:提现时间 346 | chargefee:网络手续费 347 | status:提现状态(-3:撤销中;-2:已撤销;-1:失败;0:等待提现;1:提现中;2:已汇出;3:邮箱确认;4:人工审核中5:等待身份认证) 348 | withdraw_id:提币申请Id 349 | ''' 350 | 351 | path = OK_API['WITHDRAW_INFO'][0] 352 | params = {'symbol': symbol, 353 | 'withdraw_id': withdraw_id 354 | } 355 | return self.utils.api_key_post(params, path) 356 | 357 | def get_account_records(self,symbol,type,current_page=1,page_length=50): 358 | ''' 359 | #获取用户提现/充值记录 360 | :param symbol:btc, ltc, eth, etc, bch, usdt 361 | :param type:0:充值 1 :提现 362 | :param current_page:当前页数 363 | :param page_length:每页数据条数,最多不超过50条 364 | :return: 365 | ''' 366 | path = OK_API['ACCOUNT_RECORDS'][0] 367 | params = {'symbol': symbol, 368 | 'type': type, 369 | 'current_page':current_page, 370 | 'page_length':page_length 371 | } 372 | return self.utils.api_key_post(params, path) 373 | 374 | #-----------------------------合约交易部分-------------------------# 375 | 376 | def get_future_userinfo(self): 377 | ''' 378 | 获取OKex合约账户信息(全仓,所有仓位) 379 | :return: 380 | ''' 381 | path = OK_API['F_USERINFO'][0] 382 | params = {} 383 | 384 | return self.utils.api_key_post(params, path) 385 | 386 | def get_future_position(self,symbol,contract_type='this_week'): 387 | ''' 388 | #获取用户持仓获取OKex合约账户信息(全仓) 389 | :param symbol:btc_usd ltc_usd eth_usd etc_usd bch_usd 390 | :param contract_type:合约类型: this_week:当周 next_week:下周 quarter:季度 391 | :return: 392 | ''' 393 | path = OK_API['F_POSITION'][0] 394 | params = {'symbol':symbol, 395 | 'contract_type':contract_type 396 | } 397 | return self.utils.api_key_post(params, path) 398 | 399 | def send_future_order(self,symbol,type,price,amount,match_price=1,lever_rate=20,contract_type = 'this_week'): 400 | ''' 401 | #合约下单 402 | :param symbol: btc_usdt ltc_usdt eth_usdt etc_usdt bch_usdt 403 | :param type:1:开多 2:开空 3:平多 4:平空 404 | :param price: 405 | :param amount:委托数量 406 | :param match_price:是否为对手价 0:不是 1:是 ,当取值为1时,price无效 407 | :param lever_rate:杠杆倍数 value:10\20 默认10 408 | :param contract_type:合约类型: this_week:当周 next_week:下周 quarter:季度 409 | :return: 410 | ''' 411 | path = OK_API['F_TRADE'][0] 412 | params = {'symbol': symbol, 413 | 'type': type, 414 | 'price': price, 415 | 'amount': amount, 416 | 'match_price': match_price, 417 | 'lever_rate': lever_rate, 418 | 'contract_type': contract_type 419 | } 420 | return self.utils.api_key_post(params, path) 421 | 422 | def send_future_batch_orders(self, symbol, orders_data, lever_rate=20, contract_type='this_week'): 423 | ''' 424 | #合约账户批量下单 425 | :param symbol:btc_usdt ltc_usdt eth_usdt etc_usdt bch_usdt 426 | :param order_data: 427 | JSON类型的字符串 428 | 例:[{price:5,amount:2,type:1,match_price:0},{price:2,amount:3,type:1,match_price:0}] 429 | 最大下单量为5,price,amount,type,match_price参数参考future_trade接口中的说明 430 | :param lever_rate:杠杆倍数 value:10\20 默认10 431 | :param contract_type:this_week:当周 next_week:下周 quarter:季度 432 | :return: 433 | ''' 434 | 435 | path = OK_API['F_BATCH_TRADE'][0] 436 | params = {'symbol': symbol, 437 | 'orders_data': orders_data, 438 | 'lever_rate': lever_rate, 439 | 'contract_type': contract_type 440 | } 441 | return self.utils.api_key_post(params, path) 442 | 443 | def get_future_trades_history(self,symbol,date,since): 444 | ''' 445 | 获取Okex合约交易历史(非个人) 446 | :param symbol:btc_usdt ltc_usdt eth_usdt etc_usdt bch_usdt 447 | :param date:合约交割时间,格式yyyy-MM-dd 448 | :param since:交易Id起始位置 449 | :return: 450 | ''' 451 | path = OK_API['F_TRADES_HISTORY'][0] 452 | params = {'symbol': symbol, 453 | 'date': date, 454 | 'since': since 455 | } 456 | return self.utils.api_key_post(params, path) 457 | 458 | def cancel_future_order(self,symbol,order_id,contract_type='this_week'): 459 | ''' 460 | #取消合约订单 461 | :param symbol: 462 | :param order_id: 463 | :param contract_type: 464 | :return: 465 | ''' 466 | path = OK_API['F_CANCEL'][0] 467 | params = {'symbol': symbol, 468 | 'order_id': order_id, 469 | 'contract_type': contract_type 470 | } 471 | return self.utils.api_key_post(params, path) 472 | 473 | def get_future_order(self,symbol,order_id,status=1,current_page=1,page_length=1,contract_type='this_week'): 474 | ''' 475 | #获取合约订单信息 476 | :param symbol: 477 | :param order_id: 478 | :param status:查询状态 1:未完成的订单 2:已经完成的订单 479 | :param current_page:当前页数 480 | :param page_length:每页获取条数,最多不超过50 481 | :param contract_type: 482 | :return: 483 | ''' 484 | path = OK_API['F_ORDER_INFO'][0] 485 | params = {'symbol': symbol, 486 | 'order_id': order_id, 487 | 'status': status, 488 | 'page_length': page_length, 489 | 'current_page': current_page, 490 | 'contract_type': contract_type 491 | } 492 | 493 | return self.utils.api_key_post(params, path) 494 | 495 | def get_future_order_list(self,symbol,order_id,contract_type='this_week'): 496 | ''' 497 | #批量获取合约订单信息 498 | :param symbol: 499 | :param order_id:订单ID(多个订单ID中间以","分隔,一次最多允许查询50个订单) 500 | :param contract_type:合约类型: this_week:当周 next_week:下周 quarter:季度 501 | :return: 502 | ''' 503 | path = OK_API['F_ORDERS_INFO'][0] 504 | params = {'symbol': symbol, 505 | 'order_id': order_id, 506 | 'contract_type': contract_type 507 | } 508 | 509 | return self.utils.api_key_post(params, path) 510 | 511 | def get_future_userinfo_4fix(self): 512 | '''#获取逐仓合约账户信息 513 | ''' 514 | path = OK_API['F_USDERINFO_4FIX'][0] 515 | params = {} 516 | 517 | return self.utils.api_key_post(params, path) 518 | 519 | def get_future_position_4fix(self,symbol,type=0,contract_type='this_week'): 520 | ''' 521 | #逐仓用户持仓查询 522 | :param symbol: 523 | :param type:默认返回10倍杠杆持仓 type=1 返回全部持仓数据 524 | :param contract_type:合约类型: this_week:当周 next_week:下周 quarter:季度 (如不传入参数,则返回全部合约) 525 | :return: 526 | ''' 527 | path = OK_API['F_POSITION_4FIX'][0] 528 | params = {'symbol':symbol, 529 | 'type':type, 530 | 'contract_type':contract_type 531 | } 532 | 533 | return self.utils.api_key_post(params, path) 534 | 535 | def get_future_explosive(self,symbol,status=0,current_page=1,page_number=0,page_length=1,contract_type='this_week'): 536 | ''' 537 | #获取合约爆仓单 538 | :param symbol: 539 | :param status:状态 0:最近7天未成交 1:最近7天已成交 540 | :param current_page:当前页数索引值 541 | :param page_number:当前页数(使用page_number时current_page失效,current_page无需传) 542 | :param page_length:每页获取条数,最多不超过50 543 | :param contract_type:合约类型。this_week:当周;next_week:下周;quarter:季度 544 | :return: 545 | ''' 546 | path = OK_API['F_EXPLOSIVE'][0] 547 | params = {'symbol': symbol, 548 | 'status': status, 549 | 'current_page':current_page, 550 | 'page_number':page_number, 551 | 'page_length':page_length, 552 | 'contract_type': contract_type 553 | } 554 | 555 | return self.utils.api_key_post(params, path) 556 | 557 | def transfer_future_deposit(self,symbol,type,amount): 558 | ''' 559 | #个人账户资金划转 560 | :param symbol: 561 | :param type:划转类型。OK_TRANSFER_TYPE['E2F'] 1:币币转合约 OK_TRANSFER_TYPE['F2E'] 2:合约转币币 562 | :param amount:划转币的数量 563 | :return: 564 | ''' 565 | path = OK_API['F_DEVOLVE'][0] 566 | params = {'symbol': symbol, 567 | 'type': type, 568 | 'amount': amount, 569 | } 570 | 571 | return self.utils.api_key_post(params, path) 572 | 573 | 574 | 575 | if __name__ == '__main__': 576 | 577 | ok = Ok_Services() 578 | 579 | '''#1 get_depth 获取币币市场深度 580 | result = ok.get_depth('xrp_usdt') 581 | print(result['asks'][-5:-1]) 582 | print(result['bids'][0:5]) 583 | '''#1 get_depth 获取币币市场深度 584 | 585 | '''#2 get_ticker 获取币币市场行情 586 | result = ok.get_ticker('eth_usdt') 587 | print(result) 588 | '''#2 get_ticker 获取币币市场行情 589 | 590 | '''#3 get_trade_detail 获取币币市场交易信息 591 | result = ok.get_trade_detail('eth_usdt') 592 | print(result) 593 | '''#3 get_trade_detail 获取币币市场交易信息 594 | 595 | '''#4 get_kline 获取币币交易K线数据 每个周期数据条数2000左右 596 | result = ok.get_kline('eth_usdt','1min') 597 | print(result) 598 | '''#4 get_kline 获取币币交易K线数据 每个周期数据条数2000左右 599 | 600 | '''#5 get_future_ticker获取OKex合约行情数据 601 | result = ok.get_future_ticker('eth_usdt') 602 | print(result) 603 | '''#5 get_future_ticker获取OKex合约行情数据 604 | 605 | '''#6 get_future_depth 获取OKex合约深度信息 606 | result = ok.get_future_depth('eth_usdt') 607 | print(result) 608 | '''#6 get_future_depth 获取OKex合约深度信息 609 | 610 | '''#7 get_future_trades 获取OKex合约成交信息 611 | result = ok.get_future_trades('eth_usdt') 612 | print(result) 613 | '''#7 get_future_trades 获取OKex合约成交信息 614 | 615 | 616 | '''#8 get_future_index 获取OKex合约指数信息 617 | result = ok.get_future_index('eth_usdt') 618 | print(result) 619 | '''#8 get_future_index 获取OKex合约指数信息 620 | 621 | '''#9 get_exchange_usdcnyrate 获取美元人民币汇率 622 | result = ok.get_exchange_usdcnyrate() 623 | print(result) 624 | '''#9 get_exchange_usdcnyrate 获取美元人民币汇率 625 | 626 | '''#10 get_future_estimated_price获取交割预付价 627 | result = ok.get_future_estimated_price('eth_usdt') 628 | print(result) 629 | '''#10 get_future_estimated_price获取交割预付价 630 | 631 | '''#11 get_future_kline 获取虚拟合约的K线数据 632 | result = ok.get_future_kline('eth_usdt','1min') 633 | print(result) 634 | '''#11 get_future_kline 获取虚拟合约的K线数据 635 | 636 | '''#12 get_future_hold_amount 获取当前合约总持仓量 637 | result = ok.get_future_hold_amount('eth_usdt') 638 | print(result) 639 | '''#12 get_future_hold_amount 获取当前合约总持仓量 640 | 641 | '''#13 get_future_price_limit 获取合约最高限价和最低限价 642 | result = ok.get_future_price_limit('eth_usdt') 643 | print(result) 644 | '''#13 get_future_price_limit 获取合约最高限价和最低限价 645 | 646 | '''#14 get_userinfo 获取币币用户信息 647 | result = ok.get_userinfo() 648 | print(result['info']['funds']['borrow']) 649 | print(type(result['info']['funds']['borrow'])) 650 | '''#14 get_userinfo 获取币币用户信息 651 | 652 | '''#16 Get_future_userinfo 获取合约账户信息 653 | result = ok.get_future_userinfo() 654 | print(result) 655 | '''#16 Get_future_userinfo 获取合约账户信息 656 | 657 | '''#17 get_future_position获取用户持仓获取OKex合约帐户信息(全仓) 658 | result = ok.get_future_position('eth_usdt') 659 | print(result) 660 | '''#17 get_future_position 获取用户持仓获取OKex合约帐户信息(全仓) 661 | 662 | '''#18 send_future_order合约账户下单 663 | result = ok.send_future_order('eth_usdt',OK_ORDER_TYPE['KD'],1000.000,1) 664 | print(result) 665 | '''#18 send_future_order合约账户下单 666 | 667 | '''#19 send_future_batch_orders合约账户批量下单 668 | orders_data=[{'price':1000.000,'amount':1,'type':OK_ORDER_TYPE['KD'],'match_price':0}, 669 | {'price':1001.000,'amount':2,'type':OK_ORDER_TYPE['KD'],'match_price':0}] 670 | result = ok.send_future_batch_orders('eth_usdt',orders_data) 671 | print(result) 672 | '''#19 send_future_batch_orders合约账户批量下单 673 | 674 | '''#20 cancel_future_order 取消合约订单 675 | result = ok.cancel_future_order('eth_usdt',result['order_id']) 676 | print(result) 677 | '''#20 cancel_future_order 取消合约订单 678 | 679 | '''#21 get_future_order 获取合约订单信息 680 | result = ok.get_future_order('eth_ust',result['order_id']) 681 | print(result) 682 | '''#21 get_future_order 获取合约订单信息 683 | 684 | '''#22 get_future_order_list 批量获取合约订单用户 685 | result = ok.get_future_order_list('eth_usdt','') 686 | print(result) 687 | '''#22 get_future_order_list 批量获取合约订单用户 688 | 689 | '''#23 get_future_userinfo_4fix获取逐仓合约账户信息 690 | result = ok.get_future_userinfo_4fix() 691 | print(result) 692 | '''#23 get_future_userinfo_4fix获取逐仓合约账户信息 693 | 694 | '''#24 get_future_explosive 获取合约爆仓单 695 | result = ok.get_future_explosive('eth_usdt') 696 | print(result) 697 | '''#24 get_future_explosive 获取合约爆仓单 698 | 699 | '''#25 transfer_future_deposit #个人账户资金划转 700 | result = ok.transfer_future_deposit('eth_usdt',OK_TRANSFER_TYPE['F2E'],0.2) 701 | print(result) 702 | '''#25 transfer_future_deposit #个人账户资金划转 703 | 704 | '''#26 send_order #币币交易下订单 705 | result = ok.send_order('eth_usdt',OK_ORDER_TYPE['SL'],0.1,1500) 706 | print(result) 707 | '''#26 send_order #币币交易下订单 708 | 709 | '''#27 cancel_order 取消币币交易订单 710 | result = ok.cancel_order('eth_usdt', result['orders'][0]['order_id']) 711 | print(result) 712 | '''#27 cancel_order 取消币币交易订单 713 | 714 | '''#28 get_order_info 获取订单信息 715 | result = ok.get_order_info('eth_usdt','') 716 | print(result) 717 | '''#28 get_order_info 获取订单信息 718 | 719 | '''#29 获取历史订单信息,只返回最近两天的信息 720 | result = ok.get_order_history('eth_usdt') 721 | print(result['orders'][0]['order_id']) 722 | '''#29 获取历史订单信息,只返回最近两天的信息 723 | 724 | '''#30 get_account_records 获取账户充值提现记录数据 725 | result =ok.get_account_records('eth',0) 726 | print(result) 727 | '''#30 get_account_records 获取账户充值提现记录数据 -------------------------------------------------------------------------------- /app/OK_Utils.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import hashlib 6 | import urllib 7 | import urllib.parse 8 | import urllib.request 9 | 10 | import requests 11 | 12 | try: 13 | from app.settings import * 14 | except Exception as e: 15 | from settings import * 16 | 17 | 18 | class OK_Utils: 19 | 20 | def __init__(self,params): 21 | self.params = params 22 | 23 | def create_sign(self,params): 24 | sign = {} 25 | if isinstance(params, dict): 26 | for key in sorted(params.keys()): 27 | sign[key] = str(params[key]) 28 | 29 | data = sign 30 | data['secret_key'] = self.params['SECRET_KEY'] 31 | data = urllib.parse.urlencode(sign, doseq=False, safe='', encoding=None, errors=None) 32 | 33 | #print(data) 34 | else: 35 | raise TypeError('{0} should has attributes of "items"'.format(params)) 36 | 37 | return hashlib.md5(data.encode('utf8')).hexdigest().upper() 38 | 39 | def http_get_request(self,url, params, add_to_headers=None): 40 | headers = { 41 | "Content-type": "application/x-www-form-urlencoded", 42 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36', 43 | } 44 | 45 | if add_to_headers: 46 | headers.update(add_to_headers) 47 | #postdata = self.create_sign(params) 48 | postdata = urllib.parse.urlencode(params, doseq=False, safe='', encoding=None, errors=None) 49 | try: 50 | response = requests.get(url, postdata, headers=headers, timeout=20) 51 | 52 | if response.status_code == 200: 53 | return response.json() 54 | else: 55 | return None 56 | except Exception as e: 57 | print("httpGet failed, detail is:%s" % e) 58 | return None 59 | 60 | def http_post_request(self,url, params, add_to_headers=None): 61 | headers = { 62 | "Content-type": "application/x-www-form-urlencoded", 63 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36', 64 | } 65 | 66 | if add_to_headers: 67 | headers.update(add_to_headers) 68 | params['sign'] = self.create_sign(params) 69 | 70 | #print(params) 71 | #print(url) 72 | try: 73 | response = requests.post(url, params, headers=headers, timeout=20) # 74 | if response.status_code == 200: 75 | return response.json() 76 | else: 77 | return None 78 | except BaseException as e: 79 | print("httpPost failed, detail is:%s" % e) 80 | return None 81 | 82 | def api_key_get(self,params, request_path): 83 | method = 'get' 84 | host_url = self.params['URL'] 85 | 86 | url = host_url + '{0}.do?'.format(request_path) 87 | #print(url) 88 | #print(params) 89 | return self.http_get_request(url,params) 90 | 91 | def api_key_post(self,params, request_path): 92 | method = 'post' 93 | host_url = OK_PARAMS['URL'] 94 | params.update({'api_key': self.params['api_key']}) 95 | #print(params) 96 | url = host_url + '{0}.do'.format(request_path) 97 | return self.http_post_request(url,params) 98 | 99 | 100 | 101 | if __name__ == '__main__': 102 | ok = OK_Utils(OK_PARAMS) 103 | 104 | 105 | path = OK_API['DEPTH'] 106 | 107 | params={'symbol':'eth_usdt', 108 | } 109 | result = ok.api_key_get(params,path[0]) 110 | print(result['asks'][-5:-1]) -------------------------------------------------------------------------------- /app/ok_trade.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | try: 6 | from app.OK_Services import * 7 | except Exception as e: 8 | from OK_Services import * 9 | 10 | 11 | class OK_MAKER: 12 | 13 | def __init__(self,params): 14 | self._params = params 15 | self._OKServices = Ok_Services() 16 | 17 | def get_price_depth(self,symbol): 18 | ''' 19 | #获取市场行情深度 20 | :param symbol: 21 | :return: 22 | ''' 23 | sell=0.0000 24 | buy = 0.0000 25 | try: 26 | result = self._OKServices.get_future_depth(symbol) 27 | sell = result['asks'][-1][0] #获取卖二价 28 | buy = result['bids'][0][0] #获取买二价 29 | #print(result['asks'][-5:-1]) 30 | #print(result['bids'][0:5]) 31 | except Exception as e: 32 | print('error:%s'%e) 33 | return buy,sell 34 | finally: 35 | return buy,sell 36 | 37 | def send_price(self,symbol,buy,sell): 38 | 39 | if sell - buy > 1: 40 | buy_order = self._OKServices.send_future_order(symbol, OK_ORDER_TYPE['KD'], buy + 0.001, 0.001) 41 | sell_order = self._OKServices.send_future_order(symbol, OK_ORDER_TYPE['KK'], sell - 0.001, 0.001) 42 | else: 43 | buy_order,sell_order=[] 44 | 45 | return buy_order,sell_order 46 | 47 | def deal_net_pot(self): 48 | print('test') 49 | 50 | def get_user_pot(self,symbol): 51 | ''' 52 | 获取用户仓位信息 53 | :param symbol: 54 | :return: 55 | ''' 56 | result = self._OKServices.get_future_userinfo() 57 | 58 | return result['info'][symbol] 59 | 60 | def check_risk(self,symbol): 61 | ''' 62 | 检查系统风险,实现仓位控制 63 | :param symbol: 64 | :return: 65 | ''' 66 | risk = self.get_net_pot(symbol) 67 | if risk['keep_deposit']/risk['account_rights'] <= self._params['risk_rate']: 68 | print('now risk is :%s,limit risk:%s'%(risk['keep_deposit']/risk['account_rights'],self._params['risk_rate'])) 69 | return True 70 | else: 71 | print('now risk is :%s,limit risk:%s' % (risk['keep_deposit'] / risk['account_rights'], self._params['risk_rate'])) 72 | return False 73 | 74 | def get_user_position(self,symbol,contract_type='this_week'): 75 | ''' 76 | #获取仓位信息 77 | :param symbol:btc_usdt ltc_usdt eth_usdt etc_usdt bch_usdt 78 | :param contract_type: 79 | :return: 80 | ''' 81 | result={} 82 | result['status']=False 83 | hold = self._OKServices.get_future_position(symbol,contract_type) 84 | print(hold) 85 | if hold: 86 | result['buy_amount'] = hold['holding'][0]['buy_amount'] 87 | result['buy_available']=hold['holding'][0]['buy_available'] 88 | result['buy_cost'] = hold['holding'][0]['buy_price_cost'] 89 | result['sell_amount'] = hold['holding'][0]['sell_amount'] 90 | result['sell_available'] = hold['holding'][0]['sell_available'] 91 | result['sell_cost'] = hold['holding'][0]['sell_price_cost'] 92 | result['status'] = True 93 | else: 94 | result['status'] = False 95 | 96 | return result 97 | 98 | def get_ma_data(self,symbol,period='1min',size = 60,contract_type='this_week'): 99 | 100 | data={} 101 | data['status']=False 102 | ma={} 103 | sum_5=0.0000 104 | sum_10=0.0000 105 | sum_15=0.0000 106 | sum_30=0.0000 107 | sum_60=0.0000 108 | try: 109 | result = self._OKServices.get_future_kline(symbol,period,size,contract_type) 110 | if result: 111 | for i in range(0,size): 112 | if i<5: 113 | sum_5 = sum_5 + result[-1 - i][4] 114 | if i<10: 115 | sum_10 = sum_10 + result[-1 - i][4] 116 | if i<15: 117 | sum_15 = sum_15 + result[-1 - i][4] 118 | if i<30: 119 | sum_30 = sum_30+ result[-1 - i][4] 120 | if i<60: 121 | sum_60 = sum_60 + result[-1 - i][4] 122 | 123 | ma['price'] = round(result[-1][4],4) 124 | ma['ma5'] = round(sum_5/5,4) 125 | ma['ma10'] = round(sum_10/10,4) 126 | ma['ma15'] = round(sum_15/15,4) 127 | ma['ma30'] = round(sum_15 / 30, 4) 128 | ma['ma60'] = round(sum_15 / 60, 4) 129 | data['status'] = True 130 | data['data'] = ma 131 | except Exception as e: 132 | data['status']=False 133 | 134 | return data 135 | 136 | def get_trade_signal(self,symbol,period='1min',size = 60,contract_type='this_week'): 137 | 138 | result = ok_maker.get_ma_data(symbol, period,size) #获取均线数据 139 | if result['status']: 140 | 141 | if result['data']['price']>result['data']['ma10']: 142 | result['signal'] = 1 143 | 144 | if result['data']['price']result['data']['ma5'] and result['data']['ma5'] < result['data']['ma10'] result['data']['ma10'] and result['data']['ma10'] < result['data']['ma15']: 153 | # print('当前趋势price,ma5:%s,ma10:%s,ma15:%s'%(result['data']['price'],result['data']['ma5'],result['data']['ma10'],result['data']['ma15'])) 154 | result['signal'] = 2 155 | if result['data']['price']>result['data']['ma5']> result['data']['ma10']>result['data']['ma15']:#当前趋势为上涨行情 156 | # print('当前趋势price,ma5:%s,ma10:%s,ma15:%s'%(result['data']['price'],result['data']['ma5'],result['data']['ma10'],result['data']['ma15'])) 157 | result['signal'] = 3 158 | 159 | if result['data']['price'] result['data']['ma10']>result['data']['ma15']:#价格上出现变盘信号 160 | # print('当前趋势price,ma5:%s,ma10:%s,ma15:%s'%(result['data']['price'],result['data']['ma5'],result['data']['ma10'],result['data']['ma15'])) 161 | result['signal'] = -1 162 | if result['data']['ma5']< result['data']['ma10'] and result['data']['ma10'] > result['data']['ma15']: 163 | # print('当前趋势price,ma5:%s,ma10:%s,ma15:%s'%(result['data']['price'],result['data']['ma5'],result['data']['ma10'],result['data']['ma15'])) 164 | result['signal'] = -2 165 | if result['data']['price'] 1018.500: 189 | amount = random.randrange(4000, 6000) / 10000 190 | result = ok_maker.send_pc_order('eth_usdt', OK_ORDER_TYPE['PD'], price[1] - 0.001, amount) 191 | print(result) 192 | 193 | 194 | result = ok_maker._OKServices.send_future_order(symbol, type, price, amount,match_price,lever_rate,contract_type) 195 | 196 | return result 197 | 198 | def get_wt_orders(self,symbol): 199 | ''' 200 | #获取用户委托订单类型 201 | :param symbol: 202 | :return: 203 | ''' 204 | return_data={} 205 | wt_type = [] 206 | wt_orders = self._OKServices.get_future_order(symbol=symbol, order_id=-1) 207 | if wt_orders: 208 | for order in wt_orders['orders']: 209 | wt_type.append(order['type']) 210 | 211 | return_data['orders']=wt_orders['orders'] 212 | return_data['type_list']=wt_type 213 | 214 | return return_data # 返回用户委托订单类型 215 | 216 | def send_pc_orders(self,symbol,signal): 217 | #print('用户持仓数据: %s' % position) 218 | #print('signal数据:%s'%signal_data) 219 | order_info = {} 220 | user_pos = self.get_user_position(symbol) 221 | if user_pos['status']: 222 | #print('交易信号:%s'%signal['signal']) 223 | wt_orders = self.get_wt_orders(symbol) 224 | if signal['signal'] == 1: 225 | if user_pos['sell_available']>0: 226 | pk_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PK'], 227 | match_price=0, price=signal['data']['ma10']-0.5, 228 | amount=user_pos['sell_available']) 229 | print(pk_order) 230 | if pk_order: 231 | order_info = pk_order 232 | print('执行空头止损(市价):%s' % pk_order) 233 | elif signal['signal'] == 2:#表示行情开始上涨,处理空头 234 | if user_pos['sell_amount']>0 and OK_ORDER_TYPE['PK'] not in wt_orders['type_list']:#表示持有空头持仓,并且没有平空的委托订单 235 | if user_pos['sell_available']>0:#可平空数量不为0 236 | pk_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PK'], 237 | match_price=0, price=signal['ma10'], 238 | amount=user_pos['sell_available']) 239 | if pk_order: 240 | order_info = pk_order 241 | print('执行空头止损(挂单):%s' % pk_order) 242 | elif signal['signal'] == 3:#表示行情开始上涨,处理空头 # 243 | if user_pos['sell_amount'] > 0: 244 | if user_pos['sell_available']>0:#可平空数量不为0 245 | pk_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PK'], 246 | match_price=1, price=signal['ma5'], 247 | amount=user_pos['sell_available']) 248 | if pk_order: 249 | order_info = pk_order 250 | print('执行空头止损(挂单):%s' % pk_order) 251 | 252 | if OK_ORDER_TYPE['PK'] in wt_orders['type_list']:#存在平空委托订单,将平空委托订单撤销 253 | for order in wt_orders['orders']: 254 | if order['type'] == OK_ORDER_TYPE['PK']: # 表示平多未成交,摊销未成交订单,执行市价成交 255 | cancel_order = self._OKServices.cancel_future_order(symbol, order['order_id']) 256 | if cancel_order: # 撤销订单成功 257 | pk_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PD'], 258 | match_price=1, price=signal['ma5'], 259 | amount=user_pos['sell_available']) 260 | if pk_order: 261 | order_info = pk_order 262 | print('执行空头止损(市平):%s' % pk_order) 263 | #------------------------------------处理多头 头寸-----------------# 264 | elif signal['signal'] == -1: 265 | if user_pos['buy_available']>0: 266 | pd_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PD'], 267 | match_price=0, price=signal['data']['ma10']+0.5, 268 | amount=user_pos['buy_available']) 269 | print(pd_order) 270 | if pd_order: 271 | order_info = pd_order 272 | print('执行多头止损(市价):%s' % pd_order) 273 | elif signal['signal'] == -2:#表示行情开始下跌,处理多头 274 | if user_pos['buy_amount']>0 and OK_ORDER_TYPE['PK'] not in wt_orders['type_list']:#表示持有多头持仓,并且没有平多的委托订单 275 | if user_pos['buy_available']>0:#可平多数量不为0 276 | pd_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PD'], 277 | match_price=0, price=signal['ma10'], 278 | amount=user_pos['buy_available']) 279 | print(pd_order) 280 | if pd_order: 281 | order_info = pd_order 282 | print('执行多头止损(挂单):%s' % pd_order) 283 | elif signal['signal'] == -3:#表示行情开始下跌,处理多头 284 | if user_pos['buy_amount']>0:#表示持有多头持仓 285 | if user_pos['buy_available']>0:#可平多数量不为0 286 | pd_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['PD'], 287 | match_price=1, price=signal['ma5'], 288 | amount=user_pos['buy_available']) 289 | if pd_order: 290 | order_info = pd_order 291 | print('执行多头止损(挂单):%s' % pd_order) 292 | if OK_ORDER_TYPE['PD'] in wt_orders['type_list']:#表示存在多头平仓委托订单,撤销多头平仓委托订单 293 | for order in wt_orders['orders']: 294 | if order['type'] == OK_ORDER_TYPE['PD']: # 表示平多未成交,摊销未成交订单,执行市价成交 295 | cancel_order = self._OKServices.cancel_future_order(symbol, order['order_id']) 296 | if cancel_order: # 撤销订单成功 297 | pd_order = self._OKServices.send_future_order(symbol=symbol, 298 | type=OK_ORDER_TYPE['PD'], 299 | match_price=1, 300 | price=signal['ma5'], 301 | amount=user_pos['buy_available']) 302 | if pd_order: 303 | order_info = pd_order 304 | print('执行多头止损(市平):%s' % pd_order) 305 | 306 | return order_info 307 | def send_kc_orders(self,symbol,signal): 308 | 309 | order_info={} 310 | user_pos = self.get_user_position(symbol) 311 | if user_pos['status']: 312 | print('交易信号:%s' % signal['signal']) 313 | wt_orders=self.get_wt_orders(symbol) 314 | if signal['signal'] == 1:#买入 315 | if user_pos['buy_amount'] == 0 and OK_ORDER_TYPE['KD'] not in wt_orders['type_list']: 316 | buy_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['KD'], 317 | match_price=0, price=signal['data']['ma10']-0.5, 318 | amount=self._params['amount']) # 挂单在10日均线上 319 | if buy_order: 320 | order_info = buy_order 321 | print('多头已下单(市价):%s' % buy_order) 322 | elif signal['signal'] == 2: # 表示做多信号 323 | if user_pos['buy_amount'] == 0 and OK_ORDER_TYPE['KD'] not in wt_orders['type_list']: # 表示当前没有持仓,并且没有买入委托订单 324 | buy_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['KD'], 325 | match_price=0, price=signal['ma10'], 326 | amount=self._params['amount']) # 挂单在10日均线上 327 | if buy_order: 328 | order_info = buy_order 329 | print('多头已下单(挂单):%s' % buy_order) 330 | elif signal['signal'] == 3: # 表示上涨趋势已形成,不需要撤销未成交的委托订单,万一捡个漏呢 331 | if user_pos['buy_amount'] == 0:#表示当前没有买入持仓,执行市价买入 332 | buy_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['KD'], 333 | match_price=1, price=signal['ma5'], 334 | amount=self._params['amount']) # 挂单在5日均线上 335 | if buy_order: 336 | order_info = buy_order 337 | print('多头已下单(市价):%s' % buy_order) 338 | # --------------------------------------处理做空订单----------------------------------- 339 | elif signal['signal'] == -1: 340 | if user_pos['sell_amount'] == 0 and OK_ORDER_TYPE['KK'] not in wt_orders['type_list']: 341 | sell_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['KK'], 342 | match_price=0, price=signal['data']['ma10']+0.5, 343 | amount=self._params['amount']) # 挂单在5日均线上 344 | if sell_order: 345 | order_info = sell_order 346 | print('空头已下单(市价):%s' % sell_order) 347 | elif signal['signal'] == -2: # 表示做空信号 348 | if user_pos['sell_amount'] == 0 and OK_ORDER_TYPE['KK'] not in wt_orders['type_list']: # 表示没有空头持仓,执行做空操作 349 | sell_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['KK'], 350 | match_price=0,price=signal['ma10'], 351 | amount=self._params['amount']) # 挂单在10日均线上 352 | if sell_order: 353 | order_info = sell_order 354 | print('空头已下单(挂单):%s' % sell_order) 355 | elif signal['signal'] == -3: # 表示下跌趋势已形成,执行市价买入 356 | if user_pos['sell_amount'] == 0: 357 | sell_order = self._OKServices.send_future_order(symbol=symbol, type=OK_ORDER_TYPE['KK'], 358 | match_price=1, price=signal['ma5'], 359 | amount=self._params['amount']) # 挂单在5日均线上 360 | if sell_order: 361 | order_info = sell_order 362 | print('空头已下单(市价):%s' % sell_order) 363 | 364 | return order_info 365 | def trade_system(self,symbol,period='1min',size = 60,contract_type='this_week'): 366 | 367 | return_data = {} 368 | signal = self.get_trade_signal(symbol, period, size, contract_type) # 获取行情交易信号 369 | if signal['status']:#获取到交易信号 370 | #pc_order = self.send_pc_orders(symbol, signal) #处理平仓 371 | #if pc_order: 372 | #print("平仓订单:%s"%pc_order) 373 | #kc_order = self.send_kc_orders(symbol, signal) #处理开仓# 374 | #if kc_order: 375 | #print("平仓订单:%s"%kc_order) 376 | 377 | print(signal) 378 | 379 | return return_data 380 | 381 | if __name__ == '__main__': 382 | params ={'dif':1.5} 383 | params ={'m5m10':0.2} 384 | params ={'risk_rate':0.5} 385 | params ={'amount':2} 386 | 387 | ok_maker = OK_MAKER(params) 388 | symbol = 'eth_usdt' 389 | while True: 390 | ok_maker.trade_system(symbol,'5min') 391 | 392 | 393 | 394 | 395 | -------------------------------------------------------------------------------- /app/settings.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | ''' 5 | # Maintainter Robin.chen 6 | # Date:2018-01-14 7 | 8 | ''' 9 | 10 | ORDER_TYPE={'BL':'buy-limit', 11 | 'SL':'sell-limit', 12 | 'BM':'buy-market', 13 | 'SM':'sell-market'} 14 | 15 | OK_PARAMS={ 16 | 'api_key':'', 17 | 'SECRET_KEY':'', 18 | 'URL':'https://www.okex.com/api/v1/', 19 | 'ETH_ADDRESS':'0xecae55307a0e5c855d518dc3c2065f733fd0b6bc' 20 | } 21 | 22 | OK_ORDER_TYPE={'KD':1, 23 | 'KK':2, 24 | 'PD':3, 25 | 'PK':4, 26 | 'BM':'buy_market', 27 | 'BL':'buy', 28 | 'SM':'sell_market', 29 | 'SL':'sell' 30 | } 31 | 32 | OK_TRANSFER_TYPE={'E2F':1, 33 | 'F2E':2 34 | } 35 | 36 | OK_API={ 37 | #----makert api---- 38 | 'TICKER':('ticker','get'), 39 | 'DEPTH':('depth','get'), 40 | 'TRADES':('trades','get'), 41 | 'KLINE':('kline','get'), 42 | #-----合约账户API-------- 43 | 'F_TICKER':('future_ticker','get'), 44 | 'F_DEPTH':('future_depth','get'), 45 | 'F_TRADES':('future_trades','get'), 46 | 'F_INDEX':('future_index','get'), 47 | 'EXCHANGE_RATE':('exchange_rate','get'), 48 | 'F_EST_PRICE':('future_estimated_price','get'), 49 | 'F_KLINE':('future_kline','get'), 50 | 'F_H_AMOUNT':('future_hold_amount','get'), 51 | 'F_PRICE_LIMIT':('future_price_limit','get'), 52 | 53 | #---------合约账户交易API----------- 54 | 'F_USERINFO':('future_userinfo','post'), 55 | 'F_POSITION':('future_position','post'), 56 | 'F_TRADE':('future_trade','post'), 57 | 'F_TRADES_HISTORY':('future_trades_history','post'), 58 | 'F_BATCH_TRADE': ('future_batch_trade', 'post'), 59 | 'F_CANCEL': ('future_cancel', 'post'), 60 | 'F_ORDER_INFO': ('future_order_info', 'post'), 61 | 'F_ORDERS_INFO': ('future_orders_info', 'post'), 62 | 'F_USDERINFO_4FIX': ('future_userinfo_4fix', 'post'), 63 | 'F_POSITION_4FIX': ('future_position_4fix', 'post'), 64 | 'F_EXPLOSIVE': ('future_explosive', 'post'), 65 | 'F_DEVOLVE': ('future_devolve', 'post'), 66 | 67 | 68 | #---trade api---- 69 | 'USERINFO':('userinfo','post'), 70 | 'TRADE':('trade','post'), 71 | 'BATCH_TRADE':('batch_trade','post'), 72 | 'CANCEL_ORDER':('cancel_order','post'), 73 | 'ORDER_INFO':('order_info','post'), 74 | 'ORDERS_INFO':('orders_info','post'), 75 | 'ORDER_HISTORY':('order_history','post'), 76 | 'WITHDRAW':('withdraw','post'), 77 | 'CANCEL_WITHDRAW':('cancel_withdraw','post'), 78 | 'WITHDRAW_INFO':('withdraw_info','post'), 79 | 'ACCOUNT_RECORDS':('account_records','post'), 80 | } -------------------------------------------------------------------------------- /base.txt: -------------------------------------------------------------------------------- 1 | requests == 2.18.4 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jawf/OK_trade/f33576a30867912689bbb80df6da4f0ea9d3d1cb/requirements.txt --------------------------------------------------------------------------------