forked from iilunin/crypto-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
158 lines (117 loc) · 4.32 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# import atexit
import signal
from os.path import isfile, join
from os import listdir, environ
import sys
from API.APIServer import APIServer
from Bot.Target import Target
from ConsoleLauncher import ConsoleLauncher
from Bot.ConfigLoader import ConfigLoader
from Bot.Strategy.SmartOrder import SmartOrder
TRADE_FILE_PATH_PATTERN = '{}{}.json'
TEST_PORTFOLIO_PATH = environ.get('TRADE_DIR', 'Trades/Test/')
TRADE_PORTFOLIO_PATH = environ.get('TRADE_DIR', 'Trades/Portfolio/')
COMPLETED_ORDER_PATH_PORTFOLIO = environ.get('TRADE_COMPLETED_DIR', 'Trades/Completed/')
CONF_DIR = environ.get('CONF_DIR', 'Conf/')
ENABLE_CLOUD = True
def main():
launcher = ConsoleLauncher(
TRADE_PORTFOLIO_PATH,
COMPLETED_ORDER_PATH_PORTFOLIO,
CONF_DIR,
environ.get('TRADE_BUCKET') is not None and ENABLE_CLOUD)
api_mode = False
if len(sys.argv) > 1:
if sys.argv[1] == 'api':
api_mode = True
elif sys.argv[1] == 'sync':
launcher.sync_down()
return
elif sys.argv[1] == 'gen':
get_input_for_targets()
return
launcher.start_bot()
if api_mode:
api = APIServer(launcher.trade_handler)
api.run(3000)
# def signal_handler(signal=None, frame=None):
# launcher.stop_bot()
def get_input_for_targets():
smart = 'y'
if len(sys.argv) > 2:
single_arg = sys.argv[2]
args = single_arg.split('/', 4)
price = args[0]
iter = args[1]
increment = args[2]
if len(args) == 4:
smart = args[3]
else:
price = get_input("Start price: ")
iter = get_input("Iterations (5): ", 5)
increment = get_input("Price increase in percent (2): ", 2)
sl = get_input("Sl interval (3):", 3)
smart = get_input("Smart (Y/n):", 'y')
smart = True if not smart or smart.lower() == 'y' else False
generate_targets(float(price), int(iter), float(increment), smart, int(sl))
def get_input(text, default=None):
inp = input(text)
if not inp:
return default
if not inp.strip():
return default
return inp.strip()
def generate_targets(start_price, iter=4, increment=4, smart=True, sl_interval=3):
targets = []
prices = []
for i in range(0, iter):
prices.append(start_price)
vol = round(100/(iter - i), 2)
target = Target(price=start_price, vol='{}%'.format(vol), smart=smart)
if sl_interval != 0 and len(prices) >= sl_interval:
target.sl = prices[-sl_interval]
targets.append(target)
start_price = round(start_price * (1+increment/100), 8)
print(ConfigLoader.get_json_str(targets))
def test_change_order():
cl = ConfigLoader()
orders = cl.load_trade_list(cl.json_loader('trades.json'))
# orders[0].get_available_targets()[0].set_completed()
# orders[0].status = OrderStatus.COMPLETED
cl.save_trades(cl.json_saver('trades2.json'), orders)
orders = cl.load_trade_list(cl.json_loader('trades2.json'))
def test_smart_order():
price_change = []
price_change.extend(range(490, 465, -1))
price_change.extend(range(471, 479, 1))
price_change.extend(range(478, 463, -1))
price_change.extend(range(463, 473, 1))
so = SmartOrder(True, 467)
print('BUY Order')
for p in price_change:
print('Price: {}, SL {}'.format(p, so.price_update(p)))
print('SELL Order')
so = SmartOrder(False, 475)
for p in price_change:
print('Price: {}, SL {}'.format(p, so.price_update(p)))
print('-' * 20)
print('-' * 20)
price_change = []
price_change.extend(range(480, 474, -1))
price_change.extend(range(476, 480, 1))
so = SmartOrder(True, 475)
for p in price_change:
print('Price: {}, SL {}'.format(p, so.price_update(p)))
def save_new_order_file_structure(path, new_path):
target_path_list = [f for f in listdir(path) if isfile(join(path, f)) and f.lower().endswith('json')]
for t_path in target_path_list:
cl = ConfigLoader()
o_loader = cl.json_loader(join(path, t_path))
trades = cl.load_trade_list(o_loader)
for t in trades:
if t.is_completed():
continue
new_trade_path = new_path + t.symbol + '.json'
cl.save_trades(cl.json_saver(new_trade_path), [t])
if __name__ == '__main__':
main()