-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrade
executable file
·196 lines (173 loc) · 6.28 KB
/
trade
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/python3
import sys
import os
from chart import Stack, Chart, Candle, print_buy, print_sell
from config import Config
class Bot:
def __init__(self, pair: str, config: Config):
self.botState = BotState()
self.config = config
self.stacks = []
self.pair = pair
self.pause = 0
def run(self):
while True:
reading = input()
if len(reading) == 0:
continue
self.parse(reading)
def parse(self, info: str):
tmp = info.split(" ")
if tmp[0] == "settings":
self.botState.update_settings(tmp[1], tmp[2])
if tmp[0] == "update":
if tmp[1] == "game":
self.botState.update_game(tmp[2], tmp[3])
for stack in self.stacks:
stack.update(self.botState.charts[self.pair].closes[-1])
if tmp[0] == "action":
self.execute_action()
def execute_action(self):
close = self.botState.charts[self.pair].closes[-1]
self.pause = self.pause - 1 if self.pause > 0 else 0
if not self.check_stacks_bounds(close):
dollars = self.botState.stacks["USDT"]
affordable = dollars / close
if dollars > 1 and self.pause == 0:
for buy_on in self.config.get_buy_on():
conditions = buy_on["conditions"]
if self.config.check_conditions(
conditions, self.botState.charts[self.pair]
):
amount = affordable * (buy_on["risk"] / 100)
self.buy(
amount,
close,
close * float(buy_on["stop_loss"]),
close * float(buy_on["take_profit"]),
float(buy_on["trailing_stop_loss"]) if buy_on["trailing_stop_loss"] is not None else None,
)
self.pause = buy_on["pause"]
return
self.do_nothing()
else:
self.do_nothing()
else:
self.pause = 3
def buy(
self,
amount: float,
closing_price: float,
stop_loss: float,
take_profit: float,
trailing_stop: float = None,
):
self.stacks.append(
Stack(
amount,
closing_price,
stop_loss,
take_profit,
self.pair,
self.botState.transactionFee,
trailing_stop,
)
)
def sell(self, amount: float = None):
sum = 0
if amount is None:
amount = self.get_sum_of_stacks()
if amount > self.get_sum_of_stacks():
raise Exception("You are trying to sell more than you have")
if amount < 0:
raise Exception("You are trying to sell a negative amount")
if amount == 0:
raise Exception("You are trying to sell 0")
while sum < amount:
stack = self.stacks.pop(0)
sum += stack.amount
print_sell(self.pair, amount)
def get_sum_of_stacks(self):
sum = 0
for stack in self.stacks:
sum += stack.amount
return sum
def check_stacks_bounds(self, closing_price: float):
sum = 0
for stack in self.stacks:
if stack.is_out_of_bounds(closing_price):
sum += stack.amount
self.stacks.remove(stack)
print(
"Stack hitted",
"SL" if stack.stop_loss > closing_price else "TP",
file=sys.stderr,
flush=True,
)
if sum > 0:
print_sell(self.pair, sum)
return True
return False
def do_nothing(self):
print("no_moves", flush=True)
class BotState:
def __init__(self):
self.timeBank = 0
self.maxTimeBank = 0
self.timePerMove = 1
self.candleInterval = 1
self.candleFormat = []
self.candlesTotal = 0
self.candlesGiven = 0
self.initialStack = 0
self.transactionFee = 0.1
self.date = 0
self.stacks = dict()
self.charts = dict()
def update_chart(self, pair: str, new_candle_str: str):
if not (pair in self.charts):
self.charts[pair] = Chart()
new_candle_obj = Candle(self.candleFormat, new_candle_str)
self.charts[pair].add_candle(new_candle_obj)
def update_stack(self, key: str, value: float):
self.stacks[key] = value
def update_settings(self, key: str, value: str):
if key == "timebank":
self.maxTimeBank = int(value)
self.timeBank = int(value)
if key == "time_per_move":
self.timePerMove = int(value)
if key == "candle_interval":
self.candleInterval = int(value)
if key == "candle_format":
self.candleFormat = value.split(",")
if key == "candles_total":
self.candlesTotal = int(value)
if key == "candles_given":
self.candlesGiven = int(value)
if key == "initial_stack":
self.initialStack = int(value)
if key == "transaction_fee_percent":
self.transactionFee = float(value)
def update_game(self, key: str, value: str):
if key == "next_candles":
new_candles = value.split(";")
self.date = int(new_candles[0].split(",")[1])
for candle_str in new_candles:
candle_infos = candle_str.strip().split(",")
self.update_chart(candle_infos[0], candle_str)
if key == "stacks":
new_stacks = value.split(",")
for stack_str in new_stacks:
stack_infos = stack_str.strip().split(":")
self.update_stack(stack_infos[0], float(stack_infos[1]))
if __name__ == "__main__":
try:
current_file_path = os.path.dirname(os.path.realpath(__file__))
path = len(sys.argv) > 1 and sys.argv[1] or f"{current_file_path}/config.json"
config = Config(path)
mybot = Bot("USDT_BTC", config)
mybot.run()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(84)