Exemplo n.º 1
0
def wait_until_time_to_sell(market):
    utils.print_and_write_to_logfile("WAITING UNTIL TIME TO SELL")

    def process_message(msg):
        global max_price
        global reached_goal
        global percentage_change
        global price_bought
        global cur_price

        cur_price = float(msg['p'])

        percent_from_max = utils.percent_change(max_price, cur_price)
        percent_from_bought = utils.percent_change(price_bought, cur_price)

        # COMMENT THIS LINE OUT IF YOU DON'T WANT TOO MUCH DATA
        print_trade_data(price_bought, cur_price, max_price, percent_from_max, percent_from_bought)

        if reached_goal == False and percent_from_bought >= sell_order_desired_percentage_profit:
            reached_goal = True
            utils.print_and_write_to_logfile("REACHED PRICE GOAL")

        if percent_from_max < sell_percent_down_to_sell and reached_goal == True:
            utils.print_and_write_to_logfile("PERCENT DOWN FROM PEAK: " + str(percent_from_max) + ". TIME TO SELL")
            try:
                reactor.stop()
            except:
                print("REACTOR ALREADY STOPPED")

        max_price = max(cur_price, max_price)

    bm = BinanceSocketManager(binance)
    conn_key = bm.start_trade_socket(market, process_message)
    bm.run()
Exemplo n.º 2
0
class DataCatcher:
    active = 0

    def callback(self, query):
        global run
        if not self.norm['norm']:
            self.finish()
            return

        price = (float(query['data']['asks'][0][0]) +
                 float(query['data']['bids'][0][0])) / 2
        record = Record.objects.create(price=price,
                                       run_id=run,
                                       asks=str(query['data']['asks']),
                                       bids=str(query['data']['bids']))

    def __init__(self, run_id, timeout=600):
        self.manager = multiprocessing.Manager()
        self.norm = self.manager.dict()
        self.norm['norm'] = True
        self.streams = ['btcusdt@depth5']
        self.timeout = timeout
        self.manager = multiprocessing.Manager()
        self.client = init_client()

        global run
        run = run_id

        self.main_socket = BinanceSocketManager(self.client)
        self.connection_key = self.main_socket.start_multiplex_socket(
            self.streams, self.callback)
        self.socket_process = multiprocessing.Process(target=self.process_func)
        self.timeout_timer = None

    def finish(self):
        self.main_socket.stop_socket(self.connection_key)
        self.main_socket.close()
        reactor.stop()
        print('Datacatcher process ended')

    def stop(self):
        self.norm['norm'] = False

    def process_func(self):
        print('Datacatcher process started')
        self.timeout_timer = threading.Timer(self.timeout, self.finish)
        self.timeout_timer.start()
        self.main_socket.run()

    def start(self):
        DataCatcher.active = 1
        self.socket_process.start()
        return timezone.now(), self.timeout
def do():
    def process_message(msg):
        global count
        count += 1

        print(count)

        print("message type: {}".format(msg['e']))
        print(msg)
        price = msg['p']
        print("CURRENT PRICE: " + price + "\n")

        reactor.stop()

    bm = BinanceSocketManager(binance)

    conn_key = bm.start_trade_socket('XLMBTC', process_message)
    bm.run()

    print("Done")