class IQOption: def __init__(self, goal, size, maxdict, money, expiration_mode, account='PRACTICE'): ''' account : ['REAL', 'PRACTICE'] ''' import json from iqoptionapi.stable_api import IQ_Option data = json.load(open('credentials.json')) username = data['email'] password = data['password'] self.Iq = IQ_Option(username, password) print() print('logging in...') check, reason = self.Iq.connect() #connect to iqoption print('login:'******'SUCCESSFUL' if check else 'FAILED') print() assert check, True self.login_time = datetime.datetime.now() #self.Iq.reset_practice_balance() self.Iq.change_balance(account) ALL_Asset = self.Iq.get_all_open_time() if ALL_Asset["turbo"][goal]["open"]: goal = goal else: goal = goal + '-OTC' print('goal =', goal) self.goal = goal self.size = size self.maxdict = maxdict self.money = money self.expirations_mode = expiration_mode self.consecutive_error = 0 self.forex_order_id = [] instrument_type = 'forex' instrument_id = self.goal print('forex: leverage:', self.Iq.get_available_leverages(instrument_type, instrument_id)) print() #self.test_forex() def buy(self, action, check_result): buy_success, buy_order_id = self.Iq.buy(self.money, self.goal, action, self.expirations_mode) #buy_success, buy_order_id = self.Iq.buy(int(self.get_balance()*0.1),self.goal,action,self.expirations_mode) if not check_result: return None, None if buy_success: #print(action,'success', end='\r') result, earn = self.Iq.check_win_v4(buy_order_id) self.consecutive_error = 0 #print(' '*12, end='\r') return result, round(earn, 2) else: print(action + ' fail') self.consecutive_error += 1 assert self.consecutive_error < 5, 'Failed more than 5 times' return None, None def get_candles(self): self.Iq.start_candles_stream(self.goal, self.size, self.maxdict) candles = self.Iq.get_realtime_candles(self.goal, self.size) self.Iq.stop_candles_stream(self.goal, self.size) candles = list(candles.values()) d = [[c['open'], c['close'], c['min'], c['max']] for c in candles] data = pd.DataFrame(d, columns=['open', 'close', 'low', 'high']) #data = np.array(d) return data def get_balance(self): # current_balance = {'request_id': '', 'name': 'balances', # 'msg': [ # {'id': 414500451, 'user_id': 84068869, 'type': 1, 'amount': 0, 'enrolled_amount': 0, 'enrolled_sum_amount': 0, 'hold_amount': 0, 'orders_amount': 0, 'auth_amount': 0, 'equivalent': 0, 'currency': 'USD', 'tournament_id': None, 'tournament_name': None, 'is_fiat': True, 'is_marginal': False, 'has_deposits': False}, # {'id': 414500452, 'user_id': 84068869, 'type': 4, 'amount': 15023.81, 'enrolled_amount': 15023.811818, 'enrolled_sum_amount': 15023.811818, 'hold_amount': 0, 'orders_amount': 0, 'auth_amount': 0, 'equivalent': 0, 'currency': 'USD', 'tournament_id': None, 'tournament_name': None, 'is_fiat': True, 'is_marginal': False, 'has_deposits': False}, # {'id': 414500453, 'user_id': 84068869, 'type': 5, 'amount': 0, 'enrolled_amount': 0, 'enrolled_sum_amount': 0, 'hold_amount': 0, 'orders_amount': 0, 'auth_amount': 0, 'equivalent': 0, 'currency': 'BTC', 'tournament_id': None, 'tournament_name': None, 'is_fiat': False, 'is_marginal': False, 'has_deposits': False}, # {'id': 414500454, 'user_id': 84068869, 'type': 5, 'amount': 0, 'enrolled_amount': 0, 'enrolled_sum_amount': 0, 'hold_amount': 0, 'orders_amount': 0, 'auth_amount': 0, 'equivalent': 0, 'currency': 'ETH', 'tournament_id': None, 'tournament_name': None, 'is_fiat': False, 'is_marginal': False, 'has_deposits': False} # ], 'status': 0} # return self.Iq.get_balances()['msg'][1]['amount'] return self.Iq.get_balance() def buy_forex(self, action, trail_stop=False): instrument_type = 'forex' instrument_id = self.goal side = action amount = 100 #amount = round((self.get_balance() - 9668) * 0.1, 2) leverage = 50 type = 'market' limit_price = None stop_price = None #stop_lose_kind = None #stop_lose_value = None take_profit_kind = None take_profit_value = None stop_lose_kind = 'percent' stop_lose_value = 1.0 #take_profit_kind = 'percent' #take_profit_value = 1.0 use_trail_stop = trail_stop auto_margin_call = False use_token_for_commission = False check, order_id = self.Iq.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) self.forex_order_id.append(order_id) ''' print('- '*10) print(self.Iq.get_order(order_id)) print('- '*10) print(self.Iq.get_positions(instrument_type)) print('- '*10) print(self.Iq.get_position_history(instrument_type)) print('- '*10) print(self.Iq.get_available_leverages(instrument_type, instrument_id)) print('- '*10) import time time.sleep(10) print(self.Iq.close_position(order_id)) print('- '*10) print(self.Iq.get_overnight_fee(instrument_type, instrument_id)) print('- '*10) ''' def all_positions_closed_forex(self): all_close = True self.num_open_positions = 0 for order_id in self.forex_order_id: order_type, order_status = self.get_position_forex(order_id) if order_status == 'open': self.num_open_positions += 1 all_close = False return all_close def close_forex(self): self.Iq.close_position(self.forex_order_id) def close_all_forex(self): for order_id in self.forex_order_id: self.Iq.close_position(order_id) def get_position_forex(self, order_id): #return self.Iq.get_position(self.forex_order_id)[1]['position']['status'] order = self.Iq.get_position(order_id)[1]['position'] order_type = order['type'] order_status = order['status'] return order_type, order_status def test_forex(self): import time print('= ' * 20) self.buy_forex('sell') print(self.get_position_forex()) print() time.sleep(5) self.close_forex() print(self.get_position_forex()) print() print('= ' * 20) print() self.buy_forex('buy') print(self.get_position_forex()) print() time.sleep(5) self.close_forex() print(self.get_position_forex()) print() print('= ' * 20) def reconnect_after_10_minutes(self): b = datetime.datetime.now() #print((b-self.login_time).seconds, 'seconds') if (b - self.login_time).seconds > 60 * 10: check, reason = self.Iq.connect() #connect to iqoption assert check, True #print(f'reconnected after {(b-self.login_time).seconds} seconds!') self.login_time = datetime.datetime.now()
"limit_price: {}," "stop_price: {}," "stop_lose_value: {}," "take_profit_value: {}," "take_profit_kind: {}," "use_trail_stop: {}," "auto_margin_call: {}," "use_token_for_commission: {}".format( instrument_type, instrument_id, side, amount, leverage, type, limit_price, stop_price, stop_lose_value, stop_lose_kind, take_profit_value, take_profit_kind, use_trail_stop, auto_margin_call, use_token_for_commission)) check, id = Iq.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 Iq.get_async_order(id) == None: pass order_data = Iq.get_async_order(id) print(Iq.get_async_order(id))
def testfunc(jobid, email, password, instrumentid_tmp, instrumenttype_tmp, direction_tmp, leverage_tmp, starttimehour_tmp, starttimeminute_tmp, stoptimehour_tmp, stoptimeminute_tmp, day_str, amount_tmp): print("{}: JobId executing... current time {}".format( jobid, datetime.now())) print("{}: JobId parameters " "{}: email" "{}: password" "instrumentId: {} " "instrumentType: {} " "diection_tmp: {} " "leverage_tmp: {}" "starttimehour_tmp: {}" "starttimeminute_tmp: {}" "stoptimehour_tmp: {}" "stoptimeminute_tmp: {}" "day_str: {}" "amount_tmp: {}".format(jobid, email, password, instrumentid_tmp, instrumenttype_tmp, direction_tmp, leverage_tmp, starttimehour_tmp, starttimeminute_tmp, stoptimehour_tmp, stoptimeminute_tmp, day_str, amount_tmp)) if instrumenttype_tmp == 'BINARY' or instrumenttype_tmp == 'binary': print("In binary") Iq = IQ_Option(email=email, password=password) # Iq = IQ_Option("*****@*****.**", "aswdkl;123") Iq.connect() print(Iq.connect()) # Money = 1 # ACTIVES = "EURUSD" Money = int(amount_tmp) ACTIVES = instrumentid_tmp if direction_tmp == "BUY": ACTION = "call" # or "put" else: ACTION = "put" expirations_mode = 1 check, id = Iq.buy(Money, ACTIVES, ACTION, expirations_mode) if check: print("{}: JobId {} {} at {}".format(jobid, ACTIVES, ACTION, datetime.now())) else: print("buy fail") elif instrumenttype_tmp == 'DIGITAL' or instrumenttype_tmp == 'digital': print("In digital!") Iq = IQ_Option(email=email, password=password) # Iq = IQ_Option("*****@*****.**", "aswdkl;123") Iq.connect() print(Iq.connect()) # Money = 1 # ACTIVES = "EURUSD" Money = int(amount_tmp) ACTIVES = str(instrumentid_tmp) if direction_tmp == "BUY": ACTION = "call" # or "put" else: ACTION = "put" DURATION = 1 print("Money: {} ACTIVES: {} ACTION: {} expiration_mode: {}".format( Money, ACTIVES, ACTION, DURATION)) # print(type(Money)) # print(type(ACTIVES)) # print(type(ACTION)) # print(type(DURATION)) check, id = Iq.buy_digital_spot(ACTIVES, Money, ACTION, DURATION) if check: print("{}: JobId {} {} at {}".format(jobid, ACTIVES, ACTION, datetime.now())) else: print("buy fail") else: Iq = IQ_Option(email=email, password=password) # Iq = IQ_Option("*****@*****.**", "aswdkl;123") Iq.connect() print(Iq.connect()) print("__For_Forex_Stock_Commodities_Crypto_ETFs") instrument_type = instrumenttype_tmp.lower() print(instrument_type) instrument_id = instrumentid_tmp if direction_tmp == "BUY": side = "buy" # or "put" else: side = "sell" amount = int(amount_tmp) leverage = int(leverage_tmp) type = "market" limit_price = None stop_price = None stop_lose_kind = "percent" stop_lose_value = 95 take_profit_kind = None take_profit_value = None use_trail_stop = True auto_margin_call = False use_token_for_commission = False print("instrument_type: {}," "instrument_id: {}," "side: {}," "leverage: {}," "type: {}," "limit_price: {}," "stop_price: {}," "stop_lose_value: {}," "take_profit_value: {}," "take_profit_kind: {}," "use_trail_stop: {}," "auto_margin_call: {}," "use_token_for_commission: {}".format( instrument_type, instrument_id, side, amount, leverage, type, limit_price, stop_price, stop_lose_value, stop_lose_kind, take_profit_value, take_profit_kind, use_trail_stop, auto_margin_call, use_token_for_commission)) check, id = Iq.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) # if check: # print(check) # else: # print("no check") if check: print("{}: JobId {} {} at {}".format(jobid, instrument_type, instrument_id, datetime.now())) else: print("buy fail") while Iq.get_async_order(id) == None: pass order_data = Iq.get_async_order(id) print(Iq.get_async_order(id))