I am aware that this question was asked before and I have checked the answers, tried the solutions but so far I couldn't achieve what I want.
Essentially I defined 2 different functions which print values from different sources. What I am trying to do is grab those values and compare them with each other.
coins_list_text_file= open("coins.text","r").read().split("\n")
coins_list_text_file = list(filter(None, coins_list_text_file))
def wazi():
symbol = list(map(lambda x: "{}{}".format(x, "inr").lower(), coins_list_text_file))
response = requests.get('https://api.wazirx.com/sapi/v1/tickers/24hr')
wazirx_list = json.loads(response.content)
filtered_list = [d for d in wazirx_list if d["symbol"] in symbol]
for values in filtered_list:
w_symbol = str(values["symbol"])
w_bid_price = float(values["bidPrice"])
w_ask_price = float(values["askPrice"])
def binance(): #check prices on binance exchange
symbol = list(map(lambda x: "{}{}".format(x, "USDT"), coins_list_text_file))
response = requests.get('https://api.binance.com/api/v3/ticker/24hr')
binance_list = json.loads(response.content)
filtered_list = [d for d in binance_list if d["symbol"] in symbol] #filter binance list
for values in filtered_list:
binance_symbol = str(values["symbol"])
binance_bid_price = float(values["bidPrice"])
binance_ask_price = float(values["askPrice"])
def compare(): #this part is not working
if binance_symbol == w_symbol
if binance_ask_price < w_ask_price:
print("buy" binance_symbol "on Binance")
else:
print("buy" binance_symbol "on Wazirx")
compare()
CodePudding user response:
I think you are looking for something like this, try this it will work:
import requests
import json
def compare():
wazi_symbol = list(map(lambda x: "{}{}".format(x, "USDT").lower(), coins_list_text_file))
response_wazi = requests.get('https://api.wazirx.com/sapi/v1/tickers/24hr')
wazirx_list = json.loads(response_wazi.content)
wazi_filtered_list = [d for d in wazirx_list if d["symbol"] in wazi_symbol]
binance_symbol = list(map(lambda x: "{}{}".format(x, "USDT"), coins_list_text_file))
binance_response = requests.get('https://api.binance.com/api/v3/ticker/24hr')
binance_list = json.loads(binance_response.content)
binance_filtered_list = [d for d in binance_list if d["symbol"] in binance_symbol]
for wazir_value in wazi_filtered_list:
for binance_value in binance_filtered_list:
#print(str(wazir_value["symbol"]).lower()," ",str(binance_value["symbol"]).lower())
if str(wazir_value["symbol"]).lower() == str(binance_value["symbol"]).lower():
if float(wazir_value["bidPrice"]) < float(binance_value["bidPrice"]):
print("buy {} on Binance".format(str(binance_value["symbol"])))
else:
print("buy {} on wazir".format(str(binance_value["symbol"])))
compare()
CodePudding user response:
The reason why your code is not working is that the variables you are trying to compare
if binance_symbol == w_symbol
if binance_ask_price < w_ask_price:
print("buy" binance_symbol "on Binance")
else:
print("buy" binance_symbol "on Wazirx")
which are binance_symbol and w_symbol
are not defined in the scope of the function.
When defining a function, all variables defined inside the function are only available in that context. If you would like to access those variables outside the function you would either have to define them as global variables, which is not as good a solution, or return them and get them outside of that function.
You are doing neither.
You should add return statements at the end of the functions manipulating the variables you are interested in (in your case those will be wazi and binance) and, get the results and use those when doing the Comparison.
CodePudding user response:
coins_list_text_file= open("coins.text","r").read().split("\n")
coins_list_text_file = list(filter(None, coins_list_text_file))
def wazi():
symbol = list(map(lambda x: "{}{}".format(x, "inr").lower(), coins_list_text_file))
response = requests.get('https://api.wazirx.com/sapi/v1/tickers/24hr')
wazirx_list = json.loads(response.content)
filtered_list = [d for d in wazirx_list if d["symbol"] in symbol]
w_symbol=[]
w_bid_price=[]
w_ask_price=[]
for values in filtered_list:
w_symbol.append(str(values["symbol"]))
w_bid_price.append(float(values["bidPrice"]))
w_ask_price.append(float(values["askPrice"]))
return w_symbol, w_ask_price
def binance(): #check prices on binance exchange
symbol = list(map(lambda x: "{}{}".format(x, "USDT"), coins_list_text_file))
response = requests.get('https://api.binance.com/api/v3/ticker/24hr')
binance_list = json.loads(response.content)
filtered_list = [d for d in binance_list if d["symbol"] in symbol] #filter binance list
binance_symbol=[]
binance_bid_price=[]
binance_ask_price=[]
for values in filtered_list:
binance_symbol.append(str(values["symbol"]))
binance_bid_price.append(float(values["bidPrice"]))
binance_ask_price.append(float(values["askPrice"]))
return binance_symbol, binance_ask_price
def compare():
binance_symbol, binance_ask_price = binance()
w_symbol, w_ask_price = wazi()
for i in range(0, len(binance_symbol)):
if binance_symbol[i] == w_symbol[i]:
if binance_ask_price[i] < w_ask_price[i]:
print("buy" binance_symbol[i] "on Binance")
else:
print("buy" binance_symbol[i] "on Wazirx")
compare()
Just return the desired outputs from wazi() and binance(). Then call them explicitly from the compare() function and store the returned outputs to the variables. You are then good to go with these functions. It didn't work because you didn't return any outputs from wazi() and binance(). Then in the compare() function, you didn't call any wazi() or binance(), instead, you called local variables used in other functions. Local variables used in other functions can't be called from the function you're working with.
OR TRY THIS ONE:
def compare():
binance_symbol = []
binance_ask_price = []
w_symbol = []
w_ask_price = []
for _ in starmap(list.extend, zip([binance_symbol, binance_ask_price], binance())):
pass
for _ in starmap(list.extend, zip([w_symbol, w_ask_price], wazi())):
pass
for i in range(0, len(binance_symbol)):
if binance_symbol[i] == w_symbol[i]:
if binance_ask_price[i] < w_ask_price[i]:
print("buy" binance_symbol[i] "on Binance")
else:
print("buy" binance_symbol[i] "on Wazirx")
