forked from CoinTK/CoinTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.py
51 lines (46 loc) · 2.09 KB
/
naive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from .core import Strategy
from ..order import Order
from collections import deque
class NaiveStrategy(Strategy):
'''
Very basic strategy: buy if we're on a rising price trend,
sell if we've been losing money
This is just to provide a 'baseline' to test the performance of our
smarter algorithms
This is significantly faster than naive.py, since it doesn't
recalculate uptrend/downtrends from scratch every time. Instead, as we
encounter new data points, we simply increment/decrement the
uptrend/downtrend counters by 1
'''
def __init__(self, n_prices=1000, qty=0.01, threshold=0.8, price_inc=0.1):
super().__init__()
self.n_prices = n_prices
self.old_prices = deque()
self.qty = qty
self.threshold = threshold
self.price_inc = price_inc
self.uptrend_count = 0
self.downtrend_count = 0
def gen_order(self, ts, price, qty, funds, balance):
order = None
if len(self.old_prices) > 1:
if price >= self.old_prices[-1]:
self.uptrend_count += 1
if price <= self.old_prices[-1]:
self.downtrend_count += 1
if len(self.old_prices) > self.n_prices + 1:
if self.old_prices[1] >= self.old_prices[0]:
self.uptrend_count -= 1
if self.old_prices[1] <= self.old_prices[0]:
self.downtrend_count -= 1
self.old_prices.popleft()
# simply Naive strategy: buy if we're on an uptrend,
# sell if we're on a downtrend
if self.uptrend_count / len(self.old_prices) > self.threshold:
order = Order(buy=True, price=price + self.price_inc,
qty=self.qty, identifier=len(self.orders))
if self.downtrend_count / len(self.old_prices) > self.threshold:
order = Order(sell=True, price=price - self.price_inc,
qty=self.qty, identifier=len(self.orders))
self.old_prices.append(price)
return order