65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
import datetime
|
|
import transaction
|
|
|
|
|
|
class Config:
|
|
LIST_SIZE=20
|
|
INITIAL_LIMIT=5
|
|
FAST_SD=0.75
|
|
FAST_UP=0.05
|
|
FAST_DOWN=0.05
|
|
SLOW_SD=0.2
|
|
SLOW_UP=0.4
|
|
SLOW_DOWN=0.4
|
|
TIME_LIMIT=6
|
|
TIME_DOWN=0.02
|
|
IGNORE_PRICE=50000
|
|
|
|
|
|
class DataChecker:
|
|
def __init__(self, **kwargs):
|
|
self.tr_list = []
|
|
self.condition_limit = Config.INITIAL_LIMIT
|
|
self.condition_cnt = 0
|
|
self.type = 'bid'
|
|
|
|
def add(self, tr: transaction.Transaction):
|
|
if len(self.tr_list) >= Config.LIST_SIZE:
|
|
self.tr_list = self.tr_list[1:]
|
|
self.tr_list.append(tr)
|
|
|
|
avg = sum(tr.price for tr in self.tr_list) / len(self.tr_list)
|
|
variance = sum((tr.price-avg)**2 for tr in self.tr_list) / len(self.tr_list)
|
|
sd = variance**0.5
|
|
|
|
if sd >= Config.FAST_SD:
|
|
if tr.price >= avg:
|
|
self.condition_limit += Config.FAST_UP
|
|
else:
|
|
self.condition_limit -= Config.FAST_DOWN
|
|
else:
|
|
if tr.price >= avg:
|
|
self.condition_limit += Config.SLOW_UP
|
|
else:
|
|
self.condition_limit -= Config.SLOW_DOWN
|
|
|
|
if self.type == tr.type:
|
|
self.condition_cnt += 1
|
|
else:
|
|
self.condition_cnt = 0
|
|
|
|
buy_or_sell = False
|
|
if self.condition_cnt >= self.condition_limit:
|
|
buy_or_sell = True
|
|
self.type = 'ask' if self.type == 'bid' else 'bid'
|
|
self.condition_cnt = 0
|
|
self.condition_limit = Config.INITIAL_LIMIT
|
|
|
|
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 ''))
|
|
|
|
|