def conectar(id_activo): velas = None while velas is None: print "Intento de conexión..." api = IQOptionAPI("iqoption.com", "usuario", "contraseña") api.connect() api.getcandles(id_activo, 1) velas = api.candles.candles_data return velas
def conectar(id_activo): for i in range(4): api = IQOptionAPI("iqoption.com", "usuario", "contraseña") api.connect() for j in range(4): api.getcandles(1, 1) time.sleep(0.5) velas = api.candles.candles_data time.sleep(30) return velas
class IQ_Option: __version__ = "2.1.1" def __init__(self, email, password): self.size = [ 1, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200, 14400, 28800, 43200, 86400, 604800, 2592000 ] self.email = email self.password = password self.suspend = 0.5 self.thread = None self.subscribe_candle = [] self.subscribe_candle_all_size = [] self.subscribe_mood = [] #--start self.connect() self.update_ACTIVES_OPCODE() self.get_balance_id() def get_server_time(self): return self.api.timesync.server_timestamp def connect(self): while True: try: self.api.close() except: pass #logging.error('**warning** self.api.close() fail') self.api = IQOptionAPI("iqoption.com", self.email, self.password) check = None try: check = self.api.connect() except: logging.error('**error** connect() fail') if check == True: #-------------reconnect subscribe_candle try: for ac in self.subscribe_candle: sp = ac.split(",") self.start_candles_one_stream(sp[0], sp[1]) except: pass #----------------- try: for ac in self.subscribe_candle_all_size: self.start_candles_all_size_stream(ac) except: pass #-------------reconnect subscribe_mood try: for ac in self.subscribe_mood: self.start_mood_stream(ac) except: pass break time.sleep(self.suspend * 2) def check_connect(self): #True/False return self.api.websocket_alive() #wait for timestamp getting #_________________________UPDATE ACTIVES OPCODE_____________________ def get_all_ACTIVES_OPCODE(self): return OP_code.ACTIVES def update_ACTIVES_OPCODE(self): #update from binary option self.get_ALL_Binary_ACTIVES_OPCODE() #crypto /dorex/cfd self.get_all_instruments() dicc = {} for lis in sorted(OP_code.ACTIVES.items(), key=operator.itemgetter(1)): dicc[lis[0]] = lis[1] OP_code.ACTIVES = dicc def instruments_input(self, types): time.sleep(self.suspend) self.api.instruments = None while self.api.instruments == None: try: self.api.get_instruments(types) start = time.time() while self.api.instruments == None and time.time() - start < 10: pass except: logging.error('**error** api.get_instruments need reconnect') self.connect() for ins in self.api.instruments["instruments"]: OP_code.ACTIVES[ins["id"]] = ins["active_id"] def get_all_instruments(self): self.instruments_input("crypto") self.instruments_input("forex") self.instruments_input("cfd") def get_ALL_Binary_ACTIVES_OPCODE(self): init_info = self.get_all_init() for i in init_info["result"]["binary"]["actives"]: OP_code.ACTIVES[(init_info["result"]["binary"]["actives"][i] ["name"]).split(".")[1]] = int(i) #_________________________self.api.get_api_option_init_all() wss______________________ def get_all_init(self): self.api.api_option_init_all_result = None while True: try: self.api.get_api_option_init_all() start = time.time() while True: if time.time() - start > 30: logging.error('**warning** get_all_init late 30 sec') break try: if self.api.api_option_init_all_result != None: break except: time.sleep(self.suspend * 5) break if self.api.api_option_init_all_result["isSuccessful"] == True: break except: logging.error('**error** get_all_init need reconnect') self.connect() return self.api.api_option_init_all_result #return OP_code.ACTIVES def get_profit(self, ACTIVES): init_info = self.get_all_init() return (100.0 - init_info["result"]["turbo"]["actives"][str( OP_code.ACTIVES[ACTIVES])]["option"]["profit"]["commission"] ) / 100.0 def get_all_profit(self): all_profit = {} init_info = self.get_all_init() for active in OP_code.ACTIVES: try: prof = (100.0 - init_info["result"]["turbo"]["actives"][str( OP_code.ACTIVES[active])]["option"]["profit"]["commission"] ) / 100.0 all_profit[active] = prof except: pass return all_profit #______________________________________self.api.getprofile() https________________________________ def get_profile(self): while True: try: respon = self.api.getprofile().json() time.sleep(self.suspend) if respon["isSuccessful"] == True: return respon except: logging.error('**error** get_profile try reconnect') self.connect() def get_balance_id(self): self.api.profile.balance_id = None while True: try: respon = self.get_profile() self.api.profile.balance_id = respon["result"]["balance_id"] break except: logging.error('**error** get_balance()') time.sleep(self.suspend) return self.api.profile.balance def get_balance(self): self.api.profile.balance = None while True: try: respon = self.get_profile() self.api.profile.balance = respon["result"]["balance"] break except: logging.error('**error** get_balance()') time.sleep(self.suspend) return self.api.profile.balance def get_balances(self): #self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balances = respon["result"]["balances"] break except: logging.error('**error** get_balances()') pass time.sleep(self.suspend) return self.api.profile.balances def get_balance_mode(self): #self.api.profile.balance_type=None while True: try: respon = self.get_profile() self.api.profile.balance_type = respon["result"][ "balance_type"] break except: logging.error('**error** get_balance_mode()') pass time.sleep(self.suspend) if self.api.profile.balance_type == 1: return "REAL" elif self.api.profile.balance_type == 4: return "PRACTICE" def change_balance(self, Balance_MODE): real_id = None practice_id = None while True: try: self.get_balances() for accunt in self.api.profile.balances: if accunt["type"] == 1: real_id = accunt["id"] if accunt["type"] == 4: practice_id = accunt["id"] break except: logging.error('**error** change_balance()') pass while self.get_balance_mode() != Balance_MODE: if Balance_MODE == "REAL": self.api.changebalance(real_id) elif Balance_MODE == "PRACTICE": self.api.changebalance(practice_id) else: print("ERROR doesn't have this mode") exit(1) #________________________________________________________________________ #_______________________ CANDLE _____________________________ #________________________self.api.getcandles() wss________________________ def get_candles(self, ACTIVES, interval, count, endtime): self.api.candles.candles_data = None while True: try: self.api.getcandles(OP_code.ACTIVES[ACTIVES], interval, count, endtime) while self.check_connect and self.api.candles.candles_data == None: pass if self.api.candles.candles_data != None: break except: logging.error('**error** get_candles need reconnect') self.connect() return self.api.candles.candles_data ####################################################### #______________________________________________________ #_____________________REAL TIME CANDLE_________________ #______________________________________________________ ####################################################### def start_candles_stream(self, ACTIVE, size, maxdict): if size == "all": for s in self.size: self.full_realtime_get_candle(ACTIVE, s, maxdict) self.api.real_time_candles_maxdict_table[ACTIVE][s] = maxdict self.start_candles_all_size_stream(ACTIVE) elif size in self.size: self.api.real_time_candles_maxdict_table[ACTIVE][size] = maxdict self.full_realtime_get_candle(ACTIVE, size, maxdict) self.start_candles_one_stream(ACTIVE, size) else: logging.error( '**error** start_candles_stream please input right size') def stop_candles_stream(self, ACTIVE, size): if size == "all": self.stop_candles_all_size_stream(ACTIVE) elif size in self.size: self.stop_candles_one_stream(ACTIVE, size) else: logging.error( '**error** start_candles_stream please input right size') def get_realtime_candles(self, ACTIVE, size): if size == "all": try: return self.api.real_time_candles[ACTIVE] except: logging.error( '**error** get_realtime_candles() size="all" can not get candle' ) return False elif size in self.size: try: return self.api.real_time_candles[ACTIVE][size] except: logging.error('**error** get_realtime_candles() size=' + str(size) + ' can not get candle') return False else: logging.error( '**error** get_realtime_candles() please input right "size"') def get_all_realtime_candles(self): return self.api.real_time_candles ################################################ #---------REAL TIME CANDLE Subset Function--------- ################################################ #---------------------full dict get_candle----------------------- def full_realtime_get_candle(self, ACTIVE, size, maxdict): candles = self.get_candles(ACTIVE, size, maxdict, self.api.timesync.server_timestamp) for can in candles: self.api.real_time_candles[str(ACTIVE)][int(size)][ can["from"]] = can #------------------------Subscribe ONE SIZE----------------------- def start_candles_one_stream(self, ACTIVE, size): if (str(ACTIVE + "," + str(size)) in self.subscribe_candle) == False: self.subscribe_candle.append((ACTIVE + "," + str(size))) start = time.time() self.api.candle_generated_check[str(ACTIVE)][int(size)] = {} while True: if time.time() - start > 20: logging.error( '**error** start_candles_one_stream late for 20 sec') return False try: if self.api.candle_generated_check[str(ACTIVE)][int( size)] == True: return True except: pass try: self.api.subscribe(OP_code.ACTIVES[ACTIVE], size) except: logging.error('**error** start_candles_stream reconnect') self.connect() time.sleep(1) def stop_candles_one_stream(self, ACTIVE, size): if ((ACTIVE + "," + str(size)) in self.subscribe_candle) == True: self.subscribe_candle.remove(ACTIVE + "," + str(size)) while True: try: if self.api.candle_generated_check[str(ACTIVE)][int( size)] == {}: return True except: pass self.api.candle_generated_check[str(ACTIVE)][int(size)] = {} self.api.unsubscribe(OP_code.ACTIVES[ACTIVE], size) time.sleep(self.suspend * 10) #------------------------Subscribe ALL SIZE----------------------- def start_candles_all_size_stream(self, ACTIVE): self.api.candle_generated_all_size_check[str(ACTIVE)] = {} if (str(ACTIVE) in self.subscribe_candle_all_size) == False: self.subscribe_candle_all_size.append(str(ACTIVE)) start = time.time() while True: if time.time() - start > 20: logging.error('**error** fail ' + ACTIVE + ' start_candles_all_size_stream late for 10 sec') return False try: if self.api.candle_generated_all_size_check[str( ACTIVE)] == True: return True except: pass try: self.api.subscribe_all_size(OP_code.ACTIVES[ACTIVE]) except: logging.error( '**error** start_candles_all_size_stream reconnect') self.connect() time.sleep(1) def stop_candles_all_size_stream(self, ACTIVE): if (str(ACTIVE) in self.subscribe_candle_all_size) == True: self.subscribe_candle_all_size.remove(str(ACTIVE)) while True: try: if self.api.candle_generated_all_size_check[str(ACTIVE)] == {}: break except: pass self.api.candle_generated_all_size_check[str(ACTIVE)] = {} self.api.unsubscribe_all_size(OP_code.ACTIVES[ACTIVE]) time.sleep(self.suspend * 10) #--------------------------------------------------------------------- ################################################ ################################################ #----------------------------------------------- #-----------------traders_mood---------------------- def start_mood_stream(self, ACTIVES): if ACTIVES in self.subscribe_mood == False: self.subscribe_mood.append(ACTIVES) while True: self.api.subscribe_Traders_mood(OP_code.ACTIVES[ACTIVES]) try: self.api.traders_mood[OP_code.ACTIVES[ACTIVES]] break except: time.sleep(1) def stop_mood_stream(self, ACTIVES): if ACTIVES in self.subscribe_mood == True: del self.subscribe_mood[ACTIVES] self.api.unsubscribe_Traders_mood(OP_code.ACTIVES[ACTIVES]) def get_traders_mood(self, ACTIVES): #return highter % return self.api.traders_mood[OP_code.ACTIVES[ACTIVES]] def get_all_traders_mood(self): #return highter % return self.api.traders_mood ############################################################################################## def check_win(self, id_number): #'win':win money 'equal':no win no loose 'loose':loose money while True: try: listinfodata_dict = self.api.listinfodata.get(id_number) if listinfodata_dict["game_state"] == 1: break except: pass self.api.listinfodata.delete(id_number) return listinfodata_dict["win"] def check_win_v2(self, id_number): while True: check, data = self.get_betinfo(id_number) if check: return data[str(id_number)]["win"] time.sleep(self.suspend) def get_betinfo(self, id_number): #INPUT:int while True: self.api.game_betinfo.isSuccessful = None start = time.time() try: self.api.get_betinfo(id_number) except: logging.error( '**error** def get_betinfo self.api.get_betinfo reconnect' ) self.connect() while self.api.game_betinfo.isSuccessful == None: if time.time() - start > 10: logging.error( '**error** get_betinfo time out need reconnect') self.connect() self.api.get_betinfo(id_number) time.sleep(self.suspend * 10) if self.api.game_betinfo.isSuccessful == True: return self.api.game_betinfo.isSuccessful, self.api.game_betinfo.dict else: return self.api.game_betinfo.isSuccessful, None time.sleep(self.suspend * 10) #__________________________BUY__________________________ #__________________FOR OPTION____________________________ def buy(self, price, ACTIVES, ACTION, expirations, force_buy=True): self.api.buy_successful == None while True: while True: try: self.api.buy(price, OP_code.ACTIVES[ACTIVES], ACTION, expirations) break except: if force_buy == False: return (False, None) logging.error('self.api.buy error') self.connect() pass start = time.time() while self.api.buy_successful == None: if time.time() - start > 60: logging.error('check buy_successful time late 60sec') break if self.api.buy_successful: return (True, self.api.buy_id) else: if force_buy == False: return (False, None) logging.error('**error** buy error...') self.connect() #__________________for digit_____________ def get_strike_list_data(self, ACTIVES, expirations): try: self.api.strike_list.del_data(ACTIVES, expirations) except: pass while True: self.api.get_strike_list(ACTIVES, expirations) all_strike_list_data = self.api.strike_list.get_All_data() try: return all_strike_list_data[str(ACTIVES), str(expirations)] except: pass time.sleep(self.suspend * 3) def buy_digit(self, price, direction, instrument_id): self.api.digit_buy(price, direction, instrument_id) #---------------------------------------------------------- #-----------------BUY_for__Forex__&&__stock(cfd)__&&__ctrpto def buy_order(self, instrument_type, instrument_id, side, type, amount, limit_price, leverage, stop_lose_price, take_profit_price): self.api.buy_order_id = None self.api.buy_order(instrument_type=instrument_type, instrument_id=instrument_id, side=side, type=type, amount=amount, limit_price=limit_price, stop_price=0, leverage=leverage, stop_lose_price=stop_lose_price, take_profit_price=take_profit_price) while self.api.buy_order_id == None: pass check, data = self.get_order(self.api.buy_order_id) while data["status"] == "pending_new": check, data = self.get_order(self.api.buy_order_id) time.sleep(1) if check: if data["status"] != "rejected": return True, self.api.buy_order_id else: return False, None else: return False, None def get_order(self, buy_order_id): #self.api.order_data["status"] #reject:you can not get this order #pending_new:this order is working now #filled:this order is ok now #new self.api.order_data = None self.api.get_order(buy_order_id) while self.api.order_data == None: pass if self.api.order_data["status"] == 2000: return True, self.api.order_data["msg"] else: return False, None #this function is heavy def get_positions(self, instrument_type): self.api.positions = None self.api.get_positions(instrument_type) while self.api.positions == None: pass if self.api.positions["status"] == 2000: return True, self.api.positions["msg"] else: return False, None #this function is heavy def get_position_history(self, instrument_type): self.api.position_history = None self.api.get_position_history(instrument_type) while self.api.position_history == None: pass if self.api.position_history["status"] == 2000: return True, self.api.position_history["msg"] else: return False, None def get_available_leverages(self, instrument_type, actives): self.api.available_leverages = None self.api.get_available_leverages(instrument_type, OP_code.ACTIVES[actives]) while self.api.available_leverages == None: pass if self.api.available_leverages["status"] == 2000: return True, self.api.available_leverages["msg"] else: return False, None def cancel_order(self, buy_order_id): self.api.order_canceled = None self.api.cancel_order(buy_order_id) while self.api.order_canceled == None: pass if self.api.order_canceled["status"] == 2000: return True else: return False def close_position(self, buy_order_id): check, data = self.get_order(buy_order_id) if data["position_id"] != None: self.api.close_position_data = None self.api.close_position(data["position_id"]) while self.api.close_position_data == None: pass if self.api.close_position_data["status"] == 2000: return True else: return False else: return False def get_overnight_fee(self, instrument_type, active): self.api.overnight_fee = None self.api.get_overnight_fee(instrument_type, OP_code.ACTIVES[active]) while self.api.overnight_fee == None: pass if self.api.overnight_fee["status"] == 2000: return True, self.api.overnight_fee["msg"] else: return False, None
class IQ_Option: __version__ = "4.5" def __init__(self, email, password): self.size = [ 1, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200, 14400, 28800, 43200, 86400, 604800, 2592000 ] self.email = email self.password = password self.suspend = 0.5 self.thread = None self.subscribe_candle = [] self.subscribe_candle_all_size = [] self.subscribe_mood = [] # for digit self.get_realtime_strike_list_temp_data = {} self.get_realtime_strike_list_temp_expiration = 0 # self.max_reconnect = 5 self.connect_count = 0 # --start self.connect() # self.update_ACTIVES_OPCODE() this auto function delay too long self.get_balance_id() # -------------------------------------------------------------------------- def get_server_timestamp(self): return self.api.timesync.server_timestamp def set_max_reconnect(self, number): self.max_reconnect = number def connect(self): while True: try: self.api.close() except: pass #logging.error('**warning** self.api.close() fail') if self.connect_count < self.max_reconnect or self.max_reconnect < 0: self.api = IQOptionAPI("iqoption.com", self.email, self.password) check = None check = self.api.connect() if check == True: # -------------reconnect subscribe_candle try: for ac in self.subscribe_candle: sp = ac.split(",") self.start_candles_one_stream(sp[0], sp[1]) except: pass # ----------------- try: for ac in self.subscribe_candle_all_size: self.start_candles_all_size_stream(ac) except: pass # -------------reconnect subscribe_mood try: for ac in self.subscribe_mood: self.start_mood_stream(ac) except: pass #---------for async get name: "position-changed", microserviceName self.api.setOptions(1, True) self.api.subscribe_position_changed( "position-changed", "multi-option", 2) self.api.subscribe_position_changed( "trading-fx-option.position-changed", "fx-option", 3) self.api.subscribe_position_changed( "position-changed", "crypto", 4) self.api.subscribe_position_changed( "position-changed", "forex", 5) self.api.subscribe_position_changed( "digital-options.position-changed", "digital-option", 6) self.api.subscribe_position_changed( "position-changed", "cfd", 7) break time.sleep(self.suspend * 2) self.connect_count = self.connect_count + 1 else: logging.error( '**error** reconnect() too many time please look log file') exit(1) def check_connect(self): # True/False idle_time = abs(self.api.timesync.server_timestamp - time.time()) if idle_time > 6: return False else: return True # wait for timestamp getting # _________________________UPDATE ACTIVES OPCODE_____________________ def get_all_ACTIVES_OPCODE(self): return OP_code.ACTIVES def update_ACTIVES_OPCODE(self): # update from binary option self.get_ALL_Binary_ACTIVES_OPCODE() #crypto /dorex/cfd self.instruments_input_all_in_ACTIVES() dicc = {} for lis in sorted(OP_code.ACTIVES.items(), key=operator.itemgetter(1)): dicc[lis[0]] = lis[1] OP_code.ACTIVES = dicc def get_name_by_activeId(self, activeId): info = self.get_financial_information(activeId) try: return info["msg"]["data"]["active"]["name"] except: return None def get_financial_information(self, activeId): self.api.financial_information = None self.api.get_financial_information(activeId) while self.api.financial_information == None: pass return self.api.financial_information def get_instruments(self, type): #type="crypto"/"forex"/"cfd" time.sleep(self.suspend) self.api.instruments = None while self.api.instruments == None: try: self.api.get_instruments(type) start = time.time() while self.api.instruments == None and time.time() - start < 10: pass except: logging.error('**error** api.get_instruments need reconnect') self.connect() return self.api.instruments def instruments_input_to_ACTIVES(self, type): instruments = self.get_instruments(type) for ins in instruments["instruments"]: OP_code.ACTIVES[ins["id"]] = ins["active_id"] def instruments_input_all_in_ACTIVES(self): self.instruments_input_to_ACTIVES("crypto") self.instruments_input_to_ACTIVES("forex") self.instruments_input_to_ACTIVES("cfd") def get_ALL_Binary_ACTIVES_OPCODE(self): init_info = self.get_all_init() for dirr in (["binary", "turbo"]): for i in init_info["result"][dirr]["actives"]: OP_code.ACTIVES[(init_info["result"][dirr]["actives"][i] ["name"]).split(".")[1]] = int(i) # _________________________self.api.get_api_option_init_all() wss______________________ def get_all_init(self): while True: self.api.api_option_init_all_result = None while True: try: self.api.get_api_option_init_all() break except: logging.error('**error** get_all_init need reconnect') self.connect() time.sleep(5) start = time.time() while True: if time.time() - start > 30: logging.error('**warning** get_all_init late 30 sec') break try: if self.api.api_option_init_all_result != None: break except: pass try: if self.api.api_option_init_all_result["isSuccessful"] == True: return self.api.api_option_init_all_result except: pass def get_all_init_v2(self): self.api.api_option_init_all_result_v2 = None self.api.get_api_option_init_all_v2() start_t = time.time() while self.api.api_option_init_all_result_v2 == None: if time.time() - start_t >= 30: logging.error('**warning** get_all_init_v2 late 30 sec') return None return self.api.api_option_init_all_result_v2 # return OP_code.ACTIVES #------- chek if binary/digit/cfd/stock... if open or not def get_all_open_time(self): #for binary option turbo and binary OPEN_TIME = nested_dict(3, dict) binary_data = self.get_all_init_v2() binary_list = ["binary", "turbo"] for option in binary_list: for actives_id in binary_data[option]["actives"]: active = binary_data[option]["actives"][actives_id] name = str(active["name"]).split(".")[1] if active["enabled"] == True: if active["is_suspended"] == True: OPEN_TIME[option][name]["open"] = False else: OPEN_TIME[option][name]["open"] = True else: OPEN_TIME[option][name]["open"] = active["enabled"] #for digital digital_data = self.get_digital_underlying_list_data()["underlying"] for digital in digital_data: name = digital["underlying"] schedule = digital["schedule"] OPEN_TIME["digital"][name]["open"] = False for schedule_time in schedule: start = schedule_time["open"] end = schedule_time["close"] if start < time.time() < end: OPEN_TIME["digital"][name]["open"] = True #for OTHER instrument_list = ["cfd", "forex", "crypto"] for instruments_type in instrument_list: ins_data = self.get_instruments(instruments_type)["instruments"] for detail in ins_data: name = detail["name"] schedule = detail["schedule"] OPEN_TIME[instruments_type][name]["open"] = False for schedule_time in schedule: start = schedule_time["open"] end = schedule_time["close"] if start < time.time() < end: OPEN_TIME[instruments_type][name]["open"] = True return OPEN_TIME # --------for binary option detail def get_binary_option_detail(self): detail = nested_dict(2, dict) init_info = self.get_all_init() for actives in init_info["result"]["turbo"]["actives"]: name = init_info["result"]["turbo"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] detail[name]["turbo"] = init_info["result"]["turbo"]["actives"][ actives] for actives in init_info["result"]["binary"]["actives"]: name = init_info["result"]["binary"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] detail[name]["binary"] = init_info["result"]["binary"]["actives"][ actives] return detail def get_all_profit(self): all_profit = nested_dict(2, dict) init_info = self.get_all_init() for actives in init_info["result"]["turbo"]["actives"]: name = init_info["result"]["turbo"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] all_profit[name]["turbo"] = (100.0 - init_info["result"]["turbo"][ "actives"][actives]["option"]["profit"]["commission"]) / 100.0 for actives in init_info["result"]["binary"]["actives"]: name = init_info["result"]["binary"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] all_profit[name]["binary"] = ( 100.0 - init_info["result"]["binary"]["actives"][actives] ["option"]["profit"]["commission"]) / 100.0 return all_profit # ---------------------------------------- # ______________________________________self.api.getprofile() https________________________________ def get_profile(self): while True: try: respon = self.api.getprofile().json() time.sleep(self.suspend) if respon["isSuccessful"] == True: return respon except: logging.error('**error** get_profile try reconnect') self.connect() def get_balance_id(self): self.api.profile.balance_id = None while True: try: respon = self.get_profile() self.api.profile.balance_id = respon["result"]["balance_id"] break except: logging.error('**error** get_balance()') time.sleep(self.suspend) return self.api.profile.balance def get_balance(self): self.api.profile.balance = None while True: try: respon = self.get_profile() self.api.profile.balance = respon["result"]["balance"] break except: logging.error('**error** get_balance()') time.sleep(self.suspend) return self.api.profile.balance def get_balances(self): # self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balances = respon["result"]["balances"] break except: logging.error('**error** get_balances()') pass time.sleep(self.suspend) return self.api.profile.balances def get_balance_mode(self): # self.api.profile.balance_type=None while True: try: respon = self.get_profile() self.api.profile.balance_type = respon["result"][ "balance_type"] break except: logging.error('**error** get_balance_mode()') pass time.sleep(self.suspend) if self.api.profile.balance_type == 1: return "REAL" elif self.api.profile.balance_type == 4: return "PRACTICE" def reset_practice_balance(self): self.api.training_balance_reset_request = None self.api.reset_training_balance() while self.api.training_balance_reset_request == None: pass return self.api.training_balance_reset_request def change_balance(self, Balance_MODE): real_id = None practice_id = None while True: try: self.get_balances() for accunt in self.api.profile.balances: if accunt["type"] == 1: real_id = accunt["id"] if accunt["type"] == 4: practice_id = accunt["id"] break except: logging.error('**error** change_balance()') pass while self.get_balance_mode() != Balance_MODE: if Balance_MODE == "REAL": self.api.changebalance(real_id) elif Balance_MODE == "PRACTICE": self.api.changebalance(practice_id) else: logging.error("ERROR doesn't have this mode") exit(1) # ________________________________________________________________________ # _______________________ CANDLE _____________________________ # ________________________self.api.getcandles() wss________________________ def get_candles(self, ACTIVES, interval, count, endtime): self.api.candles.candles_data = None while True: try: self.api.getcandles(OP_code.ACTIVES[ACTIVES], interval, count, endtime) while self.check_connect and self.api.candles.candles_data == None: pass if self.api.candles.candles_data != None: break except: logging.error('**error** get_candles need reconnect') self.connect() return self.api.candles.candles_data ####################################################### # ______________________________________________________ # _____________________REAL TIME CANDLE_________________ # ______________________________________________________ ####################################################### def start_candles_stream(self, ACTIVE, size, maxdict): if size == "all": for s in self.size: self.full_realtime_get_candle(ACTIVE, s, maxdict) self.api.real_time_candles_maxdict_table[ACTIVE][s] = maxdict self.start_candles_all_size_stream(ACTIVE) elif size in self.size: self.api.real_time_candles_maxdict_table[ACTIVE][size] = maxdict self.full_realtime_get_candle(ACTIVE, size, maxdict) self.start_candles_one_stream(ACTIVE, size) else: logging.error( '**error** start_candles_stream please input right size') def stop_candles_stream(self, ACTIVE, size): if size == "all": self.stop_candles_all_size_stream(ACTIVE) elif size in self.size: self.stop_candles_one_stream(ACTIVE, size) else: logging.error( '**error** start_candles_stream please input right size') def get_realtime_candles(self, ACTIVE, size): if size == "all": try: return self.api.real_time_candles[ACTIVE] except: logging.error( '**error** get_realtime_candles() size="all" can not get candle' ) return False elif size in self.size: try: return self.api.real_time_candles[ACTIVE][size] except: logging.error('**error** get_realtime_candles() size=' + str(size) + ' can not get candle') return False else: logging.error( '**error** get_realtime_candles() please input right "size"') def get_all_realtime_candles(self): return self.api.real_time_candles ################################################ # ---------REAL TIME CANDLE Subset Function--------- ################################################ # ---------------------full dict get_candle----------------------- def full_realtime_get_candle(self, ACTIVE, size, maxdict): candles = self.get_candles(ACTIVE, size, maxdict, self.api.timesync.server_timestamp) for can in candles: self.api.real_time_candles[str(ACTIVE)][int(size)][ can["from"]] = can # ------------------------Subscribe ONE SIZE----------------------- def start_candles_one_stream(self, ACTIVE, size): if (str(ACTIVE + "," + str(size)) in self.subscribe_candle) == False: self.subscribe_candle.append((ACTIVE + "," + str(size))) start = time.time() self.api.candle_generated_check[str(ACTIVE)][int(size)] = {} while True: if time.time() - start > 20: logging.error( '**error** start_candles_one_stream late for 20 sec') return False try: if self.api.candle_generated_check[str(ACTIVE)][int( size)] == True: return True except: pass try: self.api.subscribe(OP_code.ACTIVES[ACTIVE], size) except: logging.error('**error** start_candles_stream reconnect') self.connect() time.sleep(1) def stop_candles_one_stream(self, ACTIVE, size): if ((ACTIVE + "," + str(size)) in self.subscribe_candle) == True: self.subscribe_candle.remove(ACTIVE + "," + str(size)) while True: try: if self.api.candle_generated_check[str(ACTIVE)][int( size)] == {}: return True except: pass self.api.candle_generated_check[str(ACTIVE)][int(size)] = {} self.api.unsubscribe(OP_code.ACTIVES[ACTIVE], size) time.sleep(self.suspend * 10) # ------------------------Subscribe ALL SIZE----------------------- def start_candles_all_size_stream(self, ACTIVE): self.api.candle_generated_all_size_check[str(ACTIVE)] = {} if (str(ACTIVE) in self.subscribe_candle_all_size) == False: self.subscribe_candle_all_size.append(str(ACTIVE)) start = time.time() while True: if time.time() - start > 20: logging.error('**error** fail ' + ACTIVE + ' start_candles_all_size_stream late for 10 sec') return False try: if self.api.candle_generated_all_size_check[str( ACTIVE)] == True: return True except: pass try: self.api.subscribe_all_size(OP_code.ACTIVES[ACTIVE]) except: logging.error( '**error** start_candles_all_size_stream reconnect') self.connect() time.sleep(1) def stop_candles_all_size_stream(self, ACTIVE): if (str(ACTIVE) in self.subscribe_candle_all_size) == True: self.subscribe_candle_all_size.remove(str(ACTIVE)) while True: try: if self.api.candle_generated_all_size_check[str(ACTIVE)] == {}: break except: pass self.api.candle_generated_all_size_check[str(ACTIVE)] = {} self.api.unsubscribe_all_size(OP_code.ACTIVES[ACTIVE]) time.sleep(self.suspend * 10) # ------------------------top_assets_updated--------------------------------------------- def subscribe_top_assets_updated(self, instrument_type): self.api.Subscribe_Top_Assets_Updated(instrument_type) def unsubscribe_top_assets_updated(self, instrument_type): self.api.Unsubscribe_Top_Assets_Updated(instrument_type) def get_top_assets_updated(self, instrument_type): if instrument_type in self.api.top_assets_updated_data: return self.api.top_assets_updated_data[instrument_type] else: return None # ----------------------------------------------- # -----------------traders_mood---------------------- def start_mood_stream(self, ACTIVES): if ACTIVES in self.subscribe_mood == False: self.subscribe_mood.append(ACTIVES) while True: self.api.subscribe_Traders_mood(OP_code.ACTIVES[ACTIVES]) try: self.api.traders_mood[OP_code.ACTIVES[ACTIVES]] break except: time.sleep(5) def stop_mood_stream(self, ACTIVES): if ACTIVES in self.subscribe_mood == True: del self.subscribe_mood[ACTIVES] self.api.unsubscribe_Traders_mood(OP_code.ACTIVES[ACTIVES]) def get_traders_mood(self, ACTIVES): # return highter % return self.api.traders_mood[OP_code.ACTIVES[ACTIVES]] def get_all_traders_mood(self): # return highter % return self.api.traders_mood ############################################################################################## def check_win(self, id_number): # 'win':win money 'equal':no win no loose 'loose':loose money while True: try: listinfodata_dict = self.api.listinfodata.get(id_number) if listinfodata_dict["game_state"] == 1: break except: pass self.api.listinfodata.delete(id_number) return listinfodata_dict["win"] def check_win_v2(self, id_number): while True: check, data = self.get_betinfo(id_number) if check: return data["result"]["data"][str(id_number)]["win"] time.sleep(self.suspend) # -------------------get infomation only for binary option------------------------ def get_betinfo(self, id_number): # INPUT:int while True: self.api.game_betinfo.isSuccessful = None start = time.time() try: self.api.get_betinfo(id_number) except: logging.error( '**error** def get_betinfo self.api.get_betinfo reconnect' ) self.connect() while self.api.game_betinfo.isSuccessful == None: if time.time() - start > 10: logging.error( '**error** get_betinfo time out need reconnect') self.connect() self.api.get_betinfo(id_number) time.sleep(self.suspend * 10) if self.api.game_betinfo.isSuccessful == True: return self.api.game_betinfo.isSuccessful, self.api.game_betinfo.dict else: return self.api.game_betinfo.isSuccessful, None time.sleep(self.suspend * 10) def get_optioninfo(self, limit): self.api.api_game_getoptions_result = None self.api.get_options(limit) while self.api.api_game_getoptions_result == None: pass return self.api.api_game_getoptions_result def get_optioninfo_v2(self, limit): self.api.get_options_v2_data = None self.api.get_options_v2(limit, "binary,turbo") while self.api.get_options_v2_data == None: pass return self.api.get_options_v2_data # __________________________BUY__________________________ # __________________FOR OPTION____________________________ def buy_multi(self, price, ACTIVES, ACTION, expirations): self.api.buy_multi_option = {} if len(price) == len(ACTIVES) == len(ACTION) == len(expirations): buy_len = len(price) for idx in range(buy_len): self.api.buyv3(price[idx], OP_code.ACTIVES[ACTIVES[idx]], ACTION[idx], expirations[idx], idx) while len(self.api.buy_multi_option) < buy_len: pass buy_id = [] for key in sorted(self.api.buy_multi_option.keys()): try: value = self.api.buy_multi_option[key] buy_id.append(value["id"]) except: buy_id.append(None) return buy_id else: logging.error('buy_multi error please input all same len') def get_remaning(self, duration): for remaning in get_remaning_time(self.api.timesync.server_timestamp): if remaning[0] == duration: return remaning[1] logging.error('get_remaning(self,duration) ERROR duration') return "ERROR duration" def buy(self, price, ACTIVES, ACTION, expirations): self.api.buy_successful = None self.api.buy_id = None self.api.buy(price, OP_code.ACTIVES[ACTIVES], ACTION, expirations) start_t = time.time() while self.api.buy_successful == None and self.api.buy_id == None: if time.time() - start_t >= 30: logging.error('**warning** buy late 30 sec') return False, None return self.api.buy_successful, self.api.buy_id def sell_option(self, options_ids): self.api.sell_option(options_ids) self.api.sold_options_respond = None while self.api.sold_options_respond == None: pass return self.api.sold_options_respond # __________________for Digital___________________ def get_digital_underlying_list_data(self): self.api.underlying_list_data = None self.api.get_digital_underlying() start_t = time.time() while self.api.underlying_list_data == None: if time.time() - start_t >= 30: logging.error( '**warning** get_digital_underlying_list_data late 30 sec') return None return self.api.underlying_list_data def get_strike_list(self, ACTIVES, duration): self.api.strike_list = None self.api.get_strike_list(ACTIVES, duration) ans = {} while self.api.strike_list == None: pass try: for data in self.api.strike_list["msg"]["strike"]: temp = {} temp["call"] = data["call"]["id"] temp["put"] = data["put"]["id"] ans[("%.6f" % (float(data["value"]) * 10e-7))] = temp except: logging.error('**error** get_strike_list read problem...') return self.api.strike_list, None return self.api.strike_list, ans def subscribe_strike_list(self, ACTIVE, expiration_period): self.api.subscribe_instrument_quites_generated(ACTIVE, expiration_period) def unsubscribe_strike_list(self, ACTIVE, expiration_period): del self.api.instrument_quites_generated_data[ACTIVE] self.api.unsubscribe_instrument_quites_generated( ACTIVE, expiration_period) def get_realtime_strike_list(self, ACTIVE, duration): while True: if not self.api.instrument_quites_generated_data[ACTIVE][duration * 60]: pass else: break """ strike_list dict: price:{call:id,put:id} """ ans = {} now_timestamp = self.api.instrument_quites_generated_timestamp[ACTIVE][ duration * 60] while ans == {}: if self.get_realtime_strike_list_temp_data == {} or now_timestamp != self.get_realtime_strike_list_temp_expiration: raw_data, strike_list = self.get_strike_list(ACTIVE, duration) self.get_realtime_strike_list_temp_expiration = raw_data[ "msg"]["expiration"] self.get_realtime_strike_list_temp_data = strike_list else: strike_list = self.get_realtime_strike_list_temp_data profit = self.api.instrument_quites_generated_data[ACTIVE][duration * 60] for price_key in strike_list: try: side_data = {} for side_key in strike_list[price_key]: detail_data = {} profit_d = profit[strike_list[price_key][side_key]] detail_data["profit"] = profit_d detail_data["id"] = strike_list[price_key][side_key] side_data[side_key] = detail_data ans[price_key] = side_data except: pass return ans def get_digital_current_profit(self, ACTIVE, duration): profit = self.api.instrument_quites_generated_data[ACTIVE][duration * 60] for key in profit: if key.find("SPT") != -1: return profit[key] return False #thank thiagottjv #https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/65#issuecomment-513998357 def buy_digital_spot(self, active, amount, action, duration): #Expiration time need to be formatted like this: YYYYMMDDHHII #And need to be on GMT time #Type - P or C if action == 'put': action = 'P' elif action == 'call': action = 'C' else: logging.error('buy_digital_spot active error') return -1 #doEURUSD201907191250PT5MPSPT timestamp = int(self.api.timesync.server_timestamp) if duration == 1: exp, _ = get_expiration_time(timestamp, duration) else: now_date = datetime.fromtimestamp(timestamp) + timedelta( minutes=1, seconds=30) while True: if now_date.minute % duration == 0 and time.mktime( now_date.timetuple()) - timestamp > 30: break now_date = now_date + timedelta(minutes=1) exp = time.mktime(now_date.timetuple()) dateFormated = str( datetime.utcfromtimestamp(exp).strftime("%Y%m%d%H%M")) instrument_id = "do" + active + dateFormated + "PT" + str( duration) + "M" + action + "SPT" self.api.digital_option_placed_id = None self.api.place_digital_option(instrument_id, amount) while self.api.digital_option_placed_id == None: pass return self.api.digital_option_placed_id def buy_digital(self, amount, instrument_id): 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): data = self.get_digital_position(buy_order_id) if data["msg"]["position"]["status"] == "closed": if data["msg"]["position"]["close_reason"] == "default": return True, data["msg"]["position"]["pnl_realized"] elif data["msg"]["position"]["close_reason"] == "expired": return True, data["msg"]["position"]["pnl_realized"] - data[ "msg"]["position"]["buy_amount"] else: return False, None def check_win_digital_v2(self, buy_order_id): order_data = self.get_async_order(buy_order_id) if order_data != None: if order_data["status"] == "closed": if order_data["close_reason"] == "expired": if order_data["close_effect_amount"] == 0: return True, -1 * max(order_data["buy_amount"], order_data["sell_amount"]) else: return True, order_data["close_effect_amount"] - max( order_data["buy_amount"], order_data["sell_amount"]) elif order_data["close_reason"] == "default": return True, order_data["pnl_realized_enrolled"] else: return False, None else: return False, None # ---------------------------------------------------------- # -----------------BUY_for__Forex__&&__stock(cfd)__&&__ctrpto def buy_order(self, instrument_type, instrument_id, side, amount, leverage, type, limit_price=None, stop_price=None, stop_lose_kind=None, stop_lose_value=None, take_profit_kind=None, take_profit_value=None, use_trail_stop=False, auto_margin_call=False, use_token_for_commission=False): self.api.buy_order_id = None self.api.buy_order(instrument_type=instrument_type, instrument_id=instrument_id, side=side, amount=amount, leverage=leverage, type=type, limit_price=limit_price, stop_price=stop_price, stop_lose_value=stop_lose_value, stop_lose_kind=stop_lose_kind, 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) while self.api.buy_order_id == None: pass check, data = self.get_order(self.api.buy_order_id) while data["status"] == "pending_new": check, data = self.get_order(self.api.buy_order_id) time.sleep(1) if check: if data["status"] != "rejected": return True, self.api.buy_order_id else: return False, None else: return False, None def change_auto_margin_call(self, ID_Name, ID, auto_margin_call): self.api.auto_margin_call_changed_respond = None self.api.change_auto_margin_call(ID_Name, ID, auto_margin_call) while self.api.auto_margin_call_changed_respond == None: pass if self.api.auto_margin_call_changed_respond["status"] == 2000: return True, self.api.auto_margin_call_changed_respond else: return False, self.api.auto_margin_call_changed_respond def change_order(self, ID_Name, order_id, stop_lose_kind, stop_lose_value, take_profit_kind, take_profit_value, use_trail_stop, auto_margin_call): check = True if ID_Name == "position_id": check, order_data = self.get_order(order_id) position_id = order_data["position_id"] ID = position_id elif ID_Name == "order_id": ID = order_id else: logging.error('change_order input error ID_Name') if check: self.api.tpsl_changed_respond = None self.api.change_order(ID_Name=ID_Name, ID=ID, stop_lose_kind=stop_lose_kind, stop_lose_value=stop_lose_value, take_profit_kind=take_profit_kind, take_profit_value=take_profit_value, use_trail_stop=use_trail_stop) self.change_auto_margin_call(ID_Name=ID_Name, ID=ID, auto_margin_call=auto_margin_call) while self.api.tpsl_changed_respond == None: pass if self.api.tpsl_changed_respond["status"] == 2000: return True, self.api.tpsl_changed_respond["msg"] else: return False, self.api.tpsl_changed_respond else: logging.error('change_order fail to get position_id') return False, None def get_async_order(self, buy_order_id): if buy_order_id in self.api.position_changed_data: return self.api.position_changed_data[buy_order_id] elif buy_order_id in self.api.microserviceName_binary_options_name_option: return self.api.microserviceName_binary_options_name_option[ buy_order_id] else: return None def get_order(self, buy_order_id): # self.api.order_data["status"] # reject:you can not get this order # pending_new:this order is working now # filled:this order is ok now # new self.api.order_data = None self.api.get_order(buy_order_id) while self.api.order_data == None: pass if self.api.order_data["status"] == 2000: return True, self.api.order_data["msg"] else: return False, None def get_pending(self, instrument_type): self.api.deferred_orders = None self.api.get_pending(instrument_type) while self.api.deferred_orders == None: pass if self.api.deferred_orders["status"] == 2000: return True, self.api.deferred_orders["msg"] else: return False, None # this function is heavy def get_positions(self, instrument_type): self.api.positions = None self.api.get_positions(instrument_type) while self.api.positions == None: pass if self.api.positions["status"] == 2000: return True, self.api.positions["msg"] else: return False, None def get_position(self, buy_order_id): self.api.position = None check, order_data = self.get_order(buy_order_id) position_id = order_data["position_id"] self.api.get_position(position_id) while self.api.position == None: pass if self.api.position["status"] == 2000: return True, self.api.position["msg"] else: return False, None # this function is heavy def get_digital_position(self, order_id): self.api.position = None while self.get_async_order(order_id) == None: pass position_id = self.get_async_order(order_id)["id"] self.api.get_digital_position(position_id) while self.api.position == None: pass return self.api.position def get_position_history(self, instrument_type): self.api.position_history = None self.api.get_position_history(instrument_type) while self.api.position_history == None: pass if self.api.position_history["status"] == 2000: return True, self.api.position_history["msg"] else: return False, None def get_position_history_v2(self, instrument_type, limit, offset, start, end): #instrument_type=crypto forex fx-option multi-option cfd digital-option turbo-option self.api.position_history_v2 = None self.api.get_position_history_v2(instrument_type, limit, offset, start, end) while self.api.position_history_v2 == None: pass if self.api.position_history_v2["status"] == 2000: return True, self.api.position_history_v2["msg"] else: return False, None def get_available_leverages(self, instrument_type, actives): self.api.available_leverages = None self.api.get_available_leverages(instrument_type, OP_code.ACTIVES[actives]) while self.api.available_leverages == None: pass if self.api.available_leverages["status"] == 2000: return True, self.api.available_leverages["msg"] else: return False, None def cancel_order(self, buy_order_id): self.api.order_canceled = None self.api.cancel_order(buy_order_id) while self.api.order_canceled == None: pass if self.api.order_canceled["status"] == 2000: return True else: return False 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"]) while self.api.close_position_data == None: pass if self.api.close_position_data["status"] == 2000: return True else: 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 self.api.get_overnight_fee(instrument_type, OP_code.ACTIVES[active]) while self.api.overnight_fee == None: pass if self.api.overnight_fee["status"] == 2000: return True, self.api.overnight_fee["msg"] else: return False, None
import urllib3 import time from iqoptionapi.api import IQOptionAPI from websocket import create_connection import json ws = create_connection( "wss://iqoption.com/echo/{randomnumber}/{randomnumber}/websocket") print "Sending 'Cookie'..." ws.send("{'msg': 'MY COOKIE ID', 'name': 'ssid'}") print "Sent" print "Reeiving..." result = ws.recv() print "Received '%s'" % result urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) api = IQOptionAPI("iqoption.com", "*****@*****.**", "y61327061") api.connect() time.sleep(2) balance = api.profile.balance time.sleep(2) print balance api.setactives([73, 2]) time.sleep(2) api.getcandles(1, 1) time.sleep(2) data = api.candles.candles_data print api.candles.candles_data
class IQ_Option: __version__ = "3.6.4" def __init__(self, email, password): self.size = [ 1, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200, 14400, 28800, 43200, 86400, 604800, 2592000 ] self.email = email self.password = password self.suspend = 0.5 self.thread = None self.subscribe_candle = [] self.subscribe_candle_all_size = [] self.subscribe_mood = [] # for digit self.get_realtime_strike_list_temp_data = {} self.get_realtime_strike_list_temp_expiration = 0 # self.max_reconnect = 5 self.connect_count = 0 # --start self.connect() # self.update_ACTIVES_OPCODE() this auto function delay too long self.get_balance_id() # -------------------------------------------------------------------------- def get_server_timestamp(self): return self.api.timesync.server_timestamp def set_max_reconnect(self, number): self.max_reconnect = number def connect(self): while True: try: self.api.close() except: pass #logging.error('**warning** self.api.close() fail') if self.connect_count < self.max_reconnect: self.api = IQOptionAPI("iqoption.com", self.email, self.password) check = None try: check = self.api.connect() except: logging.error('**error** connect() fail') if check == True: # -------------reconnect subscribe_candle try: for ac in self.subscribe_candle: sp = ac.split(",") self.start_candles_one_stream(sp[0], sp[1]) except: pass # ----------------- try: for ac in self.subscribe_candle_all_size: self.start_candles_all_size_stream(ac) except: pass # -------------reconnect subscribe_mood try: for ac in self.subscribe_mood: self.start_mood_stream(ac) except: pass break time.sleep(self.suspend * 2) self.connect_count = self.connect_count + 1 else: logging.error( '**error** reconnect() too many time please look log file') exit(1) def check_connect(self): # True/False idle_time = abs(self.api.timesync.server_timestamp - time.time()) if idle_time > 6: return False else: return True # wait for timestamp getting # _________________________UPDATE ACTIVES OPCODE_____________________ def get_all_ACTIVES_OPCODE(self): return OP_code.ACTIVES def update_ACTIVES_OPCODE(self): # update from binary option self.get_ALL_Binary_ACTIVES_OPCODE() #crypto /dorex/cfd self.instruments_input_all_in_ACTIVES() dicc = {} for lis in sorted(OP_code.ACTIVES.items(), key=operator.itemgetter(1)): dicc[lis[0]] = lis[1] OP_code.ACTIVES = dicc def get_name_by_activeId(self, activeId): info = self.get_financial_information(activeId) try: return info["msg"]["data"]["active"]["name"] except: return None def get_financial_information(self, activeId): self.api.financial_information = None self.api.get_financial_information(activeId) while self.api.financial_information == None: pass return self.api.financial_information def get_instruments(self, type): #type="crypto"/"forex"/"cfd" time.sleep(self.suspend) self.api.instruments = None while self.api.instruments == None: try: self.api.get_instruments(type) start = time.time() while self.api.instruments == None and time.time() - start < 10: pass except: logging.error('**error** api.get_instruments need reconnect') self.connect() return self.api.instruments def instruments_input_to_ACTIVES(self, type): instruments = self.get_instruments(type) for ins in instruments["instruments"]: OP_code.ACTIVES[ins["id"]] = ins["active_id"] def instruments_input_all_in_ACTIVES(self): self.instruments_input_to_ACTIVES("crypto") self.instruments_input_to_ACTIVES("forex") self.instruments_input_to_ACTIVES("cfd") def get_ALL_Binary_ACTIVES_OPCODE(self): init_info = self.get_all_init() for i in init_info["result"]["binary"]["actives"]: OP_code.ACTIVES[(init_info["result"]["binary"]["actives"][i] ["name"]).split(".")[1]] = int(i) # _________________________self.api.get_api_option_init_all() wss______________________ def get_all_init(self): while True: self.api.api_option_init_all_result = None while True: try: self.api.get_api_option_init_all() break except: logging.error('**error** get_all_init need reconnect') self.connect() time.sleep(5) start = time.time() while True: if time.time() - start > 30: logging.error('**warning** get_all_init late 30 sec') break try: if self.api.api_option_init_all_result != None: break except: pass try: if self.api.api_option_init_all_result["isSuccessful"] == True: return self.api.api_option_init_all_result except: pass # return OP_code.ACTIVES # --------for binary option detail def get_binary_option_detail(self): detail = nested_dict(2, dict) init_info = self.get_all_init() for actives in init_info["result"]["turbo"]["actives"]: name = init_info["result"]["turbo"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] detail[name]["turbo"] = init_info["result"]["turbo"]["actives"][ actives] for actives in init_info["result"]["binary"]["actives"]: name = init_info["result"]["binary"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] detail[name]["binary"] = init_info["result"]["binary"]["actives"][ actives] return detail def get_all_profit(self): all_profit = nested_dict(2, dict) init_info = self.get_all_init() for actives in init_info["result"]["turbo"]["actives"]: name = init_info["result"]["turbo"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] all_profit[name]["turbo"] = (100.0 - init_info["result"]["turbo"][ "actives"][actives]["option"]["profit"]["commission"]) / 100.0 for actives in init_info["result"]["binary"]["actives"]: name = init_info["result"]["binary"]["actives"][actives]["name"] name = name[name.index(".") + 1:len(name)] all_profit[name]["binary"] = ( 100.0 - init_info["result"]["binary"]["actives"][actives] ["option"]["profit"]["commission"]) / 100.0 return all_profit # ---------------------------------------- # ______________________________________self.api.getprofile() https________________________________ def get_profile(self): while True: try: respon = self.api.getprofile().json() time.sleep(self.suspend) if respon["isSuccessful"] == True: return respon except: logging.error('**error** get_profile try reconnect') self.connect() def get_balance_id(self): self.api.profile.balance_id = None while True: try: respon = self.get_profile() self.api.profile.balance_id = respon["result"]["balance_id"] break except: logging.error('**error** get_balance()') time.sleep(self.suspend) return self.api.profile.balance def get_balance(self): self.api.profile.balance = None while True: try: respon = self.get_profile() self.api.profile.balance = respon["result"]["balance"] break except: logging.error('**error** get_balance()') time.sleep(self.suspend) return self.api.profile.balance def get_balances(self): # self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balances = respon["result"]["balances"] break except: logging.error('**error** get_balances()') pass time.sleep(self.suspend) return self.api.profile.balances def get_balance_mode(self): # self.api.profile.balance_type=None while True: try: respon = self.get_profile() self.api.profile.balance_type = respon["result"][ "balance_type"] break except: logging.error('**error** get_balance_mode()') pass time.sleep(self.suspend) if self.api.profile.balance_type == 1: return "REAL" elif self.api.profile.balance_type == 4: return "PRACTICE" def change_balance(self, Balance_MODE): real_id = None practice_id = None while True: try: self.get_balances() for accunt in self.api.profile.balances: if accunt["type"] == 1: real_id = accunt["id"] if accunt["type"] == 4: practice_id = accunt["id"] break except: logging.error('**error** change_balance()') pass while self.get_balance_mode() != Balance_MODE: if Balance_MODE == "REAL": self.api.changebalance(real_id) elif Balance_MODE == "PRACTICE": self.api.changebalance(practice_id) else: logging.error("ERROR doesn't have this mode") exit(1) # ________________________________________________________________________ # _______________________ CANDLE _____________________________ # ________________________self.api.getcandles() wss________________________ def get_candles(self, ACTIVES, interval, count, endtime): self.api.candles.candles_data = None while True: try: self.api.getcandles(OP_code.ACTIVES[ACTIVES], interval, count, endtime) while self.check_connect and self.api.candles.candles_data == None: pass if self.api.candles.candles_data != None: break except: logging.error('**error** get_candles need reconnect') self.connect() return self.api.candles.candles_data ####################################################### # ______________________________________________________ # _____________________REAL TIME CANDLE_________________ # ______________________________________________________ ####################################################### def start_candles_stream(self, ACTIVE, size, maxdict): if size == "all": for s in self.size: self.full_realtime_get_candle(ACTIVE, s, maxdict) self.api.real_time_candles_maxdict_table[ACTIVE][s] = maxdict self.start_candles_all_size_stream(ACTIVE) elif size in self.size: self.api.real_time_candles_maxdict_table[ACTIVE][size] = maxdict self.full_realtime_get_candle(ACTIVE, size, maxdict) self.start_candles_one_stream(ACTIVE, size) else: logging.error( '**error** start_candles_stream please input right size') def stop_candles_stream(self, ACTIVE, size): if size == "all": self.stop_candles_all_size_stream(ACTIVE) elif size in self.size: self.stop_candles_one_stream(ACTIVE, size) else: logging.error( '**error** start_candles_stream please input right size') def get_realtime_candles(self, ACTIVE, size): if size == "all": try: return self.api.real_time_candles[ACTIVE] except: logging.error( '**error** get_realtime_candles() size="all" can not get candle' ) return False elif size in self.size: try: return self.api.real_time_candles[ACTIVE][size] except: logging.error('**error** get_realtime_candles() size=' + str(size) + ' can not get candle') return False else: logging.error( '**error** get_realtime_candles() please input right "size"') def get_all_realtime_candles(self): return self.api.real_time_candles ################################################ # ---------REAL TIME CANDLE Subset Function--------- ################################################ # ---------------------full dict get_candle----------------------- def full_realtime_get_candle(self, ACTIVE, size, maxdict): candles = self.get_candles(ACTIVE, size, maxdict, self.api.timesync.server_timestamp) for can in candles: self.api.real_time_candles[str(ACTIVE)][int(size)][ can["from"]] = can # ------------------------Subscribe ONE SIZE----------------------- def start_candles_one_stream(self, ACTIVE, size): if (str(ACTIVE + "," + str(size)) in self.subscribe_candle) == False: self.subscribe_candle.append((ACTIVE + "," + str(size))) start = time.time() self.api.candle_generated_check[str(ACTIVE)][int(size)] = {} while True: if time.time() - start > 20: logging.error( '**error** start_candles_one_stream late for 20 sec') return False try: if self.api.candle_generated_check[str(ACTIVE)][int( size)] == True: return True except: pass try: self.api.subscribe(OP_code.ACTIVES[ACTIVE], size) except: logging.error('**error** start_candles_stream reconnect') self.connect() time.sleep(1) def stop_candles_one_stream(self, ACTIVE, size): if ((ACTIVE + "," + str(size)) in self.subscribe_candle) == True: self.subscribe_candle.remove(ACTIVE + "," + str(size)) while True: try: if self.api.candle_generated_check[str(ACTIVE)][int( size)] == {}: return True except: pass self.api.candle_generated_check[str(ACTIVE)][int(size)] = {} self.api.unsubscribe(OP_code.ACTIVES[ACTIVE], size) time.sleep(self.suspend * 10) # ------------------------Subscribe ALL SIZE----------------------- def start_candles_all_size_stream(self, ACTIVE): self.api.candle_generated_all_size_check[str(ACTIVE)] = {} if (str(ACTIVE) in self.subscribe_candle_all_size) == False: self.subscribe_candle_all_size.append(str(ACTIVE)) start = time.time() while True: if time.time() - start > 20: logging.error('**error** fail ' + ACTIVE + ' start_candles_all_size_stream late for 10 sec') return False try: if self.api.candle_generated_all_size_check[str( ACTIVE)] == True: return True except: pass try: self.api.subscribe_all_size(OP_code.ACTIVES[ACTIVE]) except: logging.error( '**error** start_candles_all_size_stream reconnect') self.connect() time.sleep(1) def stop_candles_all_size_stream(self, ACTIVE): if (str(ACTIVE) in self.subscribe_candle_all_size) == True: self.subscribe_candle_all_size.remove(str(ACTIVE)) while True: try: if self.api.candle_generated_all_size_check[str(ACTIVE)] == {}: break except: pass self.api.candle_generated_all_size_check[str(ACTIVE)] = {} self.api.unsubscribe_all_size(OP_code.ACTIVES[ACTIVE]) time.sleep(self.suspend * 10) # --------------------------------------------------------------------- ################################################ ################################################ # ----------------------------------------------- # -----------------traders_mood---------------------- def start_mood_stream(self, ACTIVES): if ACTIVES in self.subscribe_mood == False: self.subscribe_mood.append(ACTIVES) while True: self.api.subscribe_Traders_mood(OP_code.ACTIVES[ACTIVES]) try: self.api.traders_mood[OP_code.ACTIVES[ACTIVES]] break except: time.sleep(5) def stop_mood_stream(self, ACTIVES): if ACTIVES in self.subscribe_mood == True: del self.subscribe_mood[ACTIVES] self.api.unsubscribe_Traders_mood(OP_code.ACTIVES[ACTIVES]) def get_traders_mood(self, ACTIVES): # return highter % return self.api.traders_mood[OP_code.ACTIVES[ACTIVES]] def get_all_traders_mood(self): # return highter % return self.api.traders_mood ############################################################################################## def check_win(self, id_number): # 'win':win money 'equal':no win no loose 'loose':loose money while True: try: listinfodata_dict = self.api.listinfodata.get(id_number) if listinfodata_dict["game_state"] == 1: break except: pass self.api.listinfodata.delete(id_number) return listinfodata_dict["win"] def check_win_v2(self, id_number): while True: check, data = self.get_betinfo(id_number) if check: return data["result"]["data"][str(id_number)]["win"] time.sleep(self.suspend) # -------------------get infomation only for binary option------------------------ def get_betinfo(self, id_number): # INPUT:int while True: self.api.game_betinfo.isSuccessful = None start = time.time() try: self.api.get_betinfo(id_number) except: logging.error( '**error** def get_betinfo self.api.get_betinfo reconnect' ) self.connect() while self.api.game_betinfo.isSuccessful == None: if time.time() - start > 10: logging.error( '**error** get_betinfo time out need reconnect') self.connect() self.api.get_betinfo(id_number) time.sleep(self.suspend * 10) if self.api.game_betinfo.isSuccessful == True: return self.api.game_betinfo.isSuccessful, self.api.game_betinfo.dict else: return self.api.game_betinfo.isSuccessful, None time.sleep(self.suspend * 10) def get_optioninfo(self, limit): self.api.api_game_getoptions_result = None self.api.get_options(limit) while self.api.api_game_getoptions_result == None: pass return self.api.api_game_getoptions_result # __________________________BUY__________________________ # __________________FOR OPTION____________________________ def buy_multi(self, price, ACTIVES, ACTION, expirations): self.api.buy_multi_option = {} if len(price) == len(ACTIVES) == len(ACTION) == len(expirations): buy_len = len(price) for idx in range(buy_len): self.api.buyv3(price[idx], OP_code.ACTIVES[ACTIVES[idx]], ACTION[idx], expirations[idx], idx) while len(self.api.buy_multi_option) < buy_len: pass buy_id = [] for key in sorted(self.api.buy_multi_option.keys()): value = self.api.buy_multi_option[key] buy_id.append(value["id"]) return buy_id else: logging.error('buy_multi error please input all same len') def buy(self, price, ACTIVES, ACTION, expirations, force_buy=False): self.api.buy_successful = None self.api.buy_id = None while True: while True: try: self.api.buy(price, OP_code.ACTIVES[ACTIVES], ACTION, expirations) break except: logging.error('self.api.buy error') if force_buy == False: return (None) self.connect() pass start = time.time() while self.api.buy_successful == None or self.api.buy_id == None: if time.time() - start > 60: logging.error('check buy_successful time late 60sec') break if self.api.buy_successful: return (self.api.buy_id) else: if force_buy == False: return (None) logging.error('**error** buy error...') self.connect() def sell_option(self, options_ids): self.api.sell_option(options_ids) self.api.sold_options_respond = None while self.api.sold_options_respond == None: pass return self.api.sold_options_respond # __________________for Digital___________________ def get_strike_list(self, ACTIVES, duration): self.api.strike_list = None self.api.get_strike_list(ACTIVES, duration) ans = {} while self.api.strike_list == None: pass try: for data in self.api.strike_list["msg"]["strike"]: temp = {} temp["call"] = data["call"]["id"] temp["put"] = data["put"]["id"] ans[("%.6f" % (float(data["value"]) * 10e-7))] = temp except: logging.error('**error** get_strike_list read problem...') return self.api.strike_list, None return self.api.strike_list, ans def subscribe_strike_list(self, ACTIVE): self.api.subscribe_instrument_quites_generated(ACTIVE) def unsubscribe_strike_list(self, ACTIVE): del self.api.instrument_quites_generated_data[ACTIVE] self.api.unsubscribe_instrument_quites_generated(ACTIVE) def get_realtime_strike_list(self, ACTIVE, duration): while True: if not self.api.instrument_quites_generated_data[ACTIVE][duration * 60]: pass else: break """ strike_list dict: price:{call:id,put:id} """ ans = {} now_timestamp = self.api.instrument_quites_generated_timestamp[ACTIVE][ duration * 60] while ans == {}: if self.get_realtime_strike_list_temp_data == {} or now_timestamp != self.get_realtime_strike_list_temp_expiration: raw_data, strike_list = self.get_strike_list(ACTIVE, duration) self.get_realtime_strike_list_temp_expiration = raw_data[ "msg"]["expiration"] self.get_realtime_strike_list_temp_data = strike_list else: strike_list = self.get_realtime_strike_list_temp_data profit = self.api.instrument_quites_generated_data[ACTIVE][duration * 60] for price_key in strike_list: try: side_data = {} for side_key in strike_list[price_key]: detail_data = {} profit_d = profit[strike_list[price_key][side_key]] detail_data["profit"] = profit_d detail_data["id"] = strike_list[price_key][side_key] side_data[side_key] = detail_data ans[price_key] = side_data except: pass return ans 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) def check_win_digital(self, buy_order_id): check, data = self.get_position(buy_order_id) if check: if data["position"]["status"] == "closed": return True, data["position"]["close_effect_amount"] else: return False, None else: return False, None # ---------------------------------------------------------- # -----------------BUY_for__Forex__&&__stock(cfd)__&&__ctrpto def buy_order(self, instrument_type, instrument_id, side, amount, leverage, type, limit_price=None, stop_price=None, stop_lose_kind=None, stop_lose_value=None, take_profit_kind=None, take_profit_value=None, use_trail_stop=False, auto_margin_call=False, use_token_for_commission=False): self.api.buy_order_id = None self.api.buy_order(instrument_type=instrument_type, instrument_id=instrument_id, side=side, amount=amount, leverage=leverage, type=type, limit_price=limit_price, stop_price=stop_price, stop_lose_value=stop_lose_value, stop_lose_kind=stop_lose_kind, 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) while self.api.buy_order_id == None: pass check, data = self.get_order(self.api.buy_order_id) while data["status"] == "pending_new": check, data = self.get_order(self.api.buy_order_id) time.sleep(1) if check: if data["status"] != "rejected": return True, self.api.buy_order_id else: return False, None else: return False, None def change_auto_margin_call(self, ID_Name, ID, auto_margin_call): self.api.auto_margin_call_changed_respond = None self.api.change_auto_margin_call(ID_Name, ID, auto_margin_call) while self.api.auto_margin_call_changed_respond == None: pass if self.api.auto_margin_call_changed_respond["status"] == 2000: return True, self.api.auto_margin_call_changed_respond else: return False, self.api.auto_margin_call_changed_respond def change_order(self, ID_Name, order_id, stop_lose_kind, stop_lose_value, take_profit_kind, take_profit_value, use_trail_stop, auto_margin_call): check = True if ID_Name == "position_id": check, order_data = self.get_order(order_id) position_id = order_data["position_id"] ID = position_id elif ID_Name == "order_id": ID = order_id else: logging.error('change_order input error ID_Name') if check: self.api.tpsl_changed_respond = None self.api.change_order(ID_Name=ID_Name, ID=ID, stop_lose_kind=stop_lose_kind, stop_lose_value=stop_lose_value, take_profit_kind=take_profit_kind, take_profit_value=take_profit_value, use_trail_stop=use_trail_stop) self.change_auto_margin_call(ID_Name=ID_Name, ID=ID, auto_margin_call=auto_margin_call) while self.api.tpsl_changed_respond == None: pass if self.api.tpsl_changed_respond["status"] == 2000: return True, self.api.tpsl_changed_respond["msg"] else: return False, self.api.tpsl_changed_respond else: logging.error('change_order fail to get position_id') return False, None def get_order(self, buy_order_id): # self.api.order_data["status"] # reject:you can not get this order # pending_new:this order is working now # filled:this order is ok now # new self.api.order_data = None self.api.get_order(buy_order_id) while self.api.order_data == None: pass if self.api.order_data["status"] == 2000: return True, self.api.order_data["msg"] else: return False, None def get_pending(self, instrument_type): self.api.deferred_orders = None self.api.get_pending(instrument_type) while self.api.deferred_orders == None: pass if self.api.deferred_orders["status"] == 2000: return True, self.api.deferred_orders["msg"] else: return False, None # this function is heavy def get_positions(self, instrument_type): self.api.positions = None self.api.get_positions(instrument_type) while self.api.positions == None: pass if self.api.positions["status"] == 2000: return True, self.api.positions["msg"] else: return False, None def get_position(self, buy_order_id): self.api.position = None check, order_data = self.get_order(buy_order_id) position_id = order_data["position_id"] self.api.get_position(position_id) while self.api.position == None: pass if self.api.position["status"] == 2000: return True, self.api.position["msg"] else: return False, None # this function is heavy def get_position_history(self, instrument_type): self.api.position_history = None self.api.get_position_history(instrument_type) while self.api.position_history == None: pass if self.api.position_history["status"] == 2000: return True, self.api.position_history["msg"] else: return False, None def get_available_leverages(self, instrument_type, actives): self.api.available_leverages = None self.api.get_available_leverages(instrument_type, OP_code.ACTIVES[actives]) while self.api.available_leverages == None: pass if self.api.available_leverages["status"] == 2000: return True, self.api.available_leverages["msg"] else: return False, None def cancel_order(self, buy_order_id): self.api.order_canceled = None self.api.cancel_order(buy_order_id) while self.api.order_canceled == None: pass if self.api.order_canceled["status"] == 2000: return True else: return False def close_position(self, buy_order_id): check, data = self.get_order(buy_order_id) if data["position_id"] != None: self.api.close_position_data = None self.api.close_position(data["position_id"]) while self.api.close_position_data == None: pass if self.api.close_position_data["status"] == 2000: return True else: return False else: return False def get_overnight_fee(self, instrument_type, active): self.api.overnight_fee = None self.api.get_overnight_fee(instrument_type, OP_code.ACTIVES[active]) while self.api.overnight_fee == None: pass if self.api.overnight_fee["status"] == 2000: return True, self.api.overnight_fee["msg"] else: return False, None
class IQOption(): def __init__(self, user=userdata.mainUser, active_id="EURUSD"): self.username = user['username'] self.password = user['password'] self.real = user['real'] self.demo = user['demo'] self.active_id = active_id self.api = IQOptionAPI("iqoption.com", self.username, self.password) self.api.connect() self.setExpirationTime() self.setActives(self.active_id) self.api.changebalance(self.demo) def openPosition(self, direction, amount=1): while self.api.timesync.server_datetime.second != 0: sleep(0.5) self.api.buy(amount, api_constants.ACTIVES[self.active_id], "turbo", direction) def getBalance(self): return self.api.profile.balance def setExpirationTime(self, time=1): self.api.timesync.expiration_time = time def setActives(self, activeID): self.active_id = activeID self.api.setactives([api_constants.ACTIVES[self.active_id]]) def getCandles(self, duration=1): self.api.getcandles(api_constants.ACTIVES[self.active_id], duration) candles = self.api.candles.candles_data while not candles: sleep(0.1) self.api.getcandles(api_constants.ACTIVES[self.active_id], duration) candles = self.api.candles.candles_data if candles: # print 'Timestamp: ', datetime.datetime.fromtimestamp(int(candles[-1][0])).strftime('%Y-%m-%d %H:%M:%S') # print 'Open: ', candles[-1][1] # print 'Close: ', candles[-1][2] # print 'High: ', candles[-1][3] # print 'Low: ', candles[-1][4] return candles[-3], candles[-2], candles[-1] else: print('Invalid Candle!') return None def getServerDateTime(self): print(str(self.api.timesync.server_datetime)) return str(self.api.timesync.server_datetime) def getExpirationDateTime(self): print(str(self.api.timesync.expiration_datetime)) return str(self.api.timesync.expiration_datetime) def getResult(self): try: result = self.api.listinfodata.current_listinfodata.win #print('Result: ' + result) return result except: print('Result Error!') return None def changeBalance(self, real=False): # To enable the real account: if real: self.api.changebalance(self.real) else: self.api.changebalance(self.demo) #self.api.change_balance('practice') def getDataFrame(self, duration=1): candles = self.getCandles(duration) if candles: try: # candlesDF -> dict type candlesDF = { 'DateTime': [candles[-3][0], candles[-2][0]], # candles[-1][0]], 'Open': [candles[-3][1], candles[-2][1]], # candles[-1][1]], 'Close': [candles[-3][2], candles[-2][2]], # candles[-1][2]], 'High': [candles[-3][3], candles[-2][3]], # candles[-1][3]], 'Low': [candles[-3][4], candles[-2][4]] # , candles[-1][4]] } except: return None return candlesDF
class IQ_Option: def __init__(self, email, password): self.email = email self.password = password self.suspend = 0.5 self.connect() self.thread_collect_realtime = {} #*** def connect(self): while True: try: self.api = IQOptionAPI("iqoption.com", self.email, self.password) self.api.connect() time.sleep(self.suspend) break except: logging.error('**error** connect()') pass ################################################################################## def get_all_init(self): self.api.api_option_init_all_result = None while True: try: self.api.get_api_option_init_all() time.sleep(self.suspend) try: if self.api.api_option_init_all_result[ "isSuccessful"] == True: break except: pass except: logging.error('**error** get_all_init need reconnect') self.connect() return self.api.api_option_init_all_result def get_ALL_Binary_ACTIVES_OPCODE(self): init_info = self.get_all_init() for i in init_info["result"]["binary"]["actives"]: OP_code.ACTIVES[(init_info["result"]["binary"]["actives"][i] ["name"]).split(".")[1]] = i return OP_code.ACTIVES def get_profit(self, ACTIVES): init_info = self.get_all_init() return (100.0 - init_info["result"]["turbo"]["actives"][str( OP_code.ACTIVES[ACTIVES])]["option"]["profit"]["commission"] ) / 100.0 def get_all_profit(self): all_profit = {} init_info = self.get_all_init() for active in OP_code.ACTIVES: try: prof = (100.0 - init_info["result"]["turbo"]["actives"][str( OP_code.ACTIVES[active])]["option"]["profit"]["commission"] ) / 100.0 all_profit[active] = prof except: pass return all_profit ################################################################################################## def get_profile(self): while True: try: respon = self.api.getprofile().json() time.sleep(self.suspend) if respon["isSuccessful"] == True: return respon except: logging.error('**error** get_profile') #------------------------https profile------------------------ def get_balance(self): #self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balance = respon["result"]["balance"] break except: logging.error('**error** get_balance()') pass time.sleep(self.suspend) return self.api.profile.balance def get_balances(self): #self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balances = respon["result"]["balances"] break except: logging.error('**error** get_balances()') pass time.sleep(self.suspend) return self.api.profile.balances def get_balance_mode(self): #self.api.profile.balance_type=None while True: try: respon = self.get_profile() self.api.profile.balance_type = respon["result"][ "balance_type"] break except: logging.error('**error** get_balance_mode()') pass time.sleep(self.suspend) if self.api.profile.balance_type == 1: return "REAL" elif self.api.profile.balance_type == 4: return "PRACTICE" #------------------------------------------------ def change_balance(self, Balance_MODE): real_id = None practice_id = None while True: try: self.get_balances() for accunt in self.api.profile.balances: if accunt["type"] == 1: real_id = accunt["id"] if accunt["type"] == 4: practice_id = accunt["id"] break except: logging.error('**error** change_balance()') pass while self.get_balance_mode() != Balance_MODE: if Balance_MODE == "REAL": self.api.changebalance(real_id) elif Balance_MODE == "PRACTICE": self.api.changebalance(practice_id) else: print("ERROR doesn't have this mode") exit(1) #######################**********candles***********####################### def get_candles(self, ACTIVES, interval, count, endtime): while True: try: self.api.getcandles(OP_code.ACTIVES[ACTIVES], interval, count, endtime) break except: logging.error('**error** get_candles need reconnect') self.connect() pass while self.api.candles.candles_data == None: pass return self.api.candles.candles_data ################################################################################ ################### real time ########################### ################################################################################ #all def start_all_candles_stream(self): while self.api.real_time_candles == {}: for ACTIVES_name in OP_code.ACTIVES: self.api.subscribe(OP_code.ACTIVES[ACTIVES_name]) time.sleep(self.suspend) def get_all_realtime_candles(self): return self.api.real_time_candles def stop_all_candles_stream(self): while self.api.real_time_candles != {}: self.api.real_time_candles = {} time.sleep(self.suspend) for ACTIVES_name in OP_code.ACTIVES: self.api.unsubscribe(OP_code.ACTIVES[ACTIVES_name]) ##one def start_candles_stream(self, ACTIVES): while self.api.real_time_candles == {}: time.sleep(self.suspend) self.api.subscribe(OP_code.ACTIVES[ACTIVES]) def get_realtime_candles(self, ACTIVES): while True: try: return self.api.real_time_candles[ACTIVES] except: logging.error('**error** get_realtime_candles()') pass def stop_candles_stream(self, ACTIVES): while self.api.real_time_candles != {}: self.api.real_time_candles = {} time.sleep(self.suspend) self.api.unsubscribe(OP_code.ACTIVES[ACTIVES]) ###################################collect realtime################################### #####dict controler#### def dict_queue_add(self, dict, ACTIVES, maxdict, key, value): while True: if len(dict) <= maxdict: dict[(ACTIVES, key)] = value break else: #del mini key del dict[sorted(dict.keys(), reverse=False)[0]] def thread_realtime(self, ACTIVES, maxdict): t = threading.currentThread() while getattr(t, "do_run", True): candles = self.get_realtime_candles(ACTIVES) self.dict_queue_add(dict=self.thread_collect_realtime, ACTIVES=ACTIVES, maxdict=maxdict, key=candles["at"], value=candles) #self.thread_collect_realtime={} def collect_realtime_candles_thread_start(self, ACTIVES, maxdict): t = threading.Thread(target=self.thread_realtime, args=(ACTIVES, maxdict)) t.start() return t def collect_realtime_candles_thread_stop(self, thread): thread.do_run = False thread.join() def collect_realtime_candles(self, ACTIVES, collect_time): #doing while untill time stop collect = {} start = time.time() while time.time() < start + collect_time: candles = self.get_realtime_candles(ACTIVES) collect[candles["at"]] = candles return collect ############################################################################################## def get_candles_as_array(self, ACTIVES, interval, count, endtime): candles = self.get_candles(ACTIVES, interval, count, endtime) ans = np.empty(shape=(len(candles), 4)) for idx, candle in enumerate(candles): ans[idx][0] = candle["open"] ans[idx][1] = candle["close"] ans[idx][2] = candle["min"] ans[idx][3] = candle["max"] return ans def check_win(self): #'win':win money 'equal':no win no loose 'loose':loose money self.api.listinfodata.__init__() start = time.time() while True: try: state = self.api.listinfodata.current_listinfodata.game_state if state == 1: break except: if time.time() - start > 180: logging.error('check_win time late 120sec') return self.api.listinfodata.current_listinfodata.win def buy(self, price, ACTIVES, ACTION): self.api.buy_successful == None while True: while True: try: self.api.buy(price, OP_code.ACTIVES[ACTIVES], "turbo", ACTION) break except: logging.error('self.api.buy error') pass start = time.time() while self.api.buy_successful == None: if time.time() - start > 60: logging.error('check buy_successful time late 60sec') break if self.api.buy_successful: break else: logging.error('**error** buy need reconnect') self.connect() #print("buy ok") def call(self, price, ACTIVES): while True: try: self.api.buy(price, OP_code.ACTIVES[ACTIVES], "turbo", "call") except: pass def put(self, price, ACTIVES): while True: try: self.api.buy(price, OP_code.ACTIVES[ACTIVES], "turbo", "put") except: pass
class IQ_Option: def __init__(self, email, password): self.email = email self.password = password self.suspend = 0.5 self.thread = None self.connect() self.thread_collect_realtime = {} self.update_ACTIVES_OPCODE() #time.sleep(self.suspend) #*** def connect(self): while True: try: try: self.api.close() except: pass #logging.error('**warning** self.api.close() it can ignore') self.api = IQOptionAPI("iqoption.com", self.email, self.password) self.api.connect() time.sleep(self.suspend) break except: logging.error( '**error** connect():DO you install right version?') pass #wait for timestamp getting #_________________________UPDATE ACTIVES OPCODE_____________________ def get_all_ACTIVES_OPCODE(self): return OP_code.ACTIVES def update_ACTIVES_OPCODE(self): #update from binary option self.get_ALL_Binary_ACTIVES_OPCODE() #crypto /dorex/cfd self.get_all_instruments() dicc = {} for lis in sorted(OP_code.ACTIVES.items(), key=operator.itemgetter(1)): dicc[lis[0]] = lis[1] OP_code.ACTIVES = dicc def instruments_input(self, types): time.sleep(self.suspend) self.api.instruments = None while self.api.instruments == None: self.api.get_instruments(types) start = time.time() while self.api.instruments == None and time.time() - start < 10: pass for ins in self.api.instruments["instruments"]: OP_code.ACTIVES[ins["id"]] = ins["active_id"] def get_all_instruments(self): self.instruments_input("crypto") self.instruments_input("forex") self.instruments_input("cfd") def get_ALL_Binary_ACTIVES_OPCODE(self): init_info = self.get_all_init() for i in init_info["result"]["binary"]["actives"]: OP_code.ACTIVES[(init_info["result"]["binary"]["actives"][i] ["name"]).split(".")[1]] = int(i) #_________________________self.api.get_api_option_init_all() wss______________________ def get_all_init(self): self.api.api_option_init_all_result = None while True: try: self.api.get_api_option_init_all() start = time.time() while True: if time.time() - start > 30: logging.error('**warning** get_all_init late 30 sec') break try: if self.api.api_option_init_all_result != None: break except: self.api.get_api_option_init_all() time.sleep(self.suspend * 2) if self.api.api_option_init_all_result["isSuccessful"] == True: break except: logging.error('**error** get_all_init need reconnect') self.connect() return self.api.api_option_init_all_result #return OP_code.ACTIVES def get_profit(self, ACTIVES): init_info = self.get_all_init() return (100.0 - init_info["result"]["turbo"]["actives"][str( OP_code.ACTIVES[ACTIVES])]["option"]["profit"]["commission"] ) / 100.0 def get_all_profit(self): all_profit = {} init_info = self.get_all_init() for active in OP_code.ACTIVES: try: prof = (100.0 - init_info["result"]["turbo"]["actives"][str( OP_code.ACTIVES[active])]["option"]["profit"]["commission"] ) / 100.0 all_profit[active] = prof except: pass return all_profit #______________________________________self.api.getprofile() https________________________________ def get_profile(self): while True: try: respon = self.api.getprofile().json() time.sleep(self.suspend) if respon["isSuccessful"] == True: return respon except: logging.error('**error** get_profile') def get_balance(self): #self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balance = respon["result"]["balance"] break except: logging.error('**error** get_balance()') pass time.sleep(self.suspend) return self.api.profile.balance def get_balances(self): #self.api.profile.balance=None while True: try: respon = self.get_profile() self.api.profile.balances = respon["result"]["balances"] break except: logging.error('**error** get_balances()') pass time.sleep(self.suspend) return self.api.profile.balances def get_balance_mode(self): #self.api.profile.balance_type=None while True: try: respon = self.get_profile() self.api.profile.balance_type = respon["result"][ "balance_type"] break except: logging.error('**error** get_balance_mode()') pass time.sleep(self.suspend) if self.api.profile.balance_type == 1: return "REAL" elif self.api.profile.balance_type == 4: return "PRACTICE" def change_balance(self, Balance_MODE): real_id = None practice_id = None while True: try: self.get_balances() for accunt in self.api.profile.balances: if accunt["type"] == 1: real_id = accunt["id"] if accunt["type"] == 4: practice_id = accunt["id"] break except: logging.error('**error** change_balance()') pass while self.get_balance_mode() != Balance_MODE: if Balance_MODE == "REAL": self.api.changebalance(real_id) elif Balance_MODE == "PRACTICE": self.api.changebalance(practice_id) else: print("ERROR doesn't have this mode") exit(1) #________________________________________________________________________ #_______________________ CANDLE _____________________________ #________________________self.api.getcandles() wss________________________ def get_candles(self, ACTIVES, interval, count, endtime): while True: try: self.api.getcandles(OP_code.ACTIVES[ACTIVES], interval, count, endtime) break except: logging.error('**error** get_candles need reconnect') self.connect() pass while self.api.candles.candles_data == None: pass return self.api.candles.candles_data #______________________________________________________________ #_____________________________REAL TIME CANDLE_________________ #______________________________________________________________ #all need to fixxxxxxxxxxxxxxxxxxxxxxx def start_all_candles_stream(self): while self.api.real_time_candles == {}: for ACTIVES_name in OP_code.ACTIVES: self.api.subscribe(OP_code.ACTIVES[ACTIVES_name]) time.sleep(self.suspend) time.sleep(self.suspend) def get_all_realtime_candles(self): return self.api.real_time_candles def stop_all_candles_stream(self): while self.api.real_time_candles != {}: self.api.real_time_candles = {} time.sleep(self.suspend) for ACTIVES_name in OP_code.ACTIVES: self.api.unsubscribe(OP_code.ACTIVES[ACTIVES_name]) ############################################## ##one def start_candles_stream(self, ACTIVES): try: self.api.subscribe(OP_code.ACTIVES[ACTIVES]) start = time.time() while True: if time.time() - start > 20: logging.error('**error** fail ' + ACTIVES + ' start_candles_stream late for 10 sec') return False try: if self.api.real_time_candles[ACTIVES] != {}: break except: time.sleep(1) self.api.subscribe(OP_code.ACTIVES[ACTIVES]) if self.api.real_time_candles[ACTIVES] != {}: return True except: logging.error('**error** start_candles_stream reconnect') self.connect() def get_realtime_candles(self, ACTIVES): try: return self.api.real_time_candles[ACTIVES] except: logging.error('**error** get_realtime_candles()') return False def stop_candles_stream(self, ACTIVES): while True: try: if self.api.real_time_candles[ACTIVES] == {}: break except: pass self.api.real_time_candles[ACTIVES] = {} time.sleep(1) self.api.unsubscribe(OP_code.ACTIVES[ACTIVES]) #__________________________Collect realtime_____________________________ #______thread_____ def dict_queue_add(self, dict, ACTIVES, maxdict, key, value): while True: if len(dict) <= maxdict: dict[(ACTIVES, key)] = value break else: #del mini key del dict[sorted(dict.keys(), reverse=False)[0]] def thread_realtime(self, ACTIVES, maxdict): t = threading.currentThread() while getattr(t, "do_run", True): candles = self.get_realtime_candles(ACTIVES) self.dict_queue_add(dict=self.thread_collect_realtime, ACTIVES=ACTIVES, maxdict=maxdict, key=candles["at"], value=candles) #self.thread_collect_realtime={} def collect_realtime_candles_thread_start(self, ACTIVES, maxdict): t = threading.Thread(target=self.thread_realtime, args=(ACTIVES, maxdict)) t.start() return t def collect_realtime_candles_thread_stop(self, thread): thread.do_run = False thread.join() # ___None thread___ def collect_realtime_candles(self, ACTIVES, collect_time): #doing while untill time stop collect = {} start = time.time() while time.time() < start + collect_time: candles = self.get_realtime_candles(ACTIVES) collect[candles["at"]] = candles return collect ############################################################################################## def get_candles_as_array(self, ACTIVES, interval, count, endtime): candles = self.get_candles(ACTIVES, interval, count, endtime) ans = np.empty(shape=(len(candles), 4)) for idx, candle in enumerate(candles): ans[idx][0] = candle["open"] ans[idx][1] = candle["close"] ans[idx][2] = candle["min"] ans[idx][3] = candle["max"] return ans def check_win(self, id_number): #'win':win money 'equal':no win no loose 'loose':loose money while True: try: listinfodata_dict = self.api.listinfodata.get(id_number) if listinfodata_dict["game_state"] == 1: break except: pass self.api.listinfodata.delete(id_number) return listinfodata_dict["win"] #__________________________BUY__________________________ #__________________FOR OPTION____________________________ def buy(self, price, ACTIVES, ACTION, expirations, force_buy=True): self.api.buy_successful == None while True: while True: try: self.api.buy(price, OP_code.ACTIVES[ACTIVES], ACTION, expirations) break except: if force_buy == False: return (False, None) logging.error('self.api.buy error') self.connect() pass start = time.time() while self.api.buy_successful == None: if time.time() - start > 60: logging.error('check buy_successful time late 60sec') break if self.api.buy_successful: return (True, self.api.buy_id) else: if force_buy == False: return (False, None) logging.error('**error** buy error...') self.connect()