diff --git a/.gitignore b/.gitignore index c18dd8d..c8ab231 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ __pycache__/ +log/ +.vs/ diff --git a/data_checker.py b/data_checker.py index 2b353fd..946787e 100644 --- a/data_checker.py +++ b/data_checker.py @@ -1,3 +1,4 @@ +import datetime import transaction @@ -50,10 +51,14 @@ class DataChecker: buy_or_sell = False if self.condition_cnt >= self.condition_limit: buy_or_sell = True - self.type = 'ask' if self.type == 'bid' else 'ask' + self.type = 'ask' if self.type == 'bid' else 'bid' self.condition_cnt = 0 self.condition_limit = Config.INITIAL_LIMIT - print('{} {:.2f} {:.2f} {:.2f} {} ({}) {}'.format(tr, avg, sd, self.condition_limit, self.condition_cnt, self.type, 'action!' if buy_or_sell else '')) + today = datetime.datetime.now().strftime('%Y-%m-%d') + with open('log/trade-{}.txt'.format(today), 'a') as the_file: + the_file.write('{} {:.2f} {:.2f} {:.2f} {} ({}) {}\n'.format(tr, avg, sd, self.condition_limit, self.condition_cnt, self.type, 'action!' if buy_or_sell else '')) + + print('{} {} {:.2f} {:.2f} {:.2f} {} ({}) {}'.format(tr.key, tr, avg, sd, self.condition_limit, self.condition_cnt, self.type, 'action!' if buy_or_sell else '')) diff --git a/data_manager.py b/data_manager.py index 9a8020e..ebe99c7 100644 --- a/data_manager.py +++ b/data_manager.py @@ -1,19 +1,36 @@ +import datetime + import transaction import data_checker class DataMgr: + + TR_MAX_CNT = 500 + TR_MAINTAIN_CNT = 100 + def __init__(self): - self.transactions = {} + self.transactions = [] self.data_checker = data_checker.DataChecker() + def contains(self, tr: transaction.Transaction): + next_item = next((i for i, x in enumerate(self.transactions) if x.key == tr.key), None) + return (next_item is not None) + def insert(self, item): - transactions_new = {} tr = transaction.Transaction(item) - if tr.time in self.transactions: + if self.contains(tr): return False self.data_checker.add(tr) - self.transactions[tr.time] = tr + self.transactions.append(tr) + + if len(self.transactions) >= DataMgr.TR_MAX_CNT: + today = datetime.datetime.now().strftime('%Y-%m-%d') + with open('log/log-{}.txt'.format(today), 'a') as log_file: + for key in self.transactions[:-DataMgr.TR_MAINTAIN_CNT]: + log_file.write('{}\n'.format(self.transactions[key])) + + self.transactions = self.transactions[-DataMgr.TR_MAINTAIN_CNT:] return True diff --git a/main.py b/main.py index 000bcc4..acd6bdd 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +import os + from xcoin_api_client import * import sched import time @@ -32,6 +34,9 @@ rgParams = { scheduler = sched.scheduler(time.time, time.sleep) timer_delay = 0.1 +if not os.path.exists('log'): + os.makedirs('log') + def printTicker(result): print("["+time.strftime("%H:%M:%S")+"]" + " status: " + result["status"] + " last: " + result["data"]["closing_price"]) @@ -40,6 +45,8 @@ def printTransaction(result): for item in result['data']: print('\t[' + item['transaction_date']+']' + item['type'] + ' ' + item['price']); +def printTransactionItem(item): + print('{}'.format(item)) datamgr = data_manager.DataMgr() def main(arg): @@ -49,6 +56,10 @@ def main(arg): result = api.xcoinApiCall("/public/recent_transactions/" + rgParams["order_currency"], rgParams) if result is not None: + #for item in reversed(result['data']): + # printTransactionItem(item) + #print('--------------') + for item in reversed(result['data']): datamgr.insert(item) diff --git a/transaction.py b/transaction.py index a4c3ffe..4fcd5fa 100644 --- a/transaction.py +++ b/transaction.py @@ -2,6 +2,7 @@ class Transaction: def __init__(self): + self.key = None self.time = None self.price = None self.unit = 0 @@ -13,5 +14,7 @@ class Transaction: self.unit = float(item['units_traded']) self.type = item['type'] + self.key = self.time + '_' + str(self.unit) + def __str__(self, **kwargs): - return '[{}] {} {}'.format(self.time, self.type, self.price) \ No newline at end of file + return '[{}] {} {}'.format(self.time, self.type, self.price)