from tradingview_ta import TA_Handler, Interval symbol = 'BTCUSDT' exchange = 'BINANCE' pair = TA_Handler() pair.set_symbol_as(symbol) pair.set_exchange_as_crypto_or_stock(exchange) pair.set_screener_as_crypto() pair.set_interval_as(Interval.INTERVAL_1_DAY) print(pair.get_analysis().summary) # Example output: {"RECOMMENDATION": "BUY", "BUY": 8, "NEUTRAL": 6, "SELL": 3} print(pair.get_analysis().oscillators) print(pair.get_analysis().moving_averages) print(pair.get_analysis().indicators)
import config from binance.client import Client from binance.enums import * client = Client(config.API_KEY, config.API_SECRET, tld='com') now = datetime.now() fecha = now.strftime("%d-%m-%y %H:%M:%S") lista = client.get_all_tickers() strongBuy_list = [] strongSell_list = [] for i in lista: tesla = TA_Handler() tesla.set_symbol_as(i['symbol']) tesla.set_exchange_as_crypto_or_stock("BINANCE") tesla.set_screener_as_crypto() tesla.set_interval_as(Interval.INTERVAL_1_HOUR) print(i['symbol']) try: print(tesla.get_analysis().summary) except Exception as e: print("No Data") continue if ((tesla.get_analysis().summary)["RECOMMENDATION"]) == "STRONG_BUY": print(f" Compar más fuerte {i}", fecha) strongBuy_list.append(i['symbol']) elif ((tesla.get_analysis().summary)["RECOMMENDATION"]) == "STRONG_SELL": print(f" Compar más fuerte {i}", fecha) strongSell_list.append(i['symbol']) print("*** STRONG BUY LIST ***")
class Trader: def __init__(self, wallet: dict, min_price_change: float) -> None: # initialize wallet amounts self.tokens = list(wallet.keys()) self.values = list(wallet.values()) self.symbol = ''.join(self.tokens) self.price_change = min_price_change self.current_price = 0 # initialize price api self.api = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms={0}&tsyms={1}&api_key={2}'.format( self.tokens[0], self.tokens[1], cfg.CRYPTOCOMPARE_TOKEN) # initialize technical analysis api self.handler = TA_Handler() self.handler.set_symbol_as(self.symbol) self.handler.set_exchange_as_crypto_or_stock("BINANCE") self.handler.set_screener_as_crypto() self.handler.set_interval_as(Interval.INTERVAL_15_MINUTES) def check_recommendation(self) -> str: return self.handler.get_analysis().summary['RECOMMENDATION'] def get_price(self) -> float: response = requests.request("GET", self.api) json_resp = json.loads(response.text) return json_resp[self.tokens[0]][self.tokens[1]] def buy(self, percent) -> float: quantity = self.values[1] * percent self.values[0] += quantity / self.current_price self.values[1] -= quantity return quantity / self.current_price def sell(self, percent) -> float: quantity = self.values[0] * percent self.values[1] += quantity * self.current_price self.values[0] -= quantity return quantity # return'SOLD '+str(quantity) +' '+self.trading_pair[0]+' for ' + str(price) def handle_status_change(self, curr) -> float: if curr == 'STRONG_BUY' and self.values[1] > 0: return (self.buy(1), 'BUY') elif curr == 'BUY' and self.values[1] > 0: return (self.buy(0.4), 'BUY') elif curr == 'SELL' and self.values[0] > 0: return (self.sell(0.4), 'SELL') elif curr == 'STRONG_SELL' and self.values[0] > 0: return (self.sell(1), 'SELL') else: return (0, None) def wallet_pretty(self) -> str: w = emoji.emojize( ':credit_card:\n{:.4f} {}\n{:.4f} {}\n\nTotal:\n{:.4f} {}'.format( self.values[0], self.tokens[0], self.values[1], self.tokens[1], self.values[1] + self.values[0] * self.current_price, self.tokens[1])) return w def trade_pretty(self, quantity, side) -> str: if side == 'SELL': return emoji.emojize( ':left_arrow: Sold {:.4f} {} for {:.4f}'.format( quantity, self.tokens[0], self.current_price)) else: return emoji.emojize( ':right_arrow: Bought {:.4f} {} for {:.4f}'.format( quantity, self.tokens[0], self.current_price)) def persist(self) -> None: with open('./saved.pickle', 'wb') as f: pickle.dump(self, f) def run(self) -> str: logging.info('Started trading ' + self.symbol) # prev_rec = 'NONE' prev_price = self.current_price while True: try: rec = self.check_recommendation() self.current_price = self.get_price() price_change = abs(1 - (prev_price / self.current_price)) if price_change > self.price_change or 'STRONG' in rec: traded = self.handle_status_change(rec) self.persist() if traded[0] <= 0: continue prev_price = self.current_price # prev_rec = rec tradedstr = self.trade_pretty(*traded) walletstr = self.wallet_pretty() yield tradedstr logging.info(tradedstr) logging.info(walletstr) self.persist() except: logging.exception('Error occurred') finally: time.sleep(30)
from tradingview_ta import TA_Handler, Interval # from tradingview_ta import Exchange cryptos = ["BTCUSDT", "ETHUSD"] for crypto in cryptos: print("Sancando info de:", crypto, "...") handler = TA_Handler(symbol=crypto, screener="crypto", exchange="binance", interval=Interval.INTERVAL_1_DAY) handler = TA_Handler() handler.set_symbol_as("BTCUSDT") handler.set_exchange_as_crypto_or_stock("binance") handler.set_screener_as_crypto() handler.set_interval_as(Interval.INTERVAL_1_DAY) analysis = handler.get_analysis() summary = analysis.summary oscillators = analysis.oscillators moving_averages = analysis.moving_averages indicators = analysis.indicators time = analysis.time print("Resumen:", summary) print("Osciladores:", oscillators) print("Medias Moviles:", moving_averages) print("Precios:", indicators) print("Tiempo:", time)