-
Notifications
You must be signed in to change notification settings - Fork 31
/
b
executable file
·401 lines (343 loc) · 13.4 KB
/
b
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/bin/bash
BASE_CONFIGS_DIRECTORY="${PWD}/configs"
BASE_INSTANCES_DIRECTORY="${PWD}/instances"
BASE_TMP_DIRECTORY="${PWD}/.tmp"
if [ ! -d "${BASE_CONFIGS_DIRECTORY}" ] || [ ! -d "${BASE_INSTANCES_DIRECTORY}" ]; then
echo "🔴 [ERROR] You are not into a Freqtrade Trading instances directory"
exit 1
fi
prepare() {
DOCKER_FREQTRADE_IMAGE="ph3nol/ft-trading-bot:latest"
DOCKER_FREQTRADE_UI_IMAGE="ph3nol/ft-trading-bot-ui:latest"
DOCKER_CONTAINER_BASE_PREFIX="ft-trading-bots"
DOCKER_NETWORK="${DOCKER_CONTAINER_BASE_PREFIX}-network"
DOCKER_BUILD="docker build"
DOCKER_RUN="docker run --rm -it"
DOCKER_RUN_WITH_RESTART="docker run -d --restart=always -it"
DOCKER_KILL="docker kill"
DOCKER_RM="docker rm"
DOCKER_LOGS="docker logs"
if [[ `uname -m` == 'arm64' ]]; then
DOCKER_BUILD="${DOCKER_BUILD} --platform linux/amd64"
DOCKER_RUN="${DOCKER_RUN} --platform linux/amd64"
DOCKER_RUN_WITH_RESTART="${DOCKER_RUN_WITH_RESTART} --platform linux/amd64"
fi
docker network create ${DOCKER_NETWORK} > /dev/null 2>&1
CONFIGS_PRIVATE_DIRECTORY="${BASE_CONFIGS_DIRECTORY}/private"
if [ ! -d "${CONFIGS_PRIVATE_DIRECTORY}" ]; then mkdir ${CONFIGS_PRIVATE_DIRECTORY}; fi
if [[ "$(ls ${CONFIGS_PRIVATE_DIRECTORY} | wc -l)" -eq "0" ]]; then
cp -r ${PWD}/.stubs/configs/private/* ${CONFIGS_PRIVATE_DIRECTORY}/
fi
}
instance_create() {
if [ -d "${INSTANCE_DIRECTORY}" ] || [ -f "${INSTANCE_CONFIG_FILE}" ]; then
echo "🔴 [ERROR] ${INSTANCE} instance already exists"
exit 1
fi
echo "Creating..."
cp ${PWD}/.stubs/instances/stub.sh ${INSTANCE_CONFIG_FILE}
echo "$(sed "2s/.*/FT_INSTANCE_NAME=\""${INSTANCE}"\"/" ${INSTANCE_CONFIG_FILE})" > ${INSTANCE_CONFIG_FILE}
mkdir -p ${INSTANCE_DIRECTORY} ${INSTANCE_DIRECTORY}/user_data
touch ${INSTANCE_CONFIG_FILE} \
${INSTANCE_DIRECTORY}/tradesv3.sqlite \
${INSTANCE_DIRECTORY}/tradesv3.dryrun.sqlite
chmod -R 777 ${INSTANCE_DIRECTORY}
}
instance_remove() {
if [ ! -d "${INSTANCE_DIRECTORY}" ]; then
echo "🔴 [ERROR] ${INSTANCE} instance does not exist"
exit 1
fi
echo "Removing..."
rm -rf ${INSTANCE_DIRECTORY} ${INSTANCE_CONFIG_FILE}
}
instance_init() {
TMP_CONFIG_EXCHANGE_PAIRSLIST_FILE_NAME="${INSTANCE}.config.exchange.pairs.json"
TMP_CONFIG_EXCHANGE_PAIRSLIST_FILE="${BASE_TMP_DIRECTORY}/${TMP_CONFIG_EXCHANGE_PAIRSLIST_FILE_NAME}"
if [ ! -d "${INSTANCE_DIRECTORY}" ] || [ ! -f "${INSTANCE_CONFIG_FILE}" ]; then
instance_create
fi
. ${INSTANCE_CONFIG_FILE}
FT_CONFIGS_ARGS=""
for CONFIG_FILE in "${FT_INSTANCE_CONFIGS[@]}"; do FT_CONFIGS_ARGS+="--config ${CONFIG_FILE} "; done
VOLUMES=(
"/etc/localtime:/etc/localtime:ro"
"${BASE_CONFIGS_DIRECTORY}:/configs:ro"
"${BASE_TMP_DIRECTORY}:/.tmp:ro"
"${INSTANCE_DIRECTORY}/tradesv3.sqlite:/freqtrade/tradesv3.sqlite:rw"
"${INSTANCE_DIRECTORY}/tradesv3.dryrun.sqlite:/freqtrade/tradesv3.dryrun.sqlite:rw"
"${INSTANCE_DIRECTORY}/user_data:/freqtrade/user_data:rw"
"${PWD}/strategies:/freqtrade/user_data/strategies:rw"
)
VOLUMES_ARGS=""
for VOLUME in "${VOLUMES[@]}"; do VOLUMES_ARGS+="-v ${VOLUME} "; done
ENVS=(
"FREQTRADE__BOT_NAME=${FT_INSTANCE_NAME}"
"FREQTRADE__DRY_RUN=${FT_DRY_RUN}"
"FREQTRADE__DRY_RUN_WALLET=${FT_DRY_RUN_WALLET}"
)
ENVS_ARGS=""
for ENV in "${ENVS[@]}"; do ENVS_ARGS+="-e ${ENV} "; done
DOCKER_CONTAINER_BASE_NAME="${DOCKER_CONTAINER_BASE_PREFIX}-${INSTANCE}"
DOCKER_CONTAINER_NAME="${DOCKER_CONTAINER_BASE_NAME}-${ACTION}"
}
instance_update_backtesting_pairlists() {
echo "Updating backtesting pairlists..."
PAIRS_LIST=$(
${DOCKER_RUN} --name ${DOCKER_CONTAINER_NAME}-update-backtesting-pairslist --network ${DOCKER_NETWORK} \
${VOLUMES_ARGS} ${ENVS_ARGS} \
${DOCKER_FREQTRADE_IMAGE} test-pairlist ${FT_CONFIGS_ARGS} --print-json
)
PAIRS_LIST=${PAIRS_LIST##*$'\n'}
cat << EOF > ${TMP_CONFIG_EXCHANGE_PAIRSLIST_FILE}
{
"exchange": {
"pair_whitelist": ${PAIRS_LIST}
}
}
EOF
}
instance_stop() {
echo "Stopping..."
${DOCKER_KILL} "${DOCKER_CONTAINER_BASE_NAME}-data-update-backtesting-pairslist" > /dev/null 2>&1
${DOCKER_RM} "${DOCKER_CONTAINER_BASE_NAME}-data-update-backtesting-pairslist" > /dev/null 2>&1
${DOCKER_KILL} "${DOCKER_CONTAINER_BASE_NAME}-pairs" > /dev/null 2>&1
${DOCKER_RM} "${DOCKER_CONTAINER_BASE_NAME}-pairs" > /dev/null 2>&1
${DOCKER_KILL} "${DOCKER_CONTAINER_BASE_NAME}-trade" > /dev/null 2>&1
${DOCKER_RM} "${DOCKER_CONTAINER_BASE_NAME}-trade" > /dev/null 2>&1
${DOCKER_KILL} "${DOCKER_CONTAINER_BASE_NAME}-data" > /dev/null 2>&1
${DOCKER_RM} "${DOCKER_CONTAINER_BASE_NAME}-data" > /dev/null 2>&1
${DOCKER_KILL} "${DOCKER_CONTAINER_BASE_NAME}-backtesting" > /dev/null 2>&1
${DOCKER_RM} "${DOCKER_CONTAINER_BASE_NAME}-backtesting" > /dev/null 2>&1
}
instance_logs() {
echo "Tailing instance logs..."
${DOCKER_LOGS} -f ${DOCKER_CONTAINER_NAME}
}
instance_init_backtesting() {
for CONFIG_BACKTEST_FILE in "${FT_INSTANCE_CONFIGS_BACKTESTING[@]}"; do FT_CONFIGS_ARGS+="--config ${CONFIG_BACKTEST_FILE} "; done
if [ -f "${TMP_CONFIG_EXCHANGE_PAIRSLIST_FILE}" ]; then
FT_CONFIGS_ARGS+="--config /.tmp/${TMP_CONFIG_EXCHANGE_PAIRSLIST_FILE_NAME} "
fi
}
instance_display_informations() {
echo ""
if [[ ${FT_DRY_RUN} = "true" ]]; then
DISPLAYED_MODE="😴 [DRY-RUN]"
else
DISPLAYED_MODE="🚀 [LIVE]"
fi
echo "${DISPLAYED_MODE} ${FT_INSTANCE_NAME} @ Port ${FT_API_SERVER_PORT}"
echo " • Strategy .................. ${FT_STRATEGY}"
echo " • Port ...................... ${FT_API_SERVER_PORT}"
if [[ ${FT_DRY_RUN} -eq "true" ]]; then
echo " • Dry-run Wallet ............ ${FT_DRY_RUN_WALLET}"
fi
}
display_help() {
echo "🚀 Trading Bot command usage:"
echo ""
echo " ./`basename ${0}` instance <instance-name> create ...................... Create/Init an instance"
echo " ./`basename ${0}` instance <instance-name> trade ....................... Trade"
echo " ./`basename ${0}` instance <instance-name> pairs <quote> ............... List available exchange pairs"
echo " ./`basename ${0}` instance <instance-name> configs-pairs <quote> ....... Set TMP pairs, from configs"
echo " ./`basename ${0}` instance <instance-name> data <days-count> ........... Download data for backtests"
echo " ./`basename ${0}` instance <instance-name> backtesting <from-date> ..... Run backtest (<from-date> format: 20211102)"
echo " ./`basename ${0}` instance <instance-name> reset ....................... Reset instance data"
echo " ./`basename ${0}` instance <instance-name> remove ...................... Remove instance"
echo " ./`basename ${0}` instance <instance-name> logs ........................ Tail running instance Freqtrade logs"
echo ""
echo " ./`basename ${0}` ui start ............................................. Start UI"
echo " ./`basename ${0}` ui stop .............................................. Stop UI"
echo ""
echo " 👉 Note that you can use './`basename ${0}` i <...>' as an alias for './`basename ${0}` instance <...>'"
}
handle_instance() {
case ${ACTION} in
create)
instance_create
exit 0
;;
remove)
instance_remove
exit 0
;;
reset)
instance_remove && instance_create
exit 0
;;
pairs)
instance_init
echo "🚥 Loading pairs..."
${DOCKER_RUN} --name ${DOCKER_CONTAINER_NAME} --network ${DOCKER_NETWORK} \
${VOLUMES_ARGS} ${ENVS_ARGS} \
${DOCKER_FREQTRADE_IMAGE} list-pairs ${FT_CONFIGS_ARGS} --quote ${ACTION_ARGS[0]} --print-json
exit 0
;;
configs-pairs)
instance_init
instance_update_backtesting_pairlists
exit 0
;;
trade)
instance_init
instance_stop > /dev/null 2>&1
echo "🚥 Trading..."
${DOCKER_RUN_WITH_RESTART} --name ${DOCKER_CONTAINER_NAME} --network ${DOCKER_NETWORK} \
${VOLUMES_ARGS} ${ENVS_ARGS} -p ${FT_API_SERVER_PORT}:8080 \
${DOCKER_FREQTRADE_IMAGE} trade --strategy ${FT_STRATEGY} ${FT_CONFIGS_ARGS} \
> /dev/null 2>&1
instance_display_informations
exit 0
;;
data)
instance_init
instance_update_backtesting_pairlists
instance_init_backtesting
echo "🚥 Downloading data..."
if [ ! "${ACTION_ARGS[1]}" = "" ]; then
${DOCKER_RUN} --name ${DOCKER_CONTAINER_NAME} --network ${DOCKER_NETWORK} \
${VOLUMES_ARGS} ${ENVS_ARGS} \
${DOCKER_FREQTRADE_IMAGE} download-data ${FT_CONFIGS_ARGS} --days ${ACTION_ARGS[0]} -t {1m,5m,15m,1h,4h,12h,1d} \
--pairs ${ACTION_ARGS[1]}
else
${DOCKER_RUN} --name ${DOCKER_CONTAINER_NAME} --network ${DOCKER_NETWORK} \
${VOLUMES_ARGS} ${ENVS_ARGS} \
${DOCKER_FREQTRADE_IMAGE} download-data ${FT_CONFIGS_ARGS} --days ${ACTION_ARGS[0]} -t {1m,5m,15m,1h,4h,12h,1d} \
--erase
fi
exit 0
;;
backtesting)
instance_init && instance_init_backtesting
echo "🚥 Backtesting..."
FT_BACKTEST_TIMERANGE_ARG=""
if [ ! -z ${ACTION_ARGS[0]} ]; then
FT_BACKTEST_TIMERANGE_ARG="--timerange ${ACTION_ARGS[0]}"
fi
${DOCKER_RUN} --name ${DOCKER_CONTAINER_NAME} --network ${DOCKER_NETWORK} \
${VOLUMES_ARGS} ${ENVS_ARGS} \
${DOCKER_FREQTRADE_IMAGE} backtesting --enable-protections --strategy ${FT_STRATEGY} ${FT_CONFIGS_ARGS} ${FT_BACKTEST_TIMERANGE_ARG}
exit 0
;;
stop)
instance_init && instance_stop
exit 0
;;
logs)
instance_init && instance_logs
exit 0
;;
*)
display_help
exit 1
;;
esac
}
ui_init() {
VOLUMES=(
"/etc/localtime:/etc/localtime:ro"
)
VOLUMES_ARGS=""
ENVS_ARGS=""
DOCKER_CONTAINER_BASE_NAME="${DOCKER_CONTAINER_BASE_PREFIX}-${INSTANCE}"
DOCKER_CONTAINER_NAME="${DOCKER_CONTAINER_BASE_NAME}-ui"
UI_PUBLIC_PORT="${ACTION_ARGS[0]:-22222}"
}
ui_start() {
echo "🚥 Starting..."
${DOCKER_RUN_WITH_RESTART} --name ${DOCKER_CONTAINER_NAME} \
${VOLUMES_ARGS} ${ENVS_ARGS} -p ${UI_PUBLIC_PORT}:80 \
${DOCKER_FREQTRADE_UI_IMAGE} \
> /dev/null 2>&1
echo "Note: UI is available from http(s)://<your-ip-or-host>:${UI_PUBLIC_PORT}"
}
ui_stop() {
echo "🚥 Stopping..."
${DOCKER_KILL} ${DOCKER_CONTAINER_NAME} > /dev/null 2>&1
${DOCKER_RM} ${DOCKER_CONTAINER_NAME} > /dev/null 2>&1
}
handle_ui() {
case ${ACTION} in
start)
ui_init && ui_start
exit 0
;;
stop)
ui_init && ui_stop
exit 0
;;
*)
display_help
exit 1
;;
esac
}
handle_list() {
INSTANCES=$(docker ps --no-trunc -f "name=${DOCKER_CONTAINER_BASE_PREFIX}" | sed "1 d")
# @todo To be continued
}
handle_init_install_upgrade() {
echo "🚥 Initializing/Installing/Upgrading..."
echo "--- Be patient, it could take from some seconds to some minutes! ---"
echo ""
echo " > Downloading Freqtrade Docker image..."
docker pull --quiet ${DOCKER_FREQTRADE_IMAGE} > /dev/null 2>&1
echo " > Downloading Freqtrade UI Docker image..."
docker pull --quiet ${DOCKER_FREQTRADE_UI_IMAGE} > /dev/null 2>&1
echo " > Downloading first official Freqtrade strategies and hyperopts..."
rm -rf .tmp/freqtrade-strategies && \
git clone --quiet https://github.com/freqtrade/freqtrade-strategies.git .tmp/freqtrade-strategies \
&& mkdir -p strategies/official \
&& cp -r .tmp/freqtrade-strategies/user_data/strategies/* strategies/official/ \
&& mkdir -p hyperopts/official \
&& cp -r .tmp/freqtrade-strategies/user_data/hyperopts/* hyperopts/official/ \
&& rm -rf .tmp/freqtrade-strategies
echo ""
echo "✅ Ready to use!"
echo ""
display_help
}
prepare
COMMAND="${1}"
case ${COMMAND} in
init | install | upgrade)
handle_init_install_upgrade
exit 0
;;
list)
handle_list
exit 0
;;
i | instance)
INSTANCE="${2}"
ACTION="${3}"
ACTION_ARGS=(${@:4})
if [ -z "${INSTANCE}" ] || [ -z "${ACTION}" ]; then
display_help
exit 1
fi
INSTANCE_DIRECTORY="${BASE_INSTANCES_DIRECTORY}/${INSTANCE}"
INSTANCE_CONFIG_FILE="${BASE_INSTANCES_DIRECTORY}/${INSTANCE}.sh"
handle_instance
exit 0
;;
ui)
ACTION="${2}"
ACTION_ARGS=(${@:3})
if [ -z "${ACTION}" ]; then
display_help
exit 1
fi
handle_ui
exit 0
;;
help)
display_help
exit 0
;;
*)
display_help
exit 1
;;
esac