def _Get_Ping(self, delay_time):
     loop_times = int(delay_time / 15)
     for time in range(loop_times):
         ping = yield from self.ws.recv()
         print(ping)
         yield from self.ws.send(Request.Pong())
         print(Request.Pong())
Exemplo n.º 2
0
def setup(test=False):
    from databases.Database import Database
    from logger.Logger import Logger
    from realms import Realm
    from config import REALMS, DATABASE
    from operations import Operation
    from Requests import Request
    from config import CREDENTIALS

    logger = Logger(os.getcwd(), "d")

    logger.log(msg="\n" * 3, timestamped=False, level_display=False)
    logger.log(msg="*" * 150, timestamped=False, level_display=False)
    logger.log(msg="*" * 65 + "Started new session!" + "*" * 65,
               timestamped=False,
               level_display=False)
    logger.log(msg="*" * 150, timestamped=False, level_display=False)

    db = Database(DATABASE, logger, test)
    request = Request(CREDENTIALS, db, logger)
    operation = Operation(db, logger)

    for realm_id in REALMS:
        realm = Realm(realm_id, db, logger, request)
        operation.realms.append(realm)

    operation.setLiveData(request)

    return operation, request
Exemplo n.º 3
0
 def Trade(cls, amount, crypto_x, crypto_y, price_x, price_y, account,
           cond):
     if cond == 'over':
         if account[crypto_y] > account[crypto_x]:
             yield from WS.send(
                 Request.Place_Order(crypto_y, amount, price_y,
                                     type='sell'))
         else:
             yield from WS.send(
                 Request.Place_Order(crypto_x, amount, price_x, type='buy'))
     elif cond == 'under':
         if account[crypto_y] > account[crypto_x]:
             yield from WS.send(
                 Request.Place_Order(crypto_y, amount, price_y, type='buy'))
         else:
             yield from WS.send(
                 Request.Place_Order(crypto_x, amount, price_x,
                                     type='sell'))
    def _Subscribe_Order_Book(self, crypto, money, subscribe, depth,
                              delay_time):
        # Let the connecting coroutine connect first. So sleep awhile.

        yield from asyncio.sleep(3)
        yield from self.ws.send(
            Request.Order_Book(crypto=crypto,
                               money=money,
                               subscribe=subscribe,
                               depth=depth))
        for count in range(100):
            print(count)
            result_json = yield from self.ws.recv()
            result = json.loads(result_json)
            print(result)
            yield from self.ws.send(Request.Pong())
            yield from asyncio.sleep(delay_time)

        self.loop.stop()
    def _PairTrade(self, crypto_x, crypto_y, money, delay_time, num_data,
                   num_sigma, amount):
        self.buffer[crypto_x] = list()
        self.buffer[crypto_y] = list()

        yield from asyncio.sleep(3)

        # Get the information of my account
        yield from self.ws.send(Request.Get_Balance())
        result_json = yield from self.ws.recv()
        result = json.loads(result_json)
        self.account = result['data']['balance']
        print(self.account)
        count = 0
        try:
            while True:
                yield from asyncio.wait([
                    self._Send_Ticker_Append_Result(crypto_x),
                    self._Send_Ticker_Append_Result(crypto_y)
                ])

                print(self.buffer)
                if len(self.buffer[crypto_x]) > 2:
                    lm, res = PairTrading.LogistLinearRegression(
                        self.buffer[crypto_x][0:250],
                        self.buffer[crypto_y][0:250])
                    sigma = np.std(res)
                    if sigma > 0:
                        yield from PairTrading.PairTrade(
                            crypto_x,
                            crypto_y,
                            self.buffer[crypto_x][-1],
                            self.buffer[crypto_y][-1],
                            lm=lm,
                            amount=amount,
                            sigma=sigma,
                            account=self.account,
                            num_sigma=num_sigma)

                count += 1
                if count == num_data:
                    break
                else:
                    yield from self._Sleep(delay_time)

        except AttributeError:
            raise NotImplementedError(
                'You should Connect to the server first.')

        finally:
            pass
    def _Send_Ticker_Recv_Result(self, crypto):
        yield from self.ws.send(Request.Ticker(crypto, 'USD'))
        result_json = yield from self.ws.recv()
        print(result_json)
        try:
            result = json.loads(result_json)['data']
        # KeyError would happen when the 'ping pong' problem happens. So, just save the current data.
        except KeyError:
            print('Ping Pong problem occurs.')
            self.loop.stop()

        self.training_data[crypto]['high'].append(float(result['high']))
        self.training_data[crypto]['low'].append(float(result['low']))
        self.training_data[crypto]['last'].append(float(result['last']))
