import time
from tradingview_ta import TA_Handler, Interval, Exchange
import tradingview_ta
import sys
filename = str(sys.argv[0]).replace('.py','').replace('./','')
file = open(filename+'_version.txt','w+')
file.write('5/3/2022/9am')
file.close()
symbol_list_len = 0
try:
	username = input('PowerTrader username: ')
	rsi_choice = float(input('Choose RSI level: ')) 
	file = open(username+'_symbol_list.txt', 'r')
	symbol_list = file.read().replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(",","")
	symbol_list = symbol_list.split(' ')
	file.close()
	last_signal = []
	while True:
		last_signal.append('SELL')
		if len(last_signal)==len(symbol_list):
			break
		else:
			continue
	try:
		file = open(username+filename+'buy_coins.txt','r')
		file.close()
	except:
		file = open(username+filename+'buy_coins.txt','w+')
		file.write('')
		file.close()
	print('bot is running')	
	while True:
		file = open(filename+'_timestamp.txt','w+')
		file.write(str(time.time()))
		file.close()
		file = open(str(username)+'_onoff.txt','r')
		onoff = file.read()
		file.close()
		if onoff == 'off':
			time.sleep(1)
			continue
		else:
			pass

		while True:
			try:
				file = open(username+'_symbol_list.txt', 'r')
				symbol_list = file.read().replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(","," ")
				symbol_list = symbol_list.split(' ')
				file.close()
			except:
				time.sleep(.1)
				continue
			if len(symbol_list)>symbol_list_len:
				last_signal.append('SELL')
				symbol_list_len = len(symbol_list)
				continue
			else:
				break
		index = 0
		print('yep')
		while True:
			while True:
				file = open(str(username)+'_list_choice.txt','r')
				list_choice = file.read()
				file.close()
				if list_choice == 'A' or list_choice == 'a':

					file = open(str(username)+'_avoidance_coins.txt','r')
					avoidance_coins = file.read().replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(",","")
					file.close()
					avoidance_coins = avoidance_coins.split(' ')
					break
				elif list_choice == 'T' or list_choice == 't':
					file = open(str(username)+'_trade_only_coins.txt','r')
					trade_only_coins = file.read().replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(",","")
					file.close()
					trade_only_coins = trade_only_coins.split(' ')
					break
				else:
					print('Not a valid selection.')
					continue
			

				

			

			btc_buy = 'no'

			skip = 'no'
			current_coin = symbol_list[index]
			if list_choice == 'A' or list_choice == 'a':
				this_index = 0
				while True:
					if index>=len(symbol_list):
						break
					else:
						pass



					if str(current_coin.replace('-USDT','')).lower() == str(avoidance_coins[this_index]).lower():
						skip = 'yes'
						
						break
					else:
						this_index = this_index + 1
						if this_index >= len(avoidance_coins):
							break
						else:
							continue
				
			else:
				this_index = 0
				while True:
					if index>=len(symbol_list):
						break
					else:
						pass



		
					if str(current_coin.replace('-USDT','')).lower() == str(trade_only_coins[this_index]).lower():

						break
					else:
						this_index = this_index + 1
						if this_index >= len(trade_only_coins):
							skip = 'yes'
							break
						else:
							continue
			if skip == 'yes':
				index = index + 1
				if index>=len(symbol_list):
					index = 0
					symbol_list_len = len(symbol_list)
					break
				else:
					continue
			else:
				pass	

			file = open(str(username)+'_onoff.txt','r')
			onoff = file.read()
			file.close()
			if onoff == 'off':
				time.sleep(1)
				continue
			else:
				pass
			print(index)
			try:
				handler = TA_Handler(
					symbol=symbol_list[index].replace('-',''),
					exchange="kucoin",
					screener="crypto",
					interval=Interval.INTERVAL_1_HOUR,
					timeout=10
				)
				analysis = handler.get_analysis()
				strongbuy_check = str(analysis.summary).replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(",","").split(' ')
				rsi_check = str(analysis.indicators["RSI"]).replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(",","").split(' ')
				RSI = float(rsi_check[0])
			except Exception as e:
				print(str(e))
				index += 1
				if index >= len(symbol_list):
					index = 0
					symbol_list_len = len(symbol_list)
					break
				else:
					continue
			print(symbol_list[index]+'      last signal: ' + last_signal[index] + '      current signal: ' + strongbuy_check[1]+ '      current RSI: ' + str(RSI))
			if 'STRONG_SELL' != strongbuy_check[1] and last_signal[index] == 'STRONG_SELL' and RSI<rsi_choice:
				try:
					file = open(username+filename+'buy_coins.txt','r')
					buy_coins = file.read().replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(","," ")
					buy_coins = buy_coins.split(' ')
					file.close()
				except:
					file = open(username+filename+'buy_coins.txt','w+')
					file.write('')
					file.close()
					buy_coins = []
				print(symbol_list[index])
				print('ADDED')
				try:
					in_it = buy_coins.index(symbol_list[index].replace('-USDT','')+'~'+filename)
				except:
					buy_coins.append(symbol_list[index].replace('-USDT','')+'~'+filename)
					file = open(username+filename+'buy_coins.txt','w')
					file.write(str(buy_coins).replace('"','').replace("'","").replace("[","").replace("{","").replace("]","").replace("}","").replace(":","").replace(",",""))
					file.close()
			else:
				pass
			del last_signal[index]
			last_signal.insert(index, strongbuy_check[1])
			index += 1
			if index >= len(symbol_list):
				index = 0
				symbol_list_len = len(symbol_list)
				break
			else:
				continue
except Exception as e:
	print(str(e))
