Created
April 20, 2021 17:37
-
-
Save XanderVi/e127cc613b45cc30d40bcca751bb53d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
""" | |
other possible urls: | |
api.binance.com | |
api1.binance.com | |
api2.binance.com | |
""" | |
API_URL = 'https://api3.binance.com/api/v3/avgPrice' | |
def get_price(coin: str) -> float: | |
""" | |
:param coin: token name, e.g. BTC for Bitcoin, LTC for Litecoin | |
:return: coin's current price in stable USDT. 1USDT ~ 1$ USA | |
""" | |
data = {'symbol': f'{coin}USDT'} # e.g. BTCUSDT | |
response = requests.get(API_URL, data) | |
return float(response.json()['price']) | |
def count_profit(coin: dict, current_price: float) -> float: | |
""" | |
:param coin: e.g.: | |
{"amount": 1.4466, | |
"buy_price": 200, | |
"desired_sell_price": 219, | |
"last_five_prices": [201.4, 205, 203, 211, 222.2], | |
"desired_price_fall": 10} | |
:param current_price: coin's current price in stable USDT. 1USDT ~ 1$ USA | |
:return: profit in USDT | |
""" | |
# Example: (220 - 200) * 0.5 == 10 | |
return round((current_price - coin['buy_price']) * coin['amount'], 2) | |
def count_price_fall(coin: dict, current_price: float) -> float: | |
""" | |
:param coin: same as in count_profit() | |
:param current_price: same as in count_profit() | |
:return: max diff between last 5 prices and current price, in USDT | |
""" | |
# Example: max([201.4, 205, 203, 211, 222.2]) - 190 == 32.2 | |
try: | |
return max(coin['last_five_prices']) - current_price | |
except: | |
# may happens if coin['last_five_prices'] is empty | |
return 0 | |
def should_i_buy(price_fall: float, desired_fall: float, current_price: float) -> bool: | |
""" | |
:param price_fall: output of count_price_fall() | |
:param desired_fall: desired price fall in PERCENTS! not in USDT. e.g. 10% | |
:param current_price: coin's current price in stable USDT | |
:return: True if you should buy, False otherwise | |
""" | |
# Example: 21 / 200 * 100 == 10.5% and 10.5 >= 10, so -> True | |
if price_fall / current_price * 100 >= desired_fall: | |
return True | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import json | |
from telegram.ext import Updater, CommandHandler | |
# https://python-telegram-bot.readthedocs.io/en/stable/telegram.html | |
from calculations import get_price, count_profit, count_price_fall, should_i_buy | |
TOKEN = 'INSERT_YOUR_TOKEN_HERE' | |
def make_money(update, context): | |
chat_id = update.effective_chat.id | |
while True: | |
with open('my_cryptocoins.json', 'r') as my_coins_data: | |
my_coins = json.loads(my_coins_data.read()) | |
final_message = [] | |
for coin_name in my_coins.keys(): | |
coin = my_coins[coin_name] | |
coin_price = get_price(coin_name) | |
possible_profit = count_profit(coin, coin_price) | |
price_fall = count_price_fall(coin, coin_price) | |
last_prices = coin['last_prices'] | |
if len(last_prices) <= 60: | |
last_prices.append(coin_price) | |
if len(last_prices) <= 59: | |
price_fall = 0 | |
# not enough data to provide correct calculations | |
else: | |
last_prices = last_prices[1:] + [coin_price] | |
my_coins[coin_name]['last_prices'] = last_prices | |
message = '' | |
if coin_price >= coin['desired_sell_price'] and coin['amount'] > 0: | |
message += f'{coin_name} --> TIME TO SELL\n' | |
message += f'possible profit = {possible_profit}$' | |
else: | |
if should_i_buy(price_fall, coin['desired_price_fall'], coin_price): | |
message += f'{coin_name} --> TIME TO BUY\n' | |
message += f'price fall = {round(price_fall / coin_price * 100, 1)}%' | |
else: | |
message += f'{coin_name} --> nothing to do right now...' | |
final_message.append(message) | |
time.sleep(2) | |
with open('my_cryptocoins.json', 'w') as my_coins_data: | |
my_coins_data.write(json.dumps(my_coins, sort_keys=True, indent=2)) | |
context.bot.send_message(chat_id=chat_id, text='\n'.join(final_message)) | |
time.sleep(60) # run every minute | |
def main(): | |
updater = Updater(token=TOKEN, use_context=True) | |
dp = updater.dispatcher | |
dp.add_handler(CommandHandler('make_money', make_money)) | |
updater.start_polling() | |
updater.idle() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ALICE": { | |
"amount": 0, | |
"buy_price": 38, | |
"desired_price_fall": 5, | |
"desired_sell_price": 44.4, | |
"last_prices": [ | |
12.71004841, | |
12.71728863, | |
12.71983559, | |
12.73806222, | |
12.74204871 | |
] | |
}, | |
"ARDR": { | |
"amount": 0, | |
"buy_price": 0.35657, | |
"desired_price_fall": 5, | |
"desired_sell_price": 0.385, | |
"last_prices": [ | |
0.69192631, | |
0.69205345, | |
0.68947941, | |
0.68280223, | |
0.67819879 | |
] | |
}, | |
"BCH": { | |
"amount": 0, | |
"buy_price": 650, | |
"desired_price_fall": 5, | |
"desired_sell_price": 750, | |
"last_prices": [ | |
628.07961283, | |
628.60812333, | |
629.01582194, | |
629.29318231, | |
629.12820048, | |
660.64513681 | |
] | |
}, | |
"CHZ": { | |
"amount": 345.6, | |
"buy_price": 0.43935, | |
"desired_price_fall": 5, | |
"desired_sell_price": 0.525, | |
"last_prices": [ | |
0.47372701, | |
0.47399699, | |
0.47404154, | |
0.47423784, | |
0.4743948, | |
0.47480966, | |
0.47488213, | |
0.47454167, | |
0.47440684, | |
0.47416734, | |
0.47402859, | |
0.47364201, | |
0.47320798, | |
0.473166, | |
0.47306656, | |
0.47329848, | |
0.47325571, | |
0.47322269, | |
0.47318959, | |
0.47320587, | |
0.47330456, | |
0.47334745, | |
0.47312584, | |
0.47295299, | |
0.47272819, | |
0.47260858, | |
0.47237592, | |
0.47236715, | |
0.47269164, | |
0.47303713, | |
0.47321989, | |
0.47321752, | |
0.47305879, | |
0.47298299, | |
0.47189559, | |
0.47143374, | |
0.47103156, | |
0.47071662, | |
0.46992494, | |
0.4696435, | |
0.4695788, | |
0.46941569, | |
0.46953848, | |
0.46941545, | |
0.46925208, | |
0.46908322, | |
0.46866076, | |
0.46842141, | |
0.46819481, | |
0.46808106, | |
0.46834739, | |
0.46862408, | |
0.46877851, | |
0.46883275, | |
0.46898339, | |
0.46960723, | |
0.47049429, | |
0.47100322, | |
0.47140227, | |
0.47116174, | |
0.47096054 | |
] | |
}, | |
"DOT": { | |
"amount": 2.47, | |
"buy_price": 40.65, | |
"desired_price_fall": 5, | |
"desired_sell_price": 44.44, | |
"last_prices": [ | |
40.59707343, | |
40.60759453, | |
40.62486944, | |
40.66087333, | |
40.70293227, | |
40.72148372, | |
40.74692746, | |
40.78215639, | |
40.80679528, | |
40.84112786, | |
40.85074101, | |
40.84543538, | |
40.8321324, | |
40.80070412, | |
40.8132425, | |
40.8424596, | |
40.88214062, | |
40.95977301, | |
41.01259554, | |
41.05039603, | |
41.08479031, | |
41.10728636, | |
41.10719815, | |
41.09407156, | |
41.07555888, | |
41.03898577, | |
41.01090213, | |
41.00284393, | |
40.96449067, | |
40.93678703, | |
40.93800652, | |
40.93664171, | |
40.9316184, | |
40.93327836, | |
42.33760575, | |
42.32814433, | |
42.32327887, | |
42.28516575, | |
42.24177476, | |
42.22444765, | |
42.21106597, | |
42.18999788, | |
42.16848019, | |
42.16191566, | |
42.1373609, | |
42.11165695, | |
42.10106004, | |
42.07728763, | |
42.07073835, | |
42.06635536, | |
42.05983837, | |
42.06763649, | |
42.09329441, | |
42.10433151, | |
42.10416508, | |
42.10535895, | |
42.10116383, | |
42.12940523, | |
42.14840714, | |
42.14704743, | |
42.14707188 | |
] | |
}, | |
"EOS": { | |
"amount": 33.98, | |
"buy_price": 6.2202, | |
"desired_price_fall": 5, | |
"desired_sell_price": 6.66, | |
"last_prices": [ | |
6.18960397, | |
6.19024069, | |
6.19151979, | |
6.19584598, | |
6.2004835, | |
6.20997308, | |
6.2132434, | |
6.21895638, | |
6.22274377, | |
6.22509695, | |
6.22413178, | |
6.22155621, | |
6.21689108, | |
6.20973185, | |
6.20718744, | |
6.20641556, | |
6.21103663, | |
6.21566096, | |
6.21604428, | |
6.21560666, | |
6.21404783, | |
6.21129971, | |
6.20917954, | |
6.20049126, | |
6.19492617, | |
6.18992898, | |
6.18824076, | |
6.1883058, | |
6.18548708, | |
6.18684329, | |
6.18947857, | |
6.19180148, | |
6.19336537, | |
6.19681152, | |
6.37600216, | |
6.38238043, | |
6.39174511, | |
6.39508228, | |
6.4042308, | |
6.41233341, | |
6.42429301, | |
6.43021998, | |
6.43075966, | |
6.42990664, | |
6.42643805, | |
6.42771966, | |
6.4235438, | |
6.41616051, | |
6.40657908, | |
6.40269485, | |
6.39911374, | |
6.39979996, | |
6.39971797, | |
6.39831004, | |
6.39414447, | |
6.3933325, | |
6.39749828, | |
6.39978086, | |
6.40653359, | |
6.40735853, | |
6.40220942 | |
] | |
}, | |
"LINK": { | |
"amount": 3.16, | |
"buy_price": 31.6424, | |
"desired_price_fall": 5, | |
"desired_sell_price": 34.45, | |
"last_prices": [ | |
31.5036039, | |
31.51282683, | |
31.5224485, | |
31.52983563, | |
31.54114078, | |
31.55026523, | |
31.56489275, | |
31.57938985, | |
31.60062222, | |
31.60269155, | |
31.60821898, | |
31.61374471, | |
31.59018186, | |
31.5805574, | |
31.57169725, | |
31.56919259, | |
31.58333892, | |
31.60281081, | |
31.60221433, | |
31.60556279, | |
31.60587026, | |
31.60864604, | |
31.62651557, | |
31.62076356, | |
31.62105937, | |
31.6205996, | |
31.59811239, | |
31.62959425, | |
31.64048122, | |
31.65598457, | |
31.67118363, | |
31.68532582, | |
31.67997785, | |
31.68350938, | |
32.73603403, | |
32.71808411, | |
32.70273699, | |
32.69037089, | |
32.65555152, | |
32.63557058, | |
32.62077328, | |
32.60613716, | |
32.60008117, | |
32.59975446, | |
32.58797578, | |
32.55634939, | |
32.52355158, | |
32.51699348, | |
32.5001376, | |
32.4933562, | |
32.49272596, | |
32.50221073, | |
32.51416628, | |
32.50396069, | |
32.49574317, | |
32.48808872, | |
32.48819037, | |
32.50838219, | |
32.51811969, | |
32.52172111, | |
32.53073008 | |
] | |
}, | |
"LTC": { | |
"amount": 0, | |
"buy_price": 200, | |
"desired_price_fall": 5, | |
"desired_sell_price": 250, | |
"last_prices": [ | |
219.86489156, | |
219.89392475, | |
219.96408683, | |
220.07773126, | |
220.27194868, | |
220.41463508, | |
220.71482046, | |
220.91043112, | |
220.99349824, | |
221.06806428, | |
221.05869042, | |
220.93096444, | |
220.83634253, | |
220.76612093, | |
220.7241666, | |
220.78963412, | |
220.86807305, | |
220.87564486, | |
220.84655221, | |
220.84057134, | |
220.86163973, | |
220.88509074, | |
220.81364965, | |
220.56980768, | |
220.4641237, | |
220.24649026, | |
220.12137339, | |
219.99724436, | |
219.9371435, | |
220.05278457, | |
220.12490445, | |
220.15094832, | |
220.19264215, | |
220.36761952, | |
233.54048246, | |
233.35964016, | |
233.23391109, | |
233.13663933, | |
233.11602344, | |
233.10547839, | |
233.01892178, | |
232.69250166, | |
232.44335262, | |
232.33106257, | |
232.11660744, | |
231.95184585, | |
231.65753056, | |
231.5158513, | |
231.5050244, | |
231.47576463, | |
231.46171649, | |
231.48068523, | |
231.49626282, | |
231.48989635, | |
231.40849146, | |
231.3635276, | |
231.638959, | |
231.76794331, | |
232.04061865, | |
232.15560443, | |
232.23692582 | |
] | |
}, | |
"MATIC": { | |
"amount": 293.96, | |
"buy_price": 0.35, | |
"desired_price_fall": 5, | |
"desired_sell_price": 0.385, | |
"last_prices": [ | |
0.36345784, | |
0.36356949, | |
0.36408799, | |
0.36455101, | |
0.36515641, | |
0.36541472, | |
0.36561146, | |
0.36565267, | |
0.36583102, | |
0.3659081, | |
0.36601495, | |
0.36603398, | |
0.36574679, | |
0.36555669, | |
0.36578754, | |
0.36592325, | |
0.36593593, | |
0.36578102, | |
0.36547231, | |
0.3653709, | |
0.36539453, | |
0.36562691, | |
0.36569135, | |
0.36587162, | |
0.36583213, | |
0.36559482, | |
0.36538461, | |
0.36604021, | |
0.36629961, | |
0.36708592, | |
0.36802762, | |
0.36939981, | |
0.36991533, | |
0.37013589, | |
0.36530999, | |
0.36533721, | |
0.36529329, | |
0.36511802, | |
0.36481519, | |
0.3636809, | |
0.36315293, | |
0.36292036, | |
0.36258649, | |
0.36258951, | |
0.36250484, | |
0.36239916, | |
0.3622357, | |
0.36192012, | |
0.36178695, | |
0.36184698, | |
0.36191936, | |
0.36214734, | |
0.36253149, | |
0.36287772, | |
0.36307014, | |
0.36331815, | |
0.36343468, | |
0.36350615, | |
0.36369092, | |
0.36388953, | |
0.36415388 | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment