-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIstrategy.py
137 lines (124 loc) · 4.57 KB
/
Istrategy.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from abc import ABC, abstractmethod
import time
from pathlib import Path
import importlib
import config
import constants
import datahandler_factory
import reporter_factory
import utility
import trade
class Istrategy(ABC):
def __init__(self):
self.data_handler = datahandler_factory.data_handler_factory().get_data_handler()
self.reporter = reporter_factory.reporter_factory().get_reporter()
self.utility = utility.utility()
self.trades = {}
pair_list = config.PAIR_LIST.split()
for pair in pair_list:
self.trades[pair] = trade.trade(pair)
def run(self, start_date=None, end_date=None):
state = config.STATE
timeframe = config.TIMEFRAME
if(state == "trade"):
tickers = self.data_handler.refresh_tickers()
while True:
self.reporter.notify_time()
# try:
# tickers = self.data_handler.update_live_tickers(tickers)
# self.on_ticker(tickers)
# except:
# print("There was a problem with the data")
tickers = self.data_handler.update_live_tickers(tickers)
self.on_ticker(tickers)
time.sleep(self.utility.timeframe_to_timestamp())
elif(state == "backtest"):
start_date = self.utility.parse_date(start_date)
end_date = self.utility.parse_date(end_date)
start_date = start_date-self.utility.timeframe_to_timestamp()*30
tickers = self.data_handler.fetch_backtest_tickers(start_date, end_date)
self.backtest(tickers)
def on_ticker(self, tickers):
for ticker_name, ticker in tickers.items():
ticker_displayname = self.utility.get_pair_displayname(ticker_name)
df = self.indicator(ticker)
if self.buy_trend(df):
self.reporter.notify_buy(ticker_displayname)
if self.sell_trend(df):
self.reporter.notify_sell(ticker_displayname)
if self.general_sell_strategy(ticker_name, df):
self.reporter.notify_sell(ticker_displayname)
def backtest(self, tickers):
money_change = []
time_change = []
for ticker_name, ticker in tickers.items():
df = self.indicator(ticker)
index = min(30, (len(df.index)-1))
lenght = (len(df.index)-1)
while True:
if index > lenght:
break
if self.buy_trend(df.head(index)):
if self.trades[ticker_name].open==0:
self.trades[ticker_name].open = df['close'].iloc[index]
self.trades[ticker_name].num += 1
if self.sell_trend(df.head(index)):
if self.trades[ticker_name].open!=0:
self.trades[ticker_name].close = df['close'].iloc[index]
profit = (self.trades[ticker_name].close/self.trades[ticker_name].open)-1
self.trades[ticker_name].profit += profit
self.trades[ticker_name].open = 0
self.trades[ticker_name].close = 0
total_money = profit
money_change.append(total_money)
time_change.append(df['date'].iloc[index])
if self.general_sell_strategy(ticker_name, df.head(index)):
if self.trades[ticker_name].open!=0:
self.trades[ticker_name].close = df['close'].iloc[index]
profit = (self.trades[ticker_name].close/self.trades[ticker_name].open)-1
self.trades[ticker_name].profit += profit
self.trades[ticker_name].open = 0
self.trades[ticker_name].close = 0
total_money = profit
money_change.append(total_money)
time_change.append(df['date'].iloc[index])
index += 1
if self.trades[ticker_name].open!=0:
if self.trades[ticker_name].close==0:
self.trades[ticker_name].close = df['close'].iloc[-1]
profit = (self.trades[ticker_name].close/self.trades[ticker_name].open)-1
self.trades[ticker_name].profit += profit
self.trades[ticker_name].open = 0
self.trades[ticker_name].close = 0
total_money = profit
money_change.append(total_money)
time_change.append(df['date'].iloc[-1])
self.utility.analyze_profit(self.trades, (money_change,time_change))
def general_sell_strategy(self, ticker_name, ticker):
if(self.trades[ticker_name].open != 0):
# Stoploss
if((ticker['close'].iloc[-1]/self.trades[ticker_name].open)-1) < -0.02:
return True
# Take Profit
# if((ticker['close'].iloc[-1]/self.trades[ticker_name].open)-1) > 0.02:
# return True
return False
@abstractmethod
def indicator(self, ticker):
pass
@abstractmethod
def buy_trend(self, ticker):
pass
@abstractmethod
def sell_trend(self, ticker):
pass
class strategy_factory():
def get_strategy(self):
strategy_name = config.STRATEGY
strategy = Path(constants.STRATEGY_PATH + strategy_name + ".py")
if strategy.is_file():
strategy_module = importlib.import_module(constants.STRATEGY_PATH[:-1] + "." + strategy_name, package=None)
strategy = getattr(strategy_module, strategy_name)()
return strategy
else:
print("Strategy Not Found!!!")