Exemplo n.º 7
0
    def init():
        from logger.Logger import Logger
        from databases.Database import Database
        from operations import Operation
        from config import DATABASE as db_data
        from Requests import Request
        from config import CREDENTIALS as data

        logger = Logger(os.getcwd())
        db = Database(db_data, logger, test=True)
        request = Request(data, db, logger)
        operation = Operation(db, logger)

        return data, db, logger, request, operation
    def _Send_Ticker_Append_Result(self, crypto):
        yield from self.ws.send(Request.Ticker(crypto, 'USD'))
        result_json = yield from self.ws.recv()
        try:
            result = json.loads(result_json)['data']
        # KeyError would happen when the 'ping pong' problem happens. So, just save the current data.
        except KeyError:
            print('Ping Pong problem occurs.')
            self.loop.stop()

        # I use last 250 data as the traing for the new realization.
        if len(self.buffer[crypto]) == 251:
            self.buffer[crypto].pop()

        self.buffer[crypto].append(float(result['last']))
Exemplo n.º 9
0
    def init():
        from databases.Database import Database
        from logger.Logger import Logger
        from Requests import Request
        from config import DATABASE as db_data, CREDENTIALS as data

        logger = Logger(os.getcwd())
        db = Database(db_data, logger, test=True)
        request = Request(data, db, logger)
        store = [True, False, True]
        region = [True, False, False]
        indices = [0, 506, 1096]
        realms = [Realm(indices[x], db, logger, request, store[x], region[x]) for x in range(3)]

        return realms, db, logger, request
Exemplo n.º 10
0
    def init():
        from mounts import Mount
        from operations import Operation
        from logger.Logger import Logger
        from databases.Database import Database
        from Requests import Request
        from config import DATABASE, CREDENTIALS

        logger = Logger(os.getcwd())
        db = Database(DATABASE, logger, test=True)
        operation = Operation(db, logger)
        request = Request(CREDENTIALS, db, logger)

        new_mount = Mount(operation, request, _id=69)
        rebuild_mount = Mount(_id=85, **{"name":"Swift Mistsaber", "source":"Vendor", "faction":"Alliance"})

        return new_mount, rebuild_mount, operation, request
Exemplo n.º 11
0
        self.assertEqual({}, operation.insert_data)
        self.assertEqual({}, operation.update_data)

    def test_setTimePosted(self):
        from operations import setTimePosted

        #testing test case
        test_time = setTimePosted(test=True)
        self.assertEqual(12, test_time.hour)
        self.assertEqual(30, test_time.minute)
        self.assertEqual(30, test_time.second)
        self.assertEqual(500000, test_time.microsecond)

        # testing not testcase
        now = dt.now()
        time = setTimePosted()
        self.assertEqual(now.hour, time.hour)

    @unittest.skip
    def test_setTimeSold(self):
        pass


if __name__ == "__main__":
    logger = Logger(os.getcwd())
    db = Database(DATABASE, logger, test=True)
    operation = Operation(db, logger)
    request = Request(CREDENTIALS, db, logger)
    realm = Realm(1096, db, logger, request)
    unittest.main()