From 04bc6b2114d7f9a6c3fd32764391945cf9c0621e Mon Sep 17 00:00:00 2001 From: lu-yi-hsun Date: Wed, 2 Oct 2019 11:58:24 +0800 Subject: [PATCH] 3.9.3 --- README.md | 24 +++++++++++--- iqoptionapi/api.py | 9 ++++-- iqoptionapi/expiration.py | 24 ++++++++------ iqoptionapi/stable_api.py | 40 +++++++++++++++++++----- iqoptionapi/ws/chanels/digital_option.py | 12 +++++++ iqoptionapi/ws/client.py | 3 +- 6 files changed, 87 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d519642a..46b9848d 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,21 @@ [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.me/iqoptionapi) -last Version:3.9.2 +last update:2019/10/2 -last update:2019/9/11 +last Version:3.9.3 + +change and fix close digit(close_position) to close_digital_option + +fix buy_digital problem + +get_order only work for forex..., digital not work + +fix close_position api for forex... + +fix binary option buy api support python2&python3 + + Version:3.9.2 @@ -623,11 +635,11 @@ else: #### close digital ```python -I_want_money.close_position(id) +I_want_money.close_digital_option(id) ``` #### get digital data ```python -print(I_want_money.get_order(id)) +#print(I_want_money.get_order(id))#not work for digital print(I_want_money.get_positions("digital-option")) print(I_want_money.get_position_history("digital-option")) ``` @@ -702,7 +714,7 @@ check,order_id=I_want_money.buy_order(instrument_type=instrument_type, instrumen take_profit_value=take_profit_value, take_profit_kind=take_profit_kind, use_trail_stop=use_trail_stop, auto_margin_call=auto_margin_call, use_token_for_commission=use_token_for_commission) -print(I_want_money.get_order(order_id)) +print(I_want_money.get_order(order_id)) print(I_want_money.get_positions("crypto")) print(I_want_money.get_position_history("crypto")) print(I_want_money.get_available_leverages("crypto","BTCUSD")) @@ -795,6 +807,8 @@ I_want_money.change_order(ID_Name=ID_Name,order_id=order_id, #### get_order + + get infomation about buy_order_id return (True/False,get_order,None) diff --git a/iqoptionapi/api.py b/iqoptionapi/api.py index 5e939934..4b564ef6 100644 --- a/iqoptionapi/api.py +++ b/iqoptionapi/api.py @@ -51,7 +51,7 @@ from iqoptionapi.ws.chanels.subscribe import Subscribe_Instrument_Quites_Generated from iqoptionapi.ws.chanels.unsubscribe import Unsubscribe_Instrument_Quites_Generated -from iqoptionapi.ws.chanels.digital_option import Digital_options_place_digital_option +from iqoptionapi.ws.chanels.digital_option import * from iqoptionapi.ws.chanels.api_game_getoptions import Getoptions from iqoptionapi.ws.chanels.sell_option import Sell_Option from iqoptionapi.ws.chanels.change_tpsl import Change_Tpsl @@ -128,7 +128,8 @@ class IQOptionAPI(object): # pylint: disable=too-many-instance-attributes # --for binary option multi buy buy_multi_result = None buy_multi_option = {} - + # + result=None # ------------------ def __init__(self, host, username, password, proxies=None): @@ -525,6 +526,10 @@ def unsubscribe_instrument_quites_generated(self): def place_digital_option(self): return Digital_options_place_digital_option(self) + @property + def close_digital_option(self): + return Digital_options_close_position(self) + # ____BUY_for__Forex__&&__stock(cfd)__&&__ctrpto_____ @property def buy_order(self): diff --git a/iqoptionapi/expiration.py b/iqoptionapi/expiration.py index 434ef5e8..7b5a05d6 100644 --- a/iqoptionapi/expiration.py +++ b/iqoptionapi/expiration.py @@ -2,20 +2,24 @@ import time from datetime import datetime,timedelta - - +#https://docs.python.org/3/library/datetime.html +#If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive. +#time.mktime(dt.timetuple()) +def date_to_timestamp(dt): + #local timezone to timestamp support python2 pytohn3 + return time.mktime(dt.timetuple()) def get_expiration_time(timestamp,duration): # now_date = datetime.fromtimestamp(timestamp) exp_date=now_date.replace(second=0,microsecond=0) - if (int((exp_date+timedelta(minutes=1)).timestamp())-timestamp)>30: + if (int(date_to_timestamp(exp_date+timedelta(minutes=1)))-timestamp)>30: exp_date= exp_date+timedelta(minutes=1) else: exp_date= exp_date+timedelta(minutes=2) exp=[] for _ in range(5): - exp.append(exp_date.timestamp()) + exp.append(date_to_timestamp(exp_date)) exp_date= exp_date+timedelta(minutes=1) @@ -25,8 +29,8 @@ def get_expiration_time(timestamp,duration): now_date = datetime.fromtimestamp(timestamp) exp_date=now_date.replace(second=0,microsecond=0) while index60*5: - exp.append(exp_date.timestamp()) + if int(exp_date.strftime("%M"))%15==0 and (int(date_to_timestamp(exp_date))-int(timestamp))>60*5: + exp.append(date_to_timestamp(exp_date)) index=index+1 exp_date= exp_date+timedelta(minutes=1) @@ -43,22 +47,22 @@ def get_expiration_time(timestamp,duration): def get_remaning_time(timestamp): now_date = datetime.fromtimestamp(timestamp) exp_date=now_date.replace(second=0,microsecond=0) - if (int((exp_date+timedelta(minutes=1)).timestamp())-timestamp)>30: + if (int(date_to_timestamp(exp_date+timedelta(minutes=1)))-timestamp)>30: exp_date= exp_date+timedelta(minutes=1) else: exp_date= exp_date+timedelta(minutes=2) exp=[] for _ in range(5): - exp.append(exp_date.timestamp()) + exp.append(date_to_timestamp(exp_date)) exp_date= exp_date+timedelta(minutes=1) idx=11 index=0 now_date = datetime.fromtimestamp(timestamp) exp_date=now_date.replace(second=0,microsecond=0) while index60*5: - exp.append(exp_date.timestamp()) + if int(exp_date.strftime("%M"))%15==0 and (int(date_to_timestamp(exp_date))-int(timestamp))>60*5: + exp.append(date_to_timestamp(exp_date)) index=index+1 exp_date= exp_date+timedelta(minutes=1) diff --git a/iqoptionapi/stable_api.py b/iqoptionapi/stable_api.py index c3a06ca1..58cc5b41 100644 --- a/iqoptionapi/stable_api.py +++ b/iqoptionapi/stable_api.py @@ -773,11 +773,24 @@ def buy_digital_spot(self, active,amount, action, duration): return self.api.digital_option_placed_id def buy_digital(self, amount, instrument_id): - self.api.position_changed = None - return self.buy_order(instrument_type="digital-option", - instrument_id=instrument_id, - side="buy", type="market", amount=amount, - limit_price=0, leverage=1) + self.api.digital_option_placed_id=None + self.api.place_digital_option(instrument_id,amount) + start_t=time.time() + while self.api.digital_option_placed_id==None: + if time.time()-start_t>30: + logging.error('buy_digital loss digital_option_placed_id') + return False,None + return True,self.api.digital_option_placed_id + def close_digital_option(self,position_id): + self.api.result=None + while self.get_async_order(position_id)==None: + pass + position_changed=self.get_async_order(position_id) + self.api.close_digital_option(position_changed["id"]) + while self.api.result==None: + pass + return self.api.result + def check_win_digital(self, buy_order_id): check, data = self.get_position(buy_order_id) if check: @@ -976,8 +989,8 @@ def cancel_order(self, buy_order_id): else: return False - def close_position(self, buy_order_id): - check, data = self.get_order(buy_order_id) + def close_position(self, position_id): + check, data = self.get_order(position_id) if data["position_id"] != None: self.api.close_position_data = None self.api.close_position(data["position_id"]) @@ -989,6 +1002,19 @@ def close_position(self, buy_order_id): return False else: return False + def close_position_v2(self,position_id): + while self.get_async_order(position_id)==None: + pass + position_changed=self.get_async_order(position_id) + self.api.close_position(position_changed["id"]) + while self.api.close_position_data == None: + pass + if self.api.close_position_data["status"] == 2000: + return True + else: + return False + + def get_overnight_fee(self, instrument_type, active): self.api.overnight_fee = None diff --git a/iqoptionapi/ws/chanels/digital_option.py b/iqoptionapi/ws/chanels/digital_option.py index 04ada9a3..4bb7efcf 100644 --- a/iqoptionapi/ws/chanels/digital_option.py +++ b/iqoptionapi/ws/chanels/digital_option.py @@ -21,3 +21,15 @@ def __call__(self,instrument_id,amount): } self.send_websocket_request(self.name, data) +class Digital_options_close_position(Base): + name = "sendMessage" + def __call__(self,position_id): + data = { + "name": "digital-options.close-position", + "version":"1.0", + "body":{ + "position_id":int(position_id) + } + } + self.send_websocket_request(self.name, data) + diff --git a/iqoptionapi/ws/client.py b/iqoptionapi/ws/client.py index 78ee7593..3974843b 100644 --- a/iqoptionapi/ws/client.py +++ b/iqoptionapi/ws/client.py @@ -199,7 +199,8 @@ def on_message(self, wss, message): # pylint: disable=unused-argument self.api.digital_option_placed_id=message["msg"]["id"] except: self.api.digital_option_placed_id="error" - + elif message["name"]=="result": + self.api.result=message["msg"]["success"] elif message["name"]=="instrument-quotes-generated": Active_name=list(OP_code.ACTIVES.keys())[list(OP_code.ACTIVES.values()).index(message["msg"]["active"])] period=message["msg"]["expiration"]["period"]