mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-11-29 16:43:06 +00:00
Compare commits
1273 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1322b75a9 | ||
|
|
4ce927d455 | ||
|
|
20ebd744c3 | ||
|
|
89180adb35 | ||
|
|
6b008d2237 | ||
|
|
33ce904f41 | ||
|
|
ed34c4f199 | ||
|
|
394ef35a45 | ||
|
|
490cbde652 | ||
|
|
2c49231fcd | ||
|
|
3d4019d8b7 | ||
|
|
6d2afdb146 | ||
|
|
bddf009a2b | ||
|
|
bc13b7901f | ||
|
|
743a1f1604 | ||
|
|
cec58323d4 | ||
|
|
9cbd0df644 | ||
|
|
6adab0cf6b | ||
|
|
aa104f86e8 | ||
|
|
710c7daec5 | ||
|
|
954c6e8c15 | ||
|
|
6d327658ea | ||
|
|
27003c447d | ||
|
|
bb07ad38d3 | ||
|
|
49f2c24698 | ||
|
|
0fab7f0880 | ||
|
|
81020b3612 | ||
|
|
4b78bedddd | ||
|
|
ce142496b1 | ||
|
|
53690c5ece | ||
|
|
a26cdceb4b | ||
|
|
248ff3349b | ||
|
|
55dc699d45 | ||
|
|
9019f6492f | ||
|
|
9cb5591007 | ||
|
|
eac89c244d | ||
|
|
84bbe7728d | ||
|
|
5bd79546ab | ||
|
|
2efc0113fe | ||
|
|
24aa6a1679 | ||
|
|
3775fdf9c7 | ||
|
|
fee8d0a2e1 | ||
|
|
702402e1fe | ||
|
|
4f2d3dbb41 | ||
|
|
02aacdd0c8 | ||
|
|
131dfaf263 | ||
|
|
004e0bb9a3 | ||
|
|
06276e1d24 | ||
|
|
ba5cbcbb3f | ||
|
|
872bbadded | ||
|
|
6a12591248 | ||
|
|
e7399b5046 | ||
|
|
df57c32076 | ||
|
|
f78044da6d | ||
|
|
157f7da8ce | ||
|
|
a356edb117 | ||
|
|
5fb6fa38aa | ||
|
|
99e890bc99 | ||
|
|
280886104c | ||
|
|
1cec06f808 | ||
|
|
85a81b18a3 | ||
|
|
a182cab27f | ||
|
|
b254ff9b41 | ||
|
|
586f49cafd | ||
|
|
611bb52d1f | ||
|
|
f374a062e1 | ||
|
|
fa7f74b4bc | ||
|
|
3f8d7dae39 | ||
|
|
7edbae893d | ||
|
|
7fe0ec5407 | ||
|
|
6b47c39103 | ||
|
|
bd2a6467fe | ||
|
|
4fac61387f | ||
|
|
3cee94226f | ||
|
|
a38c2121cc | ||
|
|
b4d2a3f495 | ||
|
|
6e5c14a95b | ||
|
|
ca9c5edd39 | ||
|
|
a2c3df3ac5 | ||
|
|
d393aa0f87 | ||
|
|
3f4261ad1e | ||
|
|
4c97ee45dd | ||
|
|
9d443b8bd8 | ||
|
|
71025fd374 | ||
|
|
0893431fde | ||
|
|
e5abc15c53 | ||
|
|
8d65452631 | ||
|
|
b8f322d8f6 | ||
|
|
9df5e09a82 | ||
|
|
9559f50eec | ||
|
|
62a3366fbf | ||
|
|
04c6474dd0 | ||
|
|
3553686e50 | ||
|
|
bc554faffb | ||
|
|
a5c62b5c10 | ||
|
|
f6df7df9bf | ||
|
|
33ddc540cf | ||
|
|
7078bc00bd | ||
|
|
d2aea7bdc1 | ||
|
|
d8689e5045 | ||
|
|
5327533188 | ||
|
|
5532cedcdd | ||
|
|
ed71340a90 | ||
|
|
1074415d30 | ||
|
|
90be78b283 | ||
|
|
2de63133ae | ||
|
|
31e2aa0f38 | ||
|
|
cae7be4447 | ||
|
|
a6a38735b1 | ||
|
|
93931eb32b | ||
|
|
967bf417df | ||
|
|
b67257db35 | ||
|
|
94caf82ab2 | ||
|
|
eb8503c547 | ||
|
|
89e8286cbc | ||
|
|
ebe1d3647f | ||
|
|
5ed6f70010 | ||
|
|
a99c8c4046 | ||
|
|
fd44c0e59e | ||
|
|
e6732e01e1 | ||
|
|
e907c48438 | ||
|
|
480d3876b8 | ||
|
|
ab93a61066 | ||
|
|
5f68a445cf | ||
|
|
de454924a3 | ||
|
|
4be75d862f | ||
|
|
61d5e265f5 | ||
|
|
e172bc134b | ||
|
|
0dbc0ffb6b | ||
|
|
215dea0411 | ||
|
|
4cfa3be69e | ||
|
|
d081f6afe7 | ||
|
|
adf6244eda | ||
|
|
1bdbe09b6b | ||
|
|
a10cd23990 | ||
|
|
7c393080ff | ||
|
|
d1dbefa376 | ||
|
|
c94f55807b | ||
|
|
f8e81dde9e | ||
|
|
173b640b34 | ||
|
|
0bb7cc8ab5 | ||
|
|
a8fd7a69ab | ||
|
|
b986ed5613 | ||
|
|
96ad74cd51 | ||
|
|
ea7b25766b | ||
|
|
1d43e04725 | ||
|
|
ba664c4341 | ||
|
|
aa22585d40 | ||
|
|
cf78da5fae | ||
|
|
152c4483c8 | ||
|
|
45341bb246 | ||
|
|
9ae2491b1e | ||
|
|
d685646446 | ||
|
|
de468c6fc8 | ||
|
|
6f3949bb6d | ||
|
|
25d0e5f942 | ||
|
|
4abb7e22ac | ||
|
|
f8781bc193 | ||
|
|
d7e9d8c6cc | ||
|
|
6fcc173489 | ||
|
|
bb1e38f584 | ||
|
|
c52e688979 | ||
|
|
2001c20426 | ||
|
|
5a6f6c7138 | ||
|
|
722ed48d9d | ||
|
|
38510d4b03 | ||
|
|
96a343fb29 | ||
|
|
84759073d9 | ||
|
|
0632cf0f44 | ||
|
|
bbb1a31fda | ||
|
|
6158de3729 | ||
|
|
3ba365ceb2 | ||
|
|
5b314e2f7a | ||
|
|
390501bac0 | ||
|
|
d274f13480 | ||
|
|
6148f98980 | ||
|
|
8bd0f4d0d7 | ||
|
|
bc8ca491cd | ||
|
|
6ef7b7d93d | ||
|
|
f4ec073099 | ||
|
|
766ec5ad0f | ||
|
|
383fb6d20e | ||
|
|
1d251d6151 | ||
|
|
db67b10605 | ||
|
|
4da033c7a2 | ||
|
|
a8b8ab20b7 | ||
|
|
e025dc0dba | ||
|
|
89e3729955 | ||
|
|
3b9e828fa4 | ||
|
|
cf753d5c40 | ||
|
|
314ab0a84f | ||
|
|
b799445b1a | ||
|
|
69eddbbc76 | ||
|
|
4dca84817e | ||
|
|
bf54692efb | ||
|
|
76c5cdc6e3 | ||
|
|
3e89b9685d | ||
|
|
646d1f7316 | ||
|
|
67ad9e9351 | ||
|
|
160af91f9a | ||
|
|
5e73f3431c | ||
|
|
cd28693726 | ||
|
|
ebad2b7542 | ||
|
|
293dc4da80 | ||
|
|
df13a6f333 | ||
|
|
e58cafed6f | ||
|
|
072f0b07d4 | ||
|
|
35c51c73f7 | ||
|
|
fac122891f | ||
|
|
8eed9c08a6 | ||
|
|
1911143a75 | ||
|
|
19616eba35 | ||
|
|
e0153d8203 | ||
|
|
d1bdbcd273 | ||
|
|
bf72b5bc37 | ||
|
|
ec8bf82695 | ||
|
|
f64c8cc9ce | ||
|
|
028700d86f | ||
|
|
d13d6736b9 | ||
|
|
a1ba57186b | ||
|
|
459611516c | ||
|
|
340ab0214b | ||
|
|
178d1ed423 | ||
|
|
cf013140a6 | ||
|
|
e3d222912d | ||
|
|
1134c81aad | ||
|
|
3e07d41fa9 | ||
|
|
b1230b27b7 | ||
|
|
1eecf28a8b | ||
|
|
fe28addb51 | ||
|
|
9bcdc8e14b | ||
|
|
2ce03ab1b5 | ||
|
|
5190cd507e | ||
|
|
2dd2f31431 | ||
|
|
dc105d5eae | ||
|
|
c62356438a | ||
|
|
d74543ac32 | ||
|
|
55a1f604d6 | ||
|
|
f14d6249e0 | ||
|
|
12a19e400f | ||
|
|
53b1f7ac4d | ||
|
|
6f80aff3e2 | ||
|
|
aa7aeb046e | ||
|
|
bf46f2e50d | ||
|
|
4760dd699d | ||
|
|
22c48d5cef | ||
|
|
0454b4c8d5 | ||
|
|
5c02f0983d | ||
|
|
a28ffcbcf7 | ||
|
|
c5400b6c37 | ||
|
|
a071571eac | ||
|
|
5cf2dd79f2 | ||
|
|
cf7c6d2e9c | ||
|
|
8c7b29734e | ||
|
|
192521523f | ||
|
|
2765ee5a85 | ||
|
|
585c2e31c6 | ||
|
|
fecd9f830e | ||
|
|
6efd744497 | ||
|
|
2b6a62faa1 | ||
|
|
4b62f84cc7 | ||
|
|
3fb3d30365 | ||
|
|
2c16ba18a4 | ||
|
|
f45c64d61b | ||
|
|
7bf88333dd | ||
|
|
e1a033672f | ||
|
|
4dbc4cb652 | ||
|
|
d24cd89304 | ||
|
|
0f041b424d | ||
|
|
7688f18a25 | ||
|
|
d5435a9962 | ||
|
|
9f6aedea47 | ||
|
|
45975c9677 | ||
|
|
0a42a0e814 | ||
|
|
5855f0cdfc | ||
|
|
5b71d5f3a1 | ||
|
|
613ad4c5d6 | ||
|
|
e6d6918ed8 | ||
|
|
07b7828f39 | ||
|
|
8ba08af539 | ||
|
|
3aa77360f0 | ||
|
|
c9f97149e1 | ||
|
|
529e4d0131 | ||
|
|
dc322f0423 | ||
|
|
6adeb97b19 | ||
|
|
d53d4b808b | ||
|
|
d313eb812d | ||
|
|
cac2f2b58b | ||
|
|
321e3ede30 | ||
|
|
524290d678 | ||
|
|
5f86c389b0 | ||
|
|
990a609afd | ||
|
|
271e11e065 | ||
|
|
9df2ccbceb | ||
|
|
ac006e0d52 | ||
|
|
0bf56f249a | ||
|
|
b6c6f42d40 | ||
|
|
0d04da3158 | ||
|
|
94172091ae | ||
|
|
e6c215104f | ||
|
|
7a3eb40697 | ||
|
|
7321836bfb | ||
|
|
96c54716d7 | ||
|
|
f69adc1894 | ||
|
|
21b142df40 | ||
|
|
a5155b3b20 | ||
|
|
807c067701 | ||
|
|
b8af493b56 | ||
|
|
e438422a22 | ||
|
|
91ed349e11 | ||
|
|
b8f2341998 | ||
|
|
4799e1ed44 | ||
|
|
e3b295cecc | ||
|
|
2a37034787 | ||
|
|
aae8044150 | ||
|
|
20af5049af | ||
|
|
3e777a9d87 | ||
|
|
36fa5b827d | ||
|
|
7ab2498544 | ||
|
|
df453803ce | ||
|
|
fd9c62d1c4 | ||
|
|
25ab08f422 | ||
|
|
a0dea5a51f | ||
|
|
cec8ef3599 | ||
|
|
d85b56a2bd | ||
|
|
2bccaa31c9 | ||
|
|
45a34be2ac | ||
|
|
9f8539f13e | ||
|
|
33c6ef28f8 | ||
|
|
fe730a3db0 | ||
|
|
f66958c34f | ||
|
|
b44adaa5ab | ||
|
|
3a905e3d59 | ||
|
|
cf4d25d547 | ||
|
|
3b11459a38 | ||
|
|
02079771ef | ||
|
|
7d29df3783 | ||
|
|
9b8cb05037 | ||
|
|
3593626a8e | ||
|
|
45239724c6 | ||
|
|
bb470d0aea | ||
|
|
ffb60fe8b9 | ||
|
|
40a78970e1 | ||
|
|
81ed7627bf | ||
|
|
8be94c4af4 | ||
|
|
9090715ae5 | ||
|
|
a6a479f7aa | ||
|
|
dde0695909 | ||
|
|
d824816880 | ||
|
|
776dd4a0d5 | ||
|
|
f33bc93639 | ||
|
|
67c6c380e1 | ||
|
|
022fedb5d2 | ||
|
|
50402a7805 | ||
|
|
67ddb2e7f8 | ||
|
|
432735773a | ||
|
|
781b9b6dd4 | ||
|
|
c85f498bc7 | ||
|
|
67995a2f49 | ||
|
|
1eebbebed1 | ||
|
|
a5690e707d | ||
|
|
0ff56c6e8d | ||
|
|
b547893fbf | ||
|
|
e14007ced4 | ||
|
|
42919e8864 | ||
|
|
5505845c6f | ||
|
|
95ab7c84bc | ||
|
|
f33923c784 | ||
|
|
a7a7c37121 | ||
|
|
e08003b336 | ||
|
|
29c84bf622 | ||
|
|
b7e297ebda | ||
|
|
fe2f779c47 | ||
|
|
90aae6c3a8 | ||
|
|
0baffd94a4 | ||
|
|
4fe6ae0bae | ||
|
|
477acdd635 | ||
|
|
3da12014b8 | ||
|
|
58d07eeb87 | ||
|
|
42087c9bfe | ||
|
|
5007165908 | ||
|
|
0b24fb50c0 | ||
|
|
7dc63c06e7 | ||
|
|
5819ba9a9c | ||
|
|
4e9e97ddbb | ||
|
|
30ca078cec | ||
|
|
a14d9d35c7 | ||
|
|
c968b904de | ||
|
|
ba65e12c33 | ||
|
|
c83ac5271d | ||
|
|
38101d433b | ||
|
|
30abebfe65 | ||
|
|
c400d15ed1 | ||
|
|
a6cbc1ba16 | ||
|
|
b11fe2f814 | ||
|
|
c593e909aa | ||
|
|
f4298a7323 | ||
|
|
93bd63cfbe | ||
|
|
e220ad5389 | ||
|
|
5c499d16a5 | ||
|
|
6d8252e2b6 | ||
|
|
fcb29c6da5 | ||
|
|
00f1c57279 | ||
|
|
41aa8f18fb | ||
|
|
5eb7aa07a1 | ||
|
|
1792aebaf6 | ||
|
|
eac6e05392 | ||
|
|
04010548f8 | ||
|
|
3e8088d99c | ||
|
|
1c7da95fed | ||
|
|
baae374899 | ||
|
|
a5853681e3 | ||
|
|
be75522507 | ||
|
|
dfd61bbf1d | ||
|
|
c46d78b4b9 | ||
|
|
f7e979f3ba | ||
|
|
fd8e7c2623 | ||
|
|
757a46ab12 | ||
|
|
bce6a7be61 | ||
|
|
6abbf45042 | ||
|
|
ddd62277c2 | ||
|
|
bd356f3eb4 | ||
|
|
aec481b6b3 | ||
|
|
28b1ecb109 | ||
|
|
408f120612 | ||
|
|
c0d3ac5534 | ||
|
|
960d088deb | ||
|
|
19ef682250 | ||
|
|
6171be4f46 | ||
|
|
f6df701b84 | ||
|
|
ad2a5f1717 | ||
|
|
3b6b2aa5fe | ||
|
|
998081785e | ||
|
|
e94e6292e9 | ||
|
|
d2371b5bac | ||
|
|
f40d9dbb05 | ||
|
|
f1efaffe81 | ||
|
|
36797cda30 | ||
|
|
52d881e3f9 | ||
|
|
081d3932b6 | ||
|
|
a7e561b55f | ||
|
|
cf266a67ad | ||
|
|
8bbe8a7f95 | ||
|
|
a3f84d9f21 | ||
|
|
fb110ccfd2 | ||
|
|
99de17da82 | ||
|
|
e3088647fc | ||
|
|
9d75b63a6e | ||
|
|
4a9e1cb345 | ||
|
|
a4b8db38ca | ||
|
|
ddc1b7cd49 | ||
|
|
861e065d08 | ||
|
|
14d16f2574 | ||
|
|
57757d22f9 | ||
|
|
98f808326f | ||
|
|
9a48e3b867 | ||
|
|
6cafa9120c | ||
|
|
4658b554ce | ||
|
|
4a3144ae43 | ||
|
|
fb34fe8c9a | ||
|
|
c9e1fd3fc4 | ||
|
|
423b251467 | ||
|
|
f48b493620 | ||
|
|
5e75f1d8cd | ||
|
|
b34621fadf | ||
|
|
42a135fbd9 | ||
|
|
8e5de365a5 | ||
|
|
658d16c2cd | ||
|
|
3a4ff4c76c | ||
|
|
7b292d5ca3 | ||
|
|
2509ce030d | ||
|
|
15189c28ed | ||
|
|
a0df566b2b | ||
|
|
e2e2005567 | ||
|
|
a799b7d56d | ||
|
|
0d709847ee | ||
|
|
58bcb9dfc8 | ||
|
|
04be438b35 | ||
|
|
fa3b96eb4a | ||
|
|
5723039637 | ||
|
|
6dd48fb820 | ||
|
|
12ffbf5047 | ||
|
|
c670ccfd37 | ||
|
|
8896b39231 | ||
|
|
ce963aae58 | ||
|
|
dc01807b3c | ||
|
|
fadac5fe4a | ||
|
|
99260735ae | ||
|
|
3e1a70bbb2 | ||
|
|
fd3568d48f | ||
|
|
501be8a3bc | ||
|
|
38fe7ec7cd | ||
|
|
01e10014bb | ||
|
|
0e58ab7e01 | ||
|
|
bcabb90f5a | ||
|
|
86b11a9365 | ||
|
|
71bb348698 | ||
|
|
5a82d99482 | ||
|
|
50462fdb00 | ||
|
|
354dcaac58 | ||
|
|
5db04b15e7 | ||
|
|
dd9ab5264d | ||
|
|
5a50b88f52 | ||
|
|
dce554af53 | ||
|
|
130867a6c2 | ||
|
|
b5cd9dab26 | ||
|
|
ec7bfba8df | ||
|
|
f1e176d35c | ||
|
|
92241baade | ||
|
|
f61012097c | ||
|
|
fe26ff763e | ||
|
|
6aa812aa0c | ||
|
|
344843d802 | ||
|
|
3277e491f1 | ||
|
|
80e7f37f50 | ||
|
|
61c4624f5f | ||
|
|
fc2e8b321f | ||
|
|
e5b27baa59 | ||
|
|
a62a5f814a | ||
|
|
53447e7ef5 | ||
|
|
f7a44d1cec | ||
|
|
c34a61dd55 | ||
|
|
e834a4e4f5 | ||
|
|
0328caffe4 | ||
|
|
260bb2f558 | ||
|
|
70f2aed0a7 | ||
|
|
46dc9985fc | ||
|
|
3087ca0823 | ||
|
|
372dc5b49a | ||
|
|
030aedc7d4 | ||
|
|
25e021d4b4 | ||
|
|
d48d2d08df | ||
|
|
524899ccbf | ||
|
|
d4008374f6 | ||
|
|
48432abff1 | ||
|
|
167483f777 | ||
|
|
4eca4abb21 | ||
|
|
e99286f871 | ||
|
|
d8d46890b3 | ||
|
|
98cf986934 | ||
|
|
829da096e2 | ||
|
|
a26cb4bc6b | ||
|
|
1fe86656e1 | ||
|
|
39c6e5263a | ||
|
|
46a1a2de10 | ||
|
|
05f5a1b0ee | ||
|
|
153e11f045 | ||
|
|
4781a23809 | ||
|
|
ed47ee4e29 | ||
|
|
27769f0301 | ||
|
|
feb5da0c35 | ||
|
|
3a902289f1 | ||
|
|
3ac3ead2cf | ||
|
|
0d0737d1f6 | ||
|
|
27fcf62011 | ||
|
|
bb91fdbaf9 | ||
|
|
11cbb9188b | ||
|
|
c11102cf4a | ||
|
|
02fcbbb6d2 | ||
|
|
0d6051e6f9 | ||
|
|
6a433282dc | ||
|
|
8fb404b0f8 | ||
|
|
64530c6196 | ||
|
|
86db6c9084 | ||
|
|
0abc30401c | ||
|
|
1b6b0ad9d2 | ||
|
|
7cdbd550c8 | ||
|
|
94883202b8 | ||
|
|
b0f3fd7ffb | ||
|
|
feca87345f | ||
|
|
f848a5c87d | ||
|
|
0cb57bee0e | ||
|
|
f8cc08e2a1 | ||
|
|
ad2328bbd8 | ||
|
|
d5ca77da97 | ||
|
|
69f68c428e | ||
|
|
abcdbcfd39 | ||
|
|
e67c652988 | ||
|
|
ddc711ec93 | ||
|
|
b9bf5c1118 | ||
|
|
9840e0b5b8 | ||
|
|
ffae0b2cd5 | ||
|
|
fe2b0c2862 | ||
|
|
1647e7a0c1 | ||
|
|
b35fa4c9f6 | ||
|
|
a48840509b | ||
|
|
ca8cab0ce9 | ||
|
|
bbcf6943ce | ||
|
|
fbf9bfe897 | ||
|
|
e46fcf0e02 | ||
|
|
732281bca0 | ||
|
|
f7dd5e6396 | ||
|
|
dd2ccea6e5 | ||
|
|
3d13eb2dc2 | ||
|
|
26b8661325 | ||
|
|
fa97a82568 | ||
|
|
1ae73d7da2 | ||
|
|
d8e692c9a3 | ||
|
|
ca05d1f79e | ||
|
|
9dd38aebe0 | ||
|
|
ceded8a20a | ||
|
|
9c21077dc1 | ||
|
|
fca6a09a41 | ||
|
|
bba711c89a | ||
|
|
5fbaa6d4cf | ||
|
|
5b1f84f816 | ||
|
|
65127533ef | ||
|
|
05ca00b623 | ||
|
|
4b6d855e63 | ||
|
|
7d7752efbf | ||
|
|
ce6f6ab9fe | ||
|
|
3a0569cfd3 | ||
|
|
7d21015b52 | ||
|
|
a57707071c | ||
|
|
2a347e4027 | ||
|
|
4c8ae3a7af | ||
|
|
2773ce7ebf | ||
|
|
f4e4104d14 | ||
|
|
b722a89276 | ||
|
|
4bf6711dbb | ||
|
|
5be733a174 | ||
|
|
c3cae5dfc4 | ||
|
|
0c9d862a49 | ||
|
|
975a785e68 | ||
|
|
6be607e528 | ||
|
|
80c4dea875 | ||
|
|
9e7a4c3717 | ||
|
|
c72e9c3cef | ||
|
|
8175eaa48a | ||
|
|
890083ce7f | ||
|
|
454cd16df4 | ||
|
|
7e233b536c | ||
|
|
ae19ab3dd3 | ||
|
|
bf4b2dc05e | ||
|
|
571ea6a2bc | ||
|
|
b3ea0f4ec5 | ||
|
|
d4c8ad5ba7 | ||
|
|
2432c9f290 | ||
|
|
7f7d53adb7 | ||
|
|
60ed4b9d1e | ||
|
|
83a999d16e | ||
|
|
a29f3de025 | ||
|
|
6ab0ec6aac | ||
|
|
984204e380 | ||
|
|
297166fcb9 | ||
|
|
e6e57e47cf | ||
|
|
bcde377019 | ||
|
|
2d39759d34 | ||
|
|
e4500af736 | ||
|
|
41933c31ca | ||
|
|
47675943ee | ||
|
|
74a708b794 | ||
|
|
833c7f21af | ||
|
|
f8eedc69dd | ||
|
|
797324c35e | ||
|
|
ae967a4f40 | ||
|
|
188fc69e56 | ||
|
|
be8506b45e | ||
|
|
79fcd0b06c | ||
|
|
421ccb23d3 | ||
|
|
f1969175cd | ||
|
|
7fd6d089c0 | ||
|
|
552fba773d | ||
|
|
8e272cfd53 | ||
|
|
36fbe54634 | ||
|
|
90017998fc | ||
|
|
ebe95ba1e1 | ||
|
|
c803762704 | ||
|
|
f8d8f3347a | ||
|
|
d4fcc38a57 | ||
|
|
c60ef181dc | ||
|
|
db4ad2f6f9 | ||
|
|
b5d2cfecc7 | ||
|
|
75955fcc04 | ||
|
|
050e73d960 | ||
|
|
0f2d3adbbc | ||
|
|
ea6a1c629d | ||
|
|
eb53a796e2 | ||
|
|
2d273a8509 | ||
|
|
7169ad557f | ||
|
|
b4d6250d55 | ||
|
|
45f2d01895 | ||
|
|
c176ace889 | ||
|
|
1ce4613aad | ||
|
|
eb473842b8 | ||
|
|
407eaa0870 | ||
|
|
9b09b5aa29 | ||
|
|
70d1511f73 | ||
|
|
4a717f3df8 | ||
|
|
cb7c36a512 | ||
|
|
f37c495b90 | ||
|
|
284c6c4223 | ||
|
|
fd5497cfc7 | ||
|
|
208d3770da | ||
|
|
01b49dc502 | ||
|
|
fbb19e451d | ||
|
|
a1ffa4497d | ||
|
|
e69f9dd029 | ||
|
|
fed3024302 | ||
|
|
dc2f048c98 | ||
|
|
f4ccd4609b | ||
|
|
1e3a29c049 | ||
|
|
82e9ed2ac2 | ||
|
|
ae52880f81 | ||
|
|
90236fb537 | ||
|
|
55d0d27756 | ||
|
|
d849694a70 | ||
|
|
29987c3ff6 | ||
|
|
5f696a0cce | ||
|
|
90d3c09536 | ||
|
|
3f65fc014e | ||
|
|
5344b711ea | ||
|
|
a3e827c144 | ||
|
|
52e267e864 | ||
|
|
165781a545 | ||
|
|
e10a3d1f9d | ||
|
|
0c11d4443f | ||
|
|
50be2fabbf | ||
|
|
7a2e9ef535 | ||
|
|
079f2e3609 | ||
|
|
0e0d613191 | ||
|
|
de68209f3b | ||
|
|
59546b623e | ||
|
|
0a5463fee8 | ||
|
|
cdfb18e9b4 | ||
|
|
1f635d3793 | ||
|
|
714d77dbd8 | ||
|
|
9803130848 | ||
|
|
ad44d8d42a | ||
|
|
68f81b2abb | ||
|
|
4945331093 | ||
|
|
8411844d7e | ||
|
|
00415d66a2 | ||
|
|
f7398e615a | ||
|
|
e81a9cbb17 | ||
|
|
c8c8c626b0 | ||
|
|
9f5f0ddaaa | ||
|
|
80e1e64eae | ||
|
|
37613fc056 | ||
|
|
57c6aefe38 | ||
|
|
133c467cf4 | ||
|
|
900cab4b42 | ||
|
|
f9cc556971 | ||
|
|
f2ce367cec | ||
|
|
fba9cbcff6 | ||
|
|
3e0458da7d | ||
|
|
0d605d2396 | ||
|
|
145583f0b7 | ||
|
|
847dde0d65 | ||
|
|
ab112581a7 | ||
|
|
f48f5d0f31 | ||
|
|
0abf0b0e39 | ||
|
|
965616b214 | ||
|
|
a36fd00f6a | ||
|
|
7f44ba6df4 | ||
|
|
7b0beb0afa | ||
|
|
ae0a1436e2 | ||
|
|
8537e9f40f | ||
|
|
d61d88559c | ||
|
|
9b4c0f01f2 | ||
|
|
6c8253a4f5 | ||
|
|
6464373636 | ||
|
|
dcd0a0ec61 | ||
|
|
ff6b0fc1c9 | ||
|
|
a514b92dcf | ||
|
|
de33d69eed | ||
|
|
9959d53f5e | ||
|
|
6768658300 | ||
|
|
433bf409f4 | ||
|
|
353b0d2d34 | ||
|
|
e644d57dbe | ||
|
|
50e7cef5f3 | ||
|
|
1058820e1b | ||
|
|
24bc3a8390 | ||
|
|
5309ea3820 | ||
|
|
a063680d32 | ||
|
|
10cf2ce853 | ||
|
|
871357a2e3 | ||
|
|
efe0d77dbb | ||
|
|
8d93363655 | ||
|
|
b6dd9dd227 | ||
|
|
95c6ada2ad | ||
|
|
11585f9581 | ||
|
|
8085a7b237 | ||
|
|
c99e2c12ba | ||
|
|
44a4ff0cb2 | ||
|
|
f300af0fe2 | ||
|
|
ff186c7f65 | ||
|
|
41e22657e4 | ||
|
|
974815cb14 | ||
|
|
33beab9c47 | ||
|
|
d258118b0a | ||
|
|
4dab39ed9e | ||
|
|
33293d5cdd | ||
|
|
285308dcbe | ||
|
|
c8fb6c4661 | ||
|
|
1a556198b2 | ||
|
|
98650acca0 | ||
|
|
123f2781a1 | ||
|
|
92f6db5bd7 | ||
|
|
e5f8c1e75d | ||
|
|
4c0a316e3e | ||
|
|
d613d63fdc | ||
|
|
e3941cde7e | ||
|
|
642422d5c4 | ||
|
|
117ec4e64d | ||
|
|
0219584bfe | ||
|
|
d3947fc893 | ||
|
|
fe0c26f536 | ||
|
|
e83e4909a0 | ||
|
|
ed05a1db9d | ||
|
|
21a11f5589 | ||
|
|
6288adfefd | ||
|
|
6a1caafb7a | ||
|
|
ce51749177 | ||
|
|
a68ca31684 | ||
|
|
5f1b9943d1 | ||
|
|
155ed4e501 | ||
|
|
80ef2cfed4 | ||
|
|
5efc417690 | ||
|
|
14868615d5 | ||
|
|
512fcdbcb1 | ||
|
|
6f2caf9698 | ||
|
|
a395a14eeb | ||
|
|
95fe0f4dec | ||
|
|
f6d85e021f | ||
|
|
597f08e2a2 | ||
|
|
df4784e7b9 | ||
|
|
ddd3d2d0a9 | ||
|
|
cb4ecfd3a3 | ||
|
|
f4b59492ab | ||
|
|
ae37f49b51 | ||
|
|
6e68315d2c | ||
|
|
c1c9dd03ce | ||
|
|
e00f02b603 | ||
|
|
9f907d5b5e | ||
|
|
6729574201 | ||
|
|
2a2af4878e | ||
|
|
bfb3e09d1d | ||
|
|
89ee0654f4 | ||
|
|
2ac8b685d6 | ||
|
|
4b38100ae2 | ||
|
|
d6c14d5258 | ||
|
|
cb09cabbdd | ||
|
|
77023c0ecf | ||
|
|
0b18c93d19 | ||
|
|
0617753a7f | ||
|
|
b77fad6e5f | ||
|
|
90bf6f2d4a | ||
|
|
ef7646417b | ||
|
|
01874e379f | ||
|
|
7afd8da28f | ||
|
|
3d532c6015 | ||
|
|
a692ef6715 | ||
|
|
ccb8c3c352 | ||
|
|
18f01113c2 | ||
|
|
f7def09dec | ||
|
|
82bf0be3e2 | ||
|
|
212f4fdd95 | ||
|
|
a5058ff999 | ||
|
|
ea1c16f2ac | ||
|
|
67337fadaa | ||
|
|
94c1d66e59 | ||
|
|
510e6edfbf | ||
|
|
e8c31142ae | ||
|
|
71c780a530 | ||
|
|
7e579de163 | ||
|
|
dd1a52c534 | ||
|
|
e815a43164 | ||
|
|
2f17706e76 | ||
|
|
86a94798dd | ||
|
|
a7cca4985e | ||
|
|
965c075362 | ||
|
|
05d7746f62 | ||
|
|
688326b58c | ||
|
|
0c9993cc89 | ||
|
|
0c35e6ad19 | ||
|
|
68521ea46c | ||
|
|
2fe11cd77a | ||
|
|
e27a6a7a91 | ||
|
|
5bf583cba4 | ||
|
|
a23fce519d | ||
|
|
7f3f127165 | ||
|
|
9ff1f05e66 | ||
|
|
b9c4eafd96 | ||
|
|
7fa5846c6b | ||
|
|
3b37f77a4d | ||
|
|
858d2329e5 | ||
|
|
371ee1e457 | ||
|
|
cfbfe90aa0 | ||
|
|
fd30f5dc59 | ||
|
|
0502bd3c2d | ||
|
|
3ce7ef5e8b | ||
|
|
2324aa0782 | ||
|
|
6a57a8da12 | ||
|
|
9276f3202c | ||
|
|
a6598997e2 | ||
|
|
82913cd3f4 | ||
|
|
be6939ee8a | ||
|
|
7ba4a5d24b | ||
|
|
371e6d99c9 | ||
|
|
84b105c82b | ||
|
|
c6def418cf | ||
|
|
5fce2c5712 | ||
|
|
aacd7d8987 | ||
|
|
4a707d7452 | ||
|
|
21551b3c40 | ||
|
|
7727f2cc8f | ||
|
|
9a87dcf0a1 | ||
|
|
9136e64d89 | ||
|
|
765a762ccf | ||
|
|
02ca2ed585 | ||
|
|
f3ba3ddd54 | ||
|
|
65ce948b0b | ||
|
|
383a9f6eeb | ||
|
|
43dda9c9cf | ||
|
|
7a44a1d1c1 | ||
|
|
5d934cd5b6 | ||
|
|
788cda4925 | ||
|
|
55a69e4a45 | ||
|
|
1931d31147 | ||
|
|
e9dbdc9247 | ||
|
|
86b6c6f334 | ||
|
|
cd5afd6ff4 | ||
|
|
d88cc084e6 | ||
|
|
5deaebf0c2 | ||
|
|
19734ad863 | ||
|
|
b16ccb9919 | ||
|
|
d41837817c | ||
|
|
3ab14dfe39 | ||
|
|
4a91ecd91a | ||
|
|
a3da2911e8 | ||
|
|
6f5b418f0b | ||
|
|
57691c82b1 | ||
|
|
37a74b38ba | ||
|
|
9ab81a987d | ||
|
|
4b08e3d571 | ||
|
|
187fea0c28 | ||
|
|
4e54b27398 | ||
|
|
aced5cc3ba | ||
|
|
669ec30413 | ||
|
|
0082b7abdd | ||
|
|
7903f3a546 | ||
|
|
ec75586bdd | ||
|
|
df9902d6a4 | ||
|
|
315919cdd6 | ||
|
|
63c95a3546 | ||
|
|
59d04d1d0c | ||
|
|
d1cc9e868b | ||
|
|
14de46576b | ||
|
|
bdff29a472 | ||
|
|
8655c6c264 | ||
|
|
3f4e4a23a0 | ||
|
|
b682262486 | ||
|
|
5be7be6189 | ||
|
|
3475a07522 | ||
|
|
fb7ea169d4 | ||
|
|
5469293e5f | ||
|
|
9b644b0305 | ||
|
|
0df1404d6a | ||
|
|
bb4a9ed20f | ||
|
|
77887d6fbc | ||
|
|
d89db50465 | ||
|
|
632d00e01d | ||
|
|
2a56031cdc | ||
|
|
16d412323c | ||
|
|
27a6b29c80 | ||
|
|
5d1f874041 | ||
|
|
174122a09b | ||
|
|
1b6a60ecb2 | ||
|
|
1ccb266032 | ||
|
|
a963f1820c | ||
|
|
b9983149ef | ||
|
|
c1ef3f526c | ||
|
|
6b7afb80b2 | ||
|
|
0b8afa12e9 | ||
|
|
1db0a7d4ce | ||
|
|
c12a9ebd92 | ||
|
|
d86dcc4752 | ||
|
|
0bc96241d5 | ||
|
|
a0bb7a61e6 | ||
|
|
b115963a70 | ||
|
|
2e953a937d | ||
|
|
4e05691cab | ||
|
|
b5f58724a0 | ||
|
|
b83309b55d | ||
|
|
e8101a6da5 | ||
|
|
dd9cb008fb | ||
|
|
81f7172c4a | ||
|
|
bab59fbacd | ||
|
|
0f0b10b6cc | ||
|
|
8e68c5358e | ||
|
|
660f01b514 | ||
|
|
13537e3ce4 | ||
|
|
2963a90008 | ||
|
|
15b20b83fa | ||
|
|
1c3c316e45 | ||
|
|
517879382b | ||
|
|
bcd3340a80 | ||
|
|
12ae1e111e | ||
|
|
d3b3370f23 | ||
|
|
8f817a3634 | ||
|
|
cf79b15651 | ||
|
|
a4284351e3 | ||
|
|
906caf329b | ||
|
|
3db13fae13 | ||
|
|
274972f7af | ||
|
|
83fd27e031 | ||
|
|
3126dcfcea | ||
|
|
72aec6c320 | ||
|
|
b709ccbf53 | ||
|
|
7e99b13742 | ||
|
|
8b464033ff | ||
|
|
93c525a8fa | ||
|
|
54b15c1556 | ||
|
|
029f32af63 | ||
|
|
de13df6ede | ||
|
|
0de211674d | ||
|
|
f7a27c156c | ||
|
|
98f11fc7bb | ||
|
|
013e13e546 | ||
|
|
6ff26c561a | ||
|
|
c81358c291 | ||
|
|
ed34d9f22f | ||
|
|
ee05561ef3 | ||
|
|
69ae99406a | ||
|
|
0cfbb56b6c | ||
|
|
8960373f1c | ||
|
|
349a91bd92 | ||
|
|
991b43b7e5 | ||
|
|
a0fa6abcdc | ||
|
|
86501b43c0 | ||
|
|
80592970e9 | ||
|
|
567ed4ecda | ||
|
|
fafbb0abfe | ||
|
|
0f1a36b8e9 | ||
|
|
31c03cdce1 | ||
|
|
e01c85bb3a | ||
|
|
a1b91ad1ea | ||
|
|
6ce6018bb7 | ||
|
|
18eec0f4d4 | ||
|
|
32327c45c2 | ||
|
|
ba485fe2b2 | ||
|
|
f8084b117e | ||
|
|
abdddd5193 | ||
|
|
8eeb02e592 | ||
|
|
8555271102 | ||
|
|
d921bae75e | ||
|
|
a1388ef296 | ||
|
|
ddc7c94a1d | ||
|
|
e36444df27 | ||
|
|
0395c92260 | ||
|
|
f03395b90d | ||
|
|
20d5628786 | ||
|
|
57e089efd3 | ||
|
|
fbbde9de25 | ||
|
|
3d42b9fd75 | ||
|
|
adfae9e75c | ||
|
|
117dfbb563 | ||
|
|
e66dc8b027 | ||
|
|
ae0b49f532 | ||
|
|
a37ea13fd1 | ||
|
|
cc29126d61 | ||
|
|
810f2f9243 | ||
|
|
60e651cb4c | ||
|
|
472ce8566d | ||
|
|
27ac15f298 | ||
|
|
d12dba16db | ||
|
|
0f1d114c03 | ||
|
|
3e7700e9ac | ||
|
|
60615c232c | ||
|
|
3884cfb809 | ||
|
|
caa6e22e53 | ||
|
|
19f6ff330c | ||
|
|
8fdd127f72 | ||
|
|
0a5eba64e2 | ||
|
|
b82c4444b2 | ||
|
|
95a17b8f98 | ||
|
|
325f72fd91 | ||
|
|
a237225683 | ||
|
|
29b173f4e7 | ||
|
|
50a979161c | ||
|
|
264d71e29e | ||
|
|
a873688a44 | ||
|
|
7cc8533b8e | ||
|
|
04342acff1 | ||
|
|
c37df0e70d | ||
|
|
460dfa1031 | ||
|
|
08a1d3ca1d | ||
|
|
1daeed4a52 | ||
|
|
99724e2458 | ||
|
|
cd18629433 | ||
|
|
41510fdb32 | ||
|
|
9cb249610a | ||
|
|
543857ddb2 | ||
|
|
1e5b0e8726 | ||
|
|
0d0d822904 | ||
|
|
9ff4a7b205 | ||
|
|
0e96197a94 | ||
|
|
9b9d0250f7 | ||
|
|
4a35676794 | ||
|
|
465c91b9a9 | ||
|
|
60249af04c | ||
|
|
c3653dc417 | ||
|
|
3d61095ba4 | ||
|
|
7a0be94cde | ||
|
|
fad6427078 | ||
|
|
4dfde7f9a2 | ||
|
|
e2eceaa904 | ||
|
|
f34af0ad67 | ||
|
|
e07904d436 | ||
|
|
26468bef83 | ||
|
|
ea1b1e11ea | ||
|
|
e68e6c0a1a | ||
|
|
7190226c84 | ||
|
|
6f2915e25e | ||
|
|
6f7ac0720b | ||
|
|
b76554a487 | ||
|
|
8da55c3742 | ||
|
|
05111edd04 | ||
|
|
361bdd20d3 | ||
|
|
8bdace68f6 | ||
|
|
0e1eb20781 | ||
|
|
4c2dea83c5 | ||
|
|
d066817d0b | ||
|
|
a632121368 | ||
|
|
473d09b5cd | ||
|
|
893738d6f0 | ||
|
|
22cfef7d36 | ||
|
|
e1bbe1d9a9 | ||
|
|
ec981b415a | ||
|
|
57a17697a0 | ||
|
|
f4fe09ffbf | ||
|
|
871b5e17ee | ||
|
|
9b00fc3474 | ||
|
|
3b1dc36d8a | ||
|
|
4edf8f2079 | ||
|
|
54987fd9ca | ||
|
|
da9c3e7d7d | ||
|
|
a948142ef5 | ||
|
|
4f6c3f94e0 | ||
|
|
25d6d6bbe5 | ||
|
|
649781d823 | ||
|
|
08ca7a8166 | ||
|
|
dd78c62c3d | ||
|
|
29de1645fe | ||
|
|
4139b0b0c7 | ||
|
|
0c33e917d5 | ||
|
|
e401a016f5 | ||
|
|
e0fde8665c | ||
|
|
752520c823 | ||
|
|
6ba2492360 | ||
|
|
d5d798f6fa | ||
|
|
9c9cf76a0d | ||
|
|
041e201713 | ||
|
|
e09505b22d | ||
|
|
6b15cb9b10 | ||
|
|
ff4fcdc760 | ||
|
|
f43ba44b15 | ||
|
|
79c3e0583d | ||
|
|
f6ef8383bb | ||
|
|
6f5307fda7 | ||
|
|
37004e331a | ||
|
|
57acf85b42 | ||
|
|
96790d50e5 | ||
|
|
d32ff3410c | ||
|
|
35838f5e64 | ||
|
|
913488910c | ||
|
|
17b984a7cd | ||
|
|
f79b44eefe | ||
|
|
146c254c0f | ||
|
|
ce2966dd7f | ||
|
|
0fbca8b8ef | ||
|
|
3f7a583de6 | ||
|
|
1196983d5f | ||
|
|
bbb2c7cf62 | ||
|
|
ff100bdac4 | ||
|
|
4feb038d0a | ||
|
|
1792e0fb9b | ||
|
|
d4f8b3ebbc | ||
|
|
aeef9bac33 | ||
|
|
eff361a104 | ||
|
|
389f9b45bb | ||
|
|
c9741cb291 | ||
|
|
bf6f563df2 | ||
|
|
58f34d4f4b | ||
|
|
2c4d0144ba | ||
|
|
afd1a0bf9b | ||
|
|
37f6c213f6 | ||
|
|
76736902c6 | ||
|
|
d266171ed8 | ||
|
|
e7df373544 | ||
|
|
aa4b64d0bb | ||
|
|
4559ddd74f | ||
|
|
eecc45f8ba | ||
|
|
d76476040a | ||
|
|
0c8c149b86 | ||
|
|
60a7f8614c | ||
|
|
c31b67bf7a | ||
|
|
604a888791 | ||
|
|
bfac1936d9 | ||
|
|
b1de0de5a5 | ||
|
|
75ea2c4e1a | ||
|
|
5e0f143a38 | ||
|
|
2d983db2e0 | ||
|
|
d9b01eee15 | ||
|
|
2e368ef7aa | ||
|
|
34c774c067 | ||
|
|
ac32850034 | ||
|
|
95e5c2e6c1 | ||
|
|
aef42336e6 | ||
|
|
f78427d236 | ||
|
|
b9eb266236 | ||
|
|
e0896fdd7b | ||
|
|
11f97ccf87 | ||
|
|
3506e3ceec | ||
|
|
27b2624a67 | ||
|
|
8500032bff | ||
|
|
b2522b8dbc | ||
|
|
0f3ceebcd4 | ||
|
|
f44ab2f44b | ||
|
|
3fe5302db3 | ||
|
|
ea62c49c3a | ||
|
|
02673b94dd | ||
|
|
17e8bbacc3 | ||
|
|
463123adc5 | ||
|
|
5537f0bf5b | ||
|
|
5551c9ec3b | ||
|
|
ff145b6306 | ||
|
|
add6c875d6 | ||
|
|
378b5a3b14 | ||
|
|
e14cc2e4f6 | ||
|
|
616d5b61cc | ||
|
|
9cca42e371 | ||
|
|
06ad311aa3 | ||
|
|
e42edd9de7 | ||
|
|
3456ead839 | ||
|
|
a4a1f7961a | ||
|
|
8057333501 | ||
|
|
1eee0c91bf | ||
|
|
53b4c3722e | ||
|
|
3f6f502e66 | ||
|
|
d73c656514 | ||
|
|
f409bdbba8 | ||
|
|
4b42e1af4b | ||
|
|
898ab5a370 | ||
|
|
6389778d49 | ||
|
|
b85b913657 | ||
|
|
bbef0edcd1 | ||
|
|
8d3a6279b2 | ||
|
|
df4da75535 | ||
|
|
04bba626a8 | ||
|
|
9c6c21637d | ||
|
|
09b27d2094 | ||
|
|
998a887736 | ||
|
|
00499fa0d7 | ||
|
|
0c517ee3b6 | ||
|
|
b225b0cb90 | ||
|
|
d045116297 | ||
|
|
8b859ad358 | ||
|
|
0085db825d | ||
|
|
1f1e64560a | ||
|
|
c9226a329c | ||
|
|
44cdf3e0c2 | ||
|
|
b97f0f0705 | ||
|
|
b2f4778352 | ||
|
|
272abed807 | ||
|
|
0437546e4b | ||
|
|
2df2041d53 | ||
|
|
3b9d354a62 | ||
|
|
a45073997d | ||
|
|
d102a8f8a1 | ||
|
|
0e5edd08e5 |
5
.coveragerc
Normal file
5
.coveragerc
Normal file
@@ -0,0 +1,5 @@
|
||||
[run]
|
||||
omit =
|
||||
scripts/*
|
||||
freqtrade/tests/*
|
||||
freqtrade/vendor/*
|
||||
15
.dockerignore
Normal file
15
.dockerignore
Normal file
@@ -0,0 +1,15 @@
|
||||
.git
|
||||
.gitignore
|
||||
Dockerfile
|
||||
.dockerignore
|
||||
config.json*
|
||||
*.sqlite
|
||||
.coveragerc
|
||||
.eggs
|
||||
.github
|
||||
.pylintrc
|
||||
.travis.yml
|
||||
CONTRIBUTING.md
|
||||
MANIFEST.in
|
||||
README.md
|
||||
freqtrade.service
|
||||
30
.github/ISSUE_TEMPLATE.md
vendored
Normal file
30
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
## Step 1: Have you search for this issue before posting it?
|
||||
|
||||
If you have discovered a bug in the bot, please [search our issue tracker](https://github.com/gcarq/freqtrade/issues?q=is%3Aissue).
|
||||
If it hasn't been reported, please create a new issue.
|
||||
|
||||
## Step 2: Describe your environment
|
||||
|
||||
* Python Version: _____ (`python -V`)
|
||||
* Branch: Master | Develop
|
||||
* Last Commit ID: _____ (`git log --format="%H" -n 1`)
|
||||
|
||||
## Step 3: Describe the problem:
|
||||
*Explain the problem you have encountered*
|
||||
|
||||
### Steps to reproduce:
|
||||
|
||||
1. _____
|
||||
2. _____
|
||||
3. _____
|
||||
|
||||
### Observed Results:
|
||||
|
||||
* What happened?
|
||||
* What did you expect to happen?
|
||||
|
||||
### Relevant code exceptions or logs:
|
||||
|
||||
```
|
||||
// paste your log here
|
||||
```
|
||||
15
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
15
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
Thank you for sending your pull request. But first, have you included
|
||||
unit tests, and is your code PEP8 conformant? [More details](https://github.com/gcarq/freqtrade/blob/develop/CONTRIBUTING.md)
|
||||
|
||||
## Summary
|
||||
Explain in one sentence the goal of this PR
|
||||
|
||||
Solve the issue: #___
|
||||
|
||||
## Quick changelog
|
||||
|
||||
- <change log #1>
|
||||
- <change log #2>
|
||||
|
||||
## What's new?
|
||||
*Explain in details what this PR solve or improve. You can include visuals.*
|
||||
17
.gitignore
vendored
17
.gitignore
vendored
@@ -1,3 +1,14 @@
|
||||
# Freqtrade rules
|
||||
freqtrade/tests/testdata/*.json
|
||||
hyperopt_conf.py
|
||||
config.json
|
||||
*.sqlite
|
||||
.hyperopt
|
||||
logfile.txt
|
||||
hyperopt_trials.pickle
|
||||
user_data/
|
||||
freqtrade-plot.html
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
@@ -73,11 +84,9 @@ target/
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
config.json
|
||||
preprocessor.py
|
||||
*.sqlite
|
||||
|
||||
.env
|
||||
.venv
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
.pytest_cache/
|
||||
|
||||
@@ -1,2 +1,10 @@
|
||||
[MASTER]
|
||||
extension-pkg-whitelist=numpy,talib,talib.abstract
|
||||
|
||||
[BASIC]
|
||||
good-names=logger
|
||||
ignore=vendor
|
||||
|
||||
[TYPECHECK]
|
||||
ignored-modules=numpy,talib,talib.abstract
|
||||
|
||||
|
||||
30
.travis.yml
30
.travis.yml
@@ -1,13 +1,9 @@
|
||||
sudo: false
|
||||
sudo: true
|
||||
os:
|
||||
- linux
|
||||
language: python
|
||||
python:
|
||||
- 3.6
|
||||
- nightly
|
||||
matrix:
|
||||
allow_failures:
|
||||
- python: nightly
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
@@ -15,13 +11,27 @@ addons:
|
||||
- libdw-dev
|
||||
- binutils-dev
|
||||
install:
|
||||
- wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
|
||||
- tar zxvf ta-lib-0.4.0-src.tar.gz
|
||||
- cd ta-lib && ./configure && sudo make && sudo make install && cd ..
|
||||
- ./install_ta-lib.sh
|
||||
- export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
|
||||
- pip install --upgrade flake8 coveralls pytest-random-order
|
||||
- pip install -r requirements.txt
|
||||
script:
|
||||
- python -m unittest
|
||||
- pip install -e .
|
||||
jobs:
|
||||
include:
|
||||
- script: pytest --cov=freqtrade --cov-config=.coveragerc freqtrade/tests/
|
||||
- script:
|
||||
- cp config.json.example config.json
|
||||
- python freqtrade/main.py backtesting
|
||||
- script:
|
||||
- cp config.json.example config.json
|
||||
- python freqtrade/main.py hyperopt -e 5
|
||||
- script: flake8 freqtrade
|
||||
after_success:
|
||||
- coveralls
|
||||
notifications:
|
||||
slack:
|
||||
secure: bKLXmOrx8e2aPZl7W8DA5BdPAXWGpI5UzST33oc1G/thegXcDVmHBTJrBs4sZak6bgAclQQrdZIsRd2eFYzHLalJEaw6pk7hoAw8SvLnZO0ZurWboz7qg2+aZZXfK4eKl/VUe4sM9M4e/qxjkK+yWG7Marg69c4v1ypF7ezUi1fPYILYw8u0paaiX0N5UX8XNlXy+PBlga2MxDjUY70MuajSZhPsY2pDUvYnMY1D/7XN3cFW0g+3O8zXjF0IF4q1Z/1ASQe+eYjKwPQacE+O8KDD+ZJYoTOFBAPllrtpO1jnOPFjNGf3JIbVMZw4bFjIL0mSQaiSUaUErbU3sFZ5Or79rF93XZ81V7uEZ55vD8KMfR2CB1cQJcZcj0v50BxLo0InkFqa0Y8Nra3sbpV4fV5Oe8pDmomPJrNFJnX6ULQhQ1gTCe0M5beKgVms5SITEpt4/Y0CmLUr6iHDT0CUiyMIRWAXdIgbGh1jfaWOMksybeRevlgDsIsNBjXmYI1Sw2ZZR2Eo2u4R6zyfyjOMLwYJ3vgq9IrACv2w5nmf0+oguMWHf6iWi2hiOqhlAN1W74+3HsYQcqnuM3LGOmuCnPprV1oGBqkPXjIFGpy21gNx4vHfO1noLUyJnMnlu2L7SSuN1CdLsnjJ1hVjpJjPfqB4nn8g12x87TqM1bOm+3Q=
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.cache/pip
|
||||
- ta-lib
|
||||
|
||||
45
CONTRIBUTING.md
Normal file
45
CONTRIBUTING.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# Contribute to freqtrade
|
||||
|
||||
Feel like our bot is missing a feature? We welcome your pull requests! Few pointers for contributions:
|
||||
|
||||
- Create your PR against the `develop` branch, not `master`.
|
||||
- New features need to contain unit tests and must be PEP8
|
||||
conformant (max-line-length = 100).
|
||||
|
||||
If you are unsure, discuss the feature on our [Slack](https://join.slack.com/t/highfrequencybot/shared_invite/enQtMjQ5NTM0OTYzMzY3LWMxYzE3M2MxNDdjMGM3ZTYwNzFjMGIwZGRjNTc3ZGU3MGE3NzdmZGMwNmU3NDM5ZTNmM2Y3NjRiNzk4NmM4OGE)
|
||||
or in a [issue](https://github.com/gcarq/freqtrade/issues) before a PR.
|
||||
|
||||
|
||||
**Before sending the PR:**
|
||||
|
||||
## 1. Run unit tests
|
||||
|
||||
All unit tests must pass. If a unit test is broken, change your code to
|
||||
make it pass. It means you have introduced a regression.
|
||||
|
||||
**Test the whole project**
|
||||
```bash
|
||||
pytest freqtrade
|
||||
```
|
||||
|
||||
**Test only one file**
|
||||
```bash
|
||||
pytest freqtrade/tests/test_<file_name>.py
|
||||
```
|
||||
|
||||
**Test only one method from one file**
|
||||
```bash
|
||||
pytest freqtrade/tests/test_<file_name>.py::test_<method_name>
|
||||
```
|
||||
|
||||
## 2. Test if your code is PEP8 compliant
|
||||
**Install packages** (If not already installed)
|
||||
```bash
|
||||
pip3.6 install flake8 coveralls
|
||||
```
|
||||
**Run Flake8**
|
||||
```bash
|
||||
flake8 freqtrade
|
||||
```
|
||||
|
||||
|
||||
30
Dockerfile
30
Dockerfile
@@ -1,17 +1,23 @@
|
||||
FROM python:3.6.2
|
||||
FROM python:3.6.5-slim-stretch
|
||||
|
||||
RUN pip install numpy
|
||||
RUN apt-get update
|
||||
RUN apt-get -y install build-essential
|
||||
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
|
||||
RUN tar zxvf ta-lib-0.4.0-src.tar.gz
|
||||
RUN cd ta-lib && ./configure && make && make install
|
||||
# Install TA-lib
|
||||
RUN apt-get update && apt-get -y install curl build-essential && apt-get clean
|
||||
RUN curl -L http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz | \
|
||||
tar xzvf - && \
|
||||
cd ta-lib && \
|
||||
./configure && make && make install && \
|
||||
cd .. && rm -rf ta-lib
|
||||
ENV LD_LIBRARY_PATH /usr/local/lib
|
||||
|
||||
RUN mkdir -p /freqtrade
|
||||
# Prepare environment
|
||||
RUN mkdir /freqtrade
|
||||
WORKDIR /freqtrade
|
||||
|
||||
ADD ./requirements.txt /freqtrade/requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
ADD . /freqtrade
|
||||
CMD python main.py
|
||||
# Install dependencies
|
||||
COPY requirements.txt /freqtrade/
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
# Install and execute
|
||||
COPY . /freqtrade/
|
||||
RUN pip install -e .
|
||||
ENTRYPOINT ["freqtrade"]
|
||||
|
||||
5
MANIFEST.in
Normal file
5
MANIFEST.in
Normal file
@@ -0,0 +1,5 @@
|
||||
include LICENSE
|
||||
include README.md
|
||||
include config.json.example
|
||||
recursive-include freqtrade *.py
|
||||
include freqtrade/tests/testdata/*.json
|
||||
253
README.md
253
README.md
@@ -1,90 +1,207 @@
|
||||
# freqtrade
|
||||
|
||||
[](https://travis-ci.org/gcarq/freqtrade)
|
||||
[](https://coveralls.io/github/gcarq/freqtrade?branch=develop)
|
||||
[](https://codeclimate.com/github/gcarq/freqtrade/maintainability)
|
||||
|
||||
Simple High frequency trading bot for crypto currencies.
|
||||
Currently supports trading on Bittrex exchange.
|
||||
|
||||
This software is for educational purposes only.
|
||||
Don't risk money which you are afraid to lose.
|
||||
Simple High frequency trading bot for crypto currencies designed to
|
||||
support multi exchanges and be controlled via Telegram.
|
||||
|
||||
The command interface is accessible via Telegram (not required).
|
||||
Just register a new bot on https://telegram.me/BotFather
|
||||
and enter the telegram `token` and your `chat_id` in `config.json`
|
||||

|
||||
|
||||
Persistence is achieved through sqlite.
|
||||
## Disclaimer
|
||||
This software is for educational purposes only. Do not risk money which
|
||||
you are afraid to lose. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS
|
||||
AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR TRADING RESULTS.
|
||||
|
||||
#### Telegram RPC commands:
|
||||
* /start: Starts the trader
|
||||
* /stop: Stops the trader
|
||||
* /status: Lists all open trades
|
||||
* /profit: Lists cumulative profit from all finished trades
|
||||
* /forcesell <trade_id>: Instantly sells the given trade (Ignoring `minimum_roi`).
|
||||
* /performance: Show performance of each finished trade grouped by pair
|
||||
Always start by running a trading bot in Dry-run and do not engage money
|
||||
before you understand how it works and what profit/loss you should
|
||||
expect.
|
||||
|
||||
#### Config
|
||||
`minimal_roi` is a JSON object where the key is a duration
|
||||
in minutes and the value is the minimum ROI in percent.
|
||||
See the example below:
|
||||
```
|
||||
"minimal_roi": {
|
||||
"2880": 0.005, # Sell after 48 hours if there is at least 0.5% profit
|
||||
"1440": 0.01, # Sell after 24 hours if there is at least 1% profit
|
||||
"720": 0.02, # Sell after 12 hours if there is at least 2% profit
|
||||
"360": 0.02, # Sell after 6 hours if there is at least 2% profit
|
||||
"0": 0.025 # Sell immediately if there is at least 2.5% profit
|
||||
},
|
||||
We strongly recommend you to have coding and Python knowledge. Do not
|
||||
hesitate to read the source code and understand the mechanism of this bot.
|
||||
|
||||
## Table of Contents
|
||||
- [Features](#features)
|
||||
- [Quick start](#quick-start)
|
||||
- [Documentations](https://github.com/gcarq/freqtrade/blob/develop/docs/index.md)
|
||||
- [Installation](https://github.com/gcarq/freqtrade/blob/develop/docs/installation.md)
|
||||
- [Configuration](https://github.com/gcarq/freqtrade/blob/develop/docs/configuration.md)
|
||||
- [Strategy Optimization](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md)
|
||||
- [Backtesting](https://github.com/gcarq/freqtrade/blob/develop/docs/backtesting.md)
|
||||
- [Hyperopt](https://github.com/gcarq/freqtrade/blob/develop/docs/hyperopt.md)
|
||||
- [Support](#support)
|
||||
- [Help](#help--slack)
|
||||
- [Bugs](#bugs--issues)
|
||||
- [Feature Requests](#feature-requests)
|
||||
- [Pull Requests](#pull-requests)
|
||||
- [Basic Usage](#basic-usage)
|
||||
- [Bot commands](#bot-commands)
|
||||
- [Telegram RPC commands](#telegram-rpc-commands)
|
||||
- [Requirements](#requirements)
|
||||
- [Min hardware required](#min-hardware-required)
|
||||
- [Software requirements](#software-requirements)
|
||||
|
||||
## Branches
|
||||
The project is currently setup in two main branches:
|
||||
- `develop` - This branch has often new features, but might also cause
|
||||
breaking changes.
|
||||
- `master` - This branch contains the latest stable release. The bot
|
||||
'should' be stable on this branch, and is generally well tested.
|
||||
|
||||
## Features
|
||||
- [x] **Based on Python 3.6+**: For botting on any operating system -
|
||||
Windows, macOS and Linux
|
||||
- [x] **Persistence**: Persistence is achieved through sqlite
|
||||
- [x] **Dry-run**: Run the bot without playing money.
|
||||
- [x] **Backtesting**: Run a simulation of your buy/sell strategy.
|
||||
- [x] **Strategy Optimization**: Optimize your buy/sell strategy
|
||||
parameters with Hyperopts.
|
||||
- [x] **Whitelist crypto-currencies**: Select which crypto-currency you
|
||||
want to trade.
|
||||
- [x] **Blacklist crypto-currencies**: Select which crypto-currency you
|
||||
want to avoid.
|
||||
- [x] **Manageable via Telegram**: Manage the bot with Telegram
|
||||
- [x] **Display profit/loss in fiat**: Display your profit/loss in
|
||||
33 fiat.
|
||||
- [x] **Daily summary of profit/loss**: Provide a daily summary
|
||||
of your profit/loss.
|
||||
- [x] **Performance status report**: Provide a performance status of
|
||||
your current trades.
|
||||
|
||||
### Exchange supported
|
||||
- [x] Bittrex
|
||||
- [ ] Binance
|
||||
- [ ] Others
|
||||
|
||||
## Quick start
|
||||
This quick start section is a very short explanation on how to test the
|
||||
bot in dry-run. We invite you to read the
|
||||
[bot documentation](https://github.com/gcarq/freqtrade/blob/develop/docs/index.md)
|
||||
to ensure you understand how the bot is working.
|
||||
|
||||
### Easy installation
|
||||
The script below will install all dependencies and help you to configure the bot.
|
||||
```bash
|
||||
./setup.sh --install
|
||||
```
|
||||
|
||||
`stoploss` is loss in percentage that should trigger a sale.
|
||||
For example value `-0.10` will cause immediate sell if the
|
||||
profit dips below -10% for a given trade. This parameter is optional.
|
||||
### Manual installation
|
||||
The following steps are made for Linux/MacOS environment
|
||||
|
||||
`initial_state` is an optional field that defines the initial application state.
|
||||
Possible values are `running` or `stopped`. (default=`running`)
|
||||
If the value is `stopped` the bot has to be started with `/start` first.
|
||||
|
||||
`ask_last_balance` sets the bidding price. Value `0.0` will use `ask` price, `1.0` will
|
||||
use the `last` price and values between those interpolate between ask and last price. Using `ask` price will guarantee quick success in bid, but bot will also end up paying more then would probably have been necessary.
|
||||
|
||||
The other values should be self-explanatory,
|
||||
if not feel free to raise a github issue.
|
||||
|
||||
#### Prerequisites
|
||||
* python3.6
|
||||
* sqlite
|
||||
* [TA-lib](https://github.com/mrjbq7/ta-lib#dependencies) binaries
|
||||
|
||||
#### Install
|
||||
**1. Clone the repo**
|
||||
```bash
|
||||
git clone git@github.com:gcarq/freqtrade.git
|
||||
git checkout develop
|
||||
cd freqtrade
|
||||
```
|
||||
$ cd freqtrade/
|
||||
# copy example config. Dont forget to insert your api keys
|
||||
$ cp config.json.example config.json
|
||||
$ python -m venv .env
|
||||
$ source .env/bin/activate
|
||||
$ pip install -r requirements.txt
|
||||
$ ./main.py
|
||||
**2. Create the config file**
|
||||
Switch `"dry_run": true,`
|
||||
```bash
|
||||
cp config.json.example config.json
|
||||
vi config.json
|
||||
```
|
||||
**3. Build your docker image and run it**
|
||||
```bash
|
||||
docker build -t freqtrade .
|
||||
docker run --rm -v /etc/localtime:/etc/localtime:ro -v `pwd`/config.json:/freqtrade/config.json -it freqtrade
|
||||
```
|
||||
|
||||
There is also an [article](https://www.sales4k.com/blockchain/high-frequency-trading-bot-tutorial/) about how to setup the bot (thanks [@gurghet](https://github.com/gurghet)).
|
||||
|
||||
#### Execute tests
|
||||
### Help / Slack
|
||||
For any questions not covered by the documentation or for further
|
||||
information about the bot, we encourage you to join our slack channel.
|
||||
- [Click here to join Slack channel](https://join.slack.com/t/highfrequencybot/shared_invite/enQtMjQ5NTM0OTYzMzY3LWMxYzE3M2MxNDdjMGM3ZTYwNzFjMGIwZGRjNTc3ZGU3MGE3NzdmZGMwNmU3NDM5ZTNmM2Y3NjRiNzk4NmM4OGE).
|
||||
|
||||
### [Bugs / Issues](https://github.com/gcarq/freqtrade/issues?q=is%3Aissue)
|
||||
If you discover a bug in the bot, please
|
||||
[search our issue tracker](https://github.com/gcarq/freqtrade/issues?q=is%3Aissue)
|
||||
first. If it hasn't been reported, please
|
||||
[create a new issue](https://github.com/gcarq/freqtrade/issues/new) and
|
||||
ensure you follow the template guide so that our team can assist you as
|
||||
quickly as possible.
|
||||
|
||||
### [Feature Requests](https://github.com/gcarq/freqtrade/labels/enhancement)
|
||||
Have you a great idea to improve the bot you want to share? Please,
|
||||
first search if this feature was not [already discussed](https://github.com/gcarq/freqtrade/labels/enhancement).
|
||||
If it hasn't been requested, please
|
||||
[create a new request](https://github.com/gcarq/freqtrade/issues/new)
|
||||
and ensure you follow the template guide so that it does not get lost
|
||||
in the bug reports.
|
||||
|
||||
### [Pull Requests](https://github.com/gcarq/freqtrade/pulls)
|
||||
Feel like our bot is missing a feature? We welcome your pull requests!
|
||||
Please read our
|
||||
[Contributing document](https://github.com/gcarq/freqtrade/blob/develop/CONTRIBUTING.md)
|
||||
to understand the requirements before sending your pull-requests.
|
||||
|
||||
**Important:** Always create your PR against the `develop` branch, not
|
||||
`master`.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
### Bot commands
|
||||
|
||||
```bash
|
||||
usage: main.py [-h] [-v] [--version] [-c PATH] [--dry-run-db] [--datadir PATH]
|
||||
[--dynamic-whitelist [INT]]
|
||||
{backtesting,hyperopt} ...
|
||||
|
||||
Simple High Frequency Trading Bot for crypto currencies
|
||||
|
||||
positional arguments:
|
||||
{backtesting,hyperopt}
|
||||
backtesting backtesting module
|
||||
hyperopt hyperopt module
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-v, --verbose be verbose
|
||||
--version show program's version number and exit
|
||||
-c PATH, --config PATH
|
||||
specify configuration file (default: config.json)
|
||||
--dry-run-db Force dry run to use a local DB
|
||||
"tradesv3.dry_run.sqlite" instead of memory DB. Work
|
||||
only if dry_run is enabled.
|
||||
--datadir PATH path to backtest data (default freqdata/tests/testdata
|
||||
--dynamic-whitelist [INT]
|
||||
dynamically generate and update whitelist based on 24h
|
||||
BaseVolume (Default 20 currencies)
|
||||
```
|
||||
$ python -m unittest
|
||||
```
|
||||
More details on:
|
||||
- [How to run the bot](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#bot-commands)
|
||||
- [How to use Backtesting](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#backtesting-commands)
|
||||
- [How to use Hyperopt](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#hyperopt-commands)
|
||||
|
||||
### Telegram RPC commands
|
||||
Telegram is not mandatory. However, this is a great way to control your
|
||||
bot. More details on our
|
||||
[documentation](https://github.com/gcarq/freqtrade/blob/develop/docs/index.md)
|
||||
|
||||
#### Docker
|
||||
```
|
||||
$ cd freqtrade
|
||||
$ docker build -t freqtrade .
|
||||
$ docker run --rm -it freqtrade
|
||||
```
|
||||
- `/start`: Starts the trader
|
||||
- `/stop`: Stops the trader
|
||||
- `/status [table]`: Lists all open trades
|
||||
- `/count`: Displays number of open trades
|
||||
- `/profit`: Lists cumulative profit from all finished trades
|
||||
- `/forcesell <trade_id>|all`: Instantly sells the given trade
|
||||
(Ignoring `minimum_roi`).
|
||||
- `/performance`: Show performance of each finished trade grouped by pair
|
||||
- `/balance`: Show account balance per currency
|
||||
- `/daily <n>`: Shows profit or loss per day, over the last n days
|
||||
- `/help`: Show help message
|
||||
- `/version`: Show version
|
||||
|
||||
#### Contributing
|
||||
## Requirements
|
||||
|
||||
Feel like our bot is missing a feature? We welcome your pull requests! Few pointers for contributions:
|
||||
### Min hardware required
|
||||
To run this bot we recommend you a cloud instance with a minimum of:
|
||||
* Minimal (advised) system requirements: 2GB RAM, 1GB disk space, 2vCPU
|
||||
|
||||
- Create your PR against the `develop` branch, not `master`.
|
||||
- New features need to contain unit tests.
|
||||
- If you are unsure, discuss the feature on [slack](https://join.slack.com/t/highfrequencybot/shared_invite/enQtMjQ5NTM0OTYzMzY3LWMxYzE3M2MxNDdjMGM3ZTYwNzFjMGIwZGRjNTc3ZGU3MGE3NzdmZGMwNmU3NDM5ZTNmM2Y3NjRiNzk4NmM4OGE) or in a [issue](https://github.com/gcarq/freqtrade/issues) before a PR.
|
||||
### Software requirements
|
||||
- [Python 3.6.x](http://docs.python-guide.org/en/latest/starting/installation/)
|
||||
- [pip](https://pip.pypa.io/en/stable/installing/)
|
||||
- [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
|
||||
- [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html)
|
||||
- [virtualenv](https://virtualenv.pypa.io/en/stable/installation/) (Recommended)
|
||||
- [Docker](https://www.docker.com/products/docker) (Recommended)
|
||||
|
||||
163
analyze.py
163
analyze.py
@@ -1,163 +0,0 @@
|
||||
import time
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import arrow
|
||||
import requests
|
||||
from pandas import DataFrame
|
||||
import talib.abstract as ta
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_ticker(pair: str, minimum_date: arrow.Arrow) -> dict:
|
||||
"""
|
||||
Request ticker data from Bittrex for a given currency pair
|
||||
"""
|
||||
url = 'https://bittrex.com/Api/v2.0/pub/market/GetTicks'
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
|
||||
}
|
||||
params = {
|
||||
'marketName': pair.replace('_', '-'),
|
||||
'tickInterval': 'fiveMin',
|
||||
'_': minimum_date.timestamp * 1000
|
||||
}
|
||||
data = requests.get(url, params=params, headers=headers).json()
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return data
|
||||
|
||||
|
||||
def parse_ticker_dataframe(ticker: list, minimum_date: arrow.Arrow) -> DataFrame:
|
||||
"""
|
||||
Analyses the trend for the given pair
|
||||
:param pair: pair as str in format BTC_ETH or BTC-ETH
|
||||
:return: DataFrame
|
||||
"""
|
||||
df = DataFrame(ticker) \
|
||||
.drop('BV', 1) \
|
||||
.rename(columns={'C':'close', 'V':'volume', 'O':'open', 'H':'high', 'L':'low', 'T':'date'}) \
|
||||
.sort_values('date')
|
||||
return df[df['date'].map(arrow.get) > minimum_date]
|
||||
|
||||
|
||||
def populate_indicators(dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Adds several different TA indicators to the given DataFrame
|
||||
"""
|
||||
dataframe['ema'] = ta.EMA(dataframe, timeperiod=33)
|
||||
dataframe['sar'] = ta.SAR(dataframe, 0.02, 0.22)
|
||||
dataframe['adx'] = ta.ADX(dataframe)
|
||||
|
||||
return dataframe
|
||||
|
||||
|
||||
def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the buy trend for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
prev_sar = dataframe['sar'].shift(1)
|
||||
prev_close = dataframe['close'].shift(1)
|
||||
prev_sar2 = dataframe['sar'].shift(2)
|
||||
prev_close2 = dataframe['close'].shift(2)
|
||||
|
||||
# wait for stable turn from bearish to bullish market
|
||||
dataframe.loc[
|
||||
(dataframe['close'] > dataframe['sar']) &
|
||||
(prev_close > prev_sar) &
|
||||
(prev_close2 < prev_sar2),
|
||||
'swap'
|
||||
] = 1
|
||||
|
||||
# consider prices above ema to be in upswing
|
||||
dataframe.loc[dataframe['ema'] <= dataframe['close'], 'upswing'] = 1
|
||||
|
||||
dataframe.loc[
|
||||
(dataframe['upswing'] == 1) &
|
||||
(dataframe['swap'] == 1) &
|
||||
(dataframe['adx'] > 25), # adx over 25 tells there's enough momentum
|
||||
'buy'] = 1
|
||||
dataframe.loc[dataframe['buy'] == 1, 'buy_price'] = dataframe['close']
|
||||
|
||||
return dataframe
|
||||
|
||||
|
||||
def analyze_ticker(pair: str) -> DataFrame:
|
||||
"""
|
||||
Get ticker data for given currency pair, push it to a DataFrame and
|
||||
add several TA indicators and buy signal to it
|
||||
:return DataFrame with ticker data and indicator data
|
||||
"""
|
||||
minimum_date = arrow.utcnow().shift(hours=-6)
|
||||
data = get_ticker(pair, minimum_date)
|
||||
dataframe = parse_ticker_dataframe(data['result'], minimum_date)
|
||||
dataframe = populate_indicators(dataframe)
|
||||
dataframe = populate_buy_trend(dataframe)
|
||||
return dataframe
|
||||
|
||||
def get_buy_signal(pair: str) -> bool:
|
||||
"""
|
||||
Calculates a buy signal based several technical analysis indicators
|
||||
:param pair: pair in format BTC_ANT or BTC-ANT
|
||||
:return: True if pair is good for buying, False otherwise
|
||||
"""
|
||||
dataframe = analyze_ticker(pair)
|
||||
latest = dataframe.iloc[-1]
|
||||
|
||||
# Check if dataframe is out of date
|
||||
signal_date = arrow.get(latest['date'])
|
||||
if signal_date < arrow.now() - timedelta(minutes=10):
|
||||
return False
|
||||
|
||||
signal = latest['buy'] == 1
|
||||
logger.debug('buy_trigger: %s (pair=%s, signal=%s)', latest['date'], pair, signal)
|
||||
return signal
|
||||
|
||||
|
||||
def plot_dataframe(dataframe: DataFrame, pair: str) -> None:
|
||||
"""
|
||||
Plots the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:param pair: pair as str
|
||||
:return: None
|
||||
"""
|
||||
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use("Qt5Agg")
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Two subplots sharing x axis
|
||||
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
|
||||
fig.suptitle(pair, fontsize=14, fontweight='bold')
|
||||
ax1.plot(dataframe.index.values, dataframe['sar'], 'g_', label='pSAR')
|
||||
ax1.plot(dataframe.index.values, dataframe['close'], label='close')
|
||||
# ax1.plot(dataframe.index.values, dataframe['sell'], 'ro', label='sell')
|
||||
ax1.plot(dataframe.index.values, dataframe['ema'], '--', label='EMA(20)')
|
||||
ax1.plot(dataframe.index.values, dataframe['buy'], 'bo', label='buy')
|
||||
ax1.legend()
|
||||
|
||||
ax2.plot(dataframe.index.values, dataframe['adx'], label='ADX')
|
||||
ax2.plot(dataframe.index.values, [25] * len(dataframe.index.values))
|
||||
ax2.legend()
|
||||
|
||||
# Fine-tune figure; make subplots close to each other and hide x ticks for
|
||||
# all but bottom plot.
|
||||
fig.subplots_adjust(hspace=0)
|
||||
plt.setp([a.get_xticklabels() for a in fig.axes[:-1]], visible=False)
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Install PYQT5==5.9 manually if you want to test this helper function
|
||||
while True:
|
||||
test_pair = 'BTC_ANT'
|
||||
#for pair in ['BTC_ANT', 'BTC_ETH', 'BTC_GNT', 'BTC_ETC']:
|
||||
# get_buy_signal(pair)
|
||||
plot_dataframe(analyze_ticker(test_pair), test_pair)
|
||||
time.sleep(60)
|
||||
7
bin/freqtrade
Executable file
7
bin/freqtrade
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
from freqtrade.main import main, set_loggers
|
||||
set_loggers()
|
||||
main(sys.argv[1:])
|
||||
@@ -2,36 +2,43 @@
|
||||
"max_open_trades": 3,
|
||||
"stake_currency": "BTC",
|
||||
"stake_amount": 0.05,
|
||||
"fiat_display_currency": "USD",
|
||||
"dry_run": false,
|
||||
"minimal_roi": {
|
||||
"2880": 0.005,
|
||||
"720": 0.01,
|
||||
"0": 0.02
|
||||
},
|
||||
"stoploss": -0.10,
|
||||
"unfilledtimeout": 600,
|
||||
"bid_strategy": {
|
||||
"ask_last_balance": 0.0
|
||||
},
|
||||
"bittrex": {
|
||||
"enabled": true,
|
||||
"key": "key",
|
||||
"secret": "secret",
|
||||
"exchange": {
|
||||
"name": "bittrex",
|
||||
"key": "your_exchange_key",
|
||||
"secret": "your_exchange_secret",
|
||||
"pair_whitelist": [
|
||||
"BTC_RLC",
|
||||
"BTC_TKN",
|
||||
"BTC_TRST",
|
||||
"BTC_SWT",
|
||||
"BTC_PIVX",
|
||||
"BTC_MLN",
|
||||
"BTC_XZC",
|
||||
"BTC_TIME",
|
||||
"BTC_LUN"
|
||||
"BTC_ETH",
|
||||
"BTC_LTC",
|
||||
"BTC_ETC",
|
||||
"BTC_DASH",
|
||||
"BTC_ZEC",
|
||||
"BTC_XLM",
|
||||
"BTC_NXT",
|
||||
"BTC_POWR",
|
||||
"BTC_ADA",
|
||||
"BTC_XMR"
|
||||
],
|
||||
"pair_blacklist": [
|
||||
"BTC_DOGE"
|
||||
]
|
||||
},
|
||||
"experimental": {
|
||||
"use_sell_signal": false,
|
||||
"sell_profit_only": false
|
||||
},
|
||||
"telegram": {
|
||||
"enabled": true,
|
||||
"token": "token",
|
||||
"chat_id": "chat_id"
|
||||
"token": "your_telegram_token",
|
||||
"chat_id": "your_telegram_chat_id"
|
||||
},
|
||||
"initial_state": "running"
|
||||
}
|
||||
"initial_state": "running",
|
||||
"internals": {
|
||||
"process_throttle_secs": 5
|
||||
}
|
||||
}
|
||||
|
||||
54
config_full.json.example
Normal file
54
config_full.json.example
Normal file
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"max_open_trades": 3,
|
||||
"stake_currency": "BTC",
|
||||
"stake_amount": 0.05,
|
||||
"fiat_display_currency": "USD",
|
||||
"dry_run": false,
|
||||
"ticker_interval": 5,
|
||||
"minimal_roi": {
|
||||
"40": 0.0,
|
||||
"30": 0.01,
|
||||
"20": 0.02,
|
||||
"0": 0.04
|
||||
},
|
||||
"stoploss": -0.10,
|
||||
"unfilledtimeout": 600,
|
||||
"bid_strategy": {
|
||||
"ask_last_balance": 0.0
|
||||
},
|
||||
"exchange": {
|
||||
"name": "bittrex",
|
||||
"key": "your_exchange_key",
|
||||
"secret": "your_exchange_secret",
|
||||
"pair_whitelist": [
|
||||
"BTC_ETH",
|
||||
"BTC_LTC",
|
||||
"BTC_ETC",
|
||||
"BTC_DASH",
|
||||
"BTC_ZEC",
|
||||
"BTC_XLM",
|
||||
"BTC_NXT",
|
||||
"BTC_POWR",
|
||||
"BTC_ADA",
|
||||
"BTC_XMR"
|
||||
],
|
||||
"pair_blacklist": [
|
||||
"BTC_DOGE"
|
||||
]
|
||||
},
|
||||
"experimental": {
|
||||
"use_sell_signal": false,
|
||||
"sell_profit_only": false
|
||||
},
|
||||
"telegram": {
|
||||
"enabled": true,
|
||||
"token": "your_telegram_token",
|
||||
"chat_id": "your_telegram_chat_id"
|
||||
},
|
||||
"initial_state": "running",
|
||||
"internals": {
|
||||
"process_throttle_secs": 5
|
||||
},
|
||||
"strategy": "DefaultStrategy",
|
||||
"strategy_path": "/some/folder/"
|
||||
}
|
||||
BIN
docs/assets/freqtrade-screenshot.png
Normal file
BIN
docs/assets/freqtrade-screenshot.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 142 KiB |
164
docs/backtesting.md
Normal file
164
docs/backtesting.md
Normal file
@@ -0,0 +1,164 @@
|
||||
# Backtesting
|
||||
This page explains how to validate your strategy performance by using
|
||||
Backtesting.
|
||||
|
||||
## Table of Contents
|
||||
- [Test your strategy with Backtesting](#test-your-strategy-with-backtesting)
|
||||
- [Understand the backtesting result](#understand-the-backtesting-result)
|
||||
|
||||
## Test your strategy with Backtesting
|
||||
Now you have good Buy and Sell strategies, you want to test it against
|
||||
real data. This is what we call
|
||||
[backtesting](https://en.wikipedia.org/wiki/Backtesting).
|
||||
|
||||
|
||||
Backtesting will use the crypto-currencies (pair) from your config file
|
||||
and load static tickers located in
|
||||
[/freqtrade/tests/testdata](https://github.com/gcarq/freqtrade/tree/develop/freqtrade/tests/testdata).
|
||||
If the 5 min and 1 min ticker for the crypto-currencies to test is not
|
||||
already in the `testdata` folder, backtesting will download them
|
||||
automatically. Testdata files will not be updated until you specify it.
|
||||
|
||||
The result of backtesting will confirm you if your bot as more chance to
|
||||
make a profit than a loss.
|
||||
|
||||
|
||||
The backtesting is very easy with freqtrade.
|
||||
|
||||
### Run a backtesting against the currencies listed in your config file
|
||||
**With 5 min tickers (Per default)**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --realistic-simulation
|
||||
```
|
||||
|
||||
**With 1 min tickers**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --realistic-simulation --ticker-interval 1
|
||||
```
|
||||
|
||||
**Reload your testdata files**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --realistic-simulation --refresh-pairs-cached
|
||||
```
|
||||
|
||||
**With live data (do not alter your testdata files)**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --realistic-simulation --live
|
||||
```
|
||||
|
||||
**Using a different on-disk ticker-data source**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --datadir freqtrade/tests/testdata-20180101
|
||||
```
|
||||
|
||||
**With a (custom) strategy file**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py -s currentstrategy backtesting
|
||||
```
|
||||
Where `-s currentstrategy` refers to a filename `currentstrategy.py` in `freqtrade/user_data/strategies`
|
||||
|
||||
**Exporting trades to file**
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --export trades
|
||||
```
|
||||
|
||||
**Running backtest with smaller testset**
|
||||
Use the `--timerange` argument to change how much of the testset
|
||||
you want to use. The last N ticks/timeframes will be used.
|
||||
|
||||
Example:
|
||||
```bash
|
||||
python3 ./freqtrade/main.py backtesting --timerange=-200
|
||||
```
|
||||
|
||||
***Advanced use of timerange***
|
||||
Doing `--timerange=-200` will get the last 200 timeframes
|
||||
from your inputdata. You can also specify specific dates,
|
||||
or a range span indexed by start and stop.
|
||||
|
||||
The full timerange specification:
|
||||
- Use last 123 tickframes of data: `--timerange=-123`
|
||||
- Use first 123 tickframes of data: `--timerange=123-`
|
||||
- Use tickframes from line 123 through 456: `--timerange=123-456`
|
||||
|
||||
|
||||
Incoming feature, not implemented yet:
|
||||
- `--timerange=-20180131`
|
||||
- `--timerange=20180101-`
|
||||
- `--timerange=20180101-20181231`
|
||||
|
||||
|
||||
**Update testdata directory**
|
||||
To update your testdata directory, or download into another testdata directory:
|
||||
```bash
|
||||
mkdir -p user_data/data/testdata-20180113
|
||||
cp freqtrade/tests/testdata/pairs.json user_data/data-20180113
|
||||
cd user_data/data-20180113
|
||||
```
|
||||
|
||||
Possibly edit pairs.json file to include/exclude pairs
|
||||
|
||||
```bash
|
||||
python3 freqtrade/tests/testdata/download_backtest_data.py -p pairs.json
|
||||
```
|
||||
|
||||
The script will read your pairs.json file, and download ticker data
|
||||
into the current working directory.
|
||||
|
||||
|
||||
For help about backtesting usage, please refer to
|
||||
[Backtesting commands](#backtesting-commands).
|
||||
|
||||
## Understand the backtesting result
|
||||
The most important in the backtesting is to understand the result.
|
||||
|
||||
A backtesting result will look like that:
|
||||
```
|
||||
====================== BACKTESTING REPORT ================================
|
||||
pair buy count avg profit % total profit BTC avg duration
|
||||
-------- ----------- -------------- ------------------ --------------
|
||||
BTC_ETH 56 -0.67 -0.00075455 62.3
|
||||
BTC_LTC 38 -0.48 -0.00036315 57.9
|
||||
BTC_ETC 42 -1.15 -0.00096469 67.0
|
||||
BTC_DASH 72 -0.62 -0.00089368 39.9
|
||||
BTC_ZEC 45 -0.46 -0.00041387 63.2
|
||||
BTC_XLM 24 -0.88 -0.00041846 47.7
|
||||
BTC_NXT 24 0.68 0.00031833 40.2
|
||||
BTC_POWR 35 0.98 0.00064887 45.3
|
||||
BTC_ADA 43 -0.39 -0.00032292 55.0
|
||||
BTC_XMR 40 -0.40 -0.00032181 47.4
|
||||
TOTAL 419 -0.41 -0.00348593 52.9
|
||||
```
|
||||
|
||||
The last line will give you the overall performance of your strategy,
|
||||
here:
|
||||
```
|
||||
TOTAL 419 -0.41 -0.00348593 52.9
|
||||
```
|
||||
|
||||
We understand the bot has made `419` trades for an average duration of
|
||||
`52.9` min, with a performance of `-0.41%` (loss), that means it has
|
||||
lost a total of `-0.00348593 BTC`.
|
||||
|
||||
As you will see your strategy performance will be influenced by your buy
|
||||
strategy, your sell strategy, and also by the `minimal_roi` and
|
||||
`stop_loss` you have set.
|
||||
|
||||
As for an example if your minimal_roi is only `"0": 0.01`. You cannot
|
||||
expect the bot to make more profit than 1% (because it will sell every
|
||||
time a trade will reach 1%).
|
||||
```json
|
||||
"minimal_roi": {
|
||||
"0": 0.01
|
||||
},
|
||||
```
|
||||
|
||||
On the other hand, if you set a too high `minimal_roi` like `"0": 0.55`
|
||||
(55%), there is a lot of chance that the bot will never reach this
|
||||
profit. Hence, keep in mind that your performance is a mix of your
|
||||
strategies, your configuration, and the crypto-currency you have set up.
|
||||
|
||||
## Next step
|
||||
Great, your strategy is profitable. What if the bot can give your the
|
||||
optimal parameters to use for your strategy?
|
||||
Your next step is to learn [how to find optimal parameters with Hyperopt](https://github.com/gcarq/freqtrade/blob/develop/docs/hyperopt.md)
|
||||
152
docs/bot-optimization.md
Normal file
152
docs/bot-optimization.md
Normal file
@@ -0,0 +1,152 @@
|
||||
# Bot Optimization
|
||||
This page explains where to customize your strategies, and add new
|
||||
indicators.
|
||||
|
||||
## Table of Contents
|
||||
- [Install a custom strategy file](#install-a-custom-strategy-file)
|
||||
- [Customize your strategy](#change-your-strategy)
|
||||
- [Add more Indicator](#add-more-indicator)
|
||||
- [Where is the default strategy](#where-is-the-default-strategy)
|
||||
|
||||
Since the version `0.16.0` the bot allows using custom strategy file.
|
||||
|
||||
## Install a custom strategy file
|
||||
This is very simple. Copy paste your strategy file into the folder
|
||||
`user_data/strategies`.
|
||||
|
||||
Let assume you have a class called `AwesomeStrategy` in the file `awesome-strategy.py`:
|
||||
1. Move your file into `user_data/strategies` (you should have `user_data/strategies/awesome-strategy.py`
|
||||
2. Start the bot with the param `--strategy AwesomeStrategy` (the parameter is the class name)
|
||||
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --strategy AwesomeStrategy
|
||||
```
|
||||
|
||||
## Change your strategy
|
||||
The bot includes a default strategy file. However, we recommend you to
|
||||
use your own file to not have to lose your parameters every time the default
|
||||
strategy file will be updated on Github. Put your custom strategy file
|
||||
into the folder `user_data/strategies`.
|
||||
|
||||
A strategy file contains all the information needed to build a good strategy:
|
||||
- Buy strategy rules
|
||||
- Sell strategy rules
|
||||
- Minimal ROI recommended
|
||||
- Stoploss recommended
|
||||
- Hyperopt parameter
|
||||
|
||||
The bot also include a sample strategy called `TestStrategy` you can update: `user_data/strategies/test_strategy.py`.
|
||||
You can test it with the parameter: `--strategy TestStrategy`
|
||||
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --strategy AwesomeStrategy
|
||||
```
|
||||
|
||||
### Specify custom strategy location
|
||||
If you want to use a strategy from a different folder you can pass `--strategy-path`
|
||||
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --strategy AwesomeStrategy --strategy-path /some/folder
|
||||
```
|
||||
|
||||
**For the following section we will use the [user_data/strategies/test_strategy.py](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py)
|
||||
file as reference.**
|
||||
|
||||
### Buy strategy
|
||||
Edit the method `populate_buy_trend()` into your strategy file to
|
||||
update your buy strategy.
|
||||
|
||||
Sample from `user_data/strategies/test_strategy.py`:
|
||||
```python
|
||||
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the buy signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
dataframe.loc[
|
||||
(
|
||||
(dataframe['adx'] > 30) &
|
||||
(dataframe['tema'] <= dataframe['blower']) &
|
||||
(dataframe['tema'] > dataframe['tema'].shift(1))
|
||||
),
|
||||
'buy'] = 1
|
||||
|
||||
return dataframe
|
||||
```
|
||||
|
||||
### Sell strategy
|
||||
Edit the method `populate_sell_trend()` into your strategy file to
|
||||
update your sell strategy.
|
||||
|
||||
Sample from `user_data/strategies/test_strategy.py`:
|
||||
```python
|
||||
def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the sell signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
dataframe.loc[
|
||||
(
|
||||
(dataframe['adx'] > 70) &
|
||||
(dataframe['tema'] > dataframe['blower']) &
|
||||
(dataframe['tema'] < dataframe['tema'].shift(1))
|
||||
),
|
||||
'sell'] = 1
|
||||
return dataframe
|
||||
```
|
||||
|
||||
## Add more Indicator
|
||||
As you have seen, buy and sell strategies need indicators. You can add
|
||||
more indicators by extending the list contained in
|
||||
the method `populate_indicators()` from your strategy file.
|
||||
|
||||
Sample:
|
||||
```python
|
||||
def populate_indicators(dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Adds several different TA indicators to the given DataFrame
|
||||
"""
|
||||
dataframe['sar'] = ta.SAR(dataframe)
|
||||
dataframe['adx'] = ta.ADX(dataframe)
|
||||
stoch = ta.STOCHF(dataframe)
|
||||
dataframe['fastd'] = stoch['fastd']
|
||||
dataframe['fastk'] = stoch['fastk']
|
||||
dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband']
|
||||
dataframe['sma'] = ta.SMA(dataframe, timeperiod=40)
|
||||
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
|
||||
dataframe['mfi'] = ta.MFI(dataframe)
|
||||
dataframe['rsi'] = ta.RSI(dataframe)
|
||||
dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5)
|
||||
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
|
||||
dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
|
||||
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
|
||||
dataframe['ao'] = awesome_oscillator(dataframe)
|
||||
macd = ta.MACD(dataframe)
|
||||
dataframe['macd'] = macd['macd']
|
||||
dataframe['macdsignal'] = macd['macdsignal']
|
||||
dataframe['macdhist'] = macd['macdhist']
|
||||
hilbert = ta.HT_SINE(dataframe)
|
||||
dataframe['htsine'] = hilbert['sine']
|
||||
dataframe['htleadsine'] = hilbert['leadsine']
|
||||
dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
|
||||
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
|
||||
dataframe['minus_dm'] = ta.MINUS_DM(dataframe)
|
||||
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
|
||||
return dataframe
|
||||
```
|
||||
|
||||
**Want more indicators example?**
|
||||
Look into the [user_data/strategies/test_strategy.py](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py).
|
||||
Then uncomment indicators you need.
|
||||
|
||||
|
||||
### Where is the default strategy?
|
||||
The default buy strategy is located in the file
|
||||
[freqtrade/default_strategy.py](https://github.com/gcarq/freqtrade/blob/develop/freqtrade/strategy/default_strategy.py).
|
||||
|
||||
|
||||
## Next step
|
||||
Now you have a perfect strategy you probably want to backtesting it.
|
||||
Your next step is to learn [How to use the Backtesting](https://github.com/gcarq/freqtrade/blob/develop/docs/backtesting.md).
|
||||
173
docs/bot-usage.md
Normal file
173
docs/bot-usage.md
Normal file
@@ -0,0 +1,173 @@
|
||||
# Bot usage
|
||||
This page explains the difference parameters of the bot and how to run
|
||||
it.
|
||||
|
||||
## Table of Contents
|
||||
- [Bot commands](#bot-commands)
|
||||
- [Backtesting commands](#backtesting-commands)
|
||||
- [Hyperopt commands](#hyperopt-commands)
|
||||
|
||||
## Bot commands
|
||||
```
|
||||
usage: main.py [-h] [-c PATH] [-v] [--version] [--dynamic-whitelist [INT]]
|
||||
[--dry-run-db]
|
||||
{backtesting,hyperopt} ...
|
||||
|
||||
Simple High Frequency Trading Bot for crypto currencies
|
||||
|
||||
positional arguments:
|
||||
{backtesting,hyperopt}
|
||||
backtesting backtesting module
|
||||
hyperopt hyperopt module
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-v, --verbose be verbose
|
||||
--version show program's version number and exit
|
||||
-c PATH, --config PATH
|
||||
specify configuration file (default: config.json)
|
||||
-s NAME, --strategy NAME
|
||||
specify strategy class name (default: DefaultStrategy)
|
||||
--strategy-path PATH specify additional strategy lookup path
|
||||
--dry-run-db Force dry run to use a local DB
|
||||
"tradesv3.dry_run.sqlite" instead of memory DB. Work
|
||||
only if dry_run is enabled.
|
||||
--datadir PATH
|
||||
path to backtest data (default freqdata/tests/testdata
|
||||
--dynamic-whitelist [INT]
|
||||
dynamically generate and update whitelist based on 24h
|
||||
BaseVolume (Default 20 currencies)
|
||||
```
|
||||
|
||||
### How to use a different config file?
|
||||
The bot allows you to select which config file you want to use. Per
|
||||
default, the bot will load the file `./config.json`
|
||||
|
||||
```bash
|
||||
python3 ./freqtrade/main.py -c path/far/far/away/config.json
|
||||
```
|
||||
|
||||
### How to use --strategy?
|
||||
This parameter will allow you to load your custom strategy class.
|
||||
Per default without `--strategy` or `-s` the bot will load the
|
||||
`DefaultStrategy` included with the bot (`freqtrade/strategy/default_strategy.py`).
|
||||
|
||||
The bot will search your strategy file within `user_data/strategies` and `freqtrade/strategy`.
|
||||
|
||||
To load a strategy, simply pass the class name (e.g.: `CustomStrategy`) in this parameter.
|
||||
|
||||
**Example:**
|
||||
In `user_data/strategies` you have a file `my_awesome_strategy.py` which has
|
||||
a strategy class called `AwesomeStrategy` to load it:
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --strategy AwesomeStrategy
|
||||
```
|
||||
|
||||
If the bot does not find your strategy file, it will display in an error
|
||||
message the reason (File not found, or errors in your code).
|
||||
|
||||
Learn more about strategy file in [optimize your bot](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md).
|
||||
|
||||
### How to use --strategy-path?
|
||||
This parameter allows you to add an additional strategy lookup path, which gets
|
||||
checked before the default locations (The passed path must be a folder!):
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --strategy AwesomeStrategy --strategy-path /some/folder
|
||||
```
|
||||
|
||||
#### How to install a strategy?
|
||||
This is very simple. Copy paste your strategy file into the folder
|
||||
`user_data/strategies` or use `--strategy-path`. And voila, the bot is ready to use it.
|
||||
|
||||
### How to use --dynamic-whitelist?
|
||||
Per default `--dynamic-whitelist` will retrieve the 20 currencies based
|
||||
on BaseVolume. This value can be changed when you run the script.
|
||||
|
||||
**By Default**
|
||||
Get the 20 currencies based on BaseVolume.
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --dynamic-whitelist
|
||||
```
|
||||
|
||||
**Customize the number of currencies to retrieve**
|
||||
Get the 30 currencies based on BaseVolume.
|
||||
```bash
|
||||
python3 ./freqtrade/main.py --dynamic-whitelist 30
|
||||
```
|
||||
|
||||
**Exception**
|
||||
`--dynamic-whitelist` must be greater than 0. If you enter 0 or a
|
||||
negative value (e.g -2), `--dynamic-whitelist` will use the default
|
||||
value (20).
|
||||
|
||||
### How to use --dry-run-db?
|
||||
When you run the bot in Dry-run mode, per default no transactions are
|
||||
stored in a database. If you want to store your bot actions in a DB
|
||||
using `--dry-run-db`. This command will use a separate database file
|
||||
`tradesv3.dry_run.sqlite`
|
||||
|
||||
```bash
|
||||
python3 ./freqtrade/main.py -c config.json --dry-run-db
|
||||
```
|
||||
|
||||
|
||||
## Backtesting commands
|
||||
|
||||
Backtesting also uses the config specified via `-c/--config`.
|
||||
|
||||
```
|
||||
usage: freqtrade backtesting [-h] [-l] [-i INT] [--realistic-simulation]
|
||||
[-r]
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-l, --live using live data
|
||||
-i INT, --ticker-interval INT
|
||||
specify ticker interval in minutes (default: 5)
|
||||
--realistic-simulation
|
||||
uses max_open_trades from config to simulate real
|
||||
world limitations
|
||||
-r, --refresh-pairs-cached
|
||||
refresh the pairs files in tests/testdata with
|
||||
the latest data from Bittrex. Use it if you want
|
||||
to run your backtesting with up-to-date data.
|
||||
```
|
||||
|
||||
### How to use --refresh-pairs-cached parameter?
|
||||
The first time your run Backtesting, it will take the pairs you have
|
||||
set in your config file and download data from Bittrex.
|
||||
|
||||
If for any reason you want to update your data set, you use
|
||||
`--refresh-pairs-cached` to force Backtesting to update the data it has.
|
||||
**Use it only if you want to update your data set. You will not be able
|
||||
to come back to the previous version.**
|
||||
|
||||
To test your strategy with latest data, we recommend continuing using
|
||||
the parameter `-l` or `--live`.
|
||||
|
||||
|
||||
## Hyperopt commands
|
||||
|
||||
It is possible to use hyperopt for trading strategy optimization.
|
||||
Hyperopt uses an internal json config return by `hyperopt_optimize_conf()`
|
||||
located in `freqtrade/optimize/hyperopt_conf.py`.
|
||||
|
||||
```
|
||||
usage: freqtrade hyperopt [-h] [-e INT] [--use-mongodb]
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-e INT, --epochs INT specify number of epochs (default: 100)
|
||||
--use-mongodb parallelize evaluations with mongodb (requires mongod
|
||||
in PATH)
|
||||
|
||||
```
|
||||
|
||||
## A parameter missing in the configuration?
|
||||
All parameters for `main.py`, `backtesting`, `hyperopt` are referenced
|
||||
in [misc.py](https://github.com/gcarq/freqtrade/blob/develop/freqtrade/misc.py#L84)
|
||||
|
||||
## Next step
|
||||
The optimal strategy of the bot will change with time depending of the
|
||||
market trends. The next step is to
|
||||
[optimize your bot](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md).
|
||||
140
docs/configuration.md
Normal file
140
docs/configuration.md
Normal file
@@ -0,0 +1,140 @@
|
||||
# Configure the bot
|
||||
This page explains how to configure your `config.json` file.
|
||||
|
||||
## Table of Contents
|
||||
- [Bot commands](#bot-commands)
|
||||
- [Backtesting commands](#backtesting-commands)
|
||||
- [Hyperopt commands](#hyperopt-commands)
|
||||
|
||||
## Setup config.json
|
||||
We recommend to copy and use the `config.json.example` as a template
|
||||
for your bot configuration.
|
||||
|
||||
The table below will list all configuration parameters.
|
||||
|
||||
| Command | Default | Mandatory | Description |
|
||||
|----------|---------|----------|-------------|
|
||||
| `max_open_trades` | 3 | Yes | Number of trades open your bot will have.
|
||||
| `stake_currency` | BTC | Yes | Crypto-currency used for trading.
|
||||
| `stake_amount` | 0.05 | Yes | Amount of crypto-currency your bot will use for each trade. Per default, the bot will use (0.05 BTC x 3) = 0.15 BTC in total will be always engaged.
|
||||
| `ticker_interval` | [1, 5, 30, 60, 1440] | No | The ticker interval to use (1min, 5 min, 30 min, 1 hour or 1 day). Defaut is 5 minutes
|
||||
| `fiat_display_currency` | USD | Yes | Fiat currency used to show your profits. More information below.
|
||||
| `dry_run` | true | Yes | Define if the bot must be in Dry-run or production mode.
|
||||
| `minimal_roi` | See below | No | Set the threshold in percent the bot will use to sell a trade. More information below. If set, this parameter will override `minimal_roi` from your strategy file.
|
||||
| `stoploss` | -0.10 | No | Value of the stoploss in percent used by the bot. More information below. If set, this parameter will override `stoploss` from your strategy file.
|
||||
| `unfilledtimeout` | 0 | No | How long (in minutes) the bot will wait for an unfilled order to complete, after which the order will be cancelled.
|
||||
| `bid_strategy.ask_last_balance` | 0.0 | Yes | Set the bidding price. More information below.
|
||||
| `exchange.name` | bittrex | Yes | Name of the exchange class to use.
|
||||
| `exchange.key` | key | No | API key to use for the exchange. Only required when you are in production mode.
|
||||
| `exchange.secret` | secret | No | API secret to use for the exchange. Only required when you are in production mode.
|
||||
| `exchange.pair_whitelist` | [] | No | List of currency to use by the bot. Can be overrided with `--dynamic-whitelist` param.
|
||||
| `exchange.pair_blacklist` | [] | No | List of currency the bot must avoid. Useful when using `--dynamic-whitelist` param.
|
||||
| `experimental.use_sell_signal` | false | No | Use your sell strategy in addition of the `minimal_roi`.
|
||||
| `experimental.sell_profit_only` | false | No | waits until you have made a positive profit before taking a sell decision.
|
||||
| `telegram.enabled` | true | Yes | Enable or not the usage of Telegram.
|
||||
| `telegram.token` | token | No | Your Telegram bot token. Only required if `telegram.enabled` is `true`.
|
||||
| `telegram.chat_id` | chat_id | No | Your personal Telegram account id. Only required if `telegram.enabled` is `true`.
|
||||
| `initial_state` | running | No | Defines the initial application state. More information below.
|
||||
| `strategy` | DefaultStrategy | No | Defines Strategy class to use.
|
||||
| `strategy_path` | null | No | Adds an additional strategy lookup path (must be a folder).
|
||||
| `internals.process_throttle_secs` | 5 | Yes | Set the process throttle. Value in second.
|
||||
|
||||
The definition of each config parameters is in
|
||||
[misc.py](https://github.com/gcarq/freqtrade/blob/develop/freqtrade/misc.py#L205).
|
||||
|
||||
### Understand minimal_roi
|
||||
`minimal_roi` is a JSON object where the key is a duration
|
||||
in minutes and the value is the minimum ROI in percent.
|
||||
See the example below:
|
||||
```
|
||||
"minimal_roi": {
|
||||
"40": 0.0, # Sell after 40 minutes if the profit is not negative
|
||||
"30": 0.01, # Sell after 30 minutes if there is at least 1% profit
|
||||
"20": 0.02, # Sell after 20 minutes if there is at least 2% profit
|
||||
"0": 0.04 # Sell immediately if there is at least 4% profit
|
||||
},
|
||||
```
|
||||
|
||||
Most of the strategy files already include the optimal `minimal_roi`
|
||||
value. This parameter is optional. If you use it, it will take over the
|
||||
`minimal_roi` value from the strategy file.
|
||||
|
||||
### Understand stoploss
|
||||
`stoploss` is loss in percentage that should trigger a sale.
|
||||
For example value `-0.10` will cause immediate sell if the
|
||||
profit dips below -10% for a given trade. This parameter is optional.
|
||||
|
||||
Most of the strategy files already include the optimal `stoploss`
|
||||
value. This parameter is optional. If you use it, it will take over the
|
||||
`stoploss` value from the strategy file.
|
||||
|
||||
### Understand initial_state
|
||||
`initial_state` is an optional field that defines the initial application state.
|
||||
Possible values are `running` or `stopped`. (default=`running`)
|
||||
If the value is `stopped` the bot has to be started with `/start` first.
|
||||
|
||||
### Understand ask_last_balance
|
||||
`ask_last_balance` sets the bidding price. Value `0.0` will use `ask` price, `1.0` will
|
||||
use the `last` price and values between those interpolate between ask and last
|
||||
price. Using `ask` price will guarantee quick success in bid, but bot will also
|
||||
end up paying more then would probably have been necessary.
|
||||
|
||||
### What values for fiat_display_currency?
|
||||
`fiat_display_currency` set the fiat to use for the conversion form coin to fiat in Telegram.
|
||||
The valid value are: "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD".
|
||||
|
||||
## Switch to dry-run mode
|
||||
We recommend starting the bot in dry-run mode to see how your bot will
|
||||
behave and how is the performance of your strategy. In Dry-run mode the
|
||||
bot does not engage your money. It only runs a live simulation without
|
||||
creating trades.
|
||||
|
||||
### To switch your bot in Dry-run mode:
|
||||
1. Edit your `config.json` file
|
||||
2. Switch dry-run to true
|
||||
```json
|
||||
"dry_run": true,
|
||||
```
|
||||
|
||||
3. Remove your Bittrex API key (change them by fake api credentials)
|
||||
```json
|
||||
"exchange": {
|
||||
"name": "bittrex",
|
||||
"key": "key",
|
||||
"secret": "secret",
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Once you will be happy with your bot performance, you can switch it to
|
||||
production mode.
|
||||
|
||||
## Switch to production mode
|
||||
In production mode, the bot will engage your money. Be careful a wrong
|
||||
strategy can lose all your money. Be aware of what you are doing when
|
||||
you run it in production mode.
|
||||
|
||||
### To switch your bot in production mode:
|
||||
1. Edit your `config.json` file
|
||||
|
||||
2. Switch dry-run to false
|
||||
```json
|
||||
"dry_run": false,
|
||||
```
|
||||
|
||||
3. Insert your Bittrex API key (change them by fake api keys)
|
||||
```json
|
||||
"exchange": {
|
||||
"name": "bittrex",
|
||||
"key": "af8ddd35195e9dc500b9a6f799f6f5c93d89193b",
|
||||
"secret": "08a9dc6db3d7b53e1acebd9275677f4b0a04f1a5",
|
||||
...
|
||||
}
|
||||
```
|
||||
If you have not your Bittrex API key yet,
|
||||
[see our tutorial](https://github.com/gcarq/freqtrade/blob/develop/docs/pre-requisite.md).
|
||||
|
||||
|
||||
## Next step
|
||||
Now you have configured your config.json, the next step is to
|
||||
[start your bot](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md).
|
||||
71
docs/faq.md
Normal file
71
docs/faq.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# freqtrade FAQ
|
||||
|
||||
#### I have waited 5 minutes, why hasn't the bot made any trades yet?!
|
||||
|
||||
Depending on the buy strategy, the amount of whitelisted coins, the
|
||||
situation of the market etc, it can take up to hours to find good entry
|
||||
position for a trade. Be patient!
|
||||
|
||||
#### I have made 12 trades already, why is my total profit negative?!
|
||||
|
||||
I understand your disappointment but unfortunately 12 trades is just
|
||||
not enough to say anything. If you run backtesting, you can see that our
|
||||
current algorithm does leave you on the plus side, but that is after
|
||||
thousands of trades and even there, you will be left with losses on
|
||||
specific coins that you have traded tens if not hundreds of times. We
|
||||
of course constantly aim to improve the bot but it will _always_ be a
|
||||
gamble, which should leave you with modest wins on monthly basis but
|
||||
you can't say much from few trades.
|
||||
|
||||
#### I’d like to change the stake amount. Can I just stop the bot with
|
||||
/stop and then change the config.json and run it again?
|
||||
|
||||
Not quite. Trades are persisted to a database but the configuration is
|
||||
currently only read when the bot is killed and restarted. `/stop` more
|
||||
like pauses. You can stop your bot, adjust settings and start it again.
|
||||
|
||||
#### I want to improve the bot with a new strategy
|
||||
|
||||
That's great. We have a nice backtesting and hyperoptimizing setup. See
|
||||
the tutorial [here|Testing-new-strategies-with-Hyperopt](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#hyperopt-commands).
|
||||
|
||||
#### Is there a setting to only SELL the coins being held and not
|
||||
perform anymore BUYS?
|
||||
|
||||
You can use the `/forcesell all` command from Telegram.
|
||||
|
||||
### How many epoch do I need to get a good Hyperopt result?
|
||||
Per default Hyperopts without `-e` or `--epochs` parameter will only
|
||||
run 100 epochs, means 100 evals of your triggers, guards, .... Too few
|
||||
to find a great result (unless if you are very lucky), so you probably
|
||||
have to run it for 10.000 or more. But it will take an eternity to
|
||||
compute.
|
||||
|
||||
We recommend you to run it at least 10.000 epochs:
|
||||
```bash
|
||||
python3 ./freqtrade/main.py hyperopt -e 10000
|
||||
```
|
||||
|
||||
or if you want intermediate result to see
|
||||
```bash
|
||||
for i in {1..100}; do python3 ./freqtrade/main.py hyperopt -e 100; done
|
||||
```
|
||||
|
||||
#### Why it is so long to run hyperopt?
|
||||
Finding a great Hyperopt results takes time.
|
||||
|
||||
If you wonder why it takes a while to find great hyperopt results
|
||||
|
||||
This answer was written during the under the release 0.15.1, when we had
|
||||
:
|
||||
- 8 triggers
|
||||
- 9 guards: let's say we evaluate even 10 values from each
|
||||
- 1 stoploss calculation: let's say we want 10 values from that too to
|
||||
be evaluated
|
||||
|
||||
The following calculation is still very rough and not very precise
|
||||
but it will give the idea. With only these triggers and guards there is
|
||||
already 8*10^9*10 evaluations. A roughly total of 80 billion evals.
|
||||
Did you run 100 000 evals? Congrats, you've done roughly 1 / 100 000 th
|
||||
of the search space.
|
||||
|
||||
315
docs/hyperopt.md
Normal file
315
docs/hyperopt.md
Normal file
@@ -0,0 +1,315 @@
|
||||
# Hyperopt
|
||||
This page explains how to tune your strategy by finding the optimal
|
||||
parameters with Hyperopt.
|
||||
|
||||
## Table of Contents
|
||||
- [Prepare your Hyperopt](#prepare-hyperopt)
|
||||
- [1. Configure your Guards and Triggers](#1-configure-your-guards-and-triggers)
|
||||
- [2. Update the hyperopt config file](#2-update-the-hyperopt-config-file)
|
||||
- [Advanced Hyperopt notions](#advanced-notions)
|
||||
- [Understand the Guards and Triggers](#understand-the-guards-and-triggers)
|
||||
- [Execute Hyperopt](#execute-hyperopt)
|
||||
- [Hyperopt with MongoDB](#hyperopt-with-mongoDB)
|
||||
- [Understand the hyperopts result](#understand-the-backtesting-result)
|
||||
|
||||
## Prepare Hyperopt
|
||||
Before we start digging in Hyperopt, we recommend you to take a look at
|
||||
your strategy file located into [user_data/strategies/](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py)
|
||||
|
||||
### 1. Configure your Guards and Triggers
|
||||
There are two places you need to change in your strategy file to add a
|
||||
new buy strategy for testing:
|
||||
- Inside [populate_buy_trend()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L278-L294).
|
||||
- Inside [hyperopt_space()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L244-L297) known as `SPACE`.
|
||||
|
||||
There you have two different type of indicators: 1. `guards` and 2.
|
||||
`triggers`.
|
||||
1. Guards are conditions like "never buy if ADX < 10", or never buy if
|
||||
current price is over EMA10.
|
||||
2. Triggers are ones that actually trigger buy in specific moment, like
|
||||
"buy when EMA5 crosses over EMA10" or buy when close price touches lower
|
||||
bollinger band.
|
||||
|
||||
HyperOpt will, for each eval round, pick just ONE trigger, and possibly
|
||||
multiple guards. So that the constructed strategy will be something like
|
||||
"*buy exactly when close price touches lower bollinger band, BUT only if
|
||||
ADX > 10*".
|
||||
|
||||
|
||||
If you have updated the buy strategy, means change the content of
|
||||
`populate_buy_trend()` method you have to update the `guards` and
|
||||
`triggers` hyperopts must used.
|
||||
|
||||
As for an example if your `populate_buy_trend()` method is:
|
||||
```python
|
||||
def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
|
||||
dataframe.loc[
|
||||
(dataframe['rsi'] < 35) &
|
||||
(dataframe['adx'] > 65),
|
||||
'buy'] = 1
|
||||
|
||||
return dataframe
|
||||
```
|
||||
|
||||
Your hyperopt file must contain `guards` to find the right value for
|
||||
`(dataframe['adx'] > 65)` & and `(dataframe['plus_di'] > 0.5)`. That
|
||||
means you will need to enable/disable triggers.
|
||||
|
||||
In our case the `SPACE` and `populate_buy_trend` in your strategy file
|
||||
will look like:
|
||||
```python
|
||||
space = {
|
||||
'rsi': hp.choice('rsi', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('rsi-value', 20, 40, 1)}
|
||||
]),
|
||||
'adx': hp.choice('adx', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('adx-value', 15, 50, 1)}
|
||||
]),
|
||||
'trigger': hp.choice('trigger', [
|
||||
{'type': 'lower_bb'},
|
||||
{'type': 'faststoch10'},
|
||||
{'type': 'ao_cross_zero'},
|
||||
{'type': 'ema5_cross_ema10'},
|
||||
{'type': 'macd_cross_signal'},
|
||||
{'type': 'sar_reversal'},
|
||||
{'type': 'stochf_cross'},
|
||||
{'type': 'ht_sine'},
|
||||
]),
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
conditions = []
|
||||
# GUARDS AND TRENDS
|
||||
if params['adx']['enabled']:
|
||||
conditions.append(dataframe['adx'] > params['adx']['value'])
|
||||
if params['rsi']['enabled']:
|
||||
conditions.append(dataframe['rsi'] < params['rsi']['value'])
|
||||
|
||||
# TRIGGERS
|
||||
triggers = {
|
||||
'lower_bb': dataframe['tema'] <= dataframe['blower'],
|
||||
'faststoch10': (crossed_above(dataframe['fastd'], 10.0)),
|
||||
'ao_cross_zero': (crossed_above(dataframe['ao'], 0.0)),
|
||||
'ema5_cross_ema10': (crossed_above(dataframe['ema5'], dataframe['ema10'])),
|
||||
'macd_cross_signal': (crossed_above(dataframe['macd'], dataframe['macdsignal'])),
|
||||
'sar_reversal': (crossed_above(dataframe['close'], dataframe['sar'])),
|
||||
'stochf_cross': (crossed_above(dataframe['fastk'], dataframe['fastd'])),
|
||||
'ht_sine': (crossed_above(dataframe['htleadsine'], dataframe['htsine'])),
|
||||
}
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
### 2. Update the hyperopt config file
|
||||
Hyperopt is using a dedicated config file. Currently hyperopt
|
||||
cannot use your config file. It is also made on purpose to allow you
|
||||
testing your strategy with different configurations.
|
||||
|
||||
The Hyperopt configuration is located in
|
||||
[user_data/hyperopt_conf.py](https://github.com/gcarq/freqtrade/blob/develop/user_data/hyperopt_conf.py).
|
||||
|
||||
|
||||
## Advanced notions
|
||||
### Understand the Guards and Triggers
|
||||
When you need to add the new guards and triggers to be hyperopt
|
||||
parameters, you do this by adding them into the [hyperopt_space()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L244-L297).
|
||||
|
||||
If it's a trigger, you add one line to the 'trigger' choice group and that's it.
|
||||
|
||||
If it's a guard, you will add a line like this:
|
||||
```
|
||||
'rsi': hp.choice('rsi', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('rsi-value', 20, 40, 1)}
|
||||
]),
|
||||
```
|
||||
This says, "*one of the guards is RSI, it can have two values, enabled or
|
||||
disabled. If it is enabled, try different values for it between 20 and 40*".
|
||||
|
||||
So, the part of the strategy builder using the above setting looks like
|
||||
this:
|
||||
|
||||
```
|
||||
if params['rsi']['enabled']:
|
||||
conditions.append(dataframe['rsi'] < params['rsi']['value'])
|
||||
```
|
||||
|
||||
It checks if Hyperopt wants the RSI guard to be enabled for this
|
||||
round `params['rsi']['enabled']` and if it is, then it will add a
|
||||
condition that says RSI must be smaller than the value hyperopt picked
|
||||
for this evaluation, which is given in the `params['rsi']['value']`.
|
||||
|
||||
That's it. Now you can add new parts of strategies to Hyperopt and it
|
||||
will try all the combinations with all different values in the search
|
||||
for best working algo.
|
||||
|
||||
|
||||
### Add a new Indicators
|
||||
If you want to test an indicator that isn't used by the bot currently,
|
||||
you need to add it to the `populate_indicators()` method in `hyperopt.py`.
|
||||
|
||||
## Execute Hyperopt
|
||||
Once you have updated your hyperopt configuration you can run it.
|
||||
Because hyperopt tries a lot of combination to find the best parameters
|
||||
it will take time you will have the result (more than 30 mins).
|
||||
|
||||
We strongly recommend to use `screen` to prevent any connection loss.
|
||||
```bash
|
||||
python3 ./freqtrade/main.py -c config.json hyperopt -e 5000
|
||||
```
|
||||
|
||||
The `-e` flag will set how many evaluations hyperopt will do. We recommend
|
||||
running at least several thousand evaluations.
|
||||
|
||||
### Execute hyperopt with different ticker-data source
|
||||
If you would like to hyperopt parameters using an alternate ticker data that
|
||||
you have on-disk, use the `--datadir PATH` option. Default hyperopt will
|
||||
use data from directory `user_data/data`.
|
||||
|
||||
### Running hyperopt with smaller testset
|
||||
Use the `--timeperiod` argument to change how much of the testset
|
||||
you want to use. The last N ticks/timeframes will be used.
|
||||
Example:
|
||||
|
||||
```bash
|
||||
python3 ./freqtrade/main.py hyperopt --timeperiod -200
|
||||
```
|
||||
|
||||
### Running hyperopt with smaller search space
|
||||
Use the `--spaces` argument to limit the search space used by hyperopt.
|
||||
Letting Hyperopt optimize everything is a huuuuge search space. Often it
|
||||
might make more sense to start by just searching for initial buy algorithm.
|
||||
Or maybe you just want to optimize your stoploss or roi table for that awesome
|
||||
new buy strategy you have.
|
||||
|
||||
Legal values are:
|
||||
|
||||
- `all`: optimize everything
|
||||
- `buy`: just search for a new buy strategy
|
||||
- `roi`: just optimize the minimal profit table for your strategy
|
||||
- `stoploss`: search for the best stoploss value
|
||||
- space-separated list of any of the above values for example `--spaces roi stoploss`
|
||||
|
||||
### Hyperopt with MongoDB
|
||||
Hyperopt with MongoDB, is like Hyperopt under steroids. As you saw by
|
||||
executing the previous command is the execution takes a long time.
|
||||
To accelerate it you can use hyperopt with MongoDB.
|
||||
|
||||
To run hyperopt with MongoDb you will need 3 terminals.
|
||||
|
||||
**Terminal 1: Start MongoDB**
|
||||
```bash
|
||||
cd <freqtrade>
|
||||
source .env/bin/activate
|
||||
python3 scripts/start-mongodb.py
|
||||
```
|
||||
|
||||
**Terminal 2: Start Hyperopt worker**
|
||||
```bash
|
||||
cd <freqtrade>
|
||||
source .env/bin/activate
|
||||
python3 scripts/start-hyperopt-worker.py
|
||||
```
|
||||
|
||||
**Terminal 3: Start Hyperopt with MongoDB**
|
||||
```bash
|
||||
cd <freqtrade>
|
||||
source .env/bin/activate
|
||||
python3 ./freqtrade/main.py -c config.json hyperopt --use-mongodb
|
||||
```
|
||||
|
||||
**Re-run an Hyperopt**
|
||||
To re-run Hyperopt you have to delete the existing MongoDB table.
|
||||
```bash
|
||||
cd <freqtrade>
|
||||
rm -rf .hyperopt/mongodb/
|
||||
```
|
||||
|
||||
## Understand the hyperopts result
|
||||
Once Hyperopt is completed you can use the result to adding new buy
|
||||
signal. Given following result from hyperopt:
|
||||
```
|
||||
Best parameters:
|
||||
{
|
||||
"adx": {
|
||||
"enabled": true,
|
||||
"value": 15.0
|
||||
},
|
||||
"fastd": {
|
||||
"enabled": true,
|
||||
"value": 40.0
|
||||
},
|
||||
"green_candle": {
|
||||
"enabled": true
|
||||
},
|
||||
"mfi": {
|
||||
"enabled": false
|
||||
},
|
||||
"over_sar": {
|
||||
"enabled": false
|
||||
},
|
||||
"rsi": {
|
||||
"enabled": true,
|
||||
"value": 37.0
|
||||
},
|
||||
"trigger": {
|
||||
"type": "lower_bb"
|
||||
},
|
||||
"uptrend_long_ema": {
|
||||
"enabled": true
|
||||
},
|
||||
"uptrend_short_ema": {
|
||||
"enabled": false
|
||||
},
|
||||
"uptrend_sma": {
|
||||
"enabled": false
|
||||
}
|
||||
}
|
||||
|
||||
Best Result:
|
||||
2197 trades. Avg profit 1.84%. Total profit 0.79367541 BTC. Avg duration 241.0 mins.
|
||||
```
|
||||
|
||||
You should understand this result like:
|
||||
- You should **consider** the guard "adx" (`"adx"` is `"enabled": true`)
|
||||
and the best value is `15.0` (`"value": 15.0,`)
|
||||
- You should **consider** the guard "fastd" (`"fastd"` is `"enabled":
|
||||
true`) and the best value is `40.0` (`"value": 40.0,`)
|
||||
- You should **consider** to enable the guard "green_candle"
|
||||
(`"green_candle"` is `"enabled": true`) but this guards as no
|
||||
customizable value.
|
||||
- You should **ignore** the guard "mfi" (`"mfi"` is `"enabled": false`)
|
||||
- and so on...
|
||||
|
||||
You have to look inside your strategy file into `buy_strategy_generator()`
|
||||
method, what those values match to.
|
||||
|
||||
So for example you had `adx:` with the `value: 15.0` so we would look
|
||||
at `adx`-block, that translates to the following code block:
|
||||
```
|
||||
(dataframe['adx'] > 15.0)
|
||||
```
|
||||
|
||||
Translating your whole hyperopt result to as the new buy-signal
|
||||
would be the following:
|
||||
```
|
||||
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
dataframe.loc[
|
||||
(
|
||||
(dataframe['adx'] > 15.0) & # adx-value
|
||||
(dataframe['fastd'] < 40.0) & # fastd-value
|
||||
(dataframe['close'] > dataframe['open']) & # green_candle
|
||||
(dataframe['rsi'] < 37.0) & # rsi-value
|
||||
(dataframe['ema50'] > dataframe['ema100']) # uptrend_long_ema
|
||||
),
|
||||
'buy'] = 1
|
||||
return dataframe
|
||||
```
|
||||
|
||||
## Next step
|
||||
Now you have a perfect bot and want to control it from Telegram. Your
|
||||
next step is to learn the [Telegram usage](https://github.com/gcarq/freqtrade/blob/develop/docs/telegram-usage.md).
|
||||
32
docs/index.md
Normal file
32
docs/index.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# freqtrade documentation
|
||||
Welcome to freqtrade documentation. Please feel free to contribute to
|
||||
this documentation if you see it became outdated by sending us a
|
||||
Pull-request. Do not hesitate to reach us on
|
||||
[Slack](https://join.slack.com/t/highfrequencybot/shared_invite/enQtMjQ5NTM0OTYzMzY3LWMxYzE3M2MxNDdjMGM3ZTYwNzFjMGIwZGRjNTc3ZGU3MGE3NzdmZGMwNmU3NDM5ZTNmM2Y3NjRiNzk4NmM4OGE)
|
||||
if you do not find the answer to your questions.
|
||||
|
||||
## Table of Contents
|
||||
- [Pre-requisite](https://github.com/gcarq/freqtrade/blob/develop/docs/pre-requisite.md)
|
||||
- [Setup your Bittrex account](https://github.com/gcarq/freqtrade/blob/develop/docs/pre-requisite.md#setup-your-bittrex-account)
|
||||
- [Setup your Telegram bot](https://github.com/gcarq/freqtrade/blob/develop/docs/pre-requisite.md#setup-your-telegram-bot)
|
||||
- [Bot Installation](https://github.com/gcarq/freqtrade/blob/develop/docs/installation.md)
|
||||
- [Install with Docker (all platforms)](https://github.com/gcarq/freqtrade/blob/develop/docs/installation.md#docker)
|
||||
- [Install on Linux Ubuntu](https://github.com/gcarq/freqtrade/blob/develop/docs/installation.md#21-linux---ubuntu-1604)
|
||||
- [Install on MacOS](https://github.com/gcarq/freqtrade/blob/develop/docs/installation.md#23-macos-installation)
|
||||
- [Install on Windows](https://github.com/gcarq/freqtrade/blob/develop/docs/installation.md#windows)
|
||||
- [Bot Configuration](https://github.com/gcarq/freqtrade/blob/develop/docs/configuration.md)
|
||||
- [Bot usage (Start your bot)](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md)
|
||||
- [Bot commands](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#bot-commands)
|
||||
- [Backtesting commands](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#backtesting-commands)
|
||||
- [Hyperopt commands](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#hyperopt-commands)
|
||||
- [Bot Optimization](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md)
|
||||
- [Change your strategy](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md#change-your-strategy)
|
||||
- [Add more Indicator](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md#add-more-indicator)
|
||||
- [Test your strategy with Backtesting](https://github.com/gcarq/freqtrade/blob/develop/docs/backtesting.md)
|
||||
- [Find optimal parameters with Hyperopt](https://github.com/gcarq/freqtrade/blob/develop/docs/hyperopt.md)
|
||||
- [Control the bot with telegram](https://github.com/gcarq/freqtrade/blob/develop/docs/telegram-usage.md)
|
||||
- [Contribute to the project](https://github.com/gcarq/freqtrade/blob/develop/CONTRIBUTING.md)
|
||||
- [How to contribute](https://github.com/gcarq/freqtrade/blob/develop/CONTRIBUTING.md)
|
||||
- [Run tests & Check PEP8 compliance](https://github.com/gcarq/freqtrade/blob/develop/CONTRIBUTING.md)
|
||||
- [FAQ](https://github.com/gcarq/freqtrade/blob/develop/docs/faq.md)
|
||||
- [SQL cheatsheet](https://github.com/gcarq/freqtrade/blob/develop/docs/sql_cheatsheet.md)
|
||||
350
docs/installation.md
Normal file
350
docs/installation.md
Normal file
@@ -0,0 +1,350 @@
|
||||
# Installation
|
||||
|
||||
This page explains how to prepare your environment for running the bot.
|
||||
|
||||
To understand how to set up the bot please read the [Bot Configuration](https://github.com/gcarq/freqtrade/blob/develop/docs/configuration.md) page.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* [Table of Contents](#table-of-contents)
|
||||
* [Easy Installation - Linux Script](#easy-installation---linux-script)
|
||||
* [Automatic Installation - Docker](#automatic-installation---docker)
|
||||
* [Custom Linux MacOS Installation](#custom-installation)
|
||||
- [Requirements](#requirements)
|
||||
- [Linux - Ubuntu 16.04](#linux---ubuntu-1604)
|
||||
- [MacOS](#macos)
|
||||
- [Setup Config and virtual env](#setup-config-and-virtual-env)
|
||||
* [Windows](#windows)
|
||||
|
||||
|
||||
<!-- /TOC -->
|
||||
|
||||
------
|
||||
|
||||
## Easy Installation - Linux Script
|
||||
|
||||
If you are on Debian, Ubuntu or MacOS a freqtrade provides a script to Install, Update, Configure, and Reset your bot.
|
||||
|
||||
```bash
|
||||
$ ./setup.sh
|
||||
usage:
|
||||
-i,--install Install freqtrade from scratch
|
||||
-u,--update Command git pull to update.
|
||||
-r,--reset Hard reset your develop/master branch.
|
||||
-c,--config Easy config generator (Will override your existing file).
|
||||
```
|
||||
|
||||
### --install
|
||||
This script will install everything you need to run the bot:
|
||||
* Mandatory software as: `Python3`, `ta-lib`, `wget`
|
||||
* Setup your virtualenv
|
||||
* Configure your `config.json` file
|
||||
|
||||
This script is a combination of `install script` `--reset`, `--config`
|
||||
|
||||
### --update
|
||||
Update parameter will pull the last version of your current branch and update your virtualenv.
|
||||
|
||||
### --reset
|
||||
Reset parameter will hard reset your branch (only if you are on `master` or `develop`) and recreate your virtualenv.
|
||||
|
||||
### --config
|
||||
Config parameter is a `config.json` configurator. This script will ask you questions to setup your bot and create your `config.json`.
|
||||
|
||||
------
|
||||
|
||||
## Automatic Installation - Docker
|
||||
|
||||
Start by downloading Docker for your platform:
|
||||
|
||||
* [Mac](https://www.docker.com/products/docker#/mac)
|
||||
* [Windows](https://www.docker.com/products/docker#/windows)
|
||||
* [Linux](https://www.docker.com/products/docker#/linux)
|
||||
|
||||
Once you have Docker installed, simply create the config file (e.g. `config.json`) and then create a Docker image for `freqtrade` using the Dockerfile in this repo.
|
||||
|
||||
|
||||
### 1. Prepare the Bot
|
||||
|
||||
#### 1.1. Clone the git repository
|
||||
|
||||
```bash
|
||||
git clone https://github.com/gcarq/freqtrade.git
|
||||
```
|
||||
|
||||
#### 1.2. (Optional) Checkout the develop branch
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
```
|
||||
|
||||
#### 1.3. Go into the new directory
|
||||
|
||||
```bash
|
||||
cd freqtrade
|
||||
```
|
||||
|
||||
#### 1.4. Copy `config.json.example` to `config.json`
|
||||
|
||||
```bash
|
||||
cp -n config.json.example config.json
|
||||
```
|
||||
|
||||
> To edit the config please refer to the [Bot Configuration](https://github.com/gcarq/freqtrade/blob/develop/docs/configuration.md) page.
|
||||
|
||||
#### 1.5. Create your database file *(optional - the bot will create it if it is missing)*
|
||||
|
||||
Production
|
||||
```bash
|
||||
touch tradesv3.sqlite
|
||||
````
|
||||
|
||||
Dry-Run
|
||||
```bash
|
||||
touch tradesv3.dryrun.sqlite
|
||||
```
|
||||
|
||||
|
||||
### 2. Build the Docker image
|
||||
|
||||
```bash
|
||||
cd freqtrade
|
||||
docker build -t freqtrade .
|
||||
```
|
||||
|
||||
For security reasons, your configuration file will not be included in the image, you will need to bind mount it. It is also advised to bind mount an SQLite database file (see the "5. Run a restartable docker image" section) to keep it between updates.
|
||||
|
||||
|
||||
### 3. Verify the Docker image
|
||||
|
||||
After the build process you can verify that the image was created with:
|
||||
|
||||
```bash
|
||||
docker images
|
||||
```
|
||||
|
||||
|
||||
### 4. Run the Docker image
|
||||
|
||||
You can run a one-off container that is immediately deleted upon exiting with the following command (`config.json` must be in the current working directory):
|
||||
|
||||
```bash
|
||||
docker run --rm -v /etc/localtime:/etc/localtime:ro -v `pwd`/config.json:/freqtrade/config.json -it freqtrade
|
||||
```
|
||||
|
||||
In this example, the database will be created inside the docker instance and will be lost when you will refresh your image.
|
||||
|
||||
|
||||
### 5. Run a restartable docker image
|
||||
|
||||
To run a restartable instance in the background (feel free to place your configuration and database files wherever it feels comfortable on your filesystem).
|
||||
|
||||
#### 5.1. Move your config file and database
|
||||
|
||||
```bash
|
||||
mkdir ~/.freqtrade
|
||||
mv config.json ~/.freqtrade
|
||||
mv tradesv3.sqlite ~/.freqtrade
|
||||
```
|
||||
|
||||
#### 5.2. Run the docker image
|
||||
|
||||
```bash
|
||||
docker run -d \
|
||||
--name freqtrade \
|
||||
-v /etc/localtime:/etc/localtime:ro \
|
||||
-v ~/.freqtrade/config.json:/freqtrade/config.json \
|
||||
-v ~/.freqtrade/tradesv3.sqlite:/freqtrade/tradesv3.sqlite \
|
||||
freqtrade
|
||||
```
|
||||
|
||||
If you are using `dry_run=True` it's not necessary to mount `tradesv3.sqlite`, but you can mount `tradesv3.dryrun.sqlite` if you plan to use the dry run mode with the param `--dry-run-db`.
|
||||
|
||||
### 6. Monitor your Docker instance
|
||||
|
||||
You can then use the following commands to monitor and manage your container:
|
||||
|
||||
```bash
|
||||
docker logs freqtrade
|
||||
docker logs -f freqtrade
|
||||
docker restart freqtrade
|
||||
docker stop freqtrade
|
||||
docker start freqtrade
|
||||
```
|
||||
|
||||
You do not need to rebuild the image for configuration changes, it will suffice to edit `config.json` and restart the container.
|
||||
|
||||
------
|
||||
|
||||
## Custom Installation
|
||||
|
||||
We've included/collected install instructions for Ubuntu 16.04, MacOS, and Windows. These are guidelines and your success may vary with other distros.
|
||||
|
||||
### Requirements
|
||||
|
||||
Click each one for install guide:
|
||||
* [Python 3.6.x](http://docs.python-guide.org/en/latest/starting/installation/), note the bot was not tested on Python >= 3.7.x
|
||||
* [pip](https://pip.pypa.io/en/stable/installing/)
|
||||
* [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
|
||||
* [virtualenv](https://virtualenv.pypa.io/en/stable/installation/) (Recommended)
|
||||
* [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html)
|
||||
|
||||
|
||||
### Linux - Ubuntu 16.04
|
||||
|
||||
#### 1. Install Python 3.6, Git, and wget
|
||||
|
||||
```bash
|
||||
sudo add-apt-repository ppa:jonathonf/python-3.6
|
||||
sudo apt-get update
|
||||
sudo apt-get install python3.6 python3.6-venv python3.6-dev build-essential autoconf libtool pkg-config make wget git
|
||||
```
|
||||
|
||||
#### 2. Install TA-Lib
|
||||
|
||||
Official webpage: https://mrjbq7.github.io/ta-lib/install.html
|
||||
|
||||
```bash
|
||||
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
|
||||
tar xvzf ta-lib-0.4.0-src.tar.gz
|
||||
cd ta-lib
|
||||
./configure --prefix=/usr
|
||||
make
|
||||
make install
|
||||
cd ..
|
||||
rm -rf ./ta-lib*
|
||||
```
|
||||
|
||||
#### 3. [Optional] Install MongoDB
|
||||
|
||||
Install MongoDB if you plan to optimize your strategy with Hyperopt.
|
||||
|
||||
```bash
|
||||
sudo apt-get install mongodb-org
|
||||
```
|
||||
|
||||
> Complete tutorial from Digital Ocean: [How to Install MongoDB on Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04).
|
||||
|
||||
#### 4. Install FreqTrade
|
||||
|
||||
Clone the git repository:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/gcarq/freqtrade.git
|
||||
```
|
||||
|
||||
Optionally checkout the develop branch:
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
```
|
||||
|
||||
#### 5. Configure `freqtrade` as a `systemd` service
|
||||
|
||||
From the freqtrade repo... copy `freqtrade.service` to your systemd user directory (usually `~/.config/systemd/user`) and update `WorkingDirectory` and `ExecStart` to match your setup.
|
||||
|
||||
After that you can start the daemon with:
|
||||
```bash
|
||||
systemctl --user start freqtrade
|
||||
```
|
||||
|
||||
For this to be persistent (run when user is logged out) you'll need to enable `linger` for your freqtrade user.
|
||||
|
||||
```bash
|
||||
sudo loginctl enable-linger "$USER"
|
||||
```
|
||||
|
||||
|
||||
### MacOS
|
||||
|
||||
#### 1. Install Python 3.6, git, wget and ta-lib
|
||||
|
||||
```bash
|
||||
brew install python3 git wget ta-lib
|
||||
```
|
||||
|
||||
#### 2. [Optional] Install MongoDB
|
||||
|
||||
Install MongoDB if you plan to optimize your strategy with Hyperopt.
|
||||
|
||||
```bash
|
||||
curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-3.4.10.tgz
|
||||
tar -zxvf mongodb-osx-ssl-x86_64-3.4.10.tgz
|
||||
mkdir -p <path_freqtrade>/env/mongodb
|
||||
cp -R -n mongodb-osx-x86_64-3.4.10/ <path_freqtrade>/env/mongodb
|
||||
export PATH=<path_freqtrade>/env/mongodb/bin:$PATH
|
||||
```
|
||||
|
||||
#### 3. Install FreqTrade
|
||||
|
||||
Clone the git repository:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/gcarq/freqtrade.git
|
||||
```
|
||||
|
||||
Optionally checkout the develop branch:
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
```
|
||||
|
||||
|
||||
### Setup Config and virtual env
|
||||
|
||||
#### 1. Initialize the configuration
|
||||
|
||||
```bash
|
||||
cd freqtrade
|
||||
cp config.json.example config.json
|
||||
```
|
||||
|
||||
> *To edit the config please refer to [Bot Configuration](https://github.com/gcarq/freqtrade/blob/develop/docs/configuration.md).*
|
||||
|
||||
|
||||
#### 2. Setup your Python virtual environment (virtualenv)
|
||||
|
||||
```bash
|
||||
python3.6 -m venv .env
|
||||
source .env/bin/activate
|
||||
pip3.6 install --upgrade pip
|
||||
pip3.6 install -r requirements.txt
|
||||
pip3.6 install -e .
|
||||
```
|
||||
|
||||
#### 3. Run the Bot
|
||||
|
||||
If this is the first time you run the bot, ensure you are running it in Dry-run `"dry_run": true,` otherwise it will start to buy and sell coins.
|
||||
|
||||
```bash
|
||||
python3.6 ./freqtrade/main.py -c config.json
|
||||
```
|
||||
|
||||
------
|
||||
|
||||
## Windows
|
||||
|
||||
We recommend that Windows users use [Docker](#docker) as this will work
|
||||
much easier and smoother (also more secure).
|
||||
|
||||
### Install freqtrade
|
||||
|
||||
copy paste `config.json` to ``\path\freqtrade-develop\freqtrade`
|
||||
|
||||
```cmd
|
||||
>cd \path\freqtrade-develop
|
||||
>python -m venv .env
|
||||
>cd .env\Scripts
|
||||
>activate.bat
|
||||
>cd \path\freqtrade-develop
|
||||
>pip install -r requirements.txt
|
||||
>pip install -e .
|
||||
>cd freqtrade
|
||||
>python main.py
|
||||
```
|
||||
|
||||
> Thanks [Owdr](https://github.com/Owdr) for the commands. Source: [Issue #222](https://github.com/gcarq/freqtrade/issues/222)
|
||||
|
||||
|
||||
Now you have an environment ready, the next step is
|
||||
[Bot Configuration](https://github.com/gcarq/freqtrade/blob/develop/docs/configuration.md)...
|
||||
77
docs/plotting.md
Normal file
77
docs/plotting.md
Normal file
@@ -0,0 +1,77 @@
|
||||
# Plotting
|
||||
This page explains how to plot prices, indicator, profits.
|
||||
|
||||
## Table of Contents
|
||||
- [Plot price and indicators](#plot-price-and-indicators)
|
||||
- [Plot profit](#plot-profit)
|
||||
|
||||
## Installation
|
||||
|
||||
Plotting scripts use Plotly library. Install/upgrade it with:
|
||||
|
||||
```
|
||||
pip install --upgrade plotly
|
||||
```
|
||||
|
||||
At least version 2.3.0 is required.
|
||||
|
||||
## Plot price and indicators
|
||||
Usage for the price plotter:
|
||||
|
||||
```
|
||||
script/plot_dataframe.py [-h] [-p pair] [--live]
|
||||
```
|
||||
|
||||
Example
|
||||
```
|
||||
python scripts/plot_dataframe.py -p BTC_ETH
|
||||
```
|
||||
|
||||
The `-p` pair argument, can be used to specify what
|
||||
pair you would like to plot.
|
||||
|
||||
**Advanced use**
|
||||
|
||||
To plot the current live price use the `--live` flag:
|
||||
```
|
||||
python scripts/plot_dataframe.py -p BTC_ETH --live
|
||||
```
|
||||
|
||||
To plot a timerange (to zoom in):
|
||||
```
|
||||
python scripts/plot_dataframe.py -p BTC_ETH --timerange=100-200
|
||||
```
|
||||
Timerange doesn't work with live data.
|
||||
|
||||
|
||||
## Plot profit
|
||||
|
||||
The profit plotter show a picture with three plots:
|
||||
1) Average closing price for all pairs
|
||||
2) The summarized profit made by backtesting.
|
||||
Note that this is not the real-world profit, but
|
||||
more of an estimate.
|
||||
3) Each pair individually profit
|
||||
|
||||
The first graph is good to get a grip of how the overall market
|
||||
progresses.
|
||||
|
||||
The second graph will show how you algorithm works or doesnt.
|
||||
Perhaps you want an algorithm that steadily makes small profits,
|
||||
or one that acts less seldom, but makes big swings.
|
||||
|
||||
The third graph can be useful to spot outliers, events in pairs
|
||||
that makes profit spikes.
|
||||
|
||||
Usage for the profit plotter:
|
||||
|
||||
```
|
||||
script/plot_profit.py [-h] [-p pair] [--datadir directory] [--ticker_interval num]
|
||||
```
|
||||
|
||||
The `-p` pair argument, can be used to plot a single pair
|
||||
|
||||
Example
|
||||
```
|
||||
python3 scripts/plot_profit.py --datadir ../freqtrade/freqtrade/tests/testdata-20171221/ -p BTC_LTC
|
||||
```
|
||||
48
docs/pre-requisite.md
Normal file
48
docs/pre-requisite.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# Pre-requisite
|
||||
Before running your bot in production you will need to setup few
|
||||
external API. In production mode, the bot required valid Bittrex API
|
||||
credentials and a Telegram bot (optional but recommended).
|
||||
|
||||
## Table of Contents
|
||||
- [Setup your Bittrex account](#setup-your-bittrex-account)
|
||||
- [Backtesting commands](#setup-your-telegram-bot)
|
||||
|
||||
## Setup your Bittrex account
|
||||
*To be completed, please feel free to complete this section.*
|
||||
|
||||
## Setup your Telegram bot
|
||||
The only things you need is a working Telegram bot and its API token.
|
||||
Below we explain how to create your Telegram Bot, and how to get your
|
||||
Telegram user id.
|
||||
|
||||
### 1. Create your Telegram bot
|
||||
**1.1. Start a chat with https://telegram.me/BotFather**
|
||||
**1.2. Send the message** `/newbot`
|
||||
*BotFather response:*
|
||||
```
|
||||
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
|
||||
```
|
||||
**1.3. Choose the public name of your bot (e.g "`Freqtrade bot`")**
|
||||
*BotFather response:*
|
||||
```
|
||||
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
|
||||
```
|
||||
**1.4. Choose the name id of your bot (e.g "`My_own_freqtrade_bot`")**
|
||||
**1.5. Father bot will return you the token (API key)**
|
||||
Copy it and keep it you will use it for the config parameter `token`.
|
||||
*BotFather response:*
|
||||
```
|
||||
Done! Congratulations on your new bot. You will find it at t.me/My_own_freqtrade_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
|
||||
|
||||
Use this token to access the HTTP API:
|
||||
521095879:AAEcEZEL7ADJ56FtG_qD0bQJSKETbXCBCi0
|
||||
|
||||
For a description of the Bot API, see this page: https://core.telegram.org/bots/api
|
||||
```
|
||||
**1.6. Don't forget to start the conversation with your bot, by clicking /START button**
|
||||
|
||||
### 2. Get your user id
|
||||
**2.1. Talk to https://telegram.me/userinfobot**
|
||||
**2.2. Get your "Id", you will use it for the config parameter
|
||||
`chat_id`.**
|
||||
|
||||
90
docs/sql_cheatsheet.md
Normal file
90
docs/sql_cheatsheet.md
Normal file
@@ -0,0 +1,90 @@
|
||||
# SQL Helper
|
||||
This page constains some help if you want to edit your sqlite db.
|
||||
|
||||
## Install sqlite3
|
||||
**Ubuntu/Debian installation**
|
||||
```bash
|
||||
sudo apt-get install sqlite3
|
||||
```
|
||||
|
||||
## Open the DB
|
||||
```bash
|
||||
sqlite3
|
||||
.open <filepath>
|
||||
```
|
||||
|
||||
## Table structure
|
||||
|
||||
### List tables
|
||||
```bash
|
||||
.tables
|
||||
```
|
||||
|
||||
### Display table structure
|
||||
```bash
|
||||
.schema <table_name>
|
||||
```
|
||||
|
||||
### Trade table structure
|
||||
```sql
|
||||
CREATE TABLE trades (
|
||||
id INTEGER NOT NULL,
|
||||
exchange VARCHAR NOT NULL,
|
||||
pair VARCHAR NOT NULL,
|
||||
is_open BOOLEAN NOT NULL,
|
||||
fee FLOAT NOT NULL,
|
||||
open_rate FLOAT,
|
||||
close_rate FLOAT,
|
||||
close_profit FLOAT,
|
||||
stake_amount FLOAT NOT NULL,
|
||||
amount FLOAT,
|
||||
open_date DATETIME NOT NULL,
|
||||
close_date DATETIME,
|
||||
open_order_id VARCHAR,
|
||||
PRIMARY KEY (id),
|
||||
CHECK (is_open IN (0, 1))
|
||||
);
|
||||
```
|
||||
|
||||
## Get all trades in the table
|
||||
|
||||
```sql
|
||||
SELECT * FROM trades;
|
||||
```
|
||||
|
||||
## Fix trade still open after a /forcesell
|
||||
|
||||
```sql
|
||||
UPDATE trades
|
||||
SET is_open=0, close_date=<close_date>, close_rate=<close_rate>, close_profit=close_rate/open_rate
|
||||
WHERE id=<trade_ID_to_update>;
|
||||
```
|
||||
|
||||
**Example:**
|
||||
```sql
|
||||
UPDATE trades
|
||||
SET is_open=0, close_date='2017-12-20 03:08:45.103418', close_rate=0.19638016, close_profit=0.0496
|
||||
WHERE id=31;
|
||||
```
|
||||
|
||||
## Insert manually a new trade
|
||||
|
||||
```sql
|
||||
INSERT
|
||||
INTO trades (exchange, pair, is_open, fee, open_rate, stake_amount, amount, open_date)
|
||||
VALUES ('BITTREX', 'BTC_<COIN>', 1, 0.0025, <open_rate>, <stake_amount>, <amount>, '<datetime>')
|
||||
```
|
||||
|
||||
**Example:**
|
||||
```sql
|
||||
INSERT INTO trades (exchange, pair, is_open, fee, open_rate, stake_amount, amount, open_date) VALUES ('BITTREX', 'BTC_ETC', 1, 0.0025, 0.00258580, 0.002, 0.7715262081, '2017-11-28 12:44:24.000000')
|
||||
```
|
||||
|
||||
## Fix wrong fees in the table
|
||||
If your DB was created before
|
||||
[PR#200](https://github.com/gcarq/freqtrade/pull/200) was merged
|
||||
(before 12/23/17).
|
||||
|
||||
```sql
|
||||
UPDATE trades SET fee=0.0025 WHERE fee=0.005;
|
||||
```
|
||||
140
docs/telegram-usage.md
Normal file
140
docs/telegram-usage.md
Normal file
@@ -0,0 +1,140 @@
|
||||
# Telegram usage
|
||||
|
||||
This page explains how to command your bot with Telegram.
|
||||
|
||||
## Pre-requisite
|
||||
To control your bot with Telegram, you need first to
|
||||
[set up a Telegram bot](https://github.com/gcarq/freqtrade/blob/develop/docs/pre-requisite.md)
|
||||
and add your Telegram API keys into your config file.
|
||||
|
||||
## Telegram commands
|
||||
Per default, the Telegram bot shows predefined commands. Some commands
|
||||
are only available by sending them to the bot. The table below list the
|
||||
official commands. You can ask at any moment for help with `/help`.
|
||||
|
||||
| Command | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `/start` | | Starts the trader
|
||||
| `/stop` | | Stops the trader
|
||||
| `/status` | | Lists all open trades
|
||||
| `/status table` | | List all open trades in a table format
|
||||
| `/count` | | Displays number of trades used and available
|
||||
| `/profit` | | Display a summary of your profit/loss from close trades and some stats about your performance
|
||||
| `/forcesell <trade_id>` | | Instantly sells the given trade (Ignoring `minimum_roi`).
|
||||
| `/forcesell all` | | Instantly sells all open trades (Ignoring `minimum_roi`).
|
||||
| `/performance` | | Show performance of each finished trade grouped by pair
|
||||
| `/balance` | | Show account balance per currency
|
||||
| `/daily <n>` | 7 | Shows profit or loss per day, over the last n days
|
||||
| `/help` | | Show help message
|
||||
| `/version` | | Show version
|
||||
|
||||
## Telegram commands in action
|
||||
Below, example of Telegram message you will receive for each command.
|
||||
|
||||
### /start
|
||||
> **Status:** `running`
|
||||
|
||||
### /stop
|
||||
> `Stopping trader ...`
|
||||
> **Status:** `stopped`
|
||||
|
||||
## /status
|
||||
For each open trade, the bot will send you the following message.
|
||||
|
||||
> **Trade ID:** `123`
|
||||
> **Current Pair:** BTC_CVC
|
||||
> **Open Since:** `1 days ago`
|
||||
> **Amount:** `26.64180098`
|
||||
> **Open Rate:** `0.00007489`
|
||||
> **Close Rate:** `None`
|
||||
> **Current Rate:** `0.00007489`
|
||||
> **Close Profit:** `None`
|
||||
> **Current Profit:** `12.95%`
|
||||
> **Open Order:** `None`
|
||||
|
||||
## /status table
|
||||
Return the status of all open trades in a table format.
|
||||
```
|
||||
ID Pair Since Profit
|
||||
---- -------- ------- --------
|
||||
67 BTC_SC 1 d 13.33%
|
||||
123 BTC_CVC 1 h 12.95%
|
||||
```
|
||||
|
||||
## /count
|
||||
Return the number of trades used and available.
|
||||
```
|
||||
current max
|
||||
--------- -----
|
||||
2 10
|
||||
```
|
||||
|
||||
## /profit
|
||||
Return a summary of your profit/loss and performance.
|
||||
|
||||
> **ROI:** Close trades
|
||||
> ∙ `0.00485701 BTC (258.45%)`
|
||||
> ∙ `62.968 USD`
|
||||
> **ROI:** All trades
|
||||
> ∙ `0.00255280 BTC (143.43%)`
|
||||
> ∙ `33.095 EUR`
|
||||
>
|
||||
> **Total Trade Count:** `138`
|
||||
> **First Trade opened:** `3 days ago`
|
||||
> **Latest Trade opened:** `2 minutes ago`
|
||||
> **Avg. Duration:** `2:33:45`
|
||||
> **Best Performing:** `BTC_PAY: 50.23%`
|
||||
|
||||
## /forcesell <trade_id>
|
||||
|
||||
> **BITTREX:** Selling BTC/LTC with limit `0.01650000 (profit: ~-4.07%, -0.00008168)`
|
||||
|
||||
## /performance
|
||||
Return the performance of each crypto-currency the bot has sold.
|
||||
> Performance:
|
||||
> 1. `BTC_RCN 57.77%`
|
||||
> 2. `BTC_PAY 56.91%`
|
||||
> 3. `BTC_VIB 47.07%`
|
||||
> 4. `BTC_SALT 30.24%`
|
||||
> 5. `BTC_STORJ 27.24%`
|
||||
> ...
|
||||
|
||||
## /balance
|
||||
Return the balance of all crypto-currency your have on the exchange.
|
||||
|
||||
> **Currency:** BTC
|
||||
> **Available:** 3.05890234
|
||||
> **Balance:** 3.05890234
|
||||
> **Pending:** 0.0
|
||||
|
||||
> **Currency:** CVC
|
||||
> **Available:** 86.64180098
|
||||
> **Balance:** 86.64180098
|
||||
> **Pending:** 0.0
|
||||
|
||||
## /daily <n>
|
||||
Per default `/daily` will return the 7 last days.
|
||||
The example below if for `/daily 3`:
|
||||
|
||||
> **Daily Profit over the last 3 days:**
|
||||
```
|
||||
Day Profit BTC Profit USD
|
||||
---------- -------------- ------------
|
||||
2018-01-03 0.00224175 BTC 29,142 USD
|
||||
2018-01-02 0.00033131 BTC 4,307 USD
|
||||
2018-01-01 0.00269130 BTC 34.986 USD
|
||||
```
|
||||
|
||||
## /version
|
||||
> **Version:** `0.14.3`
|
||||
|
||||
### using proxy with telegram
|
||||
in [freqtrade/freqtrade/rpc/telegram.py](https://github.com/gcarq/freqtrade/blob/develop/freqtrade/rpc/telegram.py) replace
|
||||
```
|
||||
self._updater = Updater(token=self._config['telegram']['token'], workers=0)
|
||||
```
|
||||
|
||||
with
|
||||
```
|
||||
self._updater = Updater(token=self._config['telegram']['token'], request_kwargs={'proxy_url': 'socks5://127.0.0.1:1080/'}, workers=0)
|
||||
```
|
||||
170
exchange.py
170
exchange.py
@@ -1,170 +0,0 @@
|
||||
import enum
|
||||
import logging
|
||||
from typing import List
|
||||
|
||||
from bittrex.bittrex import Bittrex
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Current selected exchange
|
||||
EXCHANGE = None
|
||||
_API = None
|
||||
_CONF = {}
|
||||
|
||||
|
||||
class Exchange(enum.Enum):
|
||||
BITTREX = 1
|
||||
|
||||
|
||||
def init(config: dict) -> None:
|
||||
"""
|
||||
Initializes this module with the given config,
|
||||
it does basic validation whether the specified
|
||||
exchange and pairs are valid.
|
||||
:param config: config to use
|
||||
:return: None
|
||||
"""
|
||||
global _API, EXCHANGE
|
||||
|
||||
_CONF.update(config)
|
||||
|
||||
if config['dry_run']:
|
||||
logger.info('Instance is running with dry_run enabled')
|
||||
|
||||
use_bittrex = config.get('bittrex', {}).get('enabled', False)
|
||||
if use_bittrex:
|
||||
EXCHANGE = Exchange.BITTREX
|
||||
_API = Bittrex(api_key=config['bittrex']['key'], api_secret=config['bittrex']['secret'])
|
||||
else:
|
||||
raise RuntimeError('No exchange specified. Aborting!')
|
||||
|
||||
# Check if all pairs are available
|
||||
markets = get_markets()
|
||||
exchange_name = EXCHANGE.name.lower()
|
||||
for pair in config[exchange_name]['pair_whitelist']:
|
||||
if pair not in markets:
|
||||
raise RuntimeError('Pair {} is not available at {}'.format(pair, exchange_name))
|
||||
|
||||
|
||||
def buy(pair: str, rate: float, amount: float) -> str:
|
||||
"""
|
||||
Places a limit buy order.
|
||||
:param pair: Pair as str, format: BTC_ETH
|
||||
:param rate: Rate limit for order
|
||||
:param amount: The amount to purchase
|
||||
:return: order_id of the placed buy order
|
||||
"""
|
||||
if _CONF['dry_run']:
|
||||
return 'dry_run'
|
||||
elif EXCHANGE == Exchange.BITTREX:
|
||||
data = _API.buy_limit(pair.replace('_', '-'), amount, rate)
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return data['result']['uuid']
|
||||
|
||||
|
||||
def sell(pair: str, rate: float, amount: float) -> str:
|
||||
"""
|
||||
Places a limit sell order.
|
||||
:param pair: Pair as str, format: BTC_ETH
|
||||
:param rate: Rate limit for order
|
||||
:param amount: The amount to sell
|
||||
:return: None
|
||||
"""
|
||||
if _CONF['dry_run']:
|
||||
return 'dry_run'
|
||||
elif EXCHANGE == Exchange.BITTREX:
|
||||
data = _API.sell_limit(pair.replace('_', '-'), amount, rate)
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return data['result']['uuid']
|
||||
|
||||
|
||||
def get_balance(currency: str) -> float:
|
||||
"""
|
||||
Get account balance.
|
||||
:param currency: currency as str, format: BTC
|
||||
:return: float
|
||||
"""
|
||||
if _CONF['dry_run']:
|
||||
return 999.9
|
||||
elif EXCHANGE == Exchange.BITTREX:
|
||||
data = _API.get_balance(currency)
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return float(data['result']['Balance'] or 0.0)
|
||||
|
||||
|
||||
def get_ticker(pair: str) -> dict:
|
||||
"""
|
||||
Get Ticker for given pair.
|
||||
:param pair: Pair as str, format: BTC_ETC
|
||||
:return: dict
|
||||
"""
|
||||
if EXCHANGE == Exchange.BITTREX:
|
||||
data = _API.get_ticker(pair.replace('_', '-'))
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return {
|
||||
'bid': float(data['result']['Bid']),
|
||||
'ask': float(data['result']['Ask']),
|
||||
'last': float(data['result']['Last']),
|
||||
}
|
||||
|
||||
|
||||
def cancel_order(order_id: str) -> None:
|
||||
"""
|
||||
Cancel order for given order_id
|
||||
:param order_id: id as str
|
||||
:return: None
|
||||
"""
|
||||
if _CONF['dry_run']:
|
||||
pass
|
||||
elif EXCHANGE == Exchange.BITTREX:
|
||||
data = _API.cancel(order_id)
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
|
||||
|
||||
def get_open_orders(pair: str) -> List[dict]:
|
||||
"""
|
||||
Get all open orders for given pair.
|
||||
:param pair: Pair as str, format: BTC_ETC
|
||||
:return: list of dicts
|
||||
"""
|
||||
if _CONF['dry_run']:
|
||||
return []
|
||||
elif EXCHANGE == Exchange.BITTREX:
|
||||
data = _API.get_open_orders(pair.replace('_', '-'))
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return [{
|
||||
'id': entry['OrderUuid'],
|
||||
'type': entry['OrderType'],
|
||||
'opened': entry['Opened'],
|
||||
'rate': entry['PricePerUnit'],
|
||||
'amount': entry['Quantity'],
|
||||
'remaining': entry['QuantityRemaining'],
|
||||
} for entry in data['result']]
|
||||
|
||||
|
||||
def get_pair_detail_url(pair: str) -> str:
|
||||
"""
|
||||
Returns the market detail url for the given pair
|
||||
:param pair: pair as str, format: BTC_ANT
|
||||
:return: url as str
|
||||
"""
|
||||
if EXCHANGE == Exchange.BITTREX:
|
||||
return 'https://bittrex.com/Market/Index?MarketName={}'.format(pair.replace('_', '-'))
|
||||
|
||||
|
||||
def get_markets() -> List[str]:
|
||||
"""
|
||||
Returns all available markets
|
||||
:return: list of all available pairs
|
||||
"""
|
||||
if EXCHANGE == Exchange. BITTREX:
|
||||
data = _API.get_markets()
|
||||
if not data['success']:
|
||||
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
||||
return [m['MarketName'].replace('-', '_') for m in data['result']]
|
||||
14
freqtrade.service
Normal file
14
freqtrade.service
Normal file
@@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=Freqtrade Daemon
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
# Set WorkingDirectory and ExecStart to your file paths accordingly
|
||||
# NOTE: %h will be resolved to /home/<username>
|
||||
WorkingDirectory=%h/freqtrade
|
||||
ExecStart=/usr/bin/freqtrade --dynamic-whitelist 40
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
|
||||
16
freqtrade/__init__.py
Normal file
16
freqtrade/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
""" FreqTrade bot """
|
||||
__version__ = '0.16.1'
|
||||
|
||||
|
||||
class DependencyException(BaseException):
|
||||
"""
|
||||
Indicates that a assumed dependency is not met.
|
||||
This could happen when there is currently not enough money on the account.
|
||||
"""
|
||||
|
||||
|
||||
class OperationalException(BaseException):
|
||||
"""
|
||||
Requires manual intervention.
|
||||
This happens when an exchange returns an unexpected error during runtime.
|
||||
"""
|
||||
214
freqtrade/analyze.py
Normal file
214
freqtrade/analyze.py
Normal file
@@ -0,0 +1,214 @@
|
||||
"""
|
||||
Functions to analyze ticker data with indicators and produce buy and sell signals
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from enum import Enum
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
import arrow
|
||||
from pandas import DataFrame, to_datetime
|
||||
|
||||
from freqtrade.exchange import get_ticker_history
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.strategy.resolver import StrategyResolver
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SignalType(Enum):
|
||||
"""
|
||||
Enum to distinguish between buy and sell signals
|
||||
"""
|
||||
BUY = "buy"
|
||||
SELL = "sell"
|
||||
|
||||
|
||||
class Analyze(object):
|
||||
"""
|
||||
Analyze class contains everything the bot need to determine if the situation is good for
|
||||
buying or selling.
|
||||
"""
|
||||
def __init__(self, config: dict) -> None:
|
||||
"""
|
||||
Init Analyze
|
||||
:param config: Bot configuration (use the one from Configuration())
|
||||
"""
|
||||
self.config = config
|
||||
self.strategy = StrategyResolver(self.config).strategy
|
||||
|
||||
@staticmethod
|
||||
def parse_ticker_dataframe(ticker: list) -> DataFrame:
|
||||
"""
|
||||
Analyses the trend for the given ticker history
|
||||
:param ticker: See exchange.get_ticker_history
|
||||
:return: DataFrame
|
||||
"""
|
||||
columns = {'C': 'close', 'V': 'volume', 'O': 'open', 'H': 'high', 'L': 'low', 'T': 'date'}
|
||||
frame = DataFrame(ticker).rename(columns=columns)
|
||||
if 'BV' in frame:
|
||||
frame.drop('BV', axis=1, inplace=True)
|
||||
|
||||
frame['date'] = to_datetime(frame['date'], utc=True, infer_datetime_format=True)
|
||||
|
||||
# group by index and aggregate results to eliminate duplicate ticks
|
||||
frame = frame.groupby(by='date', as_index=False, sort=True).agg({
|
||||
'close': 'last',
|
||||
'high': 'max',
|
||||
'low': 'min',
|
||||
'open': 'first',
|
||||
'volume': 'max',
|
||||
})
|
||||
return frame
|
||||
|
||||
def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Adds several different TA indicators to the given DataFrame
|
||||
|
||||
Performance Note: For the best performance be frugal on the number of indicators
|
||||
you are using. Let uncomment only the indicator you are using in your strategies
|
||||
or your hyperopt configuration, otherwise you will waste your memory and CPU usage.
|
||||
"""
|
||||
return self.strategy.populate_indicators(dataframe=dataframe)
|
||||
|
||||
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the buy signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
return self.strategy.populate_buy_trend(dataframe=dataframe)
|
||||
|
||||
def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the sell signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
return self.strategy.populate_sell_trend(dataframe=dataframe)
|
||||
|
||||
def get_ticker_interval(self) -> int:
|
||||
"""
|
||||
Return ticker interval to use
|
||||
:return: Ticker interval value to use
|
||||
"""
|
||||
return self.strategy.ticker_interval
|
||||
|
||||
def analyze_ticker(self, ticker_history: List[Dict]) -> DataFrame:
|
||||
"""
|
||||
Parses the given ticker history and returns a populated DataFrame
|
||||
add several TA indicators and buy signal to it
|
||||
:return DataFrame with ticker data and indicator data
|
||||
"""
|
||||
dataframe = self.parse_ticker_dataframe(ticker_history)
|
||||
dataframe = self.populate_indicators(dataframe)
|
||||
dataframe = self.populate_buy_trend(dataframe)
|
||||
dataframe = self.populate_sell_trend(dataframe)
|
||||
return dataframe
|
||||
|
||||
def get_signal(self, pair: str, interval: int) -> Tuple[bool, bool]:
|
||||
"""
|
||||
Calculates current signal based several technical analysis indicators
|
||||
:param pair: pair in format BTC_ANT or BTC-ANT
|
||||
:param interval: Interval to use (in min)
|
||||
:return: (Buy, Sell) A bool-tuple indicating buy/sell signal
|
||||
"""
|
||||
ticker_hist = get_ticker_history(pair, interval)
|
||||
if not ticker_hist:
|
||||
logger.warning('Empty ticker history for pair %s', pair)
|
||||
return False, False
|
||||
|
||||
try:
|
||||
dataframe = self.analyze_ticker(ticker_hist)
|
||||
except ValueError as error:
|
||||
logger.warning(
|
||||
'Unable to analyze ticker for pair %s: %s',
|
||||
pair,
|
||||
str(error)
|
||||
)
|
||||
return False, False
|
||||
except Exception as error:
|
||||
logger.exception(
|
||||
'Unexpected error when analyzing ticker for pair %s: %s',
|
||||
pair,
|
||||
str(error)
|
||||
)
|
||||
return False, False
|
||||
|
||||
if dataframe.empty:
|
||||
logger.warning('Empty dataframe for pair %s', pair)
|
||||
return False, False
|
||||
|
||||
latest = dataframe.iloc[-1]
|
||||
|
||||
# Check if dataframe is out of date
|
||||
signal_date = arrow.get(latest['date'])
|
||||
if signal_date < arrow.utcnow() - timedelta(minutes=(interval + 5)):
|
||||
logger.warning(
|
||||
'Outdated history for pair %s. Last tick is %s minutes old',
|
||||
pair,
|
||||
(arrow.utcnow() - signal_date).seconds // 60
|
||||
)
|
||||
return False, False
|
||||
|
||||
(buy, sell) = latest[SignalType.BUY.value] == 1, latest[SignalType.SELL.value] == 1
|
||||
logger.debug(
|
||||
'trigger: %s (pair=%s) buy=%s sell=%s',
|
||||
latest['date'],
|
||||
pair,
|
||||
str(buy),
|
||||
str(sell)
|
||||
)
|
||||
return buy, sell
|
||||
|
||||
def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool, sell: bool) -> bool:
|
||||
"""
|
||||
This function evaluate if on the condition required to trigger a sell has been reached
|
||||
if the threshold is reached and updates the trade record.
|
||||
:return: True if trade should be sold, False otherwise
|
||||
"""
|
||||
# Check if minimal roi has been reached and no longer in buy conditions (avoiding a fee)
|
||||
if self.min_roi_reached(trade=trade, current_rate=rate, current_time=date):
|
||||
logger.debug('Required profit reached. Selling..')
|
||||
return True
|
||||
|
||||
# Experimental: Check if the trade is profitable before selling it (avoid selling at loss)
|
||||
if self.config.get('experimental', {}).get('sell_profit_only', False):
|
||||
logger.debug('Checking if trade is profitable..')
|
||||
if trade.calc_profit(rate=rate) <= 0:
|
||||
return False
|
||||
|
||||
if sell and not buy and self.config.get('experimental', {}).get('use_sell_signal', False):
|
||||
logger.debug('Sell signal received. Selling..')
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def min_roi_reached(self, trade: Trade, current_rate: float, current_time: datetime) -> bool:
|
||||
"""
|
||||
Based an earlier trade and current price and ROI configuration, decides whether bot should
|
||||
sell
|
||||
:return True if bot should sell at current rate
|
||||
"""
|
||||
current_profit = trade.calc_profit_percent(current_rate)
|
||||
if self.strategy.stoploss is not None and current_profit < self.strategy.stoploss:
|
||||
logger.debug('Stop loss hit.')
|
||||
return True
|
||||
|
||||
# Check if time matches and current rate is above threshold
|
||||
time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60
|
||||
for duration, threshold in self.strategy.minimal_roi.items():
|
||||
if time_diff <= duration:
|
||||
return False
|
||||
if current_profit > threshold:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def tickerdata_to_dataframe(self, tickerdata: Dict[str, List]) -> Dict[str, DataFrame]:
|
||||
"""
|
||||
Creates a dataframe and populates indicators for given ticker data
|
||||
"""
|
||||
return {pair: self.populate_indicators(self.parse_ticker_dataframe(pair_data))
|
||||
for pair, pair_data in tickerdata.items()}
|
||||
257
freqtrade/arguments.py
Normal file
257
freqtrade/arguments.py
Normal file
@@ -0,0 +1,257 @@
|
||||
"""
|
||||
This module contains the argument manager class
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import List, Tuple, Optional
|
||||
|
||||
from freqtrade import __version__, constants
|
||||
|
||||
|
||||
class Arguments(object):
|
||||
"""
|
||||
Arguments Class. Manage the arguments received by the cli
|
||||
"""
|
||||
|
||||
def __init__(self, args: List[str], description: str):
|
||||
self.args = args
|
||||
self.parsed_arg = None
|
||||
self.parser = argparse.ArgumentParser(description=description)
|
||||
|
||||
def _load_args(self) -> None:
|
||||
self.common_args_parser()
|
||||
self._build_subcommands()
|
||||
|
||||
def get_parsed_arg(self) -> argparse.Namespace:
|
||||
"""
|
||||
Return the list of arguments
|
||||
:return: List[str] List of arguments
|
||||
"""
|
||||
if self.parsed_arg is None:
|
||||
self._load_args()
|
||||
self.parsed_arg = self.parse_args()
|
||||
|
||||
return self.parsed_arg
|
||||
|
||||
def parse_args(self) -> argparse.Namespace:
|
||||
"""
|
||||
Parses given arguments and returns an argparse Namespace instance.
|
||||
"""
|
||||
parsed_arg = self.parser.parse_args(self.args)
|
||||
|
||||
return parsed_arg
|
||||
|
||||
def common_args_parser(self) -> None:
|
||||
"""
|
||||
Parses given common arguments and returns them as a parsed object.
|
||||
"""
|
||||
self.parser.add_argument(
|
||||
'-v', '--verbose',
|
||||
help='be verbose',
|
||||
action='store_const',
|
||||
dest='loglevel',
|
||||
const=logging.DEBUG,
|
||||
default=logging.INFO,
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'--version',
|
||||
action='version',
|
||||
version='%(prog)s {}'.format(__version__),
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'-c', '--config',
|
||||
help='specify configuration file (default: %(default)s)',
|
||||
dest='config',
|
||||
default='config.json',
|
||||
type=str,
|
||||
metavar='PATH',
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'-d', '--datadir',
|
||||
help='path to backtest data (default: %(default)s',
|
||||
dest='datadir',
|
||||
default=os.path.join('freqtrade', 'tests', 'testdata'),
|
||||
type=str,
|
||||
metavar='PATH',
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'-s', '--strategy',
|
||||
help='specify strategy class name (default: %(default)s)',
|
||||
dest='strategy',
|
||||
default='DefaultStrategy',
|
||||
type=str,
|
||||
metavar='NAME',
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'--strategy-path',
|
||||
help='specify additional strategy lookup path',
|
||||
dest='strategy_path',
|
||||
type=str,
|
||||
metavar='PATH',
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'--dynamic-whitelist',
|
||||
help='dynamically generate and update whitelist \
|
||||
based on 24h BaseVolume (Default 20 currencies)', # noqa
|
||||
dest='dynamic_whitelist',
|
||||
const=constants.DYNAMIC_WHITELIST,
|
||||
type=int,
|
||||
metavar='INT',
|
||||
nargs='?',
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'--dry-run-db',
|
||||
help='Force dry run to use a local DB "tradesv3.dry_run.sqlite" \
|
||||
instead of memory DB. Work only if dry_run is enabled.',
|
||||
action='store_true',
|
||||
dest='dry_run_db',
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def backtesting_options(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
Parses given arguments for Backtesting scripts.
|
||||
"""
|
||||
parser.add_argument(
|
||||
'-l', '--live',
|
||||
help='using live data',
|
||||
action='store_true',
|
||||
dest='live',
|
||||
)
|
||||
parser.add_argument(
|
||||
'-r', '--refresh-pairs-cached',
|
||||
help='refresh the pairs files in tests/testdata with the latest data from Bittrex. \
|
||||
Use it if you want to run your backtesting with up-to-date data.',
|
||||
action='store_true',
|
||||
dest='refresh_pairs',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--export',
|
||||
help='export backtest results, argument are: trades\
|
||||
Example --export=trades',
|
||||
type=str,
|
||||
default=None,
|
||||
dest='export',
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def optimizer_shared_options(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument(
|
||||
'-i', '--ticker-interval',
|
||||
help='specify ticker interval in minutes (1, 5, 30, 60, 1440)',
|
||||
dest='ticker_interval',
|
||||
type=int,
|
||||
metavar='INT',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--realistic-simulation',
|
||||
help='uses max_open_trades from config to simulate real world limitations',
|
||||
action='store_true',
|
||||
dest='realistic_simulation',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--timerange',
|
||||
help='specify what timerange of data to use.',
|
||||
default=None,
|
||||
type=str,
|
||||
dest='timerange',
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def hyperopt_options(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
Parses given arguments for Hyperopt scripts.
|
||||
"""
|
||||
parser.add_argument(
|
||||
'-e', '--epochs',
|
||||
help='specify number of epochs (default: %(default)d)',
|
||||
dest='epochs',
|
||||
default=constants.HYPEROPT_EPOCH,
|
||||
type=int,
|
||||
metavar='INT',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--use-mongodb',
|
||||
help='parallelize evaluations with mongodb (requires mongod in PATH)',
|
||||
dest='mongodb',
|
||||
action='store_true',
|
||||
)
|
||||
parser.add_argument(
|
||||
'-s', '--spaces',
|
||||
help='Specify which parameters to hyperopt. Space separate list. \
|
||||
Default: %(default)s',
|
||||
choices=['all', 'buy', 'roi', 'stoploss'],
|
||||
default='all',
|
||||
nargs='+',
|
||||
dest='spaces',
|
||||
)
|
||||
|
||||
def _build_subcommands(self) -> None:
|
||||
"""
|
||||
Builds and attaches all subcommands
|
||||
:return: None
|
||||
"""
|
||||
from freqtrade.optimize import backtesting, hyperopt
|
||||
|
||||
subparsers = self.parser.add_subparsers(dest='subparser')
|
||||
|
||||
# Add backtesting subcommand
|
||||
backtesting_cmd = subparsers.add_parser('backtesting', help='backtesting module')
|
||||
backtesting_cmd.set_defaults(func=backtesting.start)
|
||||
self.optimizer_shared_options(backtesting_cmd)
|
||||
self.backtesting_options(backtesting_cmd)
|
||||
|
||||
# Add hyperopt subcommand
|
||||
hyperopt_cmd = subparsers.add_parser('hyperopt', help='hyperopt module')
|
||||
hyperopt_cmd.set_defaults(func=hyperopt.start)
|
||||
self.optimizer_shared_options(hyperopt_cmd)
|
||||
self.hyperopt_options(hyperopt_cmd)
|
||||
|
||||
@staticmethod
|
||||
def parse_timerange(text: str) -> Optional[Tuple[List, int, int]]:
|
||||
"""
|
||||
Parse the value of the argument --timerange to determine what is the range desired
|
||||
:param text: value from --timerange
|
||||
:return: Start and End range period
|
||||
"""
|
||||
if text is None:
|
||||
return None
|
||||
syntax = [(r'^-(\d{8})$', (None, 'date')),
|
||||
(r'^(\d{8})-$', ('date', None)),
|
||||
(r'^(\d{8})-(\d{8})$', ('date', 'date')),
|
||||
(r'^(-\d+)$', (None, 'line')),
|
||||
(r'^(\d+)-$', ('line', None)),
|
||||
(r'^(\d+)-(\d+)$', ('index', 'index'))]
|
||||
for rex, stype in syntax:
|
||||
# Apply the regular expression to text
|
||||
match = re.match(rex, text)
|
||||
if match: # Regex has matched
|
||||
rvals = match.groups()
|
||||
index = 0
|
||||
start = None
|
||||
stop = None
|
||||
if stype[0]:
|
||||
start = rvals[index]
|
||||
if stype[0] != 'date':
|
||||
start = int(start)
|
||||
index += 1
|
||||
if stype[1]:
|
||||
stop = rvals[index]
|
||||
if stype[1] != 'date':
|
||||
stop = int(stop)
|
||||
return stype, start, stop
|
||||
raise Exception('Incorrect syntax for timerange "%s"' % text)
|
||||
|
||||
def scripts_options(self) -> None:
|
||||
"""
|
||||
Parses given arguments for plot scripts.
|
||||
"""
|
||||
self.parser.add_argument(
|
||||
'-p', '--pair',
|
||||
help='Show profits for only this pairs. Pairs are comma-separated.',
|
||||
dest='pair',
|
||||
default=None
|
||||
)
|
||||
208
freqtrade/configuration.py
Normal file
208
freqtrade/configuration.py
Normal file
@@ -0,0 +1,208 @@
|
||||
"""
|
||||
This module contains the configuration class
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
from argparse import Namespace
|
||||
from typing import Dict, Any
|
||||
|
||||
from jsonschema import Draft4Validator, validate
|
||||
from jsonschema.exceptions import ValidationError, best_match
|
||||
|
||||
from freqtrade import constants
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Configuration(object):
|
||||
"""
|
||||
Class to read and init the bot configuration
|
||||
Reuse this class for the bot, backtesting, hyperopt and every script that required configuration
|
||||
"""
|
||||
def __init__(self, args: Namespace) -> None:
|
||||
self.args = args
|
||||
self.config = None
|
||||
|
||||
def load_config(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract information for sys.argv and load the bot configuration
|
||||
:return: Configuration dictionary
|
||||
"""
|
||||
logger.info('Using config: %s ...', self.args.config)
|
||||
config = self._load_config_file(self.args.config)
|
||||
|
||||
# Set strategy if not specified in config and or if it's non default
|
||||
if self.args.strategy != constants.DEFAULT_STRATEGY or not config.get('strategy'):
|
||||
config.update({'strategy': self.args.strategy})
|
||||
|
||||
if self.args.strategy_path:
|
||||
config.update({'strategy_path': self.args.strategy_path})
|
||||
|
||||
# Load Common configuration
|
||||
config = self._load_common_config(config)
|
||||
|
||||
# Load Backtesting
|
||||
config = self._load_backtesting_config(config)
|
||||
|
||||
# Load Hyperopt
|
||||
config = self._load_hyperopt_config(config)
|
||||
|
||||
return config
|
||||
|
||||
def _load_config_file(self, path: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Loads a config file from the given path
|
||||
:param path: path as str
|
||||
:return: configuration as dictionary
|
||||
"""
|
||||
try:
|
||||
with open(path) as file:
|
||||
conf = json.load(file)
|
||||
except FileNotFoundError:
|
||||
logger.critical(
|
||||
'Config file "%s" not found. Please create your config file',
|
||||
path
|
||||
)
|
||||
exit(0)
|
||||
|
||||
if 'internals' not in conf:
|
||||
conf['internals'] = {}
|
||||
logger.info('Validating configuration ...')
|
||||
|
||||
return self._validate_config(conf)
|
||||
|
||||
def _load_common_config(self, config: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract information for sys.argv and load common configuration
|
||||
:return: configuration as dictionary
|
||||
"""
|
||||
|
||||
# Log level
|
||||
if 'loglevel' in self.args and self.args.loglevel:
|
||||
config.update({'loglevel': self.args.loglevel})
|
||||
logging.basicConfig(
|
||||
level=config['loglevel'],
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
)
|
||||
logger.info('Log level set to %s', logging.getLevelName(config['loglevel']))
|
||||
|
||||
# Add dynamic_whitelist if found
|
||||
if 'dynamic_whitelist' in self.args and self.args.dynamic_whitelist:
|
||||
config.update({'dynamic_whitelist': self.args.dynamic_whitelist})
|
||||
logger.info(
|
||||
'Parameter --dynamic-whitelist detected. '
|
||||
'Using dynamically generated whitelist. '
|
||||
'(not applicable with Backtesting and Hyperopt)'
|
||||
)
|
||||
|
||||
# Add dry_run_db if found and the bot in dry run
|
||||
if self.args.dry_run_db and config.get('dry_run', False):
|
||||
config.update({'dry_run_db': True})
|
||||
logger.info('Parameter --dry-run-db detected ...')
|
||||
|
||||
if config.get('dry_run_db', False):
|
||||
if config.get('dry_run', False):
|
||||
logger.info('Dry_run will use the DB file: "tradesv3.dry_run.sqlite"')
|
||||
else:
|
||||
logger.info('Dry run is disabled. (--dry_run_db ignored)')
|
||||
|
||||
return config
|
||||
|
||||
def _load_backtesting_config(self, config: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract information for sys.argv and load Backtesting configuration
|
||||
:return: configuration as dictionary
|
||||
"""
|
||||
|
||||
# If -i/--ticker-interval is used we override the configuration parameter
|
||||
# (that will override the strategy configuration)
|
||||
if 'ticker_interval' in self.args and self.args.ticker_interval:
|
||||
config.update({'ticker_interval': self.args.ticker_interval})
|
||||
logger.info('Parameter -i/--ticker-interval detected ...')
|
||||
logger.info('Using ticker_interval: %d ...', config.get('ticker_interval'))
|
||||
|
||||
# If -l/--live is used we add it to the configuration
|
||||
if 'live' in self.args and self.args.live:
|
||||
config.update({'live': True})
|
||||
logger.info('Parameter -l/--live detected ...')
|
||||
|
||||
# If --realistic-simulation is used we add it to the configuration
|
||||
if 'realistic_simulation' in self.args and self.args.realistic_simulation:
|
||||
config.update({'realistic_simulation': True})
|
||||
logger.info('Parameter --realistic-simulation detected ...')
|
||||
logger.info('Using max_open_trades: %s ...', config.get('max_open_trades'))
|
||||
|
||||
# If --timerange is used we add it to the configuration
|
||||
if 'timerange' in self.args and self.args.timerange:
|
||||
config.update({'timerange': self.args.timerange})
|
||||
logger.info('Parameter --timerange detected: %s ...', self.args.timerange)
|
||||
|
||||
# If --datadir is used we add it to the configuration
|
||||
if 'datadir' in self.args and self.args.datadir:
|
||||
config.update({'datadir': self.args.datadir})
|
||||
logger.info('Parameter --datadir detected: %s ...', self.args.datadir)
|
||||
|
||||
# If -r/--refresh-pairs-cached is used we add it to the configuration
|
||||
if 'refresh_pairs' in self.args and self.args.refresh_pairs:
|
||||
config.update({'refresh_pairs': True})
|
||||
logger.info('Parameter -r/--refresh-pairs-cached detected ...')
|
||||
|
||||
# If --export is used we add it to the configuration
|
||||
if 'export' in self.args and self.args.export:
|
||||
config.update({'export': self.args.export})
|
||||
logger.info('Parameter --export detected: %s ...', self.args.export)
|
||||
|
||||
return config
|
||||
|
||||
def _load_hyperopt_config(self, config: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract information for sys.argv and load Hyperopt configuration
|
||||
:return: configuration as dictionary
|
||||
"""
|
||||
# If --realistic-simulation is used we add it to the configuration
|
||||
if 'epochs' in self.args and self.args.epochs:
|
||||
config.update({'epochs': self.args.epochs})
|
||||
logger.info('Parameter --epochs detected ...')
|
||||
logger.info('Will run Hyperopt with for %s epochs ...', config.get('epochs'))
|
||||
|
||||
# If --mongodb is used we add it to the configuration
|
||||
if 'mongodb' in self.args and self.args.mongodb:
|
||||
config.update({'mongodb': self.args.mongodb})
|
||||
logger.info('Parameter --use-mongodb detected ...')
|
||||
|
||||
# If --spaces is used we add it to the configuration
|
||||
if 'spaces' in self.args and self.args.spaces:
|
||||
config.update({'spaces': self.args.spaces})
|
||||
logger.info('Parameter -s/--spaces detected: %s', config.get('spaces'))
|
||||
|
||||
return config
|
||||
|
||||
def _validate_config(self, conf: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Validate the configuration follow the Config Schema
|
||||
:param conf: Config in JSON format
|
||||
:return: Returns the config if valid, otherwise throw an exception
|
||||
"""
|
||||
try:
|
||||
validate(conf, constants.CONF_SCHEMA)
|
||||
return conf
|
||||
except ValidationError as exception:
|
||||
logger.fatal(
|
||||
'Invalid configuration. See config.json.example. Reason: %s',
|
||||
exception
|
||||
)
|
||||
raise ValidationError(
|
||||
best_match(Draft4Validator(constants.CONF_SCHEMA).iter_errors(conf)).message
|
||||
)
|
||||
|
||||
def get_config(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Return the config. Use this method to get the bot config
|
||||
:return: Dict: Bot config
|
||||
"""
|
||||
if self.config is None:
|
||||
self.config = self.load_config()
|
||||
|
||||
return self.config
|
||||
116
freqtrade/constants.py
Normal file
116
freqtrade/constants.py
Normal file
@@ -0,0 +1,116 @@
|
||||
# pragma pylint: disable=too-few-public-methods
|
||||
|
||||
"""
|
||||
bot constants
|
||||
"""
|
||||
DYNAMIC_WHITELIST = 20 # pairs
|
||||
PROCESS_THROTTLE_SECS = 5 # sec
|
||||
TICKER_INTERVAL = 5 # min
|
||||
HYPEROPT_EPOCH = 100 # epochs
|
||||
RETRY_TIMEOUT = 30 # sec
|
||||
DEFAULT_STRATEGY = 'DefaultStrategy'
|
||||
|
||||
# Required json-schema for user specified config
|
||||
CONF_SCHEMA = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'max_open_trades': {'type': 'integer', 'minimum': 1},
|
||||
'ticker_interval': {'type': 'integer', 'enum': [1, 5, 30, 60, 1440]},
|
||||
'stake_currency': {'type': 'string', 'enum': ['BTC', 'ETH', 'USDT']},
|
||||
'stake_amount': {'type': 'number', 'minimum': 0.0005},
|
||||
'fiat_display_currency': {'type': 'string', 'enum': ['AUD', 'BRL', 'CAD', 'CHF',
|
||||
'CLP', 'CNY', 'CZK', 'DKK',
|
||||
'EUR', 'GBP', 'HKD', 'HUF',
|
||||
'IDR', 'ILS', 'INR', 'JPY',
|
||||
'KRW', 'MXN', 'MYR', 'NOK',
|
||||
'NZD', 'PHP', 'PKR', 'PLN',
|
||||
'RUB', 'SEK', 'SGD', 'THB',
|
||||
'TRY', 'TWD', 'ZAR', 'USD']},
|
||||
'dry_run': {'type': 'boolean'},
|
||||
'minimal_roi': {
|
||||
'type': 'object',
|
||||
'patternProperties': {
|
||||
'^[0-9.]+$': {'type': 'number'}
|
||||
},
|
||||
'minProperties': 1
|
||||
},
|
||||
'stoploss': {'type': 'number', 'maximum': 0, 'exclusiveMaximum': True},
|
||||
'unfilledtimeout': {'type': 'integer', 'minimum': 0},
|
||||
'bid_strategy': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'ask_last_balance': {
|
||||
'type': 'number',
|
||||
'minimum': 0,
|
||||
'maximum': 1,
|
||||
'exclusiveMaximum': False
|
||||
},
|
||||
},
|
||||
'required': ['ask_last_balance']
|
||||
},
|
||||
'exchange': {'$ref': '#/definitions/exchange'},
|
||||
'experimental': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'use_sell_signal': {'type': 'boolean'},
|
||||
'sell_profit_only': {'type': 'boolean'}
|
||||
}
|
||||
},
|
||||
'telegram': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'enabled': {'type': 'boolean'},
|
||||
'token': {'type': 'string'},
|
||||
'chat_id': {'type': 'string'},
|
||||
},
|
||||
'required': ['enabled', 'token', 'chat_id']
|
||||
},
|
||||
'initial_state': {'type': 'string', 'enum': ['running', 'stopped']},
|
||||
'internals': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'process_throttle_secs': {'type': 'number'},
|
||||
'interval': {'type': 'integer'}
|
||||
}
|
||||
}
|
||||
},
|
||||
'definitions': {
|
||||
'exchange': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': {'type': 'string'},
|
||||
'key': {'type': 'string'},
|
||||
'secret': {'type': 'string'},
|
||||
'pair_whitelist': {
|
||||
'type': 'array',
|
||||
'items': {
|
||||
'type': 'string',
|
||||
'pattern': '^[0-9A-Z]+_[0-9A-Z]+$'
|
||||
},
|
||||
'uniqueItems': True
|
||||
},
|
||||
'pair_blacklist': {
|
||||
'type': 'array',
|
||||
'items': {
|
||||
'type': 'string',
|
||||
'pattern': '^[0-9A-Z]+_[0-9A-Z]+$'
|
||||
},
|
||||
'uniqueItems': True
|
||||
}
|
||||
},
|
||||
'required': ['name', 'key', 'secret', 'pair_whitelist']
|
||||
}
|
||||
},
|
||||
'anyOf': [
|
||||
{'required': ['exchange']}
|
||||
],
|
||||
'required': [
|
||||
'max_open_trades',
|
||||
'stake_currency',
|
||||
'stake_amount',
|
||||
'fiat_display_currency',
|
||||
'dry_run',
|
||||
'bid_strategy',
|
||||
'telegram'
|
||||
]
|
||||
}
|
||||
185
freqtrade/exchange/__init__.py
Normal file
185
freqtrade/exchange/__init__.py
Normal file
@@ -0,0 +1,185 @@
|
||||
# pragma pylint: disable=W0603
|
||||
""" Cryptocurrency Exchanges support """
|
||||
import enum
|
||||
import logging
|
||||
from random import randint
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
import arrow
|
||||
import requests
|
||||
from cachetools import cached, TTLCache
|
||||
|
||||
from freqtrade import OperationalException
|
||||
from freqtrade.exchange.bittrex import Bittrex
|
||||
from freqtrade.exchange.interface import Exchange
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Current selected exchange
|
||||
_API: Exchange = None
|
||||
_CONF: dict = {}
|
||||
|
||||
# Holds all open sell orders for dry_run
|
||||
_DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {}
|
||||
|
||||
|
||||
class Exchanges(enum.Enum):
|
||||
"""
|
||||
Maps supported exchange names to correspondent classes.
|
||||
"""
|
||||
BITTREX = Bittrex
|
||||
|
||||
|
||||
def init(config: dict) -> None:
|
||||
"""
|
||||
Initializes this module with the given config,
|
||||
it does basic validation whether the specified
|
||||
exchange and pairs are valid.
|
||||
:param config: config to use
|
||||
:return: None
|
||||
"""
|
||||
global _CONF, _API
|
||||
|
||||
_CONF.update(config)
|
||||
|
||||
if config['dry_run']:
|
||||
logger.info('Instance is running with dry_run enabled')
|
||||
|
||||
exchange_config = config['exchange']
|
||||
|
||||
# Find matching class for the given exchange name
|
||||
name = exchange_config['name']
|
||||
try:
|
||||
exchange_class = Exchanges[name.upper()].value
|
||||
except KeyError:
|
||||
raise OperationalException('Exchange {} is not supported'.format(name))
|
||||
|
||||
_API = exchange_class(exchange_config)
|
||||
|
||||
# Check if all pairs are available
|
||||
validate_pairs(config['exchange']['pair_whitelist'])
|
||||
|
||||
|
||||
def validate_pairs(pairs: List[str]) -> None:
|
||||
"""
|
||||
Checks if all given pairs are tradable on the current exchange.
|
||||
Raises OperationalException if one pair is not available.
|
||||
:param pairs: list of pairs
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
markets = _API.get_markets()
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.warning('Unable to validate pairs (assuming they are correct). Reason: %s', e)
|
||||
return
|
||||
|
||||
stake_cur = _CONF['stake_currency']
|
||||
for pair in pairs:
|
||||
if not pair.startswith(stake_cur):
|
||||
raise OperationalException(
|
||||
'Pair {} not compatible with stake_currency: {}'.format(pair, stake_cur)
|
||||
)
|
||||
if pair not in markets:
|
||||
raise OperationalException(
|
||||
'Pair {} is not available at {}'.format(pair, _API.name.lower()))
|
||||
|
||||
|
||||
def buy(pair: str, rate: float, amount: float) -> str:
|
||||
if _CONF['dry_run']:
|
||||
global _DRY_RUN_OPEN_ORDERS
|
||||
order_id = 'dry_run_buy_{}'.format(randint(0, 10**6))
|
||||
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||
'pair': pair,
|
||||
'rate': rate,
|
||||
'amount': amount,
|
||||
'type': 'LIMIT_BUY',
|
||||
'remaining': 0.0,
|
||||
'opened': arrow.utcnow().datetime,
|
||||
'closed': arrow.utcnow().datetime,
|
||||
}
|
||||
return order_id
|
||||
|
||||
return _API.buy(pair, rate, amount)
|
||||
|
||||
|
||||
def sell(pair: str, rate: float, amount: float) -> str:
|
||||
if _CONF['dry_run']:
|
||||
global _DRY_RUN_OPEN_ORDERS
|
||||
order_id = 'dry_run_sell_{}'.format(randint(0, 10**6))
|
||||
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||
'pair': pair,
|
||||
'rate': rate,
|
||||
'amount': amount,
|
||||
'type': 'LIMIT_SELL',
|
||||
'remaining': 0.0,
|
||||
'opened': arrow.utcnow().datetime,
|
||||
'closed': arrow.utcnow().datetime,
|
||||
}
|
||||
return order_id
|
||||
|
||||
return _API.sell(pair, rate, amount)
|
||||
|
||||
|
||||
def get_balance(currency: str) -> float:
|
||||
if _CONF['dry_run']:
|
||||
return 999.9
|
||||
|
||||
return _API.get_balance(currency)
|
||||
|
||||
|
||||
def get_balances():
|
||||
if _CONF['dry_run']:
|
||||
return []
|
||||
|
||||
return _API.get_balances()
|
||||
|
||||
|
||||
def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
|
||||
return _API.get_ticker(pair, refresh)
|
||||
|
||||
|
||||
@cached(TTLCache(maxsize=100, ttl=30))
|
||||
def get_ticker_history(pair: str, tick_interval) -> List[Dict]:
|
||||
return _API.get_ticker_history(pair, tick_interval)
|
||||
|
||||
|
||||
def cancel_order(order_id: str) -> None:
|
||||
if _CONF['dry_run']:
|
||||
return
|
||||
|
||||
return _API.cancel_order(order_id)
|
||||
|
||||
|
||||
def get_order(order_id: str) -> Dict:
|
||||
if _CONF['dry_run']:
|
||||
order = _DRY_RUN_OPEN_ORDERS[order_id]
|
||||
order.update({
|
||||
'id': order_id
|
||||
})
|
||||
return order
|
||||
|
||||
return _API.get_order(order_id)
|
||||
|
||||
|
||||
def get_pair_detail_url(pair: str) -> str:
|
||||
return _API.get_pair_detail_url(pair)
|
||||
|
||||
|
||||
def get_markets() -> List[str]:
|
||||
return _API.get_markets()
|
||||
|
||||
|
||||
def get_market_summaries() -> List[Dict]:
|
||||
return _API.get_market_summaries()
|
||||
|
||||
|
||||
def get_name() -> str:
|
||||
return _API.name
|
||||
|
||||
|
||||
def get_fee() -> float:
|
||||
return _API.fee
|
||||
|
||||
|
||||
def get_wallet_health() -> List[Dict]:
|
||||
return _API.get_wallet_health()
|
||||
211
freqtrade/exchange/bittrex.py
Normal file
211
freqtrade/exchange/bittrex.py
Normal file
@@ -0,0 +1,211 @@
|
||||
import logging
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from bittrex.bittrex import API_V1_1, API_V2_0
|
||||
from bittrex.bittrex import Bittrex as _Bittrex
|
||||
from requests.exceptions import ContentDecodingError
|
||||
|
||||
from freqtrade import OperationalException
|
||||
from freqtrade.exchange.interface import Exchange
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_API: _Bittrex = None
|
||||
_API_V2: _Bittrex = None
|
||||
_EXCHANGE_CONF: dict = {}
|
||||
|
||||
|
||||
class Bittrex(Exchange):
|
||||
"""
|
||||
Bittrex API wrapper.
|
||||
"""
|
||||
# Base URL and API endpoints
|
||||
BASE_URL: str = 'https://www.bittrex.com'
|
||||
PAIR_DETAIL_METHOD: str = BASE_URL + '/Market/Index'
|
||||
|
||||
def __init__(self, config: dict) -> None:
|
||||
global _API, _API_V2, _EXCHANGE_CONF
|
||||
|
||||
_EXCHANGE_CONF.update(config)
|
||||
_API = _Bittrex(
|
||||
api_key=_EXCHANGE_CONF['key'],
|
||||
api_secret=_EXCHANGE_CONF['secret'],
|
||||
calls_per_second=1,
|
||||
api_version=API_V1_1,
|
||||
)
|
||||
_API_V2 = _Bittrex(
|
||||
api_key=_EXCHANGE_CONF['key'],
|
||||
api_secret=_EXCHANGE_CONF['secret'],
|
||||
calls_per_second=1,
|
||||
api_version=API_V2_0,
|
||||
)
|
||||
self.cached_ticker = {}
|
||||
|
||||
@staticmethod
|
||||
def _validate_response(response) -> None:
|
||||
"""
|
||||
Validates the given bittrex response
|
||||
and raises a ContentDecodingError if a non-fatal issue happened.
|
||||
"""
|
||||
temp_error_messages = [
|
||||
'NO_API_RESPONSE',
|
||||
'MIN_TRADE_REQUIREMENT_NOT_MET',
|
||||
]
|
||||
if response['message'] in temp_error_messages:
|
||||
raise ContentDecodingError(response['message'])
|
||||
|
||||
@property
|
||||
def fee(self) -> float:
|
||||
# 0.25 %: See https://bittrex.com/fees
|
||||
return 0.0025
|
||||
|
||||
def buy(self, pair: str, rate: float, amount: float) -> str:
|
||||
data = _API.buy_limit(pair.replace('_', '-'), amount, rate)
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({pair}, {rate}, {amount})'.format(
|
||||
message=data['message'],
|
||||
pair=pair,
|
||||
rate=rate,
|
||||
amount=amount))
|
||||
return data['result']['uuid']
|
||||
|
||||
def sell(self, pair: str, rate: float, amount: float) -> str:
|
||||
data = _API.sell_limit(pair.replace('_', '-'), amount, rate)
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({pair}, {rate}, {amount})'.format(
|
||||
message=data['message'],
|
||||
pair=pair,
|
||||
rate=rate,
|
||||
amount=amount))
|
||||
return data['result']['uuid']
|
||||
|
||||
def get_balance(self, currency: str) -> float:
|
||||
data = _API.get_balance(currency)
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({currency})'.format(
|
||||
message=data['message'],
|
||||
currency=currency))
|
||||
return float(data['result']['Balance'] or 0.0)
|
||||
|
||||
def get_balances(self):
|
||||
data = _API.get_balances()
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message}'.format(message=data['message']))
|
||||
return data['result']
|
||||
|
||||
def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
|
||||
if refresh or pair not in self.cached_ticker.keys():
|
||||
data = _API.get_ticker(pair.replace('_', '-'))
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({pair})'.format(
|
||||
message=data['message'],
|
||||
pair=pair))
|
||||
keys = ['Bid', 'Ask', 'Last']
|
||||
if not data.get('result') or\
|
||||
not all(key in data.get('result', {}) for key in keys) or\
|
||||
not all(data.get('result', {})[key] is not None for key in keys):
|
||||
raise ContentDecodingError('Invalid response from Bittrex params=({pair})'.format(
|
||||
pair=pair))
|
||||
# Update the pair
|
||||
self.cached_ticker[pair] = {
|
||||
'bid': float(data['result']['Bid']),
|
||||
'ask': float(data['result']['Ask']),
|
||||
'last': float(data['result']['Last']),
|
||||
}
|
||||
return self.cached_ticker[pair]
|
||||
|
||||
def get_ticker_history(self, pair: str, tick_interval: int) -> List[Dict]:
|
||||
if tick_interval == 1:
|
||||
interval = 'oneMin'
|
||||
elif tick_interval == 5:
|
||||
interval = 'fiveMin'
|
||||
elif tick_interval == 30:
|
||||
interval = 'thirtyMin'
|
||||
elif tick_interval == 60:
|
||||
interval = 'hour'
|
||||
elif tick_interval == 1440:
|
||||
interval = 'Day'
|
||||
else:
|
||||
raise ValueError('Unknown tick_interval: {}'.format(tick_interval))
|
||||
|
||||
data = _API_V2.get_candles(pair.replace('_', '-'), interval)
|
||||
|
||||
# These sanity check are necessary because bittrex cannot keep their API stable.
|
||||
if not data.get('result'):
|
||||
raise ContentDecodingError('Invalid response from Bittrex params=({pair})'.format(
|
||||
pair=pair))
|
||||
|
||||
for prop in ['C', 'V', 'O', 'H', 'L', 'T']:
|
||||
for tick in data['result']:
|
||||
if prop not in tick.keys():
|
||||
raise ContentDecodingError('Required property {} not present '
|
||||
'in response params=({})'.format(prop, pair))
|
||||
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({pair})'.format(
|
||||
message=data['message'],
|
||||
pair=pair))
|
||||
|
||||
return data['result']
|
||||
|
||||
def get_order(self, order_id: str) -> Dict:
|
||||
data = _API.get_order(order_id)
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({order_id})'.format(
|
||||
message=data['message'],
|
||||
order_id=order_id))
|
||||
data = data['result']
|
||||
return {
|
||||
'id': data['OrderUuid'],
|
||||
'type': data['Type'],
|
||||
'pair': data['Exchange'].replace('-', '_'),
|
||||
'opened': data['Opened'],
|
||||
'rate': data['PricePerUnit'],
|
||||
'amount': data['Quantity'],
|
||||
'remaining': data['QuantityRemaining'],
|
||||
'closed': data['Closed'],
|
||||
}
|
||||
|
||||
def cancel_order(self, order_id: str) -> None:
|
||||
data = _API.cancel(order_id)
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException('{message} params=({order_id})'.format(
|
||||
message=data['message'],
|
||||
order_id=order_id))
|
||||
|
||||
def get_pair_detail_url(self, pair: str) -> str:
|
||||
return self.PAIR_DETAIL_METHOD + '?MarketName={}'.format(pair.replace('_', '-'))
|
||||
|
||||
def get_markets(self) -> List[str]:
|
||||
data = _API.get_markets()
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException(data['message'])
|
||||
return [m['MarketName'].replace('-', '_') for m in data['result']]
|
||||
|
||||
def get_market_summaries(self) -> List[Dict]:
|
||||
data = _API.get_market_summaries()
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException(data['message'])
|
||||
return data['result']
|
||||
|
||||
def get_wallet_health(self) -> List[Dict]:
|
||||
data = _API_V2.get_wallet_health()
|
||||
if not data['success']:
|
||||
Bittrex._validate_response(data)
|
||||
raise OperationalException(data['message'])
|
||||
return [{
|
||||
'Currency': entry['Health']['Currency'],
|
||||
'IsActive': entry['Health']['IsActive'],
|
||||
'LastChecked': entry['Health']['LastChecked'],
|
||||
'Notice': entry['Currency'].get('Notice'),
|
||||
} for entry in data['result']]
|
||||
172
freqtrade/exchange/interface.py
Normal file
172
freqtrade/exchange/interface.py
Normal file
@@ -0,0 +1,172 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
class Exchange(ABC):
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""
|
||||
Name of the exchange.
|
||||
:return: str representation of the class name
|
||||
"""
|
||||
return self.__class__.__name__
|
||||
|
||||
@property
|
||||
def fee(self) -> float:
|
||||
"""
|
||||
Fee for placing an order
|
||||
:return: percentage in float
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def buy(self, pair: str, rate: float, amount: float) -> str:
|
||||
"""
|
||||
Places a limit buy order.
|
||||
:param pair: Pair as str, format: BTC_ETH
|
||||
:param rate: Rate limit for order
|
||||
:param amount: The amount to purchase
|
||||
:return: order_id of the placed buy order
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def sell(self, pair: str, rate: float, amount: float) -> str:
|
||||
"""
|
||||
Places a limit sell order.
|
||||
:param pair: Pair as str, format: BTC_ETH
|
||||
:param rate: Rate limit for order
|
||||
:param amount: The amount to sell
|
||||
:return: order_id of the placed sell order
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_balance(self, currency: str) -> float:
|
||||
"""
|
||||
Gets account balance.
|
||||
:param currency: Currency as str, format: BTC
|
||||
:return: float
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_balances(self) -> List[dict]:
|
||||
"""
|
||||
Gets account balances across currencies
|
||||
:return: List of dicts, format: [
|
||||
{
|
||||
'Currency': str,
|
||||
'Balance': float,
|
||||
'Available': float,
|
||||
'Pending': float,
|
||||
}
|
||||
...
|
||||
]
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
|
||||
"""
|
||||
Gets ticker for given pair.
|
||||
:param pair: Pair as str, format: BTC_ETC
|
||||
:param refresh: Shall we query a new value or a cached value is enough
|
||||
:return: dict, format: {
|
||||
'bid': float,
|
||||
'ask': float,
|
||||
'last': float
|
||||
}
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_ticker_history(self, pair: str, tick_interval: int) -> List[Dict]:
|
||||
"""
|
||||
Gets ticker history for given pair.
|
||||
:param pair: Pair as str, format: BTC_ETC
|
||||
:param tick_interval: ticker interval in minutes
|
||||
:return: list, format: [
|
||||
{
|
||||
'O': float, (Open)
|
||||
'H': float, (High)
|
||||
'L': float, (Low)
|
||||
'C': float, (Close)
|
||||
'V': float, (Volume)
|
||||
'T': datetime, (Time)
|
||||
'BV': float, (Base Volume)
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
|
||||
def get_order(self, order_id: str) -> Dict:
|
||||
"""
|
||||
Get order details for the given order_id.
|
||||
:param order_id: ID as str
|
||||
:return: dict, format: {
|
||||
'id': str,
|
||||
'type': str,
|
||||
'pair': str,
|
||||
'opened': str ISO 8601 datetime,
|
||||
'closed': str ISO 8601 datetime,
|
||||
'rate': float,
|
||||
'amount': float,
|
||||
'remaining': int
|
||||
}
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def cancel_order(self, order_id: str) -> None:
|
||||
"""
|
||||
Cancels order for given order_id.
|
||||
:param order_id: ID as str
|
||||
:return: None
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_pair_detail_url(self, pair: str) -> str:
|
||||
"""
|
||||
Returns the market detail url for the given pair.
|
||||
:param pair: Pair as str, format: BTC_ETC
|
||||
:return: URL as str
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_markets(self) -> List[str]:
|
||||
"""
|
||||
Returns all available markets.
|
||||
:return: List of all available pairs
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_market_summaries(self) -> List[Dict]:
|
||||
"""
|
||||
Returns a 24h market summary for all available markets
|
||||
:return: list, format: [
|
||||
{
|
||||
'MarketName': str,
|
||||
'High': float,
|
||||
'Low': float,
|
||||
'Volume': float,
|
||||
'Last': float,
|
||||
'TimeStamp': datetime,
|
||||
'BaseVolume': float,
|
||||
'Bid': float,
|
||||
'Ask': float,
|
||||
'OpenBuyOrders': int,
|
||||
'OpenSellOrders': int,
|
||||
'PrevDay': float,
|
||||
'Created': datetime
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_wallet_health(self) -> List[Dict]:
|
||||
"""
|
||||
Returns a list of all wallet health information
|
||||
:return: list, format: [
|
||||
{
|
||||
'Currency': str,
|
||||
'IsActive': bool,
|
||||
'LastChecked': str,
|
||||
'Notice': str
|
||||
},
|
||||
...
|
||||
"""
|
||||
193
freqtrade/fiat_convert.py
Normal file
193
freqtrade/fiat_convert.py
Normal file
@@ -0,0 +1,193 @@
|
||||
"""
|
||||
Module that define classes to convert Crypto-currency to FIAT
|
||||
e.g BTC to USD
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
|
||||
from coinmarketcap import Market
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CryptoFiat(object):
|
||||
"""
|
||||
Object to describe what is the price of Crypto-currency in a FIAT
|
||||
"""
|
||||
# Constants
|
||||
CACHE_DURATION = 6 * 60 * 60 # 6 hours
|
||||
|
||||
def __init__(self, crypto_symbol: str, fiat_symbol: str, price: float) -> None:
|
||||
"""
|
||||
Create an object that will contains the price for a crypto-currency in fiat
|
||||
:param crypto_symbol: Crypto-currency you want to convert (e.g BTC)
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g USD)
|
||||
:param price: Price in FIAT
|
||||
"""
|
||||
|
||||
# Public attributes
|
||||
self.crypto_symbol = None
|
||||
self.fiat_symbol = None
|
||||
self.price = 0.0
|
||||
|
||||
# Private attributes
|
||||
self._expiration = 0
|
||||
|
||||
self.crypto_symbol = crypto_symbol.upper()
|
||||
self.fiat_symbol = fiat_symbol.upper()
|
||||
self.set_price(price=price)
|
||||
|
||||
def set_price(self, price: float) -> None:
|
||||
"""
|
||||
Set the price of the Crypto-currency in FIAT and set the expiration time
|
||||
:param price: Price of the current Crypto currency in the fiat
|
||||
:return: None
|
||||
"""
|
||||
self.price = price
|
||||
self._expiration = time.time() + self.CACHE_DURATION
|
||||
|
||||
def is_expired(self) -> bool:
|
||||
"""
|
||||
Return if the current price is still valid or needs to be refreshed
|
||||
:return: bool, true the price is expired and needs to be refreshed, false the price is
|
||||
still valid
|
||||
"""
|
||||
return self._expiration - time.time() <= 0
|
||||
|
||||
|
||||
class CryptoToFiatConverter(object):
|
||||
"""
|
||||
Main class to initiate Crypto to FIAT.
|
||||
This object contains a list of pair Crypto, FIAT
|
||||
This object is also a Singleton
|
||||
"""
|
||||
__instance = None
|
||||
_coinmarketcap = None
|
||||
|
||||
# Constants
|
||||
SUPPORTED_FIAT = [
|
||||
"AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK",
|
||||
"EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY",
|
||||
"KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN",
|
||||
"RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD"
|
||||
]
|
||||
|
||||
CRYPTOMAP = {
|
||||
'BTC': 'bitcoin',
|
||||
'ETH': 'ethereum',
|
||||
'USDT': 'thether'
|
||||
}
|
||||
|
||||
def __new__(cls):
|
||||
if CryptoToFiatConverter.__instance is None:
|
||||
CryptoToFiatConverter.__instance = object.__new__(cls)
|
||||
try:
|
||||
CryptoToFiatConverter._coinmarketcap = Market()
|
||||
except BaseException:
|
||||
CryptoToFiatConverter._coinmarketcap = None
|
||||
return CryptoToFiatConverter.__instance
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._pairs = []
|
||||
|
||||
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
|
||||
"""
|
||||
Convert an amount of crypto-currency to fiat
|
||||
:param crypto_amount: amount of crypto-currency to convert
|
||||
:param crypto_symbol: crypto-currency used
|
||||
:param fiat_symbol: fiat to convert to
|
||||
:return: float, value in fiat of the crypto-currency amount
|
||||
"""
|
||||
price = self.get_price(crypto_symbol=crypto_symbol, fiat_symbol=fiat_symbol)
|
||||
return float(crypto_amount) * float(price)
|
||||
|
||||
def get_price(self, crypto_symbol: str, fiat_symbol: str) -> float:
|
||||
"""
|
||||
Return the price of the Crypto-currency in Fiat
|
||||
:param crypto_symbol: Crypto-currency you want to convert (e.g BTC)
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g USD)
|
||||
:return: Price in FIAT
|
||||
"""
|
||||
crypto_symbol = crypto_symbol.upper()
|
||||
fiat_symbol = fiat_symbol.upper()
|
||||
|
||||
# Check if the fiat convertion you want is supported
|
||||
if not self._is_supported_fiat(fiat=fiat_symbol):
|
||||
raise ValueError('The fiat {} is not supported.'.format(fiat_symbol))
|
||||
|
||||
# Get the pair that interest us and return the price in fiat
|
||||
for pair in self._pairs:
|
||||
if pair.crypto_symbol == crypto_symbol and pair.fiat_symbol == fiat_symbol:
|
||||
# If the price is expired we refresh it, avoid to call the API all the time
|
||||
if pair.is_expired():
|
||||
pair.set_price(
|
||||
price=self._find_price(
|
||||
crypto_symbol=pair.crypto_symbol,
|
||||
fiat_symbol=pair.fiat_symbol
|
||||
)
|
||||
)
|
||||
|
||||
# return the last price we have for this pair
|
||||
return pair.price
|
||||
|
||||
# The pair does not exist, so we create it and return the price
|
||||
return self._add_pair(
|
||||
crypto_symbol=crypto_symbol,
|
||||
fiat_symbol=fiat_symbol,
|
||||
price=self._find_price(
|
||||
crypto_symbol=crypto_symbol,
|
||||
fiat_symbol=fiat_symbol
|
||||
)
|
||||
)
|
||||
|
||||
def _add_pair(self, crypto_symbol: str, fiat_symbol: str, price: float) -> float:
|
||||
"""
|
||||
:param crypto_symbol: Crypto-currency you want to convert (e.g BTC)
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g USD)
|
||||
:return: price in FIAT
|
||||
"""
|
||||
self._pairs.append(
|
||||
CryptoFiat(
|
||||
crypto_symbol=crypto_symbol,
|
||||
fiat_symbol=fiat_symbol,
|
||||
price=price
|
||||
)
|
||||
)
|
||||
|
||||
return price
|
||||
|
||||
def _is_supported_fiat(self, fiat: str) -> bool:
|
||||
"""
|
||||
Check if the FIAT your want to convert to is supported
|
||||
:param fiat: FIAT to check (e.g USD)
|
||||
:return: bool, True supported, False not supported
|
||||
"""
|
||||
|
||||
fiat = fiat.upper()
|
||||
|
||||
return fiat in self.SUPPORTED_FIAT
|
||||
|
||||
def _find_price(self, crypto_symbol: str, fiat_symbol: str) -> float:
|
||||
"""
|
||||
Call CoinMarketCap API to retrieve the price in the FIAT
|
||||
:param crypto_symbol: Crypto-currency you want to convert (e.g BTC)
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g USD)
|
||||
:return: float, price of the crypto-currency in Fiat
|
||||
"""
|
||||
# Check if the fiat convertion you want is supported
|
||||
if not self._is_supported_fiat(fiat=fiat_symbol):
|
||||
raise ValueError('The fiat {} is not supported.'.format(fiat_symbol))
|
||||
|
||||
if crypto_symbol not in self.CRYPTOMAP:
|
||||
raise ValueError(
|
||||
'The crypto symbol {} is not supported.'.format(crypto_symbol))
|
||||
try:
|
||||
return float(
|
||||
self._coinmarketcap.ticker(
|
||||
currency=self.CRYPTOMAP[crypto_symbol],
|
||||
convert=fiat_symbol
|
||||
)[0]['price_' + fiat_symbol.lower()]
|
||||
)
|
||||
except BaseException:
|
||||
return 0.0
|
||||
526
freqtrade/freqtradebot.py
Normal file
526
freqtrade/freqtradebot.py
Normal file
@@ -0,0 +1,526 @@
|
||||
"""
|
||||
Freqtrade is the main module of this bot. It contains the class Freqtrade()
|
||||
"""
|
||||
|
||||
import copy
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Optional, Any, Callable
|
||||
|
||||
import arrow
|
||||
import requests
|
||||
from cachetools import cached, TTLCache
|
||||
|
||||
from freqtrade import (
|
||||
DependencyException, OperationalException, exchange, persistence, __version__
|
||||
)
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade import constants
|
||||
from freqtrade.fiat_convert import CryptoToFiatConverter
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.rpc.rpc_manager import RPCManager
|
||||
from freqtrade.state import State
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FreqtradeBot(object):
|
||||
"""
|
||||
Freqtrade is the main class of the bot.
|
||||
This is from here the bot start its logic.
|
||||
"""
|
||||
|
||||
def __init__(self, config: Dict[str, Any], db_url: Optional[str] = None):
|
||||
"""
|
||||
Init all variables and object the bot need to work
|
||||
:param config: configuration dict, you can use the Configuration.get_config()
|
||||
method to get the config dict.
|
||||
:param db_url: database connector string for sqlalchemy (Optional)
|
||||
"""
|
||||
|
||||
logger.info(
|
||||
'Starting freqtrade %s',
|
||||
__version__,
|
||||
)
|
||||
|
||||
# Init bot states
|
||||
self.state = State.STOPPED
|
||||
|
||||
# Init objects
|
||||
self.config = config
|
||||
self.analyze = None
|
||||
self.fiat_converter = None
|
||||
self.rpc = None
|
||||
self.persistence = None
|
||||
self.exchange = None
|
||||
|
||||
self._init_modules(db_url=db_url)
|
||||
|
||||
def _init_modules(self, db_url: Optional[str] = None) -> None:
|
||||
"""
|
||||
Initializes all modules and updates the config
|
||||
:param db_url: database connector string for sqlalchemy (Optional)
|
||||
:return: None
|
||||
"""
|
||||
# Initialize all modules
|
||||
self.analyze = Analyze(self.config)
|
||||
self.fiat_converter = CryptoToFiatConverter()
|
||||
self.rpc = RPCManager(self)
|
||||
|
||||
persistence.init(self.config, db_url)
|
||||
exchange.init(self.config)
|
||||
|
||||
# Set initial application state
|
||||
initial_state = self.config.get('initial_state')
|
||||
|
||||
if initial_state:
|
||||
self.state = State[initial_state.upper()]
|
||||
else:
|
||||
self.state = State.STOPPED
|
||||
|
||||
def clean(self) -> bool:
|
||||
"""
|
||||
Cleanup the application state und finish all pending tasks
|
||||
:return: None
|
||||
"""
|
||||
self.rpc.send_msg('*Status:* `Stopping trader...`')
|
||||
logger.info('Stopping trader and cleaning up modules...')
|
||||
self.state = State.STOPPED
|
||||
self.rpc.cleanup()
|
||||
persistence.cleanup()
|
||||
return True
|
||||
|
||||
def worker(self, old_state: None) -> State:
|
||||
"""
|
||||
Trading routine that must be run at each loop
|
||||
:param old_state: the previous service state from the previous call
|
||||
:return: current service state
|
||||
"""
|
||||
# Log state transition
|
||||
state = self.state
|
||||
if state != old_state:
|
||||
self.rpc.send_msg('*Status:* `{}`'.format(state.name.lower()))
|
||||
logger.info('Changing state to: %s', state.name)
|
||||
|
||||
if state == State.STOPPED:
|
||||
time.sleep(1)
|
||||
elif state == State.RUNNING:
|
||||
min_secs = self.config.get('internals', {}).get(
|
||||
'process_throttle_secs',
|
||||
constants.PROCESS_THROTTLE_SECS
|
||||
)
|
||||
|
||||
nb_assets = self.config.get('dynamic_whitelist', None)
|
||||
|
||||
self._throttle(func=self._process,
|
||||
min_secs=min_secs,
|
||||
nb_assets=nb_assets)
|
||||
return state
|
||||
|
||||
def _throttle(self, func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any:
|
||||
"""
|
||||
Throttles the given callable that it
|
||||
takes at least `min_secs` to finish execution.
|
||||
:param func: Any callable
|
||||
:param min_secs: minimum execution time in seconds
|
||||
:return: Any
|
||||
"""
|
||||
start = time.time()
|
||||
result = func(*args, **kwargs)
|
||||
end = time.time()
|
||||
duration = max(min_secs - (end - start), 0.0)
|
||||
logger.debug('Throttling %s for %.2f seconds', func.__name__, duration)
|
||||
time.sleep(duration)
|
||||
return result
|
||||
|
||||
def _process(self, nb_assets: Optional[int] = 0) -> bool:
|
||||
"""
|
||||
Queries the persistence layer for open trades and handles them,
|
||||
otherwise a new trade is created.
|
||||
:param: nb_assets: the maximum number of pairs to be traded at the same time
|
||||
:return: True if one or more trades has been created or closed, False otherwise
|
||||
"""
|
||||
state_changed = False
|
||||
try:
|
||||
# Refresh whitelist based on wallet maintenance
|
||||
sanitized_list = self._refresh_whitelist(
|
||||
self._gen_pair_whitelist(
|
||||
self.config['stake_currency']
|
||||
) if nb_assets else self.config['exchange']['pair_whitelist']
|
||||
)
|
||||
|
||||
# Keep only the subsets of pairs wanted (up to nb_assets)
|
||||
final_list = sanitized_list[:nb_assets] if nb_assets else sanitized_list
|
||||
self.config['exchange']['pair_whitelist'] = final_list
|
||||
|
||||
# Query trades from persistence layer
|
||||
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||
|
||||
# First process current opened trades
|
||||
for trade in trades:
|
||||
state_changed |= self.process_maybe_execute_sell(trade)
|
||||
|
||||
# Then looking for buy opportunities
|
||||
if len(trades) < self.config['max_open_trades']:
|
||||
state_changed = self.process_maybe_execute_buy()
|
||||
|
||||
if 'unfilledtimeout' in self.config:
|
||||
# Check and handle any timed out open orders
|
||||
self.check_handle_timedout(self.config['unfilledtimeout'])
|
||||
Trade.session.flush()
|
||||
|
||||
except (requests.exceptions.RequestException, json.JSONDecodeError) as error:
|
||||
logger.warning('%s, retrying in 30 seconds...', error)
|
||||
time.sleep(constants.RETRY_TIMEOUT)
|
||||
except OperationalException:
|
||||
self.rpc.send_msg(
|
||||
'*Status:* OperationalException:\n```\n{traceback}```{hint}'
|
||||
.format(
|
||||
traceback=traceback.format_exc(),
|
||||
hint='Issue `/start` if you think it is safe to restart.'
|
||||
)
|
||||
)
|
||||
logger.exception('OperationalException. Stopping trader ...')
|
||||
self.state = State.STOPPED
|
||||
return state_changed
|
||||
|
||||
@cached(TTLCache(maxsize=1, ttl=1800))
|
||||
def _gen_pair_whitelist(self, base_currency: str, key: str = 'BaseVolume') -> List[str]:
|
||||
"""
|
||||
Updates the whitelist with with a dynamically generated list
|
||||
:param base_currency: base currency as str
|
||||
:param key: sort key (defaults to 'BaseVolume')
|
||||
:return: List of pairs
|
||||
"""
|
||||
summaries = sorted(
|
||||
(s for s in exchange.get_market_summaries() if
|
||||
s['MarketName'].startswith(base_currency)),
|
||||
key=lambda s: s.get(key) or 0.0,
|
||||
reverse=True
|
||||
)
|
||||
|
||||
return [s['MarketName'].replace('-', '_') for s in summaries]
|
||||
|
||||
def _refresh_whitelist(self, whitelist: List[str]) -> List[str]:
|
||||
"""
|
||||
Check wallet health and remove pair from whitelist if necessary
|
||||
:param whitelist: the sorted list (based on BaseVolume) of pairs the user might want to
|
||||
trade
|
||||
:return: the list of pairs the user wants to trade without the one unavailable or
|
||||
black_listed
|
||||
"""
|
||||
sanitized_whitelist = whitelist
|
||||
health = exchange.get_wallet_health()
|
||||
known_pairs = set()
|
||||
for status in health:
|
||||
pair = '{}_{}'.format(self.config['stake_currency'], status['Currency'])
|
||||
# pair is not int the generated dynamic market, or in the blacklist ... ignore it
|
||||
if pair not in whitelist or pair in self.config['exchange'].get('pair_blacklist', []):
|
||||
continue
|
||||
# else the pair is valid
|
||||
known_pairs.add(pair)
|
||||
# Market is not active
|
||||
if not status['IsActive']:
|
||||
sanitized_whitelist.remove(pair)
|
||||
logger.info(
|
||||
'Ignoring %s from whitelist (reason: %s).',
|
||||
pair, status.get('Notice') or 'wallet is not active'
|
||||
)
|
||||
|
||||
# We need to remove pairs that are unknown
|
||||
final_list = [x for x in sanitized_whitelist if x in known_pairs]
|
||||
return final_list
|
||||
|
||||
def get_target_bid(self, ticker: Dict[str, float]) -> float:
|
||||
"""
|
||||
Calculates bid target between current ask price and last price
|
||||
:param ticker: Ticker to use for getting Ask and Last Price
|
||||
:return: float: Price
|
||||
"""
|
||||
if ticker['ask'] < ticker['last']:
|
||||
return ticker['ask']
|
||||
balance = self.config['bid_strategy']['ask_last_balance']
|
||||
return ticker['ask'] + balance * (ticker['last'] - ticker['ask'])
|
||||
|
||||
def create_trade(self) -> bool:
|
||||
"""
|
||||
Checks the implemented trading indicator(s) for a randomly picked pair,
|
||||
if one pair triggers the buy_signal a new trade record gets created
|
||||
:param stake_amount: amount of btc to spend
|
||||
:param interval: Ticker interval used for Analyze
|
||||
:return: True if a trade object has been created and persisted, False otherwise
|
||||
"""
|
||||
stake_amount = self.config['stake_amount']
|
||||
interval = self.analyze.get_ticker_interval()
|
||||
|
||||
logger.info(
|
||||
'Checking buy signals to create a new trade with stake_amount: %f ...',
|
||||
stake_amount
|
||||
)
|
||||
whitelist = copy.deepcopy(self.config['exchange']['pair_whitelist'])
|
||||
# Check if stake_amount is fulfilled
|
||||
if exchange.get_balance(self.config['stake_currency']) < stake_amount:
|
||||
raise DependencyException(
|
||||
'stake amount is not fulfilled (currency={})'.format(self.config['stake_currency'])
|
||||
)
|
||||
|
||||
# Remove currently opened and latest pairs from whitelist
|
||||
for trade in Trade.query.filter(Trade.is_open.is_(True)).all():
|
||||
if trade.pair in whitelist:
|
||||
whitelist.remove(trade.pair)
|
||||
logger.debug('Ignoring %s in pair whitelist', trade.pair)
|
||||
|
||||
if not whitelist:
|
||||
raise DependencyException('No currency pairs in whitelist')
|
||||
|
||||
# Pick pair based on StochRSI buy signals
|
||||
for _pair in whitelist:
|
||||
(buy, sell) = self.analyze.get_signal(_pair, interval)
|
||||
if buy and not sell:
|
||||
pair = _pair
|
||||
break
|
||||
else:
|
||||
return False
|
||||
|
||||
# Calculate amount
|
||||
buy_limit = self.get_target_bid(exchange.get_ticker(pair))
|
||||
amount = stake_amount / buy_limit
|
||||
|
||||
order_id = exchange.buy(pair, buy_limit, amount)
|
||||
|
||||
stake_amount_fiat = self.fiat_converter.convert_amount(
|
||||
stake_amount,
|
||||
self.config['stake_currency'],
|
||||
self.config['fiat_display_currency']
|
||||
)
|
||||
|
||||
# Create trade entity and return
|
||||
self.rpc.send_msg(
|
||||
'*{}:* Buying [{}]({}) with limit `{:.8f} ({:.6f} {}, {:.3f} {})` '
|
||||
.format(
|
||||
exchange.get_name().upper(),
|
||||
pair.replace('_', '/'),
|
||||
exchange.get_pair_detail_url(pair),
|
||||
buy_limit,
|
||||
stake_amount,
|
||||
self.config['stake_currency'],
|
||||
stake_amount_fiat,
|
||||
self.config['fiat_display_currency']
|
||||
)
|
||||
)
|
||||
# Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL
|
||||
trade = Trade(
|
||||
pair=pair,
|
||||
stake_amount=stake_amount,
|
||||
amount=amount,
|
||||
fee=exchange.get_fee(),
|
||||
open_rate=buy_limit,
|
||||
open_date=datetime.utcnow(),
|
||||
exchange=exchange.get_name().upper(),
|
||||
open_order_id=order_id
|
||||
)
|
||||
Trade.session.add(trade)
|
||||
Trade.session.flush()
|
||||
return True
|
||||
|
||||
def process_maybe_execute_buy(self) -> bool:
|
||||
"""
|
||||
Tries to execute a buy trade in a safe way
|
||||
:return: True if executed
|
||||
"""
|
||||
try:
|
||||
# Create entity and execute trade
|
||||
if self.create_trade():
|
||||
return True
|
||||
|
||||
logger.info('Found no buy signals for whitelisted currencies. Trying again..')
|
||||
return False
|
||||
except DependencyException as exception:
|
||||
logger.warning('Unable to create trade: %s', exception)
|
||||
return False
|
||||
|
||||
def process_maybe_execute_sell(self, trade: Trade) -> bool:
|
||||
"""
|
||||
Tries to execute a sell trade
|
||||
:return: True if executed
|
||||
"""
|
||||
# Get order details for actual price per unit
|
||||
if trade.open_order_id:
|
||||
# Update trade with order values
|
||||
logger.info('Found open order for %s', trade)
|
||||
trade.update(exchange.get_order(trade.open_order_id))
|
||||
|
||||
if trade.is_open and trade.open_order_id is None:
|
||||
# Check if we can sell our current pair
|
||||
return self.handle_trade(trade)
|
||||
return False
|
||||
|
||||
def handle_trade(self, trade: Trade) -> bool:
|
||||
"""
|
||||
Sells the current pair if the threshold is reached and updates the trade record.
|
||||
:return: True if trade has been sold, False otherwise
|
||||
"""
|
||||
if not trade.is_open:
|
||||
raise ValueError('attempt to handle closed trade: {}'.format(trade))
|
||||
|
||||
logger.debug('Handling %s ...', trade)
|
||||
current_rate = exchange.get_ticker(trade.pair)['bid']
|
||||
|
||||
(buy, sell) = (False, False)
|
||||
|
||||
if self.config.get('experimental', {}).get('use_sell_signal'):
|
||||
(buy, sell) = self.analyze.get_signal(trade.pair, self.analyze.get_ticker_interval())
|
||||
|
||||
if self.analyze.should_sell(trade, current_rate, datetime.utcnow(), buy, sell):
|
||||
self.execute_sell(trade, current_rate)
|
||||
return True
|
||||
logger.info('Found no sell signals for whitelisted currencies. Trying again..')
|
||||
return False
|
||||
|
||||
def check_handle_timedout(self, timeoutvalue: int) -> None:
|
||||
"""
|
||||
Check if any orders are timed out and cancel if neccessary
|
||||
:param timeoutvalue: Number of minutes until order is considered timed out
|
||||
:return: None
|
||||
"""
|
||||
timeoutthreashold = arrow.utcnow().shift(minutes=-timeoutvalue).datetime
|
||||
|
||||
for trade in Trade.query.filter(Trade.open_order_id.isnot(None)).all():
|
||||
try:
|
||||
order = exchange.get_order(trade.open_order_id)
|
||||
except requests.exceptions.RequestException:
|
||||
logger.info(
|
||||
'Cannot query order for %s due to %s',
|
||||
trade,
|
||||
traceback.format_exc())
|
||||
continue
|
||||
ordertime = arrow.get(order['opened'])
|
||||
|
||||
# Check if trade is still actually open
|
||||
if int(order['remaining']) == 0:
|
||||
continue
|
||||
|
||||
if order['type'] == "LIMIT_BUY" and ordertime < timeoutthreashold:
|
||||
self.handle_timedout_limit_buy(trade, order)
|
||||
elif order['type'] == "LIMIT_SELL" and ordertime < timeoutthreashold:
|
||||
self.handle_timedout_limit_sell(trade, order)
|
||||
|
||||
# FIX: 20180110, why is cancel.order unconditionally here, whereas
|
||||
# it is conditionally called in the
|
||||
# handle_timedout_limit_sell()?
|
||||
def handle_timedout_limit_buy(self, trade: Trade, order: Dict) -> bool:
|
||||
"""Buy timeout - cancel order
|
||||
:return: True if order was fully cancelled
|
||||
"""
|
||||
exchange.cancel_order(trade.open_order_id)
|
||||
if order['remaining'] == order['amount']:
|
||||
# if trade is not partially completed, just delete the trade
|
||||
Trade.session.delete(trade)
|
||||
# FIX? do we really need to flush, caller of
|
||||
# check_handle_timedout will flush afterwards
|
||||
Trade.session.flush()
|
||||
logger.info('Buy order timeout for %s.', trade)
|
||||
self.rpc.send_msg('*Timeout:* Unfilled buy order for {} cancelled'.format(
|
||||
trade.pair.replace('_', '/')))
|
||||
return True
|
||||
|
||||
# if trade is partially complete, edit the stake details for the trade
|
||||
# and close the order
|
||||
trade.amount = order['amount'] - order['remaining']
|
||||
trade.stake_amount = trade.amount * trade.open_rate
|
||||
trade.open_order_id = None
|
||||
logger.info('Partial buy order timeout for %s.', trade)
|
||||
self.rpc.send_msg('*Timeout:* Remaining buy order for {} cancelled'.format(
|
||||
trade.pair.replace('_', '/')))
|
||||
return False
|
||||
|
||||
# FIX: 20180110, should cancel_order() be cond. or unconditionally called?
|
||||
def handle_timedout_limit_sell(self, trade: Trade, order: Dict) -> bool:
|
||||
"""
|
||||
Sell timeout - cancel order and update trade
|
||||
:return: True if order was fully cancelled
|
||||
"""
|
||||
if order['remaining'] == order['amount']:
|
||||
# if trade is not partially completed, just cancel the trade
|
||||
exchange.cancel_order(trade.open_order_id)
|
||||
trade.close_rate = None
|
||||
trade.close_profit = None
|
||||
trade.close_date = None
|
||||
trade.is_open = True
|
||||
trade.open_order_id = None
|
||||
self.rpc.send_msg('*Timeout:* Unfilled sell order for {} cancelled'.format(
|
||||
trade.pair.replace('_', '/')))
|
||||
logger.info('Sell order timeout for %s.', trade)
|
||||
return True
|
||||
|
||||
# TODO: figure out how to handle partially complete sell orders
|
||||
return False
|
||||
|
||||
def execute_sell(self, trade: Trade, limit: float) -> None:
|
||||
"""
|
||||
Executes a limit sell for the given trade and limit
|
||||
:param trade: Trade instance
|
||||
:param limit: limit rate for the sell order
|
||||
:return: None
|
||||
"""
|
||||
# Execute sell and update trade record
|
||||
order_id = exchange.sell(str(trade.pair), limit, trade.amount)
|
||||
trade.open_order_id = order_id
|
||||
|
||||
fmt_exp_profit = round(trade.calc_profit_percent(rate=limit) * 100, 2)
|
||||
profit_trade = trade.calc_profit(rate=limit)
|
||||
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
||||
profit = trade.calc_profit_percent(current_rate)
|
||||
|
||||
message = "*{exchange}:* Selling\n" \
|
||||
"*Current Pair:* [{pair}]({pair_url})\n" \
|
||||
"*Limit:* `{limit}`\n" \
|
||||
"*Amount:* `{amount}`\n" \
|
||||
"*Open Rate:* `{open_rate:.8f}`\n" \
|
||||
"*Current Rate:* `{current_rate:.8f}`\n" \
|
||||
"*Profit:* `{profit:.2f}%`" \
|
||||
"".format(
|
||||
exchange=trade.exchange,
|
||||
pair=trade.pair,
|
||||
pair_url=exchange.get_pair_detail_url(trade.pair),
|
||||
limit=limit,
|
||||
open_rate=trade.open_rate,
|
||||
current_rate=current_rate,
|
||||
amount=round(trade.amount, 8),
|
||||
profit=round(profit * 100, 2),
|
||||
)
|
||||
|
||||
# For regular case, when the configuration exists
|
||||
if 'stake_currency' in self.config and 'fiat_display_currency' in self.config:
|
||||
fiat_converter = CryptoToFiatConverter()
|
||||
profit_fiat = fiat_converter.convert_amount(
|
||||
profit_trade,
|
||||
self.config['stake_currency'],
|
||||
self.config['fiat_display_currency']
|
||||
)
|
||||
message += '` ({gain}: {profit_percent:.2f}%, {profit_coin:.8f} {coin}`' \
|
||||
'` / {profit_fiat:.3f} {fiat})`' \
|
||||
''.format(
|
||||
gain="profit" if fmt_exp_profit > 0 else "loss",
|
||||
profit_percent=fmt_exp_profit,
|
||||
profit_coin=profit_trade,
|
||||
coin=self.config['stake_currency'],
|
||||
profit_fiat=profit_fiat,
|
||||
fiat=self.config['fiat_display_currency'],
|
||||
)
|
||||
# Because telegram._forcesell does not have the configuration
|
||||
# Ignore the FIAT value and does not show the stake_currency as well
|
||||
else:
|
||||
message += '` ({gain}: {profit_percent:.2f}%, {profit_coin:.8f})`'.format(
|
||||
gain="profit" if fmt_exp_profit > 0 else "loss",
|
||||
profit_percent=fmt_exp_profit,
|
||||
profit_coin=profit_trade
|
||||
)
|
||||
|
||||
# Send the message
|
||||
self.rpc.send_msg(message)
|
||||
Trade.session.flush()
|
||||
40
freqtrade/indicator_helpers.py
Normal file
40
freqtrade/indicator_helpers.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from math import exp, pi, sqrt, cos
|
||||
|
||||
import numpy as np
|
||||
import talib as ta
|
||||
from pandas import Series
|
||||
|
||||
|
||||
def went_up(series: Series) -> bool:
|
||||
return series > series.shift(1)
|
||||
|
||||
|
||||
def went_down(series: Series) -> bool:
|
||||
return series < series.shift(1)
|
||||
|
||||
|
||||
def ehlers_super_smoother(series: Series, smoothing: float = 6) -> type(Series):
|
||||
magic = pi * sqrt(2) / smoothing
|
||||
a1 = exp(-magic)
|
||||
coeff2 = 2 * a1 * cos(magic)
|
||||
coeff3 = -a1 * a1
|
||||
coeff1 = (1 - coeff2 - coeff3) / 2
|
||||
|
||||
filtered = series.copy()
|
||||
|
||||
for i in range(2, len(series)):
|
||||
filtered.iloc[i] = coeff1 * (series.iloc[i] + series.iloc[i-1]) + \
|
||||
coeff2 * filtered.iloc[i-1] + coeff3 * filtered.iloc[i-2]
|
||||
|
||||
return filtered
|
||||
|
||||
|
||||
def fishers_inverse(series: Series, smoothing: float = 0) -> np.ndarray:
|
||||
""" Does a smoothed fishers inverse transformation.
|
||||
Can be used with any oscillator that goes from 0 to 100 like RSI or MFI """
|
||||
v1 = 0.1 * (series - 50)
|
||||
if smoothing > 0:
|
||||
v2 = ta.WMA(v1.values, timeperiod=smoothing)
|
||||
else:
|
||||
v2 = v1
|
||||
return (np.exp(2 * v2)-1) / (np.exp(2 * v2) + 1)
|
||||
69
freqtrade/main.py
Executable file
69
freqtrade/main.py
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Main Freqtrade bot script.
|
||||
Read the documentation to know what cli arguments you need.
|
||||
"""
|
||||
import logging
|
||||
import sys
|
||||
from typing import List
|
||||
|
||||
from freqtrade.arguments import Arguments
|
||||
from freqtrade.configuration import Configuration
|
||||
from freqtrade.freqtradebot import FreqtradeBot
|
||||
|
||||
logger = logging.getLogger('freqtrade')
|
||||
|
||||
|
||||
def main(sysargv: List[str]) -> None:
|
||||
"""
|
||||
This function will initiate the bot and start the trading loop.
|
||||
:return: None
|
||||
"""
|
||||
arguments = Arguments(
|
||||
sysargv,
|
||||
'Simple High Frequency Trading Bot for crypto currencies'
|
||||
)
|
||||
args = arguments.get_parsed_arg()
|
||||
|
||||
# A subcommand has been issued.
|
||||
# Means if Backtesting or Hyperopt have been called we exit the bot
|
||||
if hasattr(args, 'func'):
|
||||
args.func(args)
|
||||
return
|
||||
|
||||
freqtrade = None
|
||||
return_code = 1
|
||||
try:
|
||||
# Load and validate configuration
|
||||
config = Configuration(args).get_config()
|
||||
|
||||
# Init the bot
|
||||
freqtrade = FreqtradeBot(config)
|
||||
|
||||
state = None
|
||||
while 1:
|
||||
state = freqtrade.worker(old_state=state)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logger.info('SIGINT received, aborting ...')
|
||||
return_code = 0
|
||||
except BaseException:
|
||||
logger.exception('Fatal exception!')
|
||||
finally:
|
||||
if freqtrade:
|
||||
freqtrade.clean()
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def set_loggers() -> None:
|
||||
"""
|
||||
Set the logger level for Third party libs
|
||||
:return: None
|
||||
"""
|
||||
logging.getLogger('requests.packages.urllib3').setLevel(logging.INFO)
|
||||
logging.getLogger('telegram').setLevel(logging.INFO)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
set_loggers()
|
||||
main(sys.argv[1:])
|
||||
74
freqtrade/misc.py
Normal file
74
freqtrade/misc.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
Various tool function for Freqtrade and scripts
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import Dict
|
||||
|
||||
import numpy as np
|
||||
from pandas import DataFrame
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def shorten_date(_date: str) -> str:
|
||||
"""
|
||||
Trim the date so it fits on small screens
|
||||
"""
|
||||
new_date = re.sub('seconds?', 'sec', _date)
|
||||
new_date = re.sub('minutes?', 'min', new_date)
|
||||
new_date = re.sub('hours?', 'h', new_date)
|
||||
new_date = re.sub('days?', 'd', new_date)
|
||||
new_date = re.sub('^an?', '1', new_date)
|
||||
return new_date
|
||||
|
||||
|
||||
############################################
|
||||
# Used by scripts #
|
||||
# Matplotlib doesn't support ::datetime64, #
|
||||
# so we need to convert it into ::datetime #
|
||||
############################################
|
||||
def datesarray_to_datetimearray(dates: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
Convert an pandas-array of timestamps into
|
||||
An numpy-array of datetimes
|
||||
:return: numpy-array of datetime
|
||||
"""
|
||||
times = []
|
||||
dates = dates.astype(datetime)
|
||||
for index in range(0, dates.size):
|
||||
date = dates[index].to_pydatetime()
|
||||
times.append(date)
|
||||
return np.array(times)
|
||||
|
||||
|
||||
def common_datearray(dfs: Dict[str, DataFrame]) -> np.ndarray:
|
||||
"""
|
||||
Return dates from Dataframe
|
||||
:param dfs: Dict with format pair: pair_data
|
||||
:return: List of dates
|
||||
"""
|
||||
alldates = {}
|
||||
for pair, pair_data in dfs.items():
|
||||
dates = datesarray_to_datetimearray(pair_data['date'])
|
||||
for date in dates:
|
||||
alldates[date] = 1
|
||||
lst = []
|
||||
for date, _ in alldates.items():
|
||||
lst.append(date)
|
||||
arr = np.array(lst)
|
||||
return np.sort(arr, axis=0)
|
||||
|
||||
|
||||
def file_dump_json(filename, data) -> None:
|
||||
"""
|
||||
Dump JSON data into a file
|
||||
:param filename: file to create
|
||||
:param data: JSON Data to save
|
||||
:return:
|
||||
"""
|
||||
with open(filename, 'w') as fp:
|
||||
json.dump(data, fp, default=str)
|
||||
148
freqtrade/optimize/__init__.py
Normal file
148
freqtrade/optimize/__init__.py
Normal file
@@ -0,0 +1,148 @@
|
||||
# pragma pylint: disable=missing-docstring
|
||||
|
||||
import gzip
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional, List, Dict, Tuple
|
||||
|
||||
from freqtrade import misc
|
||||
from freqtrade.exchange import get_ticker_history
|
||||
from user_data.hyperopt_conf import hyperopt_optimize_conf
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def trim_tickerlist(tickerlist: List[Dict], timerange: Tuple[Tuple, int, int]) -> List[Dict]:
|
||||
stype, start, stop = timerange
|
||||
if stype == (None, 'line'):
|
||||
return tickerlist[stop:]
|
||||
elif stype == ('line', None):
|
||||
return tickerlist[0:start]
|
||||
elif stype == ('index', 'index'):
|
||||
return tickerlist[start:stop]
|
||||
|
||||
return tickerlist
|
||||
|
||||
|
||||
def load_tickerdata_file(
|
||||
datadir: str, pair: str,
|
||||
ticker_interval: int,
|
||||
timerange: Optional[Tuple[Tuple, int, int]] = None) -> Optional[List[Dict]]:
|
||||
"""
|
||||
Load a pair from file,
|
||||
:return dict OR empty if unsuccesful
|
||||
"""
|
||||
path = make_testdata_path(datadir)
|
||||
file = os.path.join(path, '{pair}-{ticker_interval}.json'.format(
|
||||
pair=pair,
|
||||
ticker_interval=ticker_interval,
|
||||
))
|
||||
gzipfile = file + '.gz'
|
||||
|
||||
# If the file does not exist we download it when None is returned.
|
||||
# If file exists, read the file, load the json
|
||||
if os.path.isfile(gzipfile):
|
||||
logger.debug('Loading ticker data from file %s', gzipfile)
|
||||
with gzip.open(gzipfile) as tickerdata:
|
||||
pairdata = json.load(tickerdata)
|
||||
elif os.path.isfile(file):
|
||||
logger.debug('Loading ticker data from file %s', file)
|
||||
with open(file) as tickerdata:
|
||||
pairdata = json.load(tickerdata)
|
||||
else:
|
||||
return None
|
||||
|
||||
if timerange:
|
||||
pairdata = trim_tickerlist(pairdata, timerange)
|
||||
return pairdata
|
||||
|
||||
|
||||
def load_data(datadir: str, ticker_interval: int,
|
||||
pairs: Optional[List[str]] = None,
|
||||
refresh_pairs: Optional[bool] = False,
|
||||
timerange: Optional[Tuple[Tuple, int, int]] = None) -> Dict[str, List]:
|
||||
"""
|
||||
Loads ticker history data for the given parameters
|
||||
:return: dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
_pairs = pairs or hyperopt_optimize_conf()['exchange']['pair_whitelist']
|
||||
|
||||
# If the user force the refresh of pairs
|
||||
if refresh_pairs:
|
||||
logger.info('Download data for all pairs and store them in %s', datadir)
|
||||
download_pairs(datadir, _pairs, ticker_interval)
|
||||
|
||||
for pair in _pairs:
|
||||
pairdata = load_tickerdata_file(datadir, pair, ticker_interval, timerange=timerange)
|
||||
if not pairdata:
|
||||
# download the tickerdata from exchange
|
||||
download_backtesting_testdata(datadir, pair=pair, interval=ticker_interval)
|
||||
# and retry reading the pair
|
||||
pairdata = load_tickerdata_file(datadir, pair, ticker_interval, timerange=timerange)
|
||||
result[pair] = pairdata
|
||||
return result
|
||||
|
||||
|
||||
def make_testdata_path(datadir: str) -> str:
|
||||
"""Return the path where testdata files are stored"""
|
||||
return datadir or os.path.abspath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__), '..', 'tests', 'testdata'
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def download_pairs(datadir, pairs: List[str], ticker_interval: int) -> bool:
|
||||
"""For each pairs passed in parameters, download the ticker intervals"""
|
||||
for pair in pairs:
|
||||
try:
|
||||
download_backtesting_testdata(datadir, pair=pair, interval=ticker_interval)
|
||||
except BaseException:
|
||||
logger.info(
|
||||
'Failed to download the pair: "%s", Interval: %s min',
|
||||
pair,
|
||||
ticker_interval
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# FIX: 20180110, suggest rename interval to tick_interval
|
||||
def download_backtesting_testdata(datadir: str, pair: str, interval: int = 5) -> None:
|
||||
"""
|
||||
Download the latest 1 and 5 ticker intervals from Bittrex for the pairs passed in parameters
|
||||
Based on @Rybolov work: https://github.com/rybolov/freqtrade-data
|
||||
"""
|
||||
|
||||
path = make_testdata_path(datadir)
|
||||
logger.info(
|
||||
'Download the pair: "%s", Interval: %s min', pair, interval
|
||||
)
|
||||
|
||||
filename = os.path.join(path, '{pair}-{interval}.json'.format(
|
||||
pair=pair.replace("-", "_"),
|
||||
interval=interval,
|
||||
))
|
||||
|
||||
if os.path.isfile(filename):
|
||||
with open(filename, "rt") as file:
|
||||
data = json.load(file)
|
||||
else:
|
||||
data = []
|
||||
|
||||
logger.debug('Current Start: %s', data[1]['T'] if data else None)
|
||||
logger.debug('Current End: %s', data[-1:][0]['T'] if data else None)
|
||||
|
||||
# Extend data with new ticker history
|
||||
data.extend([
|
||||
row for row in get_ticker_history(pair=pair, tick_interval=int(interval))
|
||||
if row not in data
|
||||
])
|
||||
|
||||
data = sorted(data, key=lambda _data: _data['T'])
|
||||
logger.debug('New Start: %s', data[1]['T'])
|
||||
logger.debug('New End: %s', data[-1:][0]['T'])
|
||||
misc.file_dump_json(filename, data)
|
||||
307
freqtrade/optimize/backtesting.py
Normal file
307
freqtrade/optimize/backtesting.py
Normal file
@@ -0,0 +1,307 @@
|
||||
# pragma pylint: disable=missing-docstring, W0212, too-many-arguments
|
||||
|
||||
"""
|
||||
This module contains the backtesting logic
|
||||
"""
|
||||
import logging
|
||||
import operator
|
||||
from argparse import Namespace
|
||||
from typing import Dict, Tuple, Any, List, Optional
|
||||
|
||||
import arrow
|
||||
from pandas import DataFrame
|
||||
from tabulate import tabulate
|
||||
|
||||
import freqtrade.optimize as optimize
|
||||
from freqtrade import exchange
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.arguments import Arguments
|
||||
from freqtrade.configuration import Configuration
|
||||
from freqtrade.exchange import Bittrex
|
||||
from freqtrade.misc import file_dump_json
|
||||
from freqtrade.persistence import Trade
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Backtesting(object):
|
||||
"""
|
||||
Backtesting class, this class contains all the logic to run a backtest
|
||||
|
||||
To run a backtest:
|
||||
backtesting = Backtesting(config)
|
||||
backtesting.start()
|
||||
"""
|
||||
def __init__(self, config: Dict[str, Any]) -> None:
|
||||
self.config = config
|
||||
self.analyze = None
|
||||
self.ticker_interval = None
|
||||
self.tickerdata_to_dataframe = None
|
||||
self.populate_buy_trend = None
|
||||
self.populate_sell_trend = None
|
||||
self._init()
|
||||
|
||||
def _init(self) -> None:
|
||||
"""
|
||||
Init objects required for backtesting
|
||||
:return: None
|
||||
"""
|
||||
self.analyze = Analyze(self.config)
|
||||
self.ticker_interval = self.analyze.strategy.ticker_interval
|
||||
self.tickerdata_to_dataframe = self.analyze.tickerdata_to_dataframe
|
||||
self.populate_buy_trend = self.analyze.populate_buy_trend
|
||||
self.populate_sell_trend = self.analyze.populate_sell_trend
|
||||
exchange._API = Bittrex({'key': '', 'secret': ''})
|
||||
|
||||
@staticmethod
|
||||
def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]:
|
||||
"""
|
||||
Get the maximum timeframe for the given backtest data
|
||||
:param data: dictionary with preprocessed backtesting data
|
||||
:return: tuple containing min_date, max_date
|
||||
"""
|
||||
timeframe = [
|
||||
(arrow.get(min(frame.date)), arrow.get(max(frame.date)))
|
||||
for frame in data.values()
|
||||
]
|
||||
return min(timeframe, key=operator.itemgetter(0))[0], \
|
||||
max(timeframe, key=operator.itemgetter(1))[1]
|
||||
|
||||
def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str:
|
||||
"""
|
||||
Generates and returns a text table for the given backtest data and the results dataframe
|
||||
:return: pretty printed table with tabulate as str
|
||||
"""
|
||||
stake_currency = self.config.get('stake_currency')
|
||||
|
||||
floatfmt = ('s', 'd', '.2f', '.8f', '.1f')
|
||||
tabular_data = []
|
||||
headers = ['pair', 'buy count', 'avg profit %',
|
||||
'total profit ' + stake_currency, 'avg duration', 'profit', 'loss']
|
||||
for pair in data:
|
||||
result = results[results.currency == pair]
|
||||
tabular_data.append([
|
||||
pair,
|
||||
len(result.index),
|
||||
result.profit_percent.mean() * 100.0,
|
||||
result.profit_BTC.sum(),
|
||||
result.duration.mean(),
|
||||
len(result[result.profit_BTC > 0]),
|
||||
len(result[result.profit_BTC < 0])
|
||||
])
|
||||
|
||||
# Append Total
|
||||
tabular_data.append([
|
||||
'TOTAL',
|
||||
len(results.index),
|
||||
results.profit_percent.mean() * 100.0,
|
||||
results.profit_BTC.sum(),
|
||||
results.duration.mean(),
|
||||
len(results[results.profit_BTC > 0]),
|
||||
len(results[results.profit_BTC < 0])
|
||||
])
|
||||
return tabulate(tabular_data, headers=headers, floatfmt=floatfmt)
|
||||
|
||||
def _get_sell_trade_entry(
|
||||
self, pair: str, buy_row: DataFrame,
|
||||
partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]:
|
||||
|
||||
stake_amount = args['stake_amount']
|
||||
max_open_trades = args.get('max_open_trades', 0)
|
||||
trade = Trade(
|
||||
open_rate=buy_row.close,
|
||||
open_date=buy_row.date,
|
||||
stake_amount=stake_amount,
|
||||
amount=stake_amount / buy_row.open,
|
||||
fee=exchange.get_fee()
|
||||
)
|
||||
|
||||
# calculate win/lose forwards from buy point
|
||||
for sell_row in partial_ticker:
|
||||
if max_open_trades > 0:
|
||||
# Increase trade_count_lock for every iteration
|
||||
trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1
|
||||
|
||||
buy_signal = sell_row.buy
|
||||
if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal,
|
||||
sell_row.sell):
|
||||
return \
|
||||
sell_row, \
|
||||
(
|
||||
pair,
|
||||
trade.calc_profit_percent(rate=sell_row.close),
|
||||
trade.calc_profit(rate=sell_row.close),
|
||||
(sell_row.date - buy_row.date).seconds // 60
|
||||
), \
|
||||
sell_row.date
|
||||
return None
|
||||
|
||||
def backtest(self, args: Dict) -> DataFrame:
|
||||
"""
|
||||
Implements backtesting functionality
|
||||
|
||||
NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized.
|
||||
Of course try to not have ugly code. By some accessor are sometime slower than functions.
|
||||
Avoid, logging on this method
|
||||
|
||||
:param args: a dict containing:
|
||||
stake_amount: btc amount to use for each trade
|
||||
processed: a processed dictionary with format {pair, data}
|
||||
max_open_trades: maximum number of concurrent trades (default: 0, disabled)
|
||||
realistic: do we try to simulate realistic trades? (default: True)
|
||||
sell_profit_only: sell if profit only
|
||||
use_sell_signal: act on sell-signal
|
||||
:return: DataFrame
|
||||
"""
|
||||
headers = ['date', 'buy', 'open', 'close', 'sell']
|
||||
processed = args['processed']
|
||||
max_open_trades = args.get('max_open_trades', 0)
|
||||
realistic = args.get('realistic', False)
|
||||
record = args.get('record', None)
|
||||
records = []
|
||||
trades = []
|
||||
trade_count_lock = {}
|
||||
for pair, pair_data in processed.items():
|
||||
pair_data['buy'], pair_data['sell'] = 0, 0 # cleanup from previous run
|
||||
|
||||
ticker_data = self.populate_sell_trend(self.populate_buy_trend(pair_data))[headers]
|
||||
ticker = [x for x in ticker_data.itertuples()]
|
||||
|
||||
lock_pair_until = None
|
||||
for index, row in enumerate(ticker):
|
||||
if row.buy == 0 or row.sell == 1:
|
||||
continue # skip rows where no buy signal or that would immediately sell off
|
||||
|
||||
if realistic:
|
||||
if lock_pair_until is not None and row.date <= lock_pair_until:
|
||||
continue
|
||||
if max_open_trades > 0:
|
||||
# Check if max_open_trades has already been reached for the given date
|
||||
if not trade_count_lock.get(row.date, 0) < max_open_trades:
|
||||
continue
|
||||
|
||||
trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1
|
||||
|
||||
ret = self._get_sell_trade_entry(pair, row, ticker[index + 1:],
|
||||
trade_count_lock, args)
|
||||
|
||||
if ret:
|
||||
row2, trade_entry, next_date = ret
|
||||
lock_pair_until = next_date
|
||||
trades.append(trade_entry)
|
||||
if record:
|
||||
# Note, need to be json.dump friendly
|
||||
# record a tuple of pair, current_profit_percent,
|
||||
# entry-date, duration
|
||||
records.append((pair, trade_entry[1],
|
||||
row.date.strftime('%s'),
|
||||
row2.date.strftime('%s'),
|
||||
index, trade_entry[3]))
|
||||
# For now export inside backtest(), maybe change so that backtest()
|
||||
# returns a tuple like: (dataframe, records, logs, etc)
|
||||
if record and record.find('trades') >= 0:
|
||||
logger.info('Dumping backtest results')
|
||||
file_dump_json('backtest-result.json', records)
|
||||
labels = ['currency', 'profit_percent', 'profit_BTC', 'duration']
|
||||
return DataFrame.from_records(trades, columns=labels)
|
||||
|
||||
def start(self) -> None:
|
||||
"""
|
||||
Run a backtesting end-to-end
|
||||
:return: None
|
||||
"""
|
||||
data = {}
|
||||
pairs = self.config['exchange']['pair_whitelist']
|
||||
logger.info('Using stake_currency: %s ...', self.config['stake_currency'])
|
||||
logger.info('Using stake_amount: %s ...', self.config['stake_amount'])
|
||||
|
||||
if self.config.get('live'):
|
||||
logger.info('Downloading data for all pairs in whitelist ...')
|
||||
for pair in pairs:
|
||||
data[pair] = exchange.get_ticker_history(pair, self.ticker_interval)
|
||||
else:
|
||||
logger.info('Using local backtesting data (using whitelist in given config) ...')
|
||||
|
||||
timerange = Arguments.parse_timerange(self.config.get('timerange'))
|
||||
data = optimize.load_data(
|
||||
self.config['datadir'],
|
||||
pairs=pairs,
|
||||
ticker_interval=self.ticker_interval,
|
||||
refresh_pairs=self.config.get('refresh_pairs', False),
|
||||
timerange=timerange
|
||||
)
|
||||
|
||||
# Ignore max_open_trades in backtesting, except realistic flag was passed
|
||||
if self.config.get('realistic_simulation', False):
|
||||
max_open_trades = self.config['max_open_trades']
|
||||
else:
|
||||
logger.info('Ignoring max_open_trades (realistic_simulation not set) ...')
|
||||
max_open_trades = 0
|
||||
|
||||
preprocessed = self.tickerdata_to_dataframe(data)
|
||||
|
||||
# Print timeframe
|
||||
min_date, max_date = self.get_timeframe(preprocessed)
|
||||
logger.info(
|
||||
'Measuring data from %s up to %s (%s days)..',
|
||||
min_date.isoformat(),
|
||||
max_date.isoformat(),
|
||||
(max_date - min_date).days
|
||||
)
|
||||
|
||||
# Execute backtest and print results
|
||||
sell_profit_only = self.config.get('experimental', {}).get('sell_profit_only', False)
|
||||
use_sell_signal = self.config.get('experimental', {}).get('use_sell_signal', False)
|
||||
results = self.backtest(
|
||||
{
|
||||
'stake_amount': self.config.get('stake_amount'),
|
||||
'processed': preprocessed,
|
||||
'max_open_trades': max_open_trades,
|
||||
'realistic': self.config.get('realistic_simulation', False),
|
||||
'sell_profit_only': sell_profit_only,
|
||||
'use_sell_signal': use_sell_signal,
|
||||
'record': self.config.get('export')
|
||||
}
|
||||
)
|
||||
logger.info(
|
||||
'\n==================================== '
|
||||
'BACKTESTING REPORT'
|
||||
' ====================================\n'
|
||||
'%s',
|
||||
self._generate_text_table(
|
||||
data,
|
||||
results
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def setup_configuration(args: Namespace) -> Dict[str, Any]:
|
||||
"""
|
||||
Prepare the configuration for the backtesting
|
||||
:param args: Cli args from Arguments()
|
||||
:return: Configuration
|
||||
"""
|
||||
configuration = Configuration(args)
|
||||
config = configuration.get_config()
|
||||
|
||||
# Ensure we do not use Exchange credentials
|
||||
config['exchange']['key'] = ''
|
||||
config['exchange']['secret'] = ''
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def start(args: Namespace) -> None:
|
||||
"""
|
||||
Start Backtesting script
|
||||
:param args: Cli args from Arguments()
|
||||
:return: None
|
||||
"""
|
||||
# Initialize configuration
|
||||
config = setup_configuration(args)
|
||||
logger.info('Starting freqtrade in Backtesting mode')
|
||||
|
||||
# Initialize backtesting object
|
||||
backtesting = Backtesting(config)
|
||||
backtesting.start()
|
||||
608
freqtrade/optimize/hyperopt.py
Normal file
608
freqtrade/optimize/hyperopt.py
Normal file
@@ -0,0 +1,608 @@
|
||||
# pragma pylint: disable=too-many-instance-attributes, pointless-string-statement
|
||||
|
||||
"""
|
||||
This module contains the hyperopt logic
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import pickle
|
||||
import signal
|
||||
import sys
|
||||
from argparse import Namespace
|
||||
from functools import reduce
|
||||
from math import exp
|
||||
from operator import itemgetter
|
||||
from typing import Dict, Any, Callable
|
||||
|
||||
import numpy
|
||||
import talib.abstract as ta
|
||||
from hyperopt import STATUS_FAIL, STATUS_OK, Trials, fmin, hp, space_eval, tpe
|
||||
from hyperopt.mongoexp import MongoTrials
|
||||
from pandas import DataFrame
|
||||
|
||||
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
||||
from freqtrade.arguments import Arguments
|
||||
from freqtrade.configuration import Configuration
|
||||
from freqtrade.optimize import load_data
|
||||
from freqtrade.optimize.backtesting import Backtesting
|
||||
from user_data.hyperopt_conf import hyperopt_optimize_conf
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Hyperopt(Backtesting):
|
||||
"""
|
||||
Hyperopt class, this class contains all the logic to run a hyperopt simulation
|
||||
|
||||
To run a backtest:
|
||||
hyperopt = Hyperopt(config)
|
||||
hyperopt.start()
|
||||
"""
|
||||
def __init__(self, config: Dict[str, Any]) -> None:
|
||||
|
||||
super().__init__(config)
|
||||
# set TARGET_TRADES to suit your number concurrent trades so its realistic
|
||||
# to the number of days
|
||||
self.target_trades = 600
|
||||
self.total_tries = config.get('epochs', 0)
|
||||
self.current_tries = 0
|
||||
self.current_best_loss = 100
|
||||
|
||||
# max average trade duration in minutes
|
||||
# if eval ends with higher value, we consider it a failed eval
|
||||
self.max_accepted_trade_duration = 300
|
||||
|
||||
# this is expexted avg profit * expected trade count
|
||||
# for example 3.5%, 1100 trades, self.expected_max_profit = 3.85
|
||||
# check that the reported Σ% values do not exceed this!
|
||||
self.expected_max_profit = 3.0
|
||||
|
||||
# Configuration and data used by hyperopt
|
||||
self.processed = None
|
||||
|
||||
# Hyperopt Trials
|
||||
self.trials_file = os.path.join('user_data', 'hyperopt_trials.pickle')
|
||||
self.trials = Trials()
|
||||
|
||||
@staticmethod
|
||||
def populate_indicators(dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Adds several different TA indicators to the given DataFrame
|
||||
"""
|
||||
dataframe['adx'] = ta.ADX(dataframe)
|
||||
dataframe['ao'] = qtpylib.awesome_oscillator(dataframe)
|
||||
dataframe['cci'] = ta.CCI(dataframe)
|
||||
macd = ta.MACD(dataframe)
|
||||
dataframe['macd'] = macd['macd']
|
||||
dataframe['macdsignal'] = macd['macdsignal']
|
||||
dataframe['macdhist'] = macd['macdhist']
|
||||
dataframe['mfi'] = ta.MFI(dataframe)
|
||||
dataframe['minus_dm'] = ta.MINUS_DM(dataframe)
|
||||
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
|
||||
dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
|
||||
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
|
||||
dataframe['roc'] = ta.ROC(dataframe)
|
||||
dataframe['rsi'] = ta.RSI(dataframe)
|
||||
# Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy)
|
||||
rsi = 0.1 * (dataframe['rsi'] - 50)
|
||||
dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1)
|
||||
# Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy)
|
||||
dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1)
|
||||
# Stoch
|
||||
stoch = ta.STOCH(dataframe)
|
||||
dataframe['slowd'] = stoch['slowd']
|
||||
dataframe['slowk'] = stoch['slowk']
|
||||
# Stoch fast
|
||||
stoch_fast = ta.STOCHF(dataframe)
|
||||
dataframe['fastd'] = stoch_fast['fastd']
|
||||
dataframe['fastk'] = stoch_fast['fastk']
|
||||
# Stoch RSI
|
||||
stoch_rsi = ta.STOCHRSI(dataframe)
|
||||
dataframe['fastd_rsi'] = stoch_rsi['fastd']
|
||||
dataframe['fastk_rsi'] = stoch_rsi['fastk']
|
||||
# Bollinger bands
|
||||
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
||||
dataframe['bb_lowerband'] = bollinger['lower']
|
||||
dataframe['bb_middleband'] = bollinger['mid']
|
||||
dataframe['bb_upperband'] = bollinger['upper']
|
||||
# EMA - Exponential Moving Average
|
||||
dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3)
|
||||
dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5)
|
||||
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
|
||||
dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
|
||||
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
|
||||
# SAR Parabolic
|
||||
dataframe['sar'] = ta.SAR(dataframe)
|
||||
# SMA - Simple Moving Average
|
||||
dataframe['sma'] = ta.SMA(dataframe, timeperiod=40)
|
||||
# TEMA - Triple Exponential Moving Average
|
||||
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
|
||||
# Hilbert Transform Indicator - SineWave
|
||||
hilbert = ta.HT_SINE(dataframe)
|
||||
dataframe['htsine'] = hilbert['sine']
|
||||
dataframe['htleadsine'] = hilbert['leadsine']
|
||||
|
||||
# Pattern Recognition - Bullish candlestick patterns
|
||||
# ------------------------------------
|
||||
"""
|
||||
# Hammer: values [0, 100]
|
||||
dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe)
|
||||
# Inverted Hammer: values [0, 100]
|
||||
dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe)
|
||||
# Dragonfly Doji: values [0, 100]
|
||||
dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe)
|
||||
# Piercing Line: values [0, 100]
|
||||
dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100]
|
||||
# Morningstar: values [0, 100]
|
||||
dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100]
|
||||
# Three White Soldiers: values [0, 100]
|
||||
dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100]
|
||||
"""
|
||||
|
||||
# Pattern Recognition - Bearish candlestick patterns
|
||||
# ------------------------------------
|
||||
"""
|
||||
# Hanging Man: values [0, 100]
|
||||
dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe)
|
||||
# Shooting Star: values [0, 100]
|
||||
dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe)
|
||||
# Gravestone Doji: values [0, 100]
|
||||
dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe)
|
||||
# Dark Cloud Cover: values [0, 100]
|
||||
dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe)
|
||||
# Evening Doji Star: values [0, 100]
|
||||
dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe)
|
||||
# Evening Star: values [0, 100]
|
||||
dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe)
|
||||
"""
|
||||
|
||||
# Pattern Recognition - Bullish/Bearish candlestick patterns
|
||||
# ------------------------------------
|
||||
"""
|
||||
# Three Line Strike: values [0, -100, 100]
|
||||
dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe)
|
||||
# Spinning Top: values [0, -100, 100]
|
||||
dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100]
|
||||
# Engulfing: values [0, -100, 100]
|
||||
dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100]
|
||||
# Harami: values [0, -100, 100]
|
||||
dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100]
|
||||
# Three Outside Up/Down: values [0, -100, 100]
|
||||
dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100]
|
||||
# Three Inside Up/Down: values [0, -100, 100]
|
||||
dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100]
|
||||
"""
|
||||
|
||||
# Chart type
|
||||
# ------------------------------------
|
||||
# Heikinashi stategy
|
||||
heikinashi = qtpylib.heikinashi(dataframe)
|
||||
dataframe['ha_open'] = heikinashi['open']
|
||||
dataframe['ha_close'] = heikinashi['close']
|
||||
dataframe['ha_high'] = heikinashi['high']
|
||||
dataframe['ha_low'] = heikinashi['low']
|
||||
|
||||
return dataframe
|
||||
|
||||
def save_trials(self) -> None:
|
||||
"""
|
||||
Save hyperopt trials to file
|
||||
"""
|
||||
logger.info('Saving Trials to \'%s\'', self.trials_file)
|
||||
pickle.dump(self.trials, open(self.trials_file, 'wb'))
|
||||
|
||||
def read_trials(self) -> Trials:
|
||||
"""
|
||||
Read hyperopt trials file
|
||||
"""
|
||||
logger.info('Reading Trials from \'%s\'', self.trials_file)
|
||||
trials = pickle.load(open(self.trials_file, 'rb'))
|
||||
os.remove(self.trials_file)
|
||||
return trials
|
||||
|
||||
def log_trials_result(self) -> None:
|
||||
"""
|
||||
Display Best hyperopt result
|
||||
"""
|
||||
vals = json.dumps(self.trials.best_trial['misc']['vals'], indent=4)
|
||||
results = self.trials.best_trial['result']['result']
|
||||
logger.info('Best result:\n%s\nwith values:\n%s', results, vals)
|
||||
|
||||
def log_results(self, results) -> None:
|
||||
"""
|
||||
Log results if it is better than any previous evaluation
|
||||
"""
|
||||
if results['loss'] < self.current_best_loss:
|
||||
self.current_best_loss = results['loss']
|
||||
log_msg = '\n{:5d}/{}: {}. Loss {:.5f}'.format(
|
||||
results['current_tries'],
|
||||
results['total_tries'],
|
||||
results['result'],
|
||||
results['loss']
|
||||
)
|
||||
print(log_msg)
|
||||
else:
|
||||
print('.', end='')
|
||||
sys.stdout.flush()
|
||||
|
||||
def calculate_loss(self, total_profit: float, trade_count: int, trade_duration: float) -> float:
|
||||
"""
|
||||
Objective function, returns smaller number for more optimal results
|
||||
"""
|
||||
trade_loss = 1 - 0.25 * exp(-(trade_count - self.target_trades) ** 2 / 10 ** 5.8)
|
||||
profit_loss = max(0, 1 - total_profit / self.expected_max_profit)
|
||||
duration_loss = 0.4 * min(trade_duration / self.max_accepted_trade_duration, 1)
|
||||
return trade_loss + profit_loss + duration_loss
|
||||
|
||||
@staticmethod
|
||||
def generate_roi_table(params: Dict) -> Dict[int, float]:
|
||||
"""
|
||||
Generate the ROI table thqt will be used by Hyperopt
|
||||
"""
|
||||
roi_table = {}
|
||||
roi_table[0] = params['roi_p1'] + params['roi_p2'] + params['roi_p3']
|
||||
roi_table[params['roi_t3']] = params['roi_p1'] + params['roi_p2']
|
||||
roi_table[params['roi_t3'] + params['roi_t2']] = params['roi_p1']
|
||||
roi_table[params['roi_t3'] + params['roi_t2'] + params['roi_t1']] = 0
|
||||
|
||||
return roi_table
|
||||
|
||||
@staticmethod
|
||||
def roi_space() -> Dict[str, Any]:
|
||||
"""
|
||||
Values to search for each ROI steps
|
||||
"""
|
||||
return {
|
||||
'roi_t1': hp.quniform('roi_t1', 10, 120, 20),
|
||||
'roi_t2': hp.quniform('roi_t2', 10, 60, 15),
|
||||
'roi_t3': hp.quniform('roi_t3', 10, 40, 10),
|
||||
'roi_p1': hp.quniform('roi_p1', 0.01, 0.04, 0.01),
|
||||
'roi_p2': hp.quniform('roi_p2', 0.01, 0.07, 0.01),
|
||||
'roi_p3': hp.quniform('roi_p3', 0.01, 0.20, 0.01),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def stoploss_space() -> Dict[str, Any]:
|
||||
"""
|
||||
Stoploss Value to search
|
||||
"""
|
||||
return {
|
||||
'stoploss': hp.quniform('stoploss', -0.5, -0.02, 0.02),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def indicator_space() -> Dict[str, Any]:
|
||||
"""
|
||||
Define your Hyperopt space for searching strategy parameters
|
||||
"""
|
||||
return {
|
||||
'macd_below_zero': hp.choice('macd_below_zero', [
|
||||
{'enabled': False},
|
||||
{'enabled': True}
|
||||
]),
|
||||
'mfi': hp.choice('mfi', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('mfi-value', 10, 25, 5)}
|
||||
]),
|
||||
'fastd': hp.choice('fastd', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('fastd-value', 15, 45, 5)}
|
||||
]),
|
||||
'adx': hp.choice('adx', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('adx-value', 20, 50, 5)}
|
||||
]),
|
||||
'rsi': hp.choice('rsi', [
|
||||
{'enabled': False},
|
||||
{'enabled': True, 'value': hp.quniform('rsi-value', 20, 40, 5)}
|
||||
]),
|
||||
'uptrend_long_ema': hp.choice('uptrend_long_ema', [
|
||||
{'enabled': False},
|
||||
{'enabled': True}
|
||||
]),
|
||||
'uptrend_short_ema': hp.choice('uptrend_short_ema', [
|
||||
{'enabled': False},
|
||||
{'enabled': True}
|
||||
]),
|
||||
'over_sar': hp.choice('over_sar', [
|
||||
{'enabled': False},
|
||||
{'enabled': True}
|
||||
]),
|
||||
'green_candle': hp.choice('green_candle', [
|
||||
{'enabled': False},
|
||||
{'enabled': True}
|
||||
]),
|
||||
'uptrend_sma': hp.choice('uptrend_sma', [
|
||||
{'enabled': False},
|
||||
{'enabled': True}
|
||||
]),
|
||||
'trigger': hp.choice('trigger', [
|
||||
{'type': 'lower_bb'},
|
||||
{'type': 'lower_bb_tema'},
|
||||
{'type': 'faststoch10'},
|
||||
{'type': 'ao_cross_zero'},
|
||||
{'type': 'ema3_cross_ema10'},
|
||||
{'type': 'macd_cross_signal'},
|
||||
{'type': 'sar_reversal'},
|
||||
{'type': 'ht_sine'},
|
||||
{'type': 'heiken_reversal_bull'},
|
||||
{'type': 'di_cross'},
|
||||
]),
|
||||
}
|
||||
|
||||
def has_space(self, space: str) -> bool:
|
||||
"""
|
||||
Tell if a space value is contained in the configuration
|
||||
"""
|
||||
if space in self.config['spaces'] or 'all' in self.config['spaces']:
|
||||
return True
|
||||
return False
|
||||
|
||||
def hyperopt_space(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Return the space to use during Hyperopt
|
||||
"""
|
||||
spaces = {}
|
||||
if self.has_space('buy'):
|
||||
spaces = {**spaces, **Hyperopt.indicator_space()}
|
||||
if self.has_space('roi'):
|
||||
spaces = {**spaces, **Hyperopt.roi_space()}
|
||||
if self.has_space('stoploss'):
|
||||
spaces = {**spaces, **Hyperopt.stoploss_space()}
|
||||
return spaces
|
||||
|
||||
@staticmethod
|
||||
def buy_strategy_generator(params: Dict[str, Any]) -> Callable:
|
||||
"""
|
||||
Define the buy strategy parameters to be used by hyperopt
|
||||
"""
|
||||
def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Buy strategy Hyperopt will build and use
|
||||
"""
|
||||
conditions = []
|
||||
# GUARDS AND TRENDS
|
||||
if 'uptrend_long_ema' in params and params['uptrend_long_ema']['enabled']:
|
||||
conditions.append(dataframe['ema50'] > dataframe['ema100'])
|
||||
if 'macd_below_zero' in params and params['macd_below_zero']['enabled']:
|
||||
conditions.append(dataframe['macd'] < 0)
|
||||
if 'uptrend_short_ema' in params and params['uptrend_short_ema']['enabled']:
|
||||
conditions.append(dataframe['ema5'] > dataframe['ema10'])
|
||||
if 'mfi' in params and params['mfi']['enabled']:
|
||||
conditions.append(dataframe['mfi'] < params['mfi']['value'])
|
||||
if 'fastd' in params and params['fastd']['enabled']:
|
||||
conditions.append(dataframe['fastd'] < params['fastd']['value'])
|
||||
if 'adx' in params and params['adx']['enabled']:
|
||||
conditions.append(dataframe['adx'] > params['adx']['value'])
|
||||
if 'rsi' in params and params['rsi']['enabled']:
|
||||
conditions.append(dataframe['rsi'] < params['rsi']['value'])
|
||||
if 'over_sar' in params and params['over_sar']['enabled']:
|
||||
conditions.append(dataframe['close'] > dataframe['sar'])
|
||||
if 'green_candle' in params and params['green_candle']['enabled']:
|
||||
conditions.append(dataframe['close'] > dataframe['open'])
|
||||
if 'uptrend_sma' in params and params['uptrend_sma']['enabled']:
|
||||
prevsma = dataframe['sma'].shift(1)
|
||||
conditions.append(dataframe['sma'] > prevsma)
|
||||
|
||||
# TRIGGERS
|
||||
triggers = {
|
||||
'lower_bb': (
|
||||
dataframe['close'] < dataframe['bb_lowerband']
|
||||
),
|
||||
'lower_bb_tema': (
|
||||
dataframe['tema'] < dataframe['bb_lowerband']
|
||||
),
|
||||
'faststoch10': (qtpylib.crossed_above(
|
||||
dataframe['fastd'], 10.0
|
||||
)),
|
||||
'ao_cross_zero': (qtpylib.crossed_above(
|
||||
dataframe['ao'], 0.0
|
||||
)),
|
||||
'ema3_cross_ema10': (qtpylib.crossed_above(
|
||||
dataframe['ema3'], dataframe['ema10']
|
||||
)),
|
||||
'macd_cross_signal': (qtpylib.crossed_above(
|
||||
dataframe['macd'], dataframe['macdsignal']
|
||||
)),
|
||||
'sar_reversal': (qtpylib.crossed_above(
|
||||
dataframe['close'], dataframe['sar']
|
||||
)),
|
||||
'ht_sine': (qtpylib.crossed_above(
|
||||
dataframe['htleadsine'], dataframe['htsine']
|
||||
)),
|
||||
'heiken_reversal_bull': (
|
||||
(qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) &
|
||||
(dataframe['ha_low'] == dataframe['ha_open'])
|
||||
),
|
||||
'di_cross': (qtpylib.crossed_above(
|
||||
dataframe['plus_di'], dataframe['minus_di']
|
||||
)),
|
||||
}
|
||||
conditions.append(triggers.get(params['trigger']['type']))
|
||||
|
||||
dataframe.loc[
|
||||
reduce(lambda x, y: x & y, conditions),
|
||||
'buy'] = 1
|
||||
|
||||
return dataframe
|
||||
|
||||
return populate_buy_trend
|
||||
|
||||
def generate_optimizer(self, params: Dict) -> Dict:
|
||||
if self.has_space('roi'):
|
||||
self.analyze.strategy.minimal_roi = self.generate_roi_table(params)
|
||||
|
||||
if self.has_space('buy'):
|
||||
self.populate_buy_trend = self.buy_strategy_generator(params)
|
||||
|
||||
if self.has_space('stoploss'):
|
||||
self.analyze.strategy.stoploss = params['stoploss']
|
||||
|
||||
results = self.backtest(
|
||||
{
|
||||
'stake_amount': self.config['stake_amount'],
|
||||
'processed': self.processed,
|
||||
'realistic': self.config.get('realistic_simulation', False),
|
||||
}
|
||||
)
|
||||
result_explanation = self.format_results(results)
|
||||
|
||||
total_profit = results.profit_percent.sum()
|
||||
trade_count = len(results.index)
|
||||
trade_duration = results.duration.mean()
|
||||
|
||||
if trade_count == 0 or trade_duration > self.max_accepted_trade_duration:
|
||||
print('.', end='')
|
||||
return {
|
||||
'status': STATUS_FAIL,
|
||||
'loss': float('inf')
|
||||
}
|
||||
|
||||
loss = self.calculate_loss(total_profit, trade_count, trade_duration)
|
||||
|
||||
self.current_tries += 1
|
||||
|
||||
self.log_results(
|
||||
{
|
||||
'loss': loss,
|
||||
'current_tries': self.current_tries,
|
||||
'total_tries': self.total_tries,
|
||||
'result': result_explanation,
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
'loss': loss,
|
||||
'status': STATUS_OK,
|
||||
'result': result_explanation,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def format_results(results: DataFrame) -> str:
|
||||
"""
|
||||
Return the format result in a string
|
||||
"""
|
||||
return ('{:6d} trades. Avg profit {: 5.2f}%. '
|
||||
'Total profit {: 11.8f} BTC ({:.4f}Σ%). Avg duration {:5.1f} mins.').format(
|
||||
len(results.index),
|
||||
results.profit_percent.mean() * 100.0,
|
||||
results.profit_BTC.sum(),
|
||||
results.profit_percent.sum(),
|
||||
results.duration.mean(),
|
||||
)
|
||||
|
||||
def start(self) -> None:
|
||||
timerange = Arguments.parse_timerange(self.config.get('timerange'))
|
||||
data = load_data(
|
||||
datadir=self.config.get('datadir'),
|
||||
pairs=self.config['exchange']['pair_whitelist'],
|
||||
ticker_interval=self.ticker_interval,
|
||||
timerange=timerange
|
||||
)
|
||||
|
||||
if self.has_space('buy'):
|
||||
self.analyze.populate_indicators = Hyperopt.populate_indicators
|
||||
self.processed = self.tickerdata_to_dataframe(data)
|
||||
|
||||
if self.config.get('mongodb'):
|
||||
logger.info('Using mongodb ...')
|
||||
logger.info(
|
||||
'Start scripts/start-mongodb.sh and start-hyperopt-worker.sh manually!'
|
||||
)
|
||||
|
||||
db_name = 'freqtrade_hyperopt'
|
||||
self.trials = MongoTrials(
|
||||
arg='mongo://127.0.0.1:1234/{}/jobs'.format(db_name),
|
||||
exp_key='exp1'
|
||||
)
|
||||
else:
|
||||
logger.info('Preparing Trials..')
|
||||
signal.signal(signal.SIGINT, self.signal_handler)
|
||||
# read trials file if we have one
|
||||
if os.path.exists(self.trials_file) and os.path.getsize(self.trials_file) > 0:
|
||||
self.trials = self.read_trials()
|
||||
|
||||
self.current_tries = len(self.trials.results)
|
||||
self.total_tries += self.current_tries
|
||||
logger.info(
|
||||
'Continuing with trials. Current: %d, Total: %d',
|
||||
self.current_tries,
|
||||
self.total_tries
|
||||
)
|
||||
|
||||
try:
|
||||
best_parameters = fmin(
|
||||
fn=self.generate_optimizer,
|
||||
space=self.hyperopt_space(),
|
||||
algo=tpe.suggest,
|
||||
max_evals=self.total_tries,
|
||||
trials=self.trials
|
||||
)
|
||||
|
||||
results = sorted(self.trials.results, key=itemgetter('loss'))
|
||||
best_result = results[0]['result']
|
||||
|
||||
except ValueError:
|
||||
best_parameters = {}
|
||||
best_result = 'Sorry, Hyperopt was not able to find good parameters. Please ' \
|
||||
'try with more epochs (param: -e).'
|
||||
|
||||
# Improve best parameter logging display
|
||||
if best_parameters:
|
||||
best_parameters = space_eval(
|
||||
self.hyperopt_space(),
|
||||
best_parameters
|
||||
)
|
||||
|
||||
logger.info('Best parameters:\n%s', json.dumps(best_parameters, indent=4))
|
||||
if 'roi_t1' in best_parameters:
|
||||
logger.info('ROI table:\n%s', self.generate_roi_table(best_parameters))
|
||||
|
||||
logger.info('Best Result:\n%s', best_result)
|
||||
|
||||
# Store trials result to file to resume next time
|
||||
self.save_trials()
|
||||
|
||||
def signal_handler(self, sig, frame) -> None:
|
||||
"""
|
||||
Hyperopt SIGINT handler
|
||||
"""
|
||||
logger.info(
|
||||
'Hyperopt received %s',
|
||||
signal.Signals(sig).name
|
||||
)
|
||||
|
||||
self.save_trials()
|
||||
self.log_trials_result()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def start(args: Namespace) -> None:
|
||||
"""
|
||||
Start Backtesting script
|
||||
:param args: Cli args from Arguments()
|
||||
:return: None
|
||||
"""
|
||||
|
||||
# Remove noisy log messages
|
||||
logging.getLogger('hyperopt.mongoexp').setLevel(logging.WARNING)
|
||||
logging.getLogger('hyperopt.tpe').setLevel(logging.WARNING)
|
||||
|
||||
# Initialize configuration
|
||||
# Monkey patch the configuration with hyperopt_conf.py
|
||||
configuration = Configuration(args)
|
||||
logger.info('Starting freqtrade in Hyperopt mode')
|
||||
|
||||
optimize_config = hyperopt_optimize_conf()
|
||||
config = configuration._load_common_config(optimize_config)
|
||||
config = configuration._load_backtesting_config(config)
|
||||
config = configuration._load_hyperopt_config(config)
|
||||
config['exchange']['key'] = ''
|
||||
config['exchange']['secret'] = ''
|
||||
|
||||
# Initialize backtesting object
|
||||
hyperopt = Hyperopt(config)
|
||||
hyperopt.start()
|
||||
221
freqtrade/persistence.py
Normal file
221
freqtrade/persistence.py
Normal file
@@ -0,0 +1,221 @@
|
||||
"""
|
||||
This module contains the class to persist trades into SQLite
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from decimal import Decimal, getcontext
|
||||
from typing import Dict, Optional
|
||||
|
||||
import arrow
|
||||
from sqlalchemy import (Boolean, Column, DateTime, Float, Integer, String,
|
||||
create_engine)
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm.scoping import scoped_session
|
||||
from sqlalchemy.orm.session import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_CONF = {}
|
||||
_DECL_BASE = declarative_base()
|
||||
|
||||
|
||||
def init(config: dict, engine: Optional[Engine] = None) -> None:
|
||||
"""
|
||||
Initializes this module with the given config,
|
||||
registers all known command handlers
|
||||
and starts polling for message updates
|
||||
:param config: config to use
|
||||
:param engine: database engine for sqlalchemy (Optional)
|
||||
:return: None
|
||||
"""
|
||||
_CONF.update(config)
|
||||
if not engine:
|
||||
if _CONF.get('dry_run', False):
|
||||
# the user wants dry run to use a DB
|
||||
if _CONF.get('dry_run_db', False):
|
||||
engine = create_engine('sqlite:///tradesv3.dry_run.sqlite')
|
||||
# Otherwise dry run will store in memory
|
||||
else:
|
||||
engine = create_engine('sqlite://',
|
||||
connect_args={'check_same_thread': False},
|
||||
poolclass=StaticPool,
|
||||
echo=False)
|
||||
else:
|
||||
engine = create_engine('sqlite:///tradesv3.sqlite')
|
||||
|
||||
session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
|
||||
Trade.session = session()
|
||||
Trade.query = session.query_property()
|
||||
_DECL_BASE.metadata.create_all(engine)
|
||||
|
||||
# Clean dry_run DB
|
||||
if _CONF.get('dry_run', False) and _CONF.get('dry_run_db', False):
|
||||
clean_dry_run_db()
|
||||
|
||||
|
||||
def cleanup() -> None:
|
||||
"""
|
||||
Flushes all pending operations to disk.
|
||||
:return: None
|
||||
"""
|
||||
Trade.session.flush()
|
||||
|
||||
|
||||
def clean_dry_run_db() -> None:
|
||||
"""
|
||||
Remove open_order_id from a Dry_run DB
|
||||
:return: None
|
||||
"""
|
||||
for trade in Trade.query.filter(Trade.open_order_id.isnot(None)).all():
|
||||
# Check we are updating only a dry_run order not a prod one
|
||||
if 'dry_run' in trade.open_order_id:
|
||||
trade.open_order_id = None
|
||||
|
||||
|
||||
class Trade(_DECL_BASE):
|
||||
"""
|
||||
Class used to define a trade structure
|
||||
"""
|
||||
__tablename__ = 'trades'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
exchange = Column(String, nullable=False)
|
||||
pair = Column(String, nullable=False)
|
||||
is_open = Column(Boolean, nullable=False, default=True)
|
||||
fee = Column(Float, nullable=False, default=0.0)
|
||||
open_rate = Column(Float)
|
||||
close_rate = Column(Float)
|
||||
close_profit = Column(Float)
|
||||
stake_amount = Column(Float, nullable=False)
|
||||
amount = Column(Float)
|
||||
open_date = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
close_date = Column(DateTime)
|
||||
open_order_id = Column(String)
|
||||
|
||||
def __repr__(self):
|
||||
return 'Trade(id={}, pair={}, amount={:.8f}, open_rate={:.8f}, open_since={})'.format(
|
||||
self.id,
|
||||
self.pair,
|
||||
self.amount,
|
||||
self.open_rate,
|
||||
arrow.get(self.open_date).humanize() if self.is_open else 'closed'
|
||||
)
|
||||
|
||||
def update(self, order: Dict) -> None:
|
||||
"""
|
||||
Updates this entity with amount and actual open/close rates.
|
||||
:param order: order retrieved by exchange.get_order()
|
||||
:return: None
|
||||
"""
|
||||
# Ignore open and cancelled orders
|
||||
if not order['closed'] or order['rate'] is None:
|
||||
return
|
||||
|
||||
logger.info('Updating trade (id=%d) ...', self.id)
|
||||
|
||||
getcontext().prec = 8 # Bittrex do not go above 8 decimal
|
||||
if order['type'] == 'LIMIT_BUY':
|
||||
# Update open rate and actual amount
|
||||
self.open_rate = Decimal(order['rate'])
|
||||
self.amount = Decimal(order['amount'])
|
||||
logger.info('LIMIT_BUY has been fulfilled for %s.', self)
|
||||
self.open_order_id = None
|
||||
elif order['type'] == 'LIMIT_SELL':
|
||||
self.close(order['rate'])
|
||||
else:
|
||||
raise ValueError('Unknown order type: {}'.format(order['type']))
|
||||
cleanup()
|
||||
|
||||
def close(self, rate: float) -> None:
|
||||
"""
|
||||
Sets close_rate to the given rate, calculates total profit
|
||||
and marks trade as closed
|
||||
"""
|
||||
self.close_rate = Decimal(rate)
|
||||
self.close_profit = self.calc_profit_percent()
|
||||
self.close_date = datetime.utcnow()
|
||||
self.is_open = False
|
||||
self.open_order_id = None
|
||||
logger.info(
|
||||
'Marking %s as closed as the trade is fulfilled and found no open orders for it.',
|
||||
self
|
||||
)
|
||||
|
||||
def calc_open_trade_price(
|
||||
self,
|
||||
fee: Optional[float] = None) -> float:
|
||||
"""
|
||||
Calculate the open_rate in BTC
|
||||
:param fee: fee to use on the open rate (optional).
|
||||
If rate is not set self.fee will be used
|
||||
:return: Price in BTC of the open trade
|
||||
"""
|
||||
getcontext().prec = 8
|
||||
|
||||
buy_trade = (Decimal(self.amount) * Decimal(self.open_rate))
|
||||
fees = buy_trade * Decimal(fee or self.fee)
|
||||
return float(buy_trade + fees)
|
||||
|
||||
def calc_close_trade_price(
|
||||
self,
|
||||
rate: Optional[float] = None,
|
||||
fee: Optional[float] = None) -> float:
|
||||
"""
|
||||
Calculate the close_rate in BTC
|
||||
:param fee: fee to use on the close rate (optional).
|
||||
If rate is not set self.fee will be used
|
||||
:param rate: rate to compare with (optional).
|
||||
If rate is not set self.close_rate will be used
|
||||
:return: Price in BTC of the open trade
|
||||
"""
|
||||
getcontext().prec = 8
|
||||
|
||||
if rate is None and not self.close_rate:
|
||||
return 0.0
|
||||
|
||||
sell_trade = (Decimal(self.amount) * Decimal(rate or self.close_rate))
|
||||
fees = sell_trade * Decimal(fee or self.fee)
|
||||
return float(sell_trade - fees)
|
||||
|
||||
def calc_profit(
|
||||
self,
|
||||
rate: Optional[float] = None,
|
||||
fee: Optional[float] = None) -> float:
|
||||
"""
|
||||
Calculate the profit in BTC between Close and Open trade
|
||||
:param fee: fee to use on the close rate (optional).
|
||||
If rate is not set self.fee will be used
|
||||
:param rate: close rate to compare with (optional).
|
||||
If rate is not set self.close_rate will be used
|
||||
:return: profit in BTC as float
|
||||
"""
|
||||
open_trade_price = self.calc_open_trade_price()
|
||||
close_trade_price = self.calc_close_trade_price(
|
||||
rate=(rate or self.close_rate),
|
||||
fee=(fee or self.fee)
|
||||
)
|
||||
return float("{0:.8f}".format(close_trade_price - open_trade_price))
|
||||
|
||||
def calc_profit_percent(
|
||||
self,
|
||||
rate: Optional[float] = None,
|
||||
fee: Optional[float] = None) -> float:
|
||||
"""
|
||||
Calculates the profit in percentage (including fee).
|
||||
:param rate: rate to compare with (optional).
|
||||
If rate is not set self.close_rate will be used
|
||||
:param fee: fee to use on the close rate (optional).
|
||||
:return: profit in percentage as float
|
||||
"""
|
||||
getcontext().prec = 8
|
||||
|
||||
open_trade_price = self.calc_open_trade_price()
|
||||
close_trade_price = self.calc_close_trade_price(
|
||||
rate=(rate or self.close_rate),
|
||||
fee=(fee or self.fee)
|
||||
)
|
||||
|
||||
return float("{0:.8f}".format((close_trade_price / open_trade_price) - 1))
|
||||
0
freqtrade/rpc/__init__.py
Normal file
0
freqtrade/rpc/__init__.py
Normal file
383
freqtrade/rpc/rpc.py
Normal file
383
freqtrade/rpc/rpc.py
Normal file
@@ -0,0 +1,383 @@
|
||||
"""
|
||||
This module contains class to define a RPC communications
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from typing import Tuple, Any
|
||||
|
||||
import arrow
|
||||
import sqlalchemy as sql
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade import exchange
|
||||
from freqtrade.misc import shorten_date
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.state import State
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RPC(object):
|
||||
"""
|
||||
RPC class can be used to have extra feature, like bot data, and access to DB data
|
||||
"""
|
||||
def __init__(self, freqtrade) -> None:
|
||||
"""
|
||||
Initializes all enabled rpc modules
|
||||
:param freqtrade: Instance of a freqtrade bot
|
||||
:return: None
|
||||
"""
|
||||
self.freqtrade = freqtrade
|
||||
|
||||
def rpc_trade_status(self) -> Tuple[bool, Any]:
|
||||
"""
|
||||
Below follows the RPC backend it is prefixed with rpc_ to raise awareness that it is
|
||||
a remotely exposed function
|
||||
:return:
|
||||
"""
|
||||
# Fetch open trade
|
||||
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||
if self.freqtrade.state != State.RUNNING:
|
||||
return True, '*Status:* `trader is not running`'
|
||||
elif not trades:
|
||||
return True, '*Status:* `no active trade`'
|
||||
else:
|
||||
result = []
|
||||
for trade in trades:
|
||||
order = None
|
||||
if trade.open_order_id:
|
||||
order = exchange.get_order(trade.open_order_id)
|
||||
# calculate profit and send message to user
|
||||
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
||||
current_profit = trade.calc_profit_percent(current_rate)
|
||||
fmt_close_profit = '{:.2f}%'.format(
|
||||
round(trade.close_profit * 100, 2)
|
||||
) if trade.close_profit else None
|
||||
message = "*Trade ID:* `{trade_id}`\n" \
|
||||
"*Current Pair:* [{pair}]({market_url})\n" \
|
||||
"*Open Since:* `{date}`\n" \
|
||||
"*Amount:* `{amount}`\n" \
|
||||
"*Open Rate:* `{open_rate:.8f}`\n" \
|
||||
"*Close Rate:* `{close_rate}`\n" \
|
||||
"*Current Rate:* `{current_rate:.8f}`\n" \
|
||||
"*Close Profit:* `{close_profit}`\n" \
|
||||
"*Current Profit:* `{current_profit:.2f}%`\n" \
|
||||
"*Open Order:* `{open_order}`"\
|
||||
.format(
|
||||
trade_id=trade.id,
|
||||
pair=trade.pair,
|
||||
market_url=exchange.get_pair_detail_url(trade.pair),
|
||||
date=arrow.get(trade.open_date).humanize(),
|
||||
open_rate=trade.open_rate,
|
||||
close_rate=trade.close_rate,
|
||||
current_rate=current_rate,
|
||||
amount=round(trade.amount, 8),
|
||||
close_profit=fmt_close_profit,
|
||||
current_profit=round(current_profit * 100, 2),
|
||||
open_order='({} rem={:.8f})'.format(
|
||||
order['type'], order['remaining']
|
||||
) if order else None,
|
||||
)
|
||||
result.append(message)
|
||||
return False, result
|
||||
|
||||
def rpc_status_table(self) -> Tuple[bool, Any]:
|
||||
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||
if self.freqtrade.state != State.RUNNING:
|
||||
return True, '*Status:* `trader is not running`'
|
||||
elif not trades:
|
||||
return True, '*Status:* `no active order`'
|
||||
else:
|
||||
trades_list = []
|
||||
for trade in trades:
|
||||
# calculate profit and send message to user
|
||||
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
||||
trades_list.append([
|
||||
trade.id,
|
||||
trade.pair,
|
||||
shorten_date(arrow.get(trade.open_date).humanize(only_distance=True)),
|
||||
'{:.2f}%'.format(100 * trade.calc_profit_percent(current_rate))
|
||||
])
|
||||
|
||||
columns = ['ID', 'Pair', 'Since', 'Profit']
|
||||
df_statuses = DataFrame.from_records(trades_list, columns=columns)
|
||||
df_statuses = df_statuses.set_index(columns[0])
|
||||
# The style used throughout is to return a tuple
|
||||
# consisting of (error_occured?, result)
|
||||
# Another approach would be to just return the
|
||||
# result, or raise error
|
||||
return False, df_statuses
|
||||
|
||||
def rpc_daily_profit(
|
||||
self, timescale: int,
|
||||
stake_currency: str, fiat_display_currency: str) -> Tuple[bool, Any]:
|
||||
today = datetime.utcnow().date()
|
||||
profit_days = {}
|
||||
|
||||
if not (isinstance(timescale, int) and timescale > 0):
|
||||
return True, '*Daily [n]:* `must be an integer greater than 0`'
|
||||
|
||||
fiat = self.freqtrade.fiat_converter
|
||||
for day in range(0, timescale):
|
||||
profitday = today - timedelta(days=day)
|
||||
trades = Trade.query \
|
||||
.filter(Trade.is_open.is_(False)) \
|
||||
.filter(Trade.close_date >= profitday)\
|
||||
.filter(Trade.close_date < (profitday + timedelta(days=1)))\
|
||||
.order_by(Trade.close_date)\
|
||||
.all()
|
||||
curdayprofit = sum(trade.calc_profit() for trade in trades)
|
||||
profit_days[profitday] = {
|
||||
'amount': format(curdayprofit, '.8f'),
|
||||
'trades': len(trades)
|
||||
}
|
||||
|
||||
stats = [
|
||||
[
|
||||
key,
|
||||
'{value:.8f} {symbol}'.format(
|
||||
value=float(value['amount']),
|
||||
symbol=stake_currency
|
||||
),
|
||||
'{value:.3f} {symbol}'.format(
|
||||
value=fiat.convert_amount(
|
||||
value['amount'],
|
||||
stake_currency,
|
||||
fiat_display_currency
|
||||
),
|
||||
symbol=fiat_display_currency
|
||||
),
|
||||
'{value} trade{s}'.format(
|
||||
value=value['trades'],
|
||||
s='' if value['trades'] < 2 else 's'
|
||||
),
|
||||
]
|
||||
for key, value in profit_days.items()
|
||||
]
|
||||
return False, stats
|
||||
|
||||
def rpc_trade_statistics(
|
||||
self, stake_currency: str, fiat_display_currency: str) -> Tuple[bool, Any]:
|
||||
"""
|
||||
:return: cumulative profit statistics.
|
||||
"""
|
||||
trades = Trade.query.order_by(Trade.id).all()
|
||||
|
||||
profit_all_coin = []
|
||||
profit_all_percent = []
|
||||
profit_closed_coin = []
|
||||
profit_closed_percent = []
|
||||
durations = []
|
||||
|
||||
for trade in trades:
|
||||
current_rate = None
|
||||
|
||||
if not trade.open_rate:
|
||||
continue
|
||||
if trade.close_date:
|
||||
durations.append((trade.close_date - trade.open_date).total_seconds())
|
||||
|
||||
if not trade.is_open:
|
||||
profit_percent = trade.calc_profit_percent()
|
||||
profit_closed_coin.append(trade.calc_profit())
|
||||
profit_closed_percent.append(profit_percent)
|
||||
else:
|
||||
# Get current rate
|
||||
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
||||
profit_percent = trade.calc_profit_percent(rate=current_rate)
|
||||
|
||||
profit_all_coin.append(
|
||||
trade.calc_profit(rate=Decimal(trade.close_rate or current_rate))
|
||||
)
|
||||
profit_all_percent.append(profit_percent)
|
||||
|
||||
best_pair = Trade.session.query(
|
||||
Trade.pair, sql.func.sum(Trade.close_profit).label('profit_sum')
|
||||
).filter(Trade.is_open.is_(False)) \
|
||||
.group_by(Trade.pair) \
|
||||
.order_by(sql.text('profit_sum DESC')).first()
|
||||
|
||||
if not best_pair:
|
||||
return True, '*Status:* `no closed trade`'
|
||||
|
||||
bp_pair, bp_rate = best_pair
|
||||
|
||||
# FIX: we want to keep fiatconverter in a state/environment,
|
||||
# doing this will utilize its caching functionallity, instead we reinitialize it here
|
||||
fiat = self.freqtrade.fiat_converter
|
||||
# Prepare data to display
|
||||
profit_closed_coin = round(sum(profit_closed_coin), 8)
|
||||
profit_closed_percent = round(sum(profit_closed_percent) * 100, 2)
|
||||
profit_closed_fiat = fiat.convert_amount(
|
||||
profit_closed_coin,
|
||||
stake_currency,
|
||||
fiat_display_currency
|
||||
)
|
||||
profit_all_coin = round(sum(profit_all_coin), 8)
|
||||
profit_all_percent = round(sum(profit_all_percent) * 100, 2)
|
||||
profit_all_fiat = fiat.convert_amount(
|
||||
profit_all_coin,
|
||||
stake_currency,
|
||||
fiat_display_currency
|
||||
)
|
||||
num = float(len(durations) or 1)
|
||||
return (
|
||||
False,
|
||||
{
|
||||
'profit_closed_coin': profit_closed_coin,
|
||||
'profit_closed_percent': profit_closed_percent,
|
||||
'profit_closed_fiat': profit_closed_fiat,
|
||||
'profit_all_coin': profit_all_coin,
|
||||
'profit_all_percent': profit_all_percent,
|
||||
'profit_all_fiat': profit_all_fiat,
|
||||
'trade_count': len(trades),
|
||||
'first_trade_date': arrow.get(trades[0].open_date).humanize(),
|
||||
'latest_trade_date': arrow.get(trades[-1].open_date).humanize(),
|
||||
'avg_duration': str(timedelta(seconds=sum(durations) / num)).split('.')[0],
|
||||
'best_pair': bp_pair,
|
||||
'best_rate': round(bp_rate * 100, 2)
|
||||
}
|
||||
)
|
||||
|
||||
def rpc_balance(self, fiat_display_currency: str) -> Tuple[bool, Any]:
|
||||
"""
|
||||
:return: current account balance per crypto
|
||||
"""
|
||||
balances = [
|
||||
c for c in exchange.get_balances()
|
||||
if c['Balance'] or c['Available'] or c['Pending']
|
||||
]
|
||||
if not balances:
|
||||
return True, '`All balances are zero.`'
|
||||
|
||||
output = []
|
||||
total = 0.0
|
||||
for currency in balances:
|
||||
coin = currency['Currency']
|
||||
if coin == 'BTC':
|
||||
currency["Rate"] = 1.0
|
||||
else:
|
||||
if coin == 'USDT':
|
||||
currency["Rate"] = 1.0 / exchange.get_ticker('USDT_BTC', False)['bid']
|
||||
else:
|
||||
currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid']
|
||||
currency['BTC'] = currency["Rate"] * currency["Balance"]
|
||||
total = total + currency['BTC']
|
||||
output.append(
|
||||
{
|
||||
'currency': currency['Currency'],
|
||||
'available': currency['Available'],
|
||||
'balance': currency['Balance'],
|
||||
'pending': currency['Pending'],
|
||||
'est_btc': currency['BTC']
|
||||
}
|
||||
)
|
||||
fiat = self.freqtrade.fiat_converter
|
||||
symbol = fiat_display_currency
|
||||
value = fiat.convert_amount(total, 'BTC', symbol)
|
||||
return False, (output, total, symbol, value)
|
||||
|
||||
def rpc_start(self) -> (bool, str):
|
||||
"""
|
||||
Handler for start.
|
||||
"""
|
||||
if self.freqtrade.state == State.RUNNING:
|
||||
return True, '*Status:* `already running`'
|
||||
|
||||
self.freqtrade.state = State.RUNNING
|
||||
return False, '`Starting trader ...`'
|
||||
|
||||
def rpc_stop(self) -> (bool, str):
|
||||
"""
|
||||
Handler for stop.
|
||||
"""
|
||||
if self.freqtrade.state == State.RUNNING:
|
||||
self.freqtrade.state = State.STOPPED
|
||||
return False, '`Stopping trader ...`'
|
||||
|
||||
return True, '*Status:* `already stopped`'
|
||||
|
||||
# FIX: no test for this!!!!
|
||||
def rpc_forcesell(self, trade_id) -> Tuple[bool, Any]:
|
||||
"""
|
||||
Handler for forcesell <id>.
|
||||
Sells the given trade at current price
|
||||
:return: error or None
|
||||
"""
|
||||
def _exec_forcesell(trade: Trade) -> None:
|
||||
# Check if there is there is an open order
|
||||
if trade.open_order_id:
|
||||
order = exchange.get_order(trade.open_order_id)
|
||||
|
||||
# Cancel open LIMIT_BUY orders and close trade
|
||||
if order and not order['closed'] and order['type'] == 'LIMIT_BUY':
|
||||
exchange.cancel_order(trade.open_order_id)
|
||||
trade.close(order.get('rate') or trade.open_rate)
|
||||
# TODO: sell amount which has been bought already
|
||||
return
|
||||
|
||||
# Ignore trades with an attached LIMIT_SELL order
|
||||
if order and not order['closed'] and order['type'] == 'LIMIT_SELL':
|
||||
return
|
||||
|
||||
# Get current rate and execute sell
|
||||
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
||||
self.freqtrade.execute_sell(trade, current_rate)
|
||||
# ---- EOF def _exec_forcesell ----
|
||||
|
||||
if self.freqtrade.state != State.RUNNING:
|
||||
return True, '`trader is not running`'
|
||||
|
||||
if trade_id == 'all':
|
||||
# Execute sell for all open orders
|
||||
for trade in Trade.query.filter(Trade.is_open.is_(True)).all():
|
||||
_exec_forcesell(trade)
|
||||
return False, ''
|
||||
|
||||
# Query for trade
|
||||
trade = Trade.query.filter(
|
||||
sql.and_(
|
||||
Trade.id == trade_id,
|
||||
Trade.is_open.is_(True)
|
||||
)
|
||||
).first()
|
||||
if not trade:
|
||||
logger.warning('forcesell: Invalid argument received')
|
||||
return True, 'Invalid argument.'
|
||||
|
||||
_exec_forcesell(trade)
|
||||
return False, ''
|
||||
|
||||
def rpc_performance(self) -> Tuple[bool, Any]:
|
||||
"""
|
||||
Handler for performance.
|
||||
Shows a performance statistic from finished trades
|
||||
"""
|
||||
if self.freqtrade.state != State.RUNNING:
|
||||
return True, '`trader is not running`'
|
||||
|
||||
pair_rates = Trade.session.query(Trade.pair,
|
||||
sql.func.sum(Trade.close_profit).label('profit_sum'),
|
||||
sql.func.count(Trade.pair).label('count')) \
|
||||
.filter(Trade.is_open.is_(False)) \
|
||||
.group_by(Trade.pair) \
|
||||
.order_by(sql.text('profit_sum DESC')) \
|
||||
.all()
|
||||
trades = []
|
||||
for (pair, rate, count) in pair_rates:
|
||||
trades.append({'pair': pair, 'profit': round(rate * 100, 2), 'count': count})
|
||||
|
||||
return False, trades
|
||||
|
||||
def rpc_count(self) -> Tuple[bool, Any]:
|
||||
"""
|
||||
Returns the number of trades running
|
||||
:return: None
|
||||
"""
|
||||
if self.freqtrade.state != State.RUNNING:
|
||||
return True, '`trader is not running`'
|
||||
|
||||
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||
return False, trades
|
||||
56
freqtrade/rpc/rpc_manager.py
Normal file
56
freqtrade/rpc/rpc_manager.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
This module contains class to manage RPC communications (Telegram, Slack, ...)
|
||||
"""
|
||||
import logging
|
||||
|
||||
from freqtrade.rpc.telegram import Telegram
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RPCManager(object):
|
||||
"""
|
||||
Class to manage RPC objects (Telegram, Slack, ...)
|
||||
"""
|
||||
def __init__(self, freqtrade) -> None:
|
||||
"""
|
||||
Initializes all enabled rpc modules
|
||||
:param config: config to use
|
||||
:return: None
|
||||
"""
|
||||
self.freqtrade = freqtrade
|
||||
|
||||
self.registered_modules = []
|
||||
self.telegram = None
|
||||
self._init()
|
||||
|
||||
def _init(self) -> None:
|
||||
"""
|
||||
Init RPC modules
|
||||
:return:
|
||||
"""
|
||||
if self.freqtrade.config['telegram'].get('enabled', False):
|
||||
logger.info('Enabling rpc.telegram ...')
|
||||
self.registered_modules.append('telegram')
|
||||
self.telegram = Telegram(self.freqtrade)
|
||||
|
||||
def cleanup(self) -> None:
|
||||
"""
|
||||
Stops all enabled rpc modules
|
||||
:return: None
|
||||
"""
|
||||
if 'telegram' in self.registered_modules:
|
||||
logger.info('Cleaning up rpc.telegram ...')
|
||||
self.registered_modules.remove('telegram')
|
||||
self.telegram.cleanup()
|
||||
|
||||
def send_msg(self, msg: str) -> None:
|
||||
"""
|
||||
Send given markdown message to all registered rpc modules
|
||||
:param msg: message
|
||||
:return: None
|
||||
"""
|
||||
logger.info(msg)
|
||||
if 'telegram' in self.registered_modules:
|
||||
self.telegram.send_msg(msg)
|
||||
449
freqtrade/rpc/telegram.py
Normal file
449
freqtrade/rpc/telegram.py
Normal file
@@ -0,0 +1,449 @@
|
||||
# pragma pylint: disable=unused-argument, unused-variable, protected-access, invalid-name
|
||||
|
||||
"""
|
||||
This module manage Telegram communication
|
||||
"""
|
||||
import logging
|
||||
from typing import Any, Callable
|
||||
|
||||
from tabulate import tabulate
|
||||
from telegram import Bot, ParseMode, ReplyKeyboardMarkup, Update
|
||||
from telegram.error import NetworkError, TelegramError
|
||||
from telegram.ext import CommandHandler, Updater
|
||||
|
||||
from freqtrade.__init__ import __version__
|
||||
from freqtrade.rpc.rpc import RPC
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def authorized_only(command_handler: Callable[[Bot, Update], None]) -> Callable[..., Any]:
|
||||
"""
|
||||
Decorator to check if the message comes from the correct chat_id
|
||||
:param command_handler: Telegram CommandHandler
|
||||
:return: decorated function
|
||||
"""
|
||||
def wrapper(self, *args, **kwargs):
|
||||
"""
|
||||
Decorator logic
|
||||
"""
|
||||
update = kwargs.get('update') or args[1]
|
||||
|
||||
# Reject unauthorized messages
|
||||
chat_id = int(self._config['telegram']['chat_id'])
|
||||
|
||||
if int(update.message.chat_id) != chat_id:
|
||||
logger.info(
|
||||
'Rejected unauthorized message from: %s',
|
||||
update.message.chat_id
|
||||
)
|
||||
return wrapper
|
||||
|
||||
logger.info(
|
||||
'Executing handler: %s for chat_id: %s',
|
||||
command_handler.__name__,
|
||||
chat_id
|
||||
)
|
||||
try:
|
||||
return command_handler(self, *args, **kwargs)
|
||||
except BaseException:
|
||||
logger.exception('Exception occurred within Telegram module')
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class Telegram(RPC):
|
||||
"""
|
||||
Telegram, this class send messages to Telegram
|
||||
"""
|
||||
def __init__(self, freqtrade) -> None:
|
||||
"""
|
||||
Init the Telegram call, and init the super class RPC
|
||||
:param freqtrade: Instance of a freqtrade bot
|
||||
:return: None
|
||||
"""
|
||||
super().__init__(freqtrade)
|
||||
|
||||
self._updater = None
|
||||
self._config = freqtrade.config
|
||||
self._init()
|
||||
|
||||
def _init(self) -> None:
|
||||
"""
|
||||
Initializes this module with the given config,
|
||||
registers all known command handlers
|
||||
and starts polling for message updates
|
||||
:param config: config to use
|
||||
:return: None
|
||||
"""
|
||||
if not self.is_enabled():
|
||||
return
|
||||
|
||||
self._updater = Updater(token=self._config['telegram']['token'], workers=0)
|
||||
|
||||
# Register command handler and start telegram message polling
|
||||
handles = [
|
||||
CommandHandler('status', self._status),
|
||||
CommandHandler('profit', self._profit),
|
||||
CommandHandler('balance', self._balance),
|
||||
CommandHandler('start', self._start),
|
||||
CommandHandler('stop', self._stop),
|
||||
CommandHandler('forcesell', self._forcesell),
|
||||
CommandHandler('performance', self._performance),
|
||||
CommandHandler('daily', self._daily),
|
||||
CommandHandler('count', self._count),
|
||||
CommandHandler('help', self._help),
|
||||
CommandHandler('version', self._version),
|
||||
]
|
||||
for handle in handles:
|
||||
self._updater.dispatcher.add_handler(handle)
|
||||
self._updater.start_polling(
|
||||
clean=True,
|
||||
bootstrap_retries=-1,
|
||||
timeout=30,
|
||||
read_latency=60,
|
||||
)
|
||||
logger.info(
|
||||
'rpc.telegram is listening for following commands: %s',
|
||||
[h.command for h in handles]
|
||||
)
|
||||
|
||||
def cleanup(self) -> None:
|
||||
"""
|
||||
Stops all running telegram threads.
|
||||
:return: None
|
||||
"""
|
||||
if not self.is_enabled():
|
||||
return
|
||||
|
||||
self._updater.stop()
|
||||
|
||||
def is_enabled(self) -> bool:
|
||||
"""
|
||||
Returns True if the telegram module is activated, False otherwise
|
||||
"""
|
||||
return bool(self._config.get('telegram', {}).get('enabled', False))
|
||||
|
||||
@authorized_only
|
||||
def _status(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /status.
|
||||
Returns the current TradeThread status
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
|
||||
# Check if additional parameters are passed
|
||||
params = update.message.text.replace('/status', '').split(' ') \
|
||||
if update.message.text else []
|
||||
if 'table' in params:
|
||||
self._status_table(bot, update)
|
||||
return
|
||||
|
||||
# Fetch open trade
|
||||
(error, trades) = self.rpc_trade_status()
|
||||
if error:
|
||||
self.send_msg(trades, bot=bot)
|
||||
else:
|
||||
for trademsg in trades:
|
||||
self.send_msg(trademsg, bot=bot)
|
||||
|
||||
@authorized_only
|
||||
def _status_table(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /status table.
|
||||
Returns the current TradeThread status in table format
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
# Fetch open trade
|
||||
(err, df_statuses) = self.rpc_status_table()
|
||||
if err:
|
||||
self.send_msg(df_statuses, bot=bot)
|
||||
else:
|
||||
message = tabulate(df_statuses, headers='keys', tablefmt='simple')
|
||||
message = "<pre>{}</pre>".format(message)
|
||||
|
||||
self.send_msg(message, parse_mode=ParseMode.HTML)
|
||||
|
||||
@authorized_only
|
||||
def _daily(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /daily <n>
|
||||
Returns a daily profit (in BTC) over the last n days.
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
timescale = int(update.message.text.replace('/daily', '').strip())
|
||||
except (TypeError, ValueError):
|
||||
timescale = 7
|
||||
(error, stats) = self.rpc_daily_profit(
|
||||
timescale,
|
||||
self._config['stake_currency'],
|
||||
self._config['fiat_display_currency']
|
||||
)
|
||||
if error:
|
||||
self.send_msg(stats, bot=bot)
|
||||
else:
|
||||
stats = tabulate(stats,
|
||||
headers=[
|
||||
'Day',
|
||||
'Profit {}'.format(self._config['stake_currency']),
|
||||
'Profit {}'.format(self._config['fiat_display_currency'])
|
||||
],
|
||||
tablefmt='simple')
|
||||
message = '<b>Daily Profit over the last {} days</b>:\n<pre>{}</pre>'\
|
||||
.format(
|
||||
timescale,
|
||||
stats
|
||||
)
|
||||
self.send_msg(message, bot=bot, parse_mode=ParseMode.HTML)
|
||||
|
||||
@authorized_only
|
||||
def _profit(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /profit.
|
||||
Returns a cumulative profit statistics.
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
(error, stats) = self.rpc_trade_statistics(
|
||||
self._config['stake_currency'],
|
||||
self._config['fiat_display_currency']
|
||||
)
|
||||
if error:
|
||||
self.send_msg(stats, bot=bot)
|
||||
return
|
||||
|
||||
# Message to display
|
||||
markdown_msg = "*ROI:* Close trades\n" \
|
||||
"∙ `{profit_closed_coin:.8f} {coin} ({profit_closed_percent:.2f}%)`\n" \
|
||||
"∙ `{profit_closed_fiat:.3f} {fiat}`\n" \
|
||||
"*ROI:* All trades\n" \
|
||||
"∙ `{profit_all_coin:.8f} {coin} ({profit_all_percent:.2f}%)`\n" \
|
||||
"∙ `{profit_all_fiat:.3f} {fiat}`\n" \
|
||||
"*Total Trade Count:* `{trade_count}`\n" \
|
||||
"*First Trade opened:* `{first_trade_date}`\n" \
|
||||
"*Latest Trade opened:* `{latest_trade_date}`\n" \
|
||||
"*Avg. Duration:* `{avg_duration}`\n" \
|
||||
"*Best Performing:* `{best_pair}: {best_rate:.2f}%`"\
|
||||
.format(
|
||||
coin=self._config['stake_currency'],
|
||||
fiat=self._config['fiat_display_currency'],
|
||||
profit_closed_coin=stats['profit_closed_coin'],
|
||||
profit_closed_percent=stats['profit_closed_percent'],
|
||||
profit_closed_fiat=stats['profit_closed_fiat'],
|
||||
profit_all_coin=stats['profit_all_coin'],
|
||||
profit_all_percent=stats['profit_all_percent'],
|
||||
profit_all_fiat=stats['profit_all_fiat'],
|
||||
trade_count=stats['trade_count'],
|
||||
first_trade_date=stats['first_trade_date'],
|
||||
latest_trade_date=stats['latest_trade_date'],
|
||||
avg_duration=stats['avg_duration'],
|
||||
best_pair=stats['best_pair'],
|
||||
best_rate=stats['best_rate']
|
||||
)
|
||||
self.send_msg(markdown_msg, bot=bot)
|
||||
|
||||
@authorized_only
|
||||
def _balance(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /balance
|
||||
"""
|
||||
(error, result) = self.rpc_balance(self._config['fiat_display_currency'])
|
||||
if error:
|
||||
self.send_msg('`All balances are zero.`')
|
||||
return
|
||||
|
||||
(currencys, total, symbol, value) = result
|
||||
output = ''
|
||||
for currency in currencys:
|
||||
output += """*Currency*: {currency}
|
||||
*Available*: {available}
|
||||
*Balance*: {balance}
|
||||
*Pending*: {pending}
|
||||
*Est. BTC*: {est_btc: .8f}
|
||||
""".format(**currency)
|
||||
|
||||
output += """*Estimated Value*:
|
||||
*BTC*: {0: .8f}
|
||||
*{1}*: {2: .2f}
|
||||
""".format(total, symbol, value)
|
||||
self.send_msg(output)
|
||||
|
||||
@authorized_only
|
||||
def _start(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /start.
|
||||
Starts TradeThread
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
(error, msg) = self.rpc_start()
|
||||
if error:
|
||||
self.send_msg(msg, bot=bot)
|
||||
|
||||
@authorized_only
|
||||
def _stop(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /stop.
|
||||
Stops TradeThread
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
(error, msg) = self.rpc_stop()
|
||||
self.send_msg(msg, bot=bot)
|
||||
|
||||
@authorized_only
|
||||
def _forcesell(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /forcesell <id>.
|
||||
Sells the given trade at current price
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
|
||||
trade_id = update.message.text.replace('/forcesell', '').strip()
|
||||
(error, message) = self.rpc_forcesell(trade_id)
|
||||
if error:
|
||||
self.send_msg(message, bot=bot)
|
||||
return
|
||||
|
||||
@authorized_only
|
||||
def _performance(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /performance.
|
||||
Shows a performance statistic from finished trades
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
(error, trades) = self.rpc_performance()
|
||||
if error:
|
||||
self.send_msg(trades, bot=bot)
|
||||
return
|
||||
|
||||
stats = '\n'.join('{index}.\t<code>{pair}\t{profit:.2f}% ({count})</code>'.format(
|
||||
index=i + 1,
|
||||
pair=trade['pair'],
|
||||
profit=trade['profit'],
|
||||
count=trade['count']
|
||||
) for i, trade in enumerate(trades))
|
||||
message = '<b>Performance:</b>\n{}'.format(stats)
|
||||
self.send_msg(message, parse_mode=ParseMode.HTML)
|
||||
|
||||
@authorized_only
|
||||
def _count(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /count.
|
||||
Returns the number of trades running
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
(error, trades) = self.rpc_count()
|
||||
if error:
|
||||
self.send_msg(trades, bot=bot)
|
||||
return
|
||||
|
||||
message = tabulate({
|
||||
'current': [len(trades)],
|
||||
'max': [self._config['max_open_trades']],
|
||||
'total stake': [sum((trade.open_rate * trade.amount) for trade in trades)]
|
||||
}, headers=['current', 'max', 'total stake'], tablefmt='simple')
|
||||
message = "<pre>{}</pre>".format(message)
|
||||
logger.debug(message)
|
||||
self.send_msg(message, parse_mode=ParseMode.HTML)
|
||||
|
||||
@authorized_only
|
||||
def _help(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /help.
|
||||
Show commands of the bot
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
message = "*/start:* `Starts the trader`\n" \
|
||||
"*/stop:* `Stops the trader`\n" \
|
||||
"*/status [table]:* `Lists all open trades`\n" \
|
||||
" *table :* `will display trades in a table`\n" \
|
||||
"*/profit:* `Lists cumulative profit from all finished trades`\n" \
|
||||
"*/forcesell <trade_id>|all:* `Instantly sells the given trade or all trades, " \
|
||||
"regardless of profit`\n" \
|
||||
"*/performance:* `Show performance of each finished trade grouped by pair`\n" \
|
||||
"*/daily <n>:* `Shows profit or loss per day, over the last n days`\n" \
|
||||
"*/count:* `Show number of trades running compared to allowed number of trades`" \
|
||||
"\n" \
|
||||
"*/balance:* `Show account balance per currency`\n" \
|
||||
"*/help:* `This help message`\n" \
|
||||
"*/version:* `Show version`"
|
||||
|
||||
self.send_msg(message, bot=bot)
|
||||
|
||||
@authorized_only
|
||||
def _version(self, bot: Bot, update: Update) -> None:
|
||||
"""
|
||||
Handler for /version.
|
||||
Show version information
|
||||
:param bot: telegram bot
|
||||
:param update: message update
|
||||
:return: None
|
||||
"""
|
||||
self.send_msg('*Version:* `{}`'.format(__version__), bot=bot)
|
||||
|
||||
def send_msg(self, msg: str, bot: Bot = None,
|
||||
parse_mode: ParseMode = ParseMode.MARKDOWN) -> None:
|
||||
"""
|
||||
Send given markdown message
|
||||
:param msg: message
|
||||
:param bot: alternative bot
|
||||
:param parse_mode: telegram parse mode
|
||||
:return: None
|
||||
"""
|
||||
if not self.is_enabled():
|
||||
return
|
||||
|
||||
bot = bot or self._updater.bot
|
||||
|
||||
keyboard = [['/daily', '/profit', '/balance'],
|
||||
['/status', '/status table', '/performance'],
|
||||
['/count', '/start', '/stop', '/help']]
|
||||
|
||||
reply_markup = ReplyKeyboardMarkup(keyboard)
|
||||
|
||||
try:
|
||||
try:
|
||||
bot.send_message(
|
||||
self._config['telegram']['chat_id'],
|
||||
text=msg,
|
||||
parse_mode=parse_mode,
|
||||
reply_markup=reply_markup
|
||||
)
|
||||
except NetworkError as network_err:
|
||||
# Sometimes the telegram server resets the current connection,
|
||||
# if this is the case we send the message again.
|
||||
logger.warning(
|
||||
'Telegram NetworkError: %s! Trying one more time.',
|
||||
network_err.message
|
||||
)
|
||||
bot.send_message(
|
||||
self._config['telegram']['chat_id'],
|
||||
text=msg,
|
||||
parse_mode=parse_mode,
|
||||
reply_markup=reply_markup
|
||||
)
|
||||
except TelegramError as telegram_err:
|
||||
logger.warning(
|
||||
'TelegramError: %s! Giving up on that message.',
|
||||
telegram_err.message
|
||||
)
|
||||
14
freqtrade/state.py
Normal file
14
freqtrade/state.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# pragma pylint: disable=too-few-public-methods
|
||||
|
||||
"""
|
||||
Bot state constant
|
||||
"""
|
||||
import enum
|
||||
|
||||
|
||||
class State(enum.Enum):
|
||||
"""
|
||||
Bot running states
|
||||
"""
|
||||
RUNNING = 0
|
||||
STOPPED = 1
|
||||
0
freqtrade/strategy/__init__.py
Normal file
0
freqtrade/strategy/__init__.py
Normal file
240
freqtrade/strategy/default_strategy.py
Normal file
240
freqtrade/strategy/default_strategy.py
Normal file
@@ -0,0 +1,240 @@
|
||||
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
||||
|
||||
import talib.abstract as ta
|
||||
from pandas import DataFrame
|
||||
|
||||
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
||||
from freqtrade.indicator_helpers import fishers_inverse
|
||||
from freqtrade.strategy.interface import IStrategy
|
||||
|
||||
|
||||
class DefaultStrategy(IStrategy):
|
||||
"""
|
||||
Default Strategy provided by freqtrade bot.
|
||||
You can override it with your own strategy
|
||||
"""
|
||||
|
||||
# Minimal ROI designed for the strategy
|
||||
minimal_roi = {
|
||||
"40": 0.0,
|
||||
"30": 0.01,
|
||||
"20": 0.02,
|
||||
"0": 0.04
|
||||
}
|
||||
|
||||
# Optimal stoploss designed for the strategy
|
||||
stoploss = -0.10
|
||||
|
||||
# Optimal ticker interval for the strategy
|
||||
ticker_interval = 5
|
||||
|
||||
def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Adds several different TA indicators to the given DataFrame
|
||||
|
||||
Performance Note: For the best performance be frugal on the number of indicators
|
||||
you are using. Let uncomment only the indicator you are using in your strategies
|
||||
or your hyperopt configuration, otherwise you will waste your memory and CPU usage.
|
||||
"""
|
||||
|
||||
# Momentum Indicator
|
||||
# ------------------------------------
|
||||
|
||||
# ADX
|
||||
dataframe['adx'] = ta.ADX(dataframe)
|
||||
|
||||
# Awesome oscillator
|
||||
dataframe['ao'] = qtpylib.awesome_oscillator(dataframe)
|
||||
"""
|
||||
# Commodity Channel Index: values Oversold:<-100, Overbought:>100
|
||||
dataframe['cci'] = ta.CCI(dataframe)
|
||||
"""
|
||||
# MACD
|
||||
macd = ta.MACD(dataframe)
|
||||
dataframe['macd'] = macd['macd']
|
||||
dataframe['macdsignal'] = macd['macdsignal']
|
||||
dataframe['macdhist'] = macd['macdhist']
|
||||
|
||||
# MFI
|
||||
dataframe['mfi'] = ta.MFI(dataframe)
|
||||
|
||||
# Minus Directional Indicator / Movement
|
||||
dataframe['minus_dm'] = ta.MINUS_DM(dataframe)
|
||||
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
|
||||
|
||||
# Plus Directional Indicator / Movement
|
||||
dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
|
||||
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
|
||||
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
|
||||
|
||||
"""
|
||||
# ROC
|
||||
dataframe['roc'] = ta.ROC(dataframe)
|
||||
"""
|
||||
# RSI
|
||||
dataframe['rsi'] = ta.RSI(dataframe)
|
||||
|
||||
# Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy)
|
||||
dataframe['fisher_rsi'] = fishers_inverse(dataframe['rsi'])
|
||||
|
||||
# Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy)
|
||||
dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1)
|
||||
|
||||
# Stoch
|
||||
stoch = ta.STOCH(dataframe)
|
||||
dataframe['slowd'] = stoch['slowd']
|
||||
dataframe['slowk'] = stoch['slowk']
|
||||
|
||||
# Stoch fast
|
||||
stoch_fast = ta.STOCHF(dataframe)
|
||||
dataframe['fastd'] = stoch_fast['fastd']
|
||||
dataframe['fastk'] = stoch_fast['fastk']
|
||||
"""
|
||||
# Stoch RSI
|
||||
stoch_rsi = ta.STOCHRSI(dataframe)
|
||||
dataframe['fastd_rsi'] = stoch_rsi['fastd']
|
||||
dataframe['fastk_rsi'] = stoch_rsi['fastk']
|
||||
"""
|
||||
|
||||
# Overlap Studies
|
||||
# ------------------------------------
|
||||
|
||||
# Previous Bollinger bands
|
||||
# Because ta.BBANDS implementation is broken with small numbers, it actually
|
||||
# returns middle band for all the three bands. Switch to qtpylib.bollinger_bands
|
||||
# and use middle band instead.
|
||||
dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband']
|
||||
|
||||
# Bollinger bands
|
||||
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
||||
dataframe['bb_lowerband'] = bollinger['lower']
|
||||
dataframe['bb_middleband'] = bollinger['mid']
|
||||
dataframe['bb_upperband'] = bollinger['upper']
|
||||
|
||||
# EMA - Exponential Moving Average
|
||||
dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3)
|
||||
dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5)
|
||||
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
|
||||
dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
|
||||
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
|
||||
|
||||
# SAR Parabol
|
||||
dataframe['sar'] = ta.SAR(dataframe)
|
||||
|
||||
# SMA - Simple Moving Average
|
||||
dataframe['sma'] = ta.SMA(dataframe, timeperiod=40)
|
||||
|
||||
# TEMA - Triple Exponential Moving Average
|
||||
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
|
||||
|
||||
# Cycle Indicator
|
||||
# ------------------------------------
|
||||
# Hilbert Transform Indicator - SineWave
|
||||
hilbert = ta.HT_SINE(dataframe)
|
||||
dataframe['htsine'] = hilbert['sine']
|
||||
dataframe['htleadsine'] = hilbert['leadsine']
|
||||
|
||||
# Pattern Recognition - Bullish candlestick patterns
|
||||
# ------------------------------------
|
||||
"""
|
||||
# Hammer: values [0, 100]
|
||||
dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe)
|
||||
# Inverted Hammer: values [0, 100]
|
||||
dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe)
|
||||
# Dragonfly Doji: values [0, 100]
|
||||
dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe)
|
||||
# Piercing Line: values [0, 100]
|
||||
dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100]
|
||||
# Morningstar: values [0, 100]
|
||||
dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100]
|
||||
# Three White Soldiers: values [0, 100]
|
||||
dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100]
|
||||
"""
|
||||
|
||||
# Pattern Recognition - Bearish candlestick patterns
|
||||
# ------------------------------------
|
||||
"""
|
||||
# Hanging Man: values [0, 100]
|
||||
dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe)
|
||||
# Shooting Star: values [0, 100]
|
||||
dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe)
|
||||
# Gravestone Doji: values [0, 100]
|
||||
dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe)
|
||||
# Dark Cloud Cover: values [0, 100]
|
||||
dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe)
|
||||
# Evening Doji Star: values [0, 100]
|
||||
dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe)
|
||||
# Evening Star: values [0, 100]
|
||||
dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe)
|
||||
"""
|
||||
|
||||
# Pattern Recognition - Bullish/Bearish candlestick patterns
|
||||
# ------------------------------------
|
||||
"""
|
||||
# Three Line Strike: values [0, -100, 100]
|
||||
dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe)
|
||||
# Spinning Top: values [0, -100, 100]
|
||||
dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100]
|
||||
# Engulfing: values [0, -100, 100]
|
||||
dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100]
|
||||
# Harami: values [0, -100, 100]
|
||||
dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100]
|
||||
# Three Outside Up/Down: values [0, -100, 100]
|
||||
dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100]
|
||||
# Three Inside Up/Down: values [0, -100, 100]
|
||||
dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100]
|
||||
"""
|
||||
|
||||
# Chart type
|
||||
# ------------------------------------
|
||||
# Heikinashi stategy
|
||||
heikinashi = qtpylib.heikinashi(dataframe)
|
||||
dataframe['ha_open'] = heikinashi['open']
|
||||
dataframe['ha_close'] = heikinashi['close']
|
||||
dataframe['ha_high'] = heikinashi['high']
|
||||
dataframe['ha_low'] = heikinashi['low']
|
||||
|
||||
return dataframe
|
||||
|
||||
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the buy signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
dataframe.loc[
|
||||
(
|
||||
(dataframe['rsi'] < 35) &
|
||||
(dataframe['fastd'] < 35) &
|
||||
(dataframe['adx'] > 30) &
|
||||
(dataframe['plus_di'] > 0.5)
|
||||
) |
|
||||
(
|
||||
(dataframe['adx'] > 65) &
|
||||
(dataframe['plus_di'] > 0.5)
|
||||
),
|
||||
'buy'] = 1
|
||||
|
||||
return dataframe
|
||||
|
||||
def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the sell signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
dataframe.loc[
|
||||
(
|
||||
(
|
||||
(qtpylib.crossed_above(dataframe['rsi'], 70)) |
|
||||
(qtpylib.crossed_above(dataframe['fastd'], 70))
|
||||
) &
|
||||
(dataframe['adx'] > 10) &
|
||||
(dataframe['minus_di'] > 0)
|
||||
) |
|
||||
(
|
||||
(dataframe['adx'] > 70) &
|
||||
(dataframe['minus_di'] > 0.5)
|
||||
),
|
||||
'sell'] = 1
|
||||
return dataframe
|
||||
44
freqtrade/strategy/interface.py
Normal file
44
freqtrade/strategy/interface.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
IStrategy interface
|
||||
This module defines the interface to apply for strategies
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from pandas import DataFrame
|
||||
|
||||
|
||||
class IStrategy(ABC):
|
||||
"""
|
||||
Interface for freqtrade strategies
|
||||
Defines the mandatory structure must follow any custom strategies
|
||||
|
||||
Attributes you can use:
|
||||
minimal_roi -> Dict: Minimal ROI designed for the strategy
|
||||
stoploss -> float: optimal stoploss designed for the strategy
|
||||
ticker_interval -> int: value of the ticker interval to use for the strategy
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Populate indicators that will be used in the Buy and Sell strategy
|
||||
:param dataframe: Raw data from the exchange and parsed by parse_ticker_dataframe()
|
||||
:return: a Dataframe with all mandatory indicators for the strategies
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the buy signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with buy column
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame:
|
||||
"""
|
||||
Based on TA indicators, populates the sell signal for the given dataframe
|
||||
:param dataframe: DataFrame
|
||||
:return: DataFrame with sell column
|
||||
"""
|
||||
131
freqtrade/strategy/resolver.py
Normal file
131
freqtrade/strategy/resolver.py
Normal file
@@ -0,0 +1,131 @@
|
||||
# pragma pylint: disable=attribute-defined-outside-init
|
||||
|
||||
"""
|
||||
This module load custom strategies
|
||||
"""
|
||||
import importlib.util
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
from collections import OrderedDict
|
||||
from typing import Optional, Dict, Type
|
||||
|
||||
from freqtrade import constants
|
||||
from freqtrade.strategy.interface import IStrategy
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StrategyResolver(object):
|
||||
"""
|
||||
This class contains all the logic to load custom strategy class
|
||||
"""
|
||||
|
||||
__slots__ = ['strategy']
|
||||
|
||||
def __init__(self, config: Optional[Dict] = None) -> None:
|
||||
"""
|
||||
Load the custom class from config parameter
|
||||
:param config: configuration dictionary or None
|
||||
"""
|
||||
config = config or {}
|
||||
|
||||
# Verify the strategy is in the configuration, otherwise fallback to the default strategy
|
||||
strategy_name = config.get('strategy') or constants.DEFAULT_STRATEGY
|
||||
self.strategy = self._load_strategy(strategy_name, extra_dir=config.get('strategy_path'))
|
||||
|
||||
# Set attributes
|
||||
# Check if we need to override configuration
|
||||
if 'minimal_roi' in config:
|
||||
self.strategy.minimal_roi = config['minimal_roi']
|
||||
logger.info("Override strategy \'minimal_roi\' with value in config file.")
|
||||
|
||||
if 'stoploss' in config:
|
||||
self.strategy.stoploss = config['stoploss']
|
||||
logger.info(
|
||||
"Override strategy \'stoploss\' with value in config file: %s.", config['stoploss']
|
||||
)
|
||||
|
||||
if 'ticker_interval' in config:
|
||||
self.strategy.ticker_interval = config['ticker_interval']
|
||||
logger.info(
|
||||
"Override strategy \'ticker_interval\' with value in config file: %s.",
|
||||
config['ticker_interval']
|
||||
)
|
||||
|
||||
# Sort and apply type conversions
|
||||
self.strategy.minimal_roi = OrderedDict(sorted(
|
||||
{int(key): value for (key, value) in self.strategy.minimal_roi.items()}.items(),
|
||||
key=lambda t: t[0]))
|
||||
self.strategy.stoploss = float(self.strategy.stoploss)
|
||||
self.strategy.ticker_interval = int(self.strategy.ticker_interval)
|
||||
|
||||
def _load_strategy(
|
||||
self, strategy_name: str, extra_dir: Optional[str] = None) -> Optional[IStrategy]:
|
||||
"""
|
||||
Search and loads the specified strategy.
|
||||
:param strategy_name: name of the module to import
|
||||
:param extra_dir: additional directory to search for the given strategy
|
||||
:return: Strategy instance or None
|
||||
"""
|
||||
current_path = os.path.dirname(os.path.realpath(__file__))
|
||||
abs_paths = [
|
||||
os.path.join(current_path, '..', '..', 'user_data', 'strategies'),
|
||||
current_path,
|
||||
]
|
||||
|
||||
if extra_dir:
|
||||
# Add extra strategy directory on top of search paths
|
||||
abs_paths.insert(0, extra_dir)
|
||||
|
||||
for path in abs_paths:
|
||||
strategy = self._search_strategy(path, strategy_name)
|
||||
if strategy:
|
||||
logger.info('Using resolved strategy %s from \'%s\'', strategy_name, path)
|
||||
return strategy
|
||||
|
||||
raise ImportError(
|
||||
"Impossible to load Strategy '{}'. This class does not exist"
|
||||
" or contains Python code errors".format(strategy_name)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _get_valid_strategies(module_path: str, strategy_name: str) -> Optional[Type[IStrategy]]:
|
||||
"""
|
||||
Returns a list of all possible strategies for the given module_path
|
||||
:param module_path: absolute path to the module
|
||||
:param strategy_name: Class name of the strategy
|
||||
:return: Tuple with (name, class) or None
|
||||
"""
|
||||
|
||||
# Generate spec based on absolute path
|
||||
spec = importlib.util.spec_from_file_location('user_data.strategies', module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
valid_strategies_gen = (
|
||||
obj for name, obj in inspect.getmembers(module, inspect.isclass)
|
||||
if strategy_name == name and IStrategy in obj.__bases__
|
||||
)
|
||||
return next(valid_strategies_gen, None)
|
||||
|
||||
@staticmethod
|
||||
def _search_strategy(directory: str, strategy_name: str) -> Optional[IStrategy]:
|
||||
"""
|
||||
Search for the strategy_name in the given directory
|
||||
:param directory: relative or absolute directory path
|
||||
:return: name of the strategy class
|
||||
"""
|
||||
logger.debug('Searching for strategy %s in \'%s\'', strategy_name, directory)
|
||||
for entry in os.listdir(directory):
|
||||
# Only consider python files
|
||||
if not entry.endswith('.py'):
|
||||
logger.debug('Ignoring %s', entry)
|
||||
continue
|
||||
strategy = StrategyResolver._get_valid_strategies(
|
||||
os.path.abspath(os.path.join(directory, entry)), strategy_name
|
||||
)
|
||||
if strategy:
|
||||
return strategy()
|
||||
return None
|
||||
0
freqtrade/tests/__init__.py
Normal file
0
freqtrade/tests/__init__.py
Normal file
431
freqtrade/tests/conftest.py
Normal file
431
freqtrade/tests/conftest.py
Normal file
@@ -0,0 +1,431 @@
|
||||
# pragma pylint: disable=missing-docstring
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from functools import reduce
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import arrow
|
||||
import pytest
|
||||
from jsonschema import validate
|
||||
from sqlalchemy import create_engine
|
||||
from telegram import Chat, Message, Update
|
||||
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade import constants
|
||||
from freqtrade.freqtradebot import FreqtradeBot
|
||||
|
||||
logging.getLogger('').setLevel(logging.INFO)
|
||||
|
||||
|
||||
def log_has(line, logs):
|
||||
# caplog mocker returns log as a tuple: ('freqtrade.analyze', logging.WARNING, 'foobar')
|
||||
# and we want to match line against foobar in the tuple
|
||||
return reduce(lambda a, b: a or b,
|
||||
filter(lambda x: x[2] == line, logs),
|
||||
False)
|
||||
|
||||
|
||||
# Functions for recurrent object patching
|
||||
def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
|
||||
"""
|
||||
This function patch _init_modules() to not call dependencies
|
||||
:param mocker: a Mocker object to apply patches
|
||||
:param config: Config to pass to the bot
|
||||
:return: None
|
||||
"""
|
||||
mocker.patch('freqtrade.fiat_convert.Market', {'price_usd': 12345.0})
|
||||
mocker.patch('freqtrade.freqtradebot.Analyze', MagicMock())
|
||||
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
|
||||
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
|
||||
mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock())
|
||||
mocker.patch('freqtrade.freqtradebot.RPCManager._init', MagicMock())
|
||||
mocker.patch('freqtrade.freqtradebot.RPCManager.send_msg', MagicMock())
|
||||
mocker.patch('freqtrade.freqtradebot.Analyze.get_signal', MagicMock())
|
||||
|
||||
return FreqtradeBot(config, create_engine('sqlite://'))
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def default_conf():
|
||||
""" Returns validated configuration suitable for most tests """
|
||||
configuration = {
|
||||
"max_open_trades": 1,
|
||||
"stake_currency": "BTC",
|
||||
"stake_amount": 0.001,
|
||||
"fiat_display_currency": "USD",
|
||||
"ticker_interval": 5,
|
||||
"dry_run": True,
|
||||
"minimal_roi": {
|
||||
"40": 0.0,
|
||||
"30": 0.01,
|
||||
"20": 0.02,
|
||||
"0": 0.04
|
||||
},
|
||||
"stoploss": -0.10,
|
||||
"unfilledtimeout": 600,
|
||||
"bid_strategy": {
|
||||
"ask_last_balance": 0.0
|
||||
},
|
||||
"exchange": {
|
||||
"name": "bittrex",
|
||||
"enabled": True,
|
||||
"key": "key",
|
||||
"secret": "secret",
|
||||
"pair_whitelist": [
|
||||
"BTC_ETH",
|
||||
"BTC_TKN",
|
||||
"BTC_TRST",
|
||||
"BTC_SWT",
|
||||
"BTC_BCC"
|
||||
]
|
||||
},
|
||||
"telegram": {
|
||||
"enabled": True,
|
||||
"token": "token",
|
||||
"chat_id": "0"
|
||||
},
|
||||
"initial_state": "running",
|
||||
"loglevel": logging.DEBUG
|
||||
}
|
||||
validate(configuration, constants.CONF_SCHEMA)
|
||||
return configuration
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def update():
|
||||
_update = Update(0)
|
||||
_update.message = Message(0, 0, datetime.utcnow(), Chat(0, 0))
|
||||
return _update
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ticker():
|
||||
return MagicMock(return_value={
|
||||
'bid': 0.00001098,
|
||||
'ask': 0.00001099,
|
||||
'last': 0.00001098,
|
||||
})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ticker_sell_up():
|
||||
return MagicMock(return_value={
|
||||
'bid': 0.00001172,
|
||||
'ask': 0.00001173,
|
||||
'last': 0.00001172,
|
||||
})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ticker_sell_down():
|
||||
return MagicMock(return_value={
|
||||
'bid': 0.00001044,
|
||||
'ask': 0.00001043,
|
||||
'last': 0.00001044,
|
||||
})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def health():
|
||||
return MagicMock(return_value=[{
|
||||
'Currency': 'BTC',
|
||||
'IsActive': True,
|
||||
'LastChecked': '2017-11-13T20:15:00.00',
|
||||
'Notice': None
|
||||
}, {
|
||||
'Currency': 'ETH',
|
||||
'IsActive': True,
|
||||
'LastChecked': '2017-11-13T20:15:00.00',
|
||||
'Notice': None
|
||||
}, {
|
||||
'Currency': 'TRST',
|
||||
'IsActive': True,
|
||||
'LastChecked': '2017-11-13T20:15:00.00',
|
||||
'Notice': None
|
||||
}, {
|
||||
'Currency': 'SWT',
|
||||
'IsActive': True,
|
||||
'LastChecked': '2017-11-13T20:15:00.00',
|
||||
'Notice': None
|
||||
}, {
|
||||
'Currency': 'BCC',
|
||||
'IsActive': False,
|
||||
'LastChecked': '2017-11-13T20:15:00.00',
|
||||
'Notice': None
|
||||
}])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def limit_buy_order():
|
||||
return {
|
||||
'id': 'mocked_limit_buy',
|
||||
'type': 'LIMIT_BUY',
|
||||
'pair': 'mocked',
|
||||
'opened': str(arrow.utcnow().datetime),
|
||||
'rate': 0.00001099,
|
||||
'amount': 90.99181073,
|
||||
'remaining': 0.0,
|
||||
'closed': str(arrow.utcnow().datetime),
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def limit_buy_order_old():
|
||||
return {
|
||||
'id': 'mocked_limit_buy_old',
|
||||
'type': 'LIMIT_BUY',
|
||||
'pair': 'BTC_ETH',
|
||||
'opened': str(arrow.utcnow().shift(minutes=-601).datetime),
|
||||
'rate': 0.00001099,
|
||||
'amount': 90.99181073,
|
||||
'remaining': 90.99181073,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def limit_sell_order_old():
|
||||
return {
|
||||
'id': 'mocked_limit_sell_old',
|
||||
'type': 'LIMIT_SELL',
|
||||
'pair': 'BTC_ETH',
|
||||
'opened': str(arrow.utcnow().shift(minutes=-601).datetime),
|
||||
'rate': 0.00001099,
|
||||
'amount': 90.99181073,
|
||||
'remaining': 90.99181073,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def limit_buy_order_old_partial():
|
||||
return {
|
||||
'id': 'mocked_limit_buy_old_partial',
|
||||
'type': 'LIMIT_BUY',
|
||||
'pair': 'BTC_ETH',
|
||||
'opened': str(arrow.utcnow().shift(minutes=-601).datetime),
|
||||
'rate': 0.00001099,
|
||||
'amount': 90.99181073,
|
||||
'remaining': 67.99181073,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def limit_sell_order():
|
||||
return {
|
||||
'id': 'mocked_limit_sell',
|
||||
'type': 'LIMIT_SELL',
|
||||
'pair': 'mocked',
|
||||
'opened': str(arrow.utcnow().datetime),
|
||||
'rate': 0.00001173,
|
||||
'amount': 90.99181073,
|
||||
'remaining': 0.0,
|
||||
'closed': str(arrow.utcnow().datetime),
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ticker_history():
|
||||
return [
|
||||
{
|
||||
"O": 8.794e-05,
|
||||
"H": 8.948e-05,
|
||||
"L": 8.794e-05,
|
||||
"C": 8.88e-05,
|
||||
"V": 991.09056638,
|
||||
"T": "2017-11-26T08:50:00",
|
||||
"BV": 0.0877869
|
||||
},
|
||||
{
|
||||
"O": 8.88e-05,
|
||||
"H": 8.942e-05,
|
||||
"L": 8.88e-05,
|
||||
"C": 8.893e-05,
|
||||
"V": 658.77935965,
|
||||
"T": "2017-11-26T08:55:00",
|
||||
"BV": 0.05874751
|
||||
},
|
||||
{
|
||||
"O": 8.891e-05,
|
||||
"H": 8.893e-05,
|
||||
"L": 8.875e-05,
|
||||
"C": 8.877e-05,
|
||||
"V": 7920.73570705,
|
||||
"T": "2017-11-26T09:00:00",
|
||||
"BV": 0.7039405
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ticker_history_without_bv():
|
||||
return [
|
||||
{
|
||||
"O": 8.794e-05,
|
||||
"H": 8.948e-05,
|
||||
"L": 8.794e-05,
|
||||
"C": 8.88e-05,
|
||||
"V": 991.09056638,
|
||||
"T": "2017-11-26T08:50:00"
|
||||
},
|
||||
{
|
||||
"O": 8.88e-05,
|
||||
"H": 8.942e-05,
|
||||
"L": 8.88e-05,
|
||||
"C": 8.893e-05,
|
||||
"V": 658.77935965,
|
||||
"T": "2017-11-26T08:55:00"
|
||||
},
|
||||
{
|
||||
"O": 8.891e-05,
|
||||
"H": 8.893e-05,
|
||||
"L": 8.875e-05,
|
||||
"C": 8.877e-05,
|
||||
"V": 7920.73570705,
|
||||
"T": "2017-11-26T09:00:00"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
# FIX: Perhaps change result fixture to use BTC_UNITEST instead?
|
||||
@pytest.fixture
|
||||
def result():
|
||||
with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file:
|
||||
return Analyze.parse_ticker_dataframe(json.load(data_file))
|
||||
|
||||
|
||||
# FIX:
|
||||
# Create an fixture/function
|
||||
# that inserts a trade of some type and open-status
|
||||
# return the open-order-id
|
||||
# See tests in rpc/main that could use this
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def get_market_summaries_data():
|
||||
"""
|
||||
This fixture is a real result from exchange.get_market_summaries() but reduced to only
|
||||
8 entries. 4 BTC, 4 USTD
|
||||
:return: JSON market summaries
|
||||
"""
|
||||
return [
|
||||
{
|
||||
'Ask': 1.316e-05,
|
||||
'BaseVolume': 5.72599471,
|
||||
'Bid': 1.3e-05,
|
||||
'Created': '2014-04-14T00:00:00',
|
||||
'High': 1.414e-05,
|
||||
'Last': 1.298e-05,
|
||||
'Low': 1.282e-05,
|
||||
'MarketName': 'BTC-XWC',
|
||||
'OpenBuyOrders': 2000,
|
||||
'OpenSellOrders': 1484,
|
||||
'PrevDay': 1.376e-05,
|
||||
'TimeStamp': '2018-02-05T01:32:40.493',
|
||||
'Volume': 424041.21418375
|
||||
},
|
||||
{
|
||||
'Ask': 0.00627051,
|
||||
'BaseVolume': 93.23302388,
|
||||
'Bid': 0.00618192,
|
||||
'Created': '2016-10-20T04:48:30.387',
|
||||
'High': 0.00669897,
|
||||
'Last': 0.00618192,
|
||||
'Low': 0.006,
|
||||
'MarketName': 'BTC-XZC',
|
||||
'OpenBuyOrders': 343,
|
||||
'OpenSellOrders': 2037,
|
||||
'PrevDay': 0.00668229,
|
||||
'TimeStamp': '2018-02-05T01:32:43.383',
|
||||
'Volume': 14863.60730702
|
||||
},
|
||||
{
|
||||
'Ask': 0.01137247,
|
||||
'BaseVolume': 383.55922657,
|
||||
'Bid': 0.01136006,
|
||||
'Created': '2016-11-15T20:29:59.73',
|
||||
'High': 0.012,
|
||||
'Last': 0.01137247,
|
||||
'Low': 0.01119883,
|
||||
'MarketName': 'BTC-ZCL',
|
||||
'OpenBuyOrders': 1332,
|
||||
'OpenSellOrders': 5317,
|
||||
'PrevDay': 0.01179603,
|
||||
'TimeStamp': '2018-02-05T01:32:42.773',
|
||||
'Volume': 33308.07358285
|
||||
},
|
||||
{
|
||||
'Ask': 0.04155821,
|
||||
'BaseVolume': 274.75369074,
|
||||
'Bid': 0.04130002,
|
||||
'Created': '2016-10-28T17:13:10.833',
|
||||
'High': 0.04354429,
|
||||
'Last': 0.041585,
|
||||
'Low': 0.0413,
|
||||
'MarketName': 'BTC-ZEC',
|
||||
'OpenBuyOrders': 863,
|
||||
'OpenSellOrders': 5579,
|
||||
'PrevDay': 0.0429,
|
||||
'TimeStamp': '2018-02-05T01:32:43.21',
|
||||
'Volume': 6479.84033259
|
||||
},
|
||||
{
|
||||
'Ask': 210.99999999,
|
||||
'BaseVolume': 615132.70989532,
|
||||
'Bid': 210.05503736,
|
||||
'Created': '2017-07-21T01:08:49.397',
|
||||
'High': 257.396,
|
||||
'Last': 211.0,
|
||||
'Low': 209.05333589,
|
||||
'MarketName': 'USDT-XMR',
|
||||
'OpenBuyOrders': 180,
|
||||
'OpenSellOrders': 1203,
|
||||
'PrevDay': 247.93528899,
|
||||
'TimeStamp': '2018-02-05T01:32:43.117',
|
||||
'Volume': 2688.17410793
|
||||
},
|
||||
{
|
||||
'Ask': 0.79589979,
|
||||
'BaseVolume': 9349557.01853031,
|
||||
'Bid': 0.789226,
|
||||
'Created': '2017-07-14T17:10:10.737',
|
||||
'High': 0.977,
|
||||
'Last': 0.79589979,
|
||||
'Low': 0.781,
|
||||
'MarketName': 'USDT-XRP',
|
||||
'OpenBuyOrders': 1075,
|
||||
'OpenSellOrders': 6508,
|
||||
'PrevDay': 0.93300218,
|
||||
'TimeStamp': '2018-02-05T01:32:42.383',
|
||||
'Volume': 10801663.00788851
|
||||
},
|
||||
{
|
||||
'Ask': 0.05154982,
|
||||
'BaseVolume': 2311087.71232136,
|
||||
'Bid': 0.05040107,
|
||||
'Created': '2017-12-29T19:29:18.357',
|
||||
'High': 0.06668561,
|
||||
'Last': 0.0508,
|
||||
'Low': 0.05006731,
|
||||
'MarketName': 'USDT-XVG',
|
||||
'OpenBuyOrders': 655,
|
||||
'OpenSellOrders': 5544,
|
||||
'PrevDay': 0.0627,
|
||||
'TimeStamp': '2018-02-05T01:32:41.507',
|
||||
'Volume': 40031424.2152716
|
||||
},
|
||||
{
|
||||
'Ask': 332.65500022,
|
||||
'BaseVolume': 562911.87455665,
|
||||
'Bid': 330.00000001,
|
||||
'Created': '2017-07-14T17:10:10.673',
|
||||
'High': 401.59999999,
|
||||
'Last': 332.65500019,
|
||||
'Low': 330.0,
|
||||
'MarketName': 'USDT-ZEC',
|
||||
'OpenBuyOrders': 161,
|
||||
'OpenSellOrders': 1731,
|
||||
'PrevDay': 391.42,
|
||||
'TimeStamp': '2018-02-05T01:32:42.947',
|
||||
'Volume': 1571.09647946
|
||||
}
|
||||
]
|
||||
286
freqtrade/tests/exchange/test_exchange.py
Normal file
286
freqtrade/tests/exchange/test_exchange.py
Normal file
@@ -0,0 +1,286 @@
|
||||
# pragma pylint: disable=missing-docstring, C0103, bad-continuation, global-statement
|
||||
# pragma pylint: disable=protected-access
|
||||
import logging
|
||||
from random import randint
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
import freqtrade.exchange as exchange
|
||||
from freqtrade import OperationalException
|
||||
from freqtrade.exchange import init, validate_pairs, buy, sell, get_balance, get_balances, \
|
||||
get_ticker, get_ticker_history, cancel_order, get_name, get_fee
|
||||
from freqtrade.tests.conftest import log_has
|
||||
|
||||
API_INIT = False
|
||||
|
||||
|
||||
def maybe_init_api(conf, mocker, force=False):
|
||||
global API_INIT
|
||||
if force or not API_INIT:
|
||||
mocker.patch('freqtrade.exchange.validate_pairs',
|
||||
side_effect=lambda s: True)
|
||||
init(config=conf)
|
||||
API_INIT = True
|
||||
|
||||
|
||||
def test_init(default_conf, mocker, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
maybe_init_api(default_conf, mocker, True)
|
||||
assert log_has('Instance is running with dry_run enabled', caplog.record_tuples)
|
||||
|
||||
|
||||
def test_init_exception(default_conf):
|
||||
default_conf['exchange']['name'] = 'wrong_exchange_name'
|
||||
|
||||
with pytest.raises(
|
||||
OperationalException,
|
||||
match='Exchange {} is not supported'.format(default_conf['exchange']['name'])):
|
||||
init(config=default_conf)
|
||||
|
||||
|
||||
def test_validate_pairs(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_markets = MagicMock(return_value=[
|
||||
'BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT', 'BTC_BCC',
|
||||
])
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
validate_pairs(default_conf['exchange']['pair_whitelist'])
|
||||
|
||||
|
||||
def test_validate_pairs_not_available(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_markets = MagicMock(return_value=[])
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
with pytest.raises(OperationalException, match=r'not available'):
|
||||
validate_pairs(default_conf['exchange']['pair_whitelist'])
|
||||
|
||||
|
||||
def test_validate_pairs_not_compatible(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_markets = MagicMock(
|
||||
return_value=['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT'])
|
||||
default_conf['stake_currency'] = 'ETH'
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
with pytest.raises(OperationalException, match=r'not compatible'):
|
||||
validate_pairs(default_conf['exchange']['pair_whitelist'])
|
||||
|
||||
|
||||
def test_validate_pairs_exception(default_conf, mocker, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_markets = MagicMock(side_effect=RequestException())
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
# with pytest.raises(RequestException, match=r'Unable to validate pairs'):
|
||||
validate_pairs(default_conf['exchange']['pair_whitelist'])
|
||||
assert log_has('Unable to validate pairs (assuming they are correct). Reason: ',
|
||||
caplog.record_tuples)
|
||||
|
||||
|
||||
def test_buy_dry_run(default_conf, mocker):
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert 'dry_run_buy_' in buy(pair='BTC_ETH', rate=200, amount=1)
|
||||
|
||||
|
||||
def test_buy_prod(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
api_mock.buy = MagicMock(
|
||||
return_value='dry_run_buy_{}'.format(randint(0, 10**6)))
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
default_conf['dry_run'] = False
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert 'dry_run_buy_' in buy(pair='BTC_ETH', rate=200, amount=1)
|
||||
|
||||
|
||||
def test_sell_dry_run(default_conf, mocker):
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert 'dry_run_sell_' in sell(pair='BTC_ETH', rate=200, amount=1)
|
||||
|
||||
|
||||
def test_sell_prod(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
api_mock.sell = MagicMock(
|
||||
return_value='dry_run_sell_{}'.format(randint(0, 10**6)))
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
default_conf['dry_run'] = False
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert 'dry_run_sell_' in sell(pair='BTC_ETH', rate=200, amount=1)
|
||||
|
||||
|
||||
def test_get_balance_dry_run(default_conf, mocker):
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert get_balance(currency='BTC') == 999.9
|
||||
|
||||
|
||||
def test_get_balance_prod(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_balance = MagicMock(return_value=123.4)
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
default_conf['dry_run'] = False
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert get_balance(currency='BTC') == 123.4
|
||||
|
||||
|
||||
def test_get_balances_dry_run(default_conf, mocker):
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert get_balances() == []
|
||||
|
||||
|
||||
def test_get_balances_prod(default_conf, mocker):
|
||||
balance_item = {
|
||||
'Currency': '1ST',
|
||||
'Balance': 10.0,
|
||||
'Available': 10.0,
|
||||
'Pending': 0.0,
|
||||
'CryptoAddress': None
|
||||
}
|
||||
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_balances = MagicMock(
|
||||
return_value=[balance_item, balance_item, balance_item])
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
default_conf['dry_run'] = False
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert len(get_balances()) == 3
|
||||
assert get_balances()[0]['Currency'] == '1ST'
|
||||
assert get_balances()[0]['Balance'] == 10.0
|
||||
assert get_balances()[0]['Available'] == 10.0
|
||||
assert get_balances()[0]['Pending'] == 0.0
|
||||
|
||||
|
||||
# This test is somewhat redundant with
|
||||
# test_exchange_bittrex.py::test_exchange_bittrex_get_ticker
|
||||
def test_get_ticker(default_conf, mocker):
|
||||
maybe_init_api(default_conf, mocker)
|
||||
api_mock = MagicMock()
|
||||
tick = {"success": True, 'result': {'Bid': 0.00001098, 'Ask': 0.00001099, 'Last': 0.0001}}
|
||||
api_mock.get_ticker = MagicMock(return_value=tick)
|
||||
mocker.patch('freqtrade.exchange.bittrex._API', api_mock)
|
||||
|
||||
# retrieve original ticker
|
||||
ticker = get_ticker(pair='BTC_ETH')
|
||||
assert ticker['bid'] == 0.00001098
|
||||
assert ticker['ask'] == 0.00001099
|
||||
|
||||
# change the ticker
|
||||
tick = {"success": True, 'result': {"Bid": 0.5, "Ask": 1, "Last": 42}}
|
||||
api_mock.get_ticker = MagicMock(return_value=tick)
|
||||
mocker.patch('freqtrade.exchange.bittrex._API', api_mock)
|
||||
|
||||
# if not caching the result we should get the same ticker
|
||||
# if not fetching a new result we should get the cached ticker
|
||||
ticker = get_ticker(pair='BTC_ETH', refresh=False)
|
||||
assert ticker['bid'] == 0.00001098
|
||||
assert ticker['ask'] == 0.00001099
|
||||
|
||||
# force ticker refresh
|
||||
ticker = get_ticker(pair='BTC_ETH', refresh=True)
|
||||
assert ticker['bid'] == 0.5
|
||||
assert ticker['ask'] == 1
|
||||
|
||||
|
||||
def test_get_ticker_history(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
tick = 123
|
||||
api_mock.get_ticker_history = MagicMock(return_value=tick)
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
# retrieve original ticker
|
||||
ticks = get_ticker_history('BTC_ETH', int(default_conf['ticker_interval']))
|
||||
assert ticks == 123
|
||||
|
||||
# change the ticker
|
||||
tick = 999
|
||||
api_mock.get_ticker_history = MagicMock(return_value=tick)
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
|
||||
# ensure caching will still return the original ticker
|
||||
ticks = get_ticker_history('BTC_ETH', int(default_conf['ticker_interval']))
|
||||
assert ticks == 123
|
||||
|
||||
|
||||
def test_cancel_order_dry_run(default_conf, mocker):
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
|
||||
assert cancel_order(order_id='123') is None
|
||||
|
||||
|
||||
# Ensure that if not dry_run, we should call API
|
||||
def test_cancel_order(default_conf, mocker):
|
||||
default_conf['dry_run'] = False
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
api_mock = MagicMock()
|
||||
api_mock.cancel_order = MagicMock(return_value=123)
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
assert cancel_order(order_id='_') == 123
|
||||
|
||||
|
||||
def test_get_order(default_conf, mocker):
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
order = MagicMock()
|
||||
order.myid = 123
|
||||
exchange._DRY_RUN_OPEN_ORDERS['X'] = order
|
||||
print(exchange.get_order('X'))
|
||||
assert exchange.get_order('X').myid == 123
|
||||
|
||||
default_conf['dry_run'] = False
|
||||
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
|
||||
api_mock = MagicMock()
|
||||
api_mock.get_order = MagicMock(return_value=456)
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
assert exchange.get_order('X') == 456
|
||||
|
||||
|
||||
def test_get_name(default_conf, mocker):
|
||||
mocker.patch('freqtrade.exchange.validate_pairs',
|
||||
side_effect=lambda s: True)
|
||||
default_conf['exchange']['name'] = 'bittrex'
|
||||
init(default_conf)
|
||||
|
||||
assert get_name() == 'Bittrex'
|
||||
|
||||
|
||||
def test_get_fee(default_conf, mocker):
|
||||
mocker.patch('freqtrade.exchange.validate_pairs',
|
||||
side_effect=lambda s: True)
|
||||
init(default_conf)
|
||||
|
||||
assert get_fee() == 0.0025
|
||||
|
||||
|
||||
def test_exchange_misc(mocker):
|
||||
api_mock = MagicMock()
|
||||
mocker.patch('freqtrade.exchange._API', api_mock)
|
||||
exchange.get_markets()
|
||||
assert api_mock.get_markets.call_count == 1
|
||||
exchange.get_market_summaries()
|
||||
assert api_mock.get_market_summaries.call_count == 1
|
||||
api_mock.name = 123
|
||||
assert exchange.get_name() == 123
|
||||
api_mock.fee = 456
|
||||
assert exchange.get_fee() == 456
|
||||
exchange.get_wallet_health()
|
||||
assert api_mock.get_wallet_health.call_count == 1
|
||||
349
freqtrade/tests/exchange/test_exchange_bittrex.py
Normal file
349
freqtrade/tests/exchange/test_exchange_bittrex.py
Normal file
@@ -0,0 +1,349 @@
|
||||
# pragma pylint: disable=missing-docstring, C0103, protected-access, unused-argument
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from requests.exceptions import ContentDecodingError
|
||||
|
||||
import freqtrade.exchange.bittrex as btx
|
||||
from freqtrade.exchange.bittrex import Bittrex
|
||||
|
||||
|
||||
# Eat this flake8
|
||||
# +------------------+
|
||||
# | bittrex.Bittrex |
|
||||
# +------------------+
|
||||
# |
|
||||
# (mock Fake_bittrex)
|
||||
# |
|
||||
# +-----------------------------+
|
||||
# | freqtrade.exchange.Bittrex |
|
||||
# +-----------------------------+
|
||||
# Call into Bittrex will flow up to the
|
||||
# external package bittrex.Bittrex.
|
||||
# By inserting a mock, we redirect those
|
||||
# calls.
|
||||
# The faked bittrex API is called just 'fb'
|
||||
# The freqtrade.exchange.Bittrex is a
|
||||
# wrapper, and is called 'wb'
|
||||
|
||||
|
||||
def _stub_config():
|
||||
return {'key': '',
|
||||
'secret': ''}
|
||||
|
||||
|
||||
class FakeBittrex():
|
||||
def __init__(self, success=True):
|
||||
self.success = True # Believe in yourself
|
||||
self.result = None
|
||||
self.get_ticker_call_count = 0
|
||||
# This is really ugly, doing side-effect during instance creation
|
||||
# But we're allowed to in testing-code
|
||||
btx._API = MagicMock()
|
||||
btx._API.buy_limit = self.fake_buysell_limit
|
||||
btx._API.sell_limit = self.fake_buysell_limit
|
||||
btx._API.get_balance = self.fake_get_balance
|
||||
btx._API.get_balances = self.fake_get_balances
|
||||
btx._API.get_ticker = self.fake_get_ticker
|
||||
btx._API.get_order = self.fake_get_order
|
||||
btx._API.cancel = self.fake_cancel_order
|
||||
btx._API.get_markets = self.fake_get_markets
|
||||
btx._API.get_market_summaries = self.fake_get_market_summaries
|
||||
btx._API_V2 = MagicMock()
|
||||
btx._API_V2.get_candles = self.fake_get_candles
|
||||
btx._API_V2.get_wallet_health = self.fake_get_wallet_health
|
||||
|
||||
def fake_buysell_limit(self, pair, amount, limit):
|
||||
return {'success': self.success,
|
||||
'result': {'uuid': '1234'},
|
||||
'message': 'barter'}
|
||||
|
||||
def fake_get_balance(self, cur):
|
||||
return {'success': self.success,
|
||||
'result': {'Balance': 1234},
|
||||
'message': 'unbalanced'}
|
||||
|
||||
def fake_get_balances(self):
|
||||
return {'success': self.success,
|
||||
'result': [{'BTC_ETH': 1234}],
|
||||
'message': 'no balances'}
|
||||
|
||||
def fake_get_ticker(self, pair):
|
||||
self.get_ticker_call_count += 1
|
||||
return self.result or {'success': self.success,
|
||||
'result': {'Bid': 1, 'Ask': 1, 'Last': 1},
|
||||
'message': 'NO_API_RESPONSE'}
|
||||
|
||||
def fake_get_candles(self, pair, interval):
|
||||
return self.result or {'success': self.success,
|
||||
'result': [{'C': 0, 'V': 0, 'O': 0, 'H': 0, 'L': 0, 'T': 0}],
|
||||
'message': 'candles lit'}
|
||||
|
||||
def fake_get_order(self, uuid):
|
||||
return {'success': self.success,
|
||||
'result': {'OrderUuid': 'ABC123',
|
||||
'Type': 'Type',
|
||||
'Exchange': 'BTC_ETH',
|
||||
'Opened': True,
|
||||
'PricePerUnit': 1,
|
||||
'Quantity': 1,
|
||||
'QuantityRemaining': 1,
|
||||
'Closed': True},
|
||||
'message': 'lost'}
|
||||
|
||||
def fake_cancel_order(self, uuid):
|
||||
return self.result or {'success': self.success,
|
||||
'message': 'no such order'}
|
||||
|
||||
def fake_get_markets(self):
|
||||
return self.result or {'success': self.success,
|
||||
'message': 'market gone',
|
||||
'result': [{'MarketName': '-_'}]}
|
||||
|
||||
def fake_get_market_summaries(self):
|
||||
return self.result or {'success': self.success,
|
||||
'message': 'no summary',
|
||||
'result': ['sum']}
|
||||
|
||||
def fake_get_wallet_health(self):
|
||||
return self.result or {'success': self.success,
|
||||
'message': 'bad health',
|
||||
'result': [{'Health': {'Currency': 'BTC_ETH',
|
||||
'IsActive': True,
|
||||
'LastChecked': 0},
|
||||
'Currency': {'Notice': True}}]}
|
||||
|
||||
|
||||
# The freqtrade.exchange.bittrex is called wrap_bittrex
|
||||
# to not confuse naming with bittrex.bittrex
|
||||
def make_wrap_bittrex():
|
||||
conf = _stub_config()
|
||||
wb = btx.Bittrex(conf)
|
||||
return wb
|
||||
|
||||
|
||||
def test_exchange_bittrex_class():
|
||||
conf = _stub_config()
|
||||
b = Bittrex(conf)
|
||||
assert isinstance(b, Bittrex)
|
||||
slots = dir(b)
|
||||
for name in ['fee', 'buy', 'sell', 'get_balance', 'get_balances',
|
||||
'get_ticker', 'get_ticker_history', 'get_order',
|
||||
'cancel_order', 'get_pair_detail_url', 'get_markets',
|
||||
'get_market_summaries', 'get_wallet_health']:
|
||||
assert name in slots
|
||||
# FIX: ensure that the slot is also a method in the class
|
||||
# getattr(b, name) => bound method Bittrex.buy
|
||||
# type(getattr(b, name)) => class 'method'
|
||||
|
||||
|
||||
def test_exchange_bittrex_fee():
|
||||
fee = Bittrex.fee.__get__(Bittrex)
|
||||
assert fee >= 0 and fee < 0.1 # Fee is 0-10 %
|
||||
|
||||
|
||||
def test_exchange_bittrex_buy_good():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
uuid = wb.buy('BTC_ETH', 1, 1)
|
||||
assert uuid == fb.fake_buysell_limit(1, 2, 3)['result']['uuid']
|
||||
|
||||
fb.success = False
|
||||
with pytest.raises(btx.OperationalException, match=r'barter.*'):
|
||||
wb.buy('BAD', 1, 1)
|
||||
|
||||
|
||||
def test_exchange_bittrex_sell_good():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
uuid = wb.sell('BTC_ETH', 1, 1)
|
||||
assert uuid == fb.fake_buysell_limit(1, 2, 3)['result']['uuid']
|
||||
|
||||
fb.success = False
|
||||
with pytest.raises(btx.OperationalException, match=r'barter.*'):
|
||||
uuid = wb.sell('BAD', 1, 1)
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_balance():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
bal = wb.get_balance('BTC_ETH')
|
||||
assert bal == fb.fake_get_balance(1)['result']['Balance']
|
||||
|
||||
fb.success = False
|
||||
with pytest.raises(btx.OperationalException, match=r'unbalanced'):
|
||||
wb.get_balance('BTC_ETH')
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_balances():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
bals = wb.get_balances()
|
||||
assert bals == fb.fake_get_balances()['result']
|
||||
|
||||
fb.success = False
|
||||
with pytest.raises(btx.OperationalException, match=r'no balances'):
|
||||
wb.get_balances()
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_ticker():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
|
||||
# Poll ticker, which updates the cache
|
||||
tick = wb.get_ticker('BTC_ETH')
|
||||
for x in ['bid', 'ask', 'last']:
|
||||
assert x in tick
|
||||
# Ensure the side-effect was made (update the ticker cache)
|
||||
assert 'BTC_ETH' in wb.cached_ticker.keys()
|
||||
|
||||
# taint the cache, so we can recognize the cache wall utilized
|
||||
wb.cached_ticker['BTC_ETH']['bid'] = 1234
|
||||
# Poll again, getting the cached result
|
||||
fb.get_ticker_call_count = 0
|
||||
tick = wb.get_ticker('BTC_ETH', False)
|
||||
# Ensure the result was from the cache, and that we didn't call exchange
|
||||
assert wb.cached_ticker['BTC_ETH']['bid'] == 1234
|
||||
assert fb.get_ticker_call_count == 0
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_ticker_bad():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
fb.result = {'success': True, 'result': {'Bid': 1, 'Ask': 0}} # incomplete result
|
||||
|
||||
with pytest.raises(ContentDecodingError, match=r'.*Invalid response from Bittrex params.*'):
|
||||
wb.get_ticker('BTC_ETH')
|
||||
fb.result = {'success': False, 'message': 'gone bad'}
|
||||
with pytest.raises(btx.OperationalException, match=r'.*gone bad.*'):
|
||||
wb.get_ticker('BTC_ETH')
|
||||
|
||||
fb.result = {'success': True, 'result': {}} # incomplete result
|
||||
with pytest.raises(ContentDecodingError, match=r'.*Invalid response from Bittrex params.*'):
|
||||
wb.get_ticker('BTC_ETH')
|
||||
fb.result = {'success': False, 'message': 'gone bad'}
|
||||
with pytest.raises(btx.OperationalException, match=r'.*gone bad.*'):
|
||||
wb.get_ticker('BTC_ETH')
|
||||
|
||||
fb.result = {'success': True,
|
||||
'result': {'Bid': 1, 'Ask': 0, 'Last': None}} # incomplete result
|
||||
with pytest.raises(ContentDecodingError, match=r'.*Invalid response from Bittrex params.*'):
|
||||
wb.get_ticker('BTC_ETH')
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_ticker_history_intervals():
|
||||
wb = make_wrap_bittrex()
|
||||
FakeBittrex()
|
||||
for tick_interval in [1, 5, 30, 60, 1440]:
|
||||
assert ([{'C': 0, 'V': 0, 'O': 0, 'H': 0, 'L': 0, 'T': 0}] ==
|
||||
wb.get_ticker_history('BTC_ETH', tick_interval))
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_ticker_history():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
assert wb.get_ticker_history('BTC_ETH', 5)
|
||||
with pytest.raises(ValueError, match=r'.*Unknown tick_interval.*'):
|
||||
wb.get_ticker_history('BTC_ETH', 2)
|
||||
|
||||
fb.success = False
|
||||
with pytest.raises(btx.OperationalException, match=r'candles lit.*'):
|
||||
wb.get_ticker_history('BTC_ETH', 5)
|
||||
|
||||
fb.success = True
|
||||
with pytest.raises(ContentDecodingError, match=r'.*Invalid response from Bittrex.*'):
|
||||
fb.result = {'bad': 0}
|
||||
wb.get_ticker_history('BTC_ETH', 5)
|
||||
|
||||
with pytest.raises(ContentDecodingError, match=r'.*Required property C not present.*'):
|
||||
fb.result = {'success': True,
|
||||
'result': [{'V': 0, 'O': 0, 'H': 0, 'L': 0, 'T': 0}], # close is missing
|
||||
'message': 'candles lit'}
|
||||
wb.get_ticker_history('BTC_ETH', 5)
|
||||
|
||||
|
||||
def test_exchange_bittrex_get_order():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
order = wb.get_order('someUUID')
|
||||
assert order['id'] == 'ABC123'
|
||||
fb.success = False
|
||||
with pytest.raises(btx.OperationalException, match=r'lost'):
|
||||
wb.get_order('someUUID')
|
||||
|
||||
|
||||
def test_exchange_bittrex_cancel_order():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
wb.cancel_order('someUUID')
|
||||
with pytest.raises(btx.OperationalException, match=r'no such order'):
|
||||
fb.success = False
|
||||
wb.cancel_order('someUUID')
|
||||
# Note: this can be a bug in exchange.bittrex._validate_response
|
||||
with pytest.raises(KeyError):
|
||||
fb.result = {'success': False} # message is missing!
|
||||
wb.cancel_order('someUUID')
|
||||
with pytest.raises(btx.OperationalException, match=r'foo'):
|
||||
fb.result = {'success': False, 'message': 'foo'}
|
||||
wb.cancel_order('someUUID')
|
||||
|
||||
|
||||
def test_exchange_get_pair_detail_url():
|
||||
wb = make_wrap_bittrex()
|
||||
assert wb.get_pair_detail_url('BTC_ETH')
|
||||
|
||||
|
||||
def test_exchange_get_markets():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
x = wb.get_markets()
|
||||
assert x == ['__']
|
||||
with pytest.raises(btx.OperationalException, match=r'market gone'):
|
||||
fb.success = False
|
||||
wb.get_markets()
|
||||
|
||||
|
||||
def test_exchange_get_market_summaries():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
assert ['sum'] == wb.get_market_summaries()
|
||||
with pytest.raises(btx.OperationalException, match=r'no summary'):
|
||||
fb.success = False
|
||||
wb.get_market_summaries()
|
||||
|
||||
|
||||
def test_exchange_get_wallet_health():
|
||||
wb = make_wrap_bittrex()
|
||||
fb = FakeBittrex()
|
||||
x = wb.get_wallet_health()
|
||||
assert x[0]['Currency'] == 'BTC_ETH'
|
||||
with pytest.raises(btx.OperationalException, match=r'bad health'):
|
||||
fb.success = False
|
||||
wb.get_wallet_health()
|
||||
|
||||
|
||||
def test_validate_response_success():
|
||||
response = {
|
||||
'message': '',
|
||||
'result': [],
|
||||
}
|
||||
Bittrex._validate_response(response)
|
||||
|
||||
|
||||
def test_validate_response_no_api_response():
|
||||
response = {
|
||||
'message': 'NO_API_RESPONSE',
|
||||
'result': None,
|
||||
}
|
||||
with pytest.raises(ContentDecodingError, match=r'.*NO_API_RESPONSE.*'):
|
||||
Bittrex._validate_response(response)
|
||||
|
||||
|
||||
def test_validate_response_min_trade_requirement_not_met():
|
||||
response = {
|
||||
'message': 'MIN_TRADE_REQUIREMENT_NOT_MET',
|
||||
'result': None,
|
||||
}
|
||||
with pytest.raises(ContentDecodingError, match=r'.*MIN_TRADE_REQUIREMENT_NOT_MET.*'):
|
||||
Bittrex._validate_response(response)
|
||||
609
freqtrade/tests/optimize/test_backtesting.py
Normal file
609
freqtrade/tests/optimize/test_backtesting.py
Normal file
@@ -0,0 +1,609 @@
|
||||
# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, unused-argument
|
||||
|
||||
import json
|
||||
import math
|
||||
import random
|
||||
from copy import deepcopy
|
||||
from typing import List
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from arrow import Arrow
|
||||
|
||||
from freqtrade import optimize
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.arguments import Arguments
|
||||
from freqtrade.optimize.backtesting import Backtesting, start, setup_configuration
|
||||
from freqtrade.tests.conftest import default_conf, log_has
|
||||
|
||||
# Avoid to reinit the same object again and again
|
||||
_BACKTESTING = Backtesting(default_conf())
|
||||
|
||||
|
||||
def get_args(args) -> List[str]:
|
||||
return Arguments(args, '').get_parsed_arg()
|
||||
|
||||
|
||||
def trim_dictlist(dict_list, num):
|
||||
new = {}
|
||||
for pair, pair_data in dict_list.items():
|
||||
new[pair] = pair_data[num:]
|
||||
return new
|
||||
|
||||
|
||||
def load_data_test(what):
|
||||
timerange = ((None, 'line'), None, -100)
|
||||
data = optimize.load_data(None, ticker_interval=1, pairs=['BTC_UNITEST'], timerange=timerange)
|
||||
pair = data['BTC_UNITEST']
|
||||
datalen = len(pair)
|
||||
# Depending on the what parameter we now adjust the
|
||||
# loaded data looks:
|
||||
# pair :: [{'O': 0.123, 'H': 0.123, 'L': 0.123,
|
||||
# 'C': 0.123, 'V': 123.123,
|
||||
# 'T': '2017-11-04T23:02:00', 'BV': 0.123}]
|
||||
base = 0.001
|
||||
if what == 'raise':
|
||||
return {'BTC_UNITEST':
|
||||
[{'T': pair[x]['T'], # Keep old dates
|
||||
'V': pair[x]['V'], # Keep old volume
|
||||
'BV': pair[x]['BV'], # keep too
|
||||
'O': x * base, # But replace O,H,L,C
|
||||
'H': x * base + 0.0001,
|
||||
'L': x * base - 0.0001,
|
||||
'C': x * base} for x in range(0, datalen)]}
|
||||
if what == 'lower':
|
||||
return {'BTC_UNITEST':
|
||||
[{'T': pair[x]['T'], # Keep old dates
|
||||
'V': pair[x]['V'], # Keep old volume
|
||||
'BV': pair[x]['BV'], # keep too
|
||||
'O': 1 - x * base, # But replace O,H,L,C
|
||||
'H': 1 - x * base + 0.0001,
|
||||
'L': 1 - x * base - 0.0001,
|
||||
'C': 1 - x * base} for x in range(0, datalen)]}
|
||||
if what == 'sine':
|
||||
hz = 0.1 # frequency
|
||||
return {'BTC_UNITEST':
|
||||
[{'T': pair[x]['T'], # Keep old dates
|
||||
'V': pair[x]['V'], # Keep old volume
|
||||
'BV': pair[x]['BV'], # keep too
|
||||
# But replace O,H,L,C
|
||||
'O': math.sin(x * hz) / 1000 + base,
|
||||
'H': math.sin(x * hz) / 1000 + base + 0.0001,
|
||||
'L': math.sin(x * hz) / 1000 + base - 0.0001,
|
||||
'C': math.sin(x * hz) / 1000 + base} for x in range(0, datalen)]}
|
||||
return data
|
||||
|
||||
|
||||
def simple_backtest(config, contour, num_results) -> None:
|
||||
backtesting = _BACKTESTING
|
||||
|
||||
data = load_data_test(contour)
|
||||
processed = backtesting.tickerdata_to_dataframe(data)
|
||||
assert isinstance(processed, dict)
|
||||
results = backtesting.backtest(
|
||||
{
|
||||
'stake_amount': config['stake_amount'],
|
||||
'processed': processed,
|
||||
'max_open_trades': 1,
|
||||
'realistic': True
|
||||
}
|
||||
)
|
||||
# results :: <class 'pandas.core.frame.DataFrame'>
|
||||
assert len(results) == num_results
|
||||
|
||||
|
||||
def mocked_load_data(datadir, pairs=[], ticker_interval=0, refresh_pairs=False, timerange=None):
|
||||
tickerdata = optimize.load_tickerdata_file(datadir, 'BTC_UNITEST', 1, timerange=timerange)
|
||||
pairdata = {'BTC_UNITEST': tickerdata}
|
||||
return pairdata
|
||||
|
||||
|
||||
# use for mock freqtrade.exchange.get_ticker_history'
|
||||
def _load_pair_as_ticks(pair, tickfreq):
|
||||
ticks = optimize.load_data(None, ticker_interval=tickfreq, pairs=[pair])
|
||||
ticks = trim_dictlist(ticks, -200)
|
||||
return ticks[pair]
|
||||
|
||||
|
||||
# FIX: fixturize this?
|
||||
def _make_backtest_conf(conf=None, pair='BTC_UNITEST', record=None):
|
||||
data = optimize.load_data(None, ticker_interval=8, pairs=[pair])
|
||||
data = trim_dictlist(data, -200)
|
||||
return {
|
||||
'stake_amount': conf['stake_amount'],
|
||||
'processed': _BACKTESTING.tickerdata_to_dataframe(data),
|
||||
'max_open_trades': 10,
|
||||
'realistic': True,
|
||||
'record': record
|
||||
}
|
||||
|
||||
|
||||
def _trend(signals, buy_value, sell_value):
|
||||
n = len(signals['low'])
|
||||
buy = np.zeros(n)
|
||||
sell = np.zeros(n)
|
||||
for i in range(0, len(signals['buy'])):
|
||||
if random.random() > 0.5: # Both buy and sell signals at same timeframe
|
||||
buy[i] = buy_value
|
||||
sell[i] = sell_value
|
||||
signals['buy'] = buy
|
||||
signals['sell'] = sell
|
||||
return signals
|
||||
|
||||
|
||||
def _trend_alternate(dataframe=None):
|
||||
signals = dataframe
|
||||
low = signals['low']
|
||||
n = len(low)
|
||||
buy = np.zeros(n)
|
||||
sell = np.zeros(n)
|
||||
for i in range(0, len(buy)):
|
||||
if i % 2 == 0:
|
||||
buy[i] = 1
|
||||
else:
|
||||
sell[i] = 1
|
||||
signals['buy'] = buy
|
||||
signals['sell'] = sell
|
||||
return dataframe
|
||||
|
||||
|
||||
def _run_backtest_1(fun, backtest_conf):
|
||||
# strategy is a global (hidden as a singleton), so we
|
||||
# emulate strategy being pure, by override/restore here
|
||||
# if we dont do this, the override in strategy will carry over
|
||||
# to other tests
|
||||
old_buy = _BACKTESTING.populate_buy_trend
|
||||
old_sell = _BACKTESTING.populate_sell_trend
|
||||
_BACKTESTING.populate_buy_trend = fun # Override
|
||||
_BACKTESTING.populate_sell_trend = fun # Override
|
||||
results = _BACKTESTING.backtest(backtest_conf)
|
||||
_BACKTESTING.populate_buy_trend = old_buy # restore override
|
||||
_BACKTESTING.populate_sell_trend = old_sell # restore override
|
||||
return results
|
||||
|
||||
|
||||
# Unit tests
|
||||
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'backtesting'
|
||||
]
|
||||
|
||||
config = setup_configuration(get_args(args))
|
||||
assert 'max_open_trades' in config
|
||||
assert 'stake_currency' in config
|
||||
assert 'stake_amount' in config
|
||||
assert 'exchange' in config
|
||||
assert 'pair_whitelist' in config['exchange']
|
||||
assert 'datadir' in config
|
||||
assert log_has(
|
||||
'Parameter --datadir detected: {} ...'.format(config['datadir']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert 'ticker_interval' in config
|
||||
assert not log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'live' not in config
|
||||
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'realistic_simulation' not in config
|
||||
assert not log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'refresh_pairs' not in config
|
||||
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'timerange' not in config
|
||||
assert 'export' not in config
|
||||
|
||||
|
||||
def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'--datadir', '/foo/bar',
|
||||
'backtesting',
|
||||
'--ticker-interval', '1',
|
||||
'--live',
|
||||
'--realistic-simulation',
|
||||
'--refresh-pairs-cached',
|
||||
'--timerange', ':100',
|
||||
'--export', '/bar/foo'
|
||||
]
|
||||
|
||||
config = setup_configuration(get_args(args))
|
||||
assert 'max_open_trades' in config
|
||||
assert 'stake_currency' in config
|
||||
assert 'stake_amount' in config
|
||||
assert 'exchange' in config
|
||||
assert 'pair_whitelist' in config['exchange']
|
||||
assert 'datadir' in config
|
||||
assert log_has(
|
||||
'Parameter --datadir detected: {} ...'.format(config['datadir']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert 'ticker_interval' in config
|
||||
assert log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
||||
assert log_has(
|
||||
'Using ticker_interval: 1 ...',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert 'live' in config
|
||||
assert log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'realistic_simulation'in config
|
||||
assert log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
||||
assert log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
|
||||
|
||||
assert 'refresh_pairs'in config
|
||||
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
||||
assert 'timerange' in config
|
||||
assert log_has(
|
||||
'Parameter --timerange detected: {} ...'.format(config['timerange']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert 'export' in config
|
||||
assert log_has(
|
||||
'Parameter --export detected: {} ...'.format(config['export']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
def test_start(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test start() function
|
||||
"""
|
||||
start_mock = MagicMock()
|
||||
mocker.patch('freqtrade.optimize.backtesting.Backtesting.start', start_mock)
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'backtesting'
|
||||
]
|
||||
args = get_args(args)
|
||||
start(args)
|
||||
assert log_has(
|
||||
'Starting freqtrade in Backtesting mode',
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert start_mock.call_count == 1
|
||||
|
||||
|
||||
def test_backtesting__init__(mocker, default_conf) -> None:
|
||||
"""
|
||||
Test Backtesting.__init__() method
|
||||
"""
|
||||
init_mock = MagicMock()
|
||||
mocker.patch('freqtrade.optimize.backtesting.Backtesting._init', init_mock)
|
||||
|
||||
backtesting = Backtesting(default_conf)
|
||||
assert backtesting.config == default_conf
|
||||
assert backtesting.analyze is None
|
||||
assert backtesting.ticker_interval is None
|
||||
assert backtesting.tickerdata_to_dataframe is None
|
||||
assert backtesting.populate_buy_trend is None
|
||||
assert backtesting.populate_sell_trend is None
|
||||
assert init_mock.call_count == 1
|
||||
|
||||
|
||||
def test_backtesting_init(default_conf) -> None:
|
||||
"""
|
||||
Test Backtesting._init() method
|
||||
"""
|
||||
backtesting = Backtesting(default_conf)
|
||||
assert backtesting.config == default_conf
|
||||
assert isinstance(backtesting.analyze, Analyze)
|
||||
assert backtesting.ticker_interval == 5
|
||||
assert callable(backtesting.tickerdata_to_dataframe)
|
||||
assert callable(backtesting.populate_buy_trend)
|
||||
assert callable(backtesting.populate_sell_trend)
|
||||
|
||||
|
||||
def test_tickerdata_to_dataframe(default_conf) -> None:
|
||||
"""
|
||||
Test Backtesting.tickerdata_to_dataframe() method
|
||||
"""
|
||||
|
||||
timerange = ((None, 'line'), None, -100)
|
||||
tick = optimize.load_tickerdata_file(None, 'BTC_UNITEST', 1, timerange=timerange)
|
||||
tickerlist = {'BTC_UNITEST': tick}
|
||||
|
||||
backtesting = _BACKTESTING
|
||||
data = backtesting.tickerdata_to_dataframe(tickerlist)
|
||||
assert len(data['BTC_UNITEST']) == 100
|
||||
|
||||
# Load Analyze to compare the result between Backtesting function and Analyze are the same
|
||||
analyze = Analyze(default_conf)
|
||||
data2 = analyze.tickerdata_to_dataframe(tickerlist)
|
||||
assert data['BTC_UNITEST'].equals(data2['BTC_UNITEST'])
|
||||
|
||||
|
||||
def test_get_timeframe() -> None:
|
||||
"""
|
||||
Test Backtesting.get_timeframe() method
|
||||
"""
|
||||
backtesting = _BACKTESTING
|
||||
|
||||
data = backtesting.tickerdata_to_dataframe(
|
||||
optimize.load_data(
|
||||
None,
|
||||
ticker_interval=1,
|
||||
pairs=['BTC_UNITEST']
|
||||
)
|
||||
)
|
||||
min_date, max_date = backtesting.get_timeframe(data)
|
||||
assert min_date.isoformat() == '2017-11-04T23:02:00+00:00'
|
||||
assert max_date.isoformat() == '2017-11-14T22:59:00+00:00'
|
||||
|
||||
|
||||
def test_generate_text_table():
|
||||
"""
|
||||
Test Backtesting.generate_text_table() method
|
||||
"""
|
||||
backtesting = _BACKTESTING
|
||||
|
||||
results = pd.DataFrame(
|
||||
{
|
||||
'currency': ['BTC_ETH', 'BTC_ETH'],
|
||||
'profit_percent': [0.1, 0.2],
|
||||
'profit_BTC': [0.2, 0.4],
|
||||
'duration': [10, 30],
|
||||
'profit': [2, 0],
|
||||
'loss': [0, 0]
|
||||
}
|
||||
)
|
||||
|
||||
result_str = (
|
||||
'pair buy count avg profit % '
|
||||
'total profit BTC avg duration profit loss\n'
|
||||
'------- ----------- -------------- '
|
||||
'------------------ -------------- -------- ------\n'
|
||||
'BTC_ETH 2 15.00 '
|
||||
'0.60000000 20.0 2 0\n'
|
||||
'TOTAL 2 15.00 '
|
||||
'0.60000000 20.0 2 0'
|
||||
)
|
||||
|
||||
assert backtesting._generate_text_table(data={'BTC_ETH': {}}, results=results) == result_str
|
||||
|
||||
|
||||
def test_backtesting_start(default_conf, mocker, caplog) -> None:
|
||||
"""
|
||||
Test Backtesting.start() method
|
||||
"""
|
||||
def get_timeframe(input1, input2):
|
||||
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)
|
||||
|
||||
mocker.patch('freqtrade.freqtradebot.Analyze', MagicMock())
|
||||
mocker.patch('freqtrade.optimize.load_data', mocked_load_data)
|
||||
mocker.patch('freqtrade.exchange.get_ticker_history')
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.optimize.backtesting.Backtesting',
|
||||
backtest=MagicMock(),
|
||||
_generate_text_table=MagicMock(return_value='1'),
|
||||
get_timeframe=get_timeframe,
|
||||
)
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf['exchange']['pair_whitelist'] = ['BTC_UNITEST']
|
||||
conf['ticker_interval'] = 1
|
||||
conf['live'] = False
|
||||
conf['datadir'] = None
|
||||
conf['export'] = None
|
||||
conf['timerange'] = '-100'
|
||||
|
||||
backtesting = Backtesting(conf)
|
||||
backtesting.start()
|
||||
# check the logs, that will contain the backtest result
|
||||
exists = [
|
||||
'Using local backtesting data (using whitelist in given config) ...',
|
||||
'Using stake_currency: BTC ...',
|
||||
'Using stake_amount: 0.001 ...',
|
||||
'Measuring data from 2017-11-14T21:17:00+00:00 '
|
||||
'up to 2017-11-14T22:59:00+00:00 (0 days)..'
|
||||
]
|
||||
for line in exists:
|
||||
assert log_has(line, caplog.record_tuples)
|
||||
|
||||
|
||||
def test_backtest(default_conf) -> None:
|
||||
"""
|
||||
Test Backtesting.backtest() method
|
||||
"""
|
||||
backtesting = _BACKTESTING
|
||||
|
||||
data = optimize.load_data(None, ticker_interval=5, pairs=['BTC_ETH'])
|
||||
data = trim_dictlist(data, -200)
|
||||
results = backtesting.backtest(
|
||||
{
|
||||
'stake_amount': default_conf['stake_amount'],
|
||||
'processed': backtesting.tickerdata_to_dataframe(data),
|
||||
'max_open_trades': 10,
|
||||
'realistic': True
|
||||
}
|
||||
)
|
||||
assert not results.empty
|
||||
|
||||
|
||||
def test_backtest_1min_ticker_interval(default_conf) -> None:
|
||||
"""
|
||||
Test Backtesting.backtest() method with 1 min ticker
|
||||
"""
|
||||
backtesting = _BACKTESTING
|
||||
|
||||
# Run a backtesting for an exiting 5min ticker_interval
|
||||
data = optimize.load_data(None, ticker_interval=1, pairs=['BTC_UNITEST'])
|
||||
data = trim_dictlist(data, -200)
|
||||
results = backtesting.backtest(
|
||||
{
|
||||
'stake_amount': default_conf['stake_amount'],
|
||||
'processed': backtesting.tickerdata_to_dataframe(data),
|
||||
'max_open_trades': 1,
|
||||
'realistic': True
|
||||
}
|
||||
)
|
||||
assert not results.empty
|
||||
|
||||
|
||||
def test_processed() -> None:
|
||||
"""
|
||||
Test Backtesting.backtest() method with offline data
|
||||
"""
|
||||
backtesting = _BACKTESTING
|
||||
|
||||
dict_of_tickerrows = load_data_test('raise')
|
||||
dataframes = backtesting.tickerdata_to_dataframe(dict_of_tickerrows)
|
||||
dataframe = dataframes['BTC_UNITEST']
|
||||
cols = dataframe.columns
|
||||
# assert the dataframe got some of the indicator columns
|
||||
for col in ['close', 'high', 'low', 'open', 'date',
|
||||
'ema50', 'ao', 'macd', 'plus_dm']:
|
||||
assert col in cols
|
||||
|
||||
|
||||
def test_backtest_pricecontours(default_conf) -> None:
|
||||
tests = [['raise', 17], ['lower', 0], ['sine', 17]]
|
||||
for [contour, numres] in tests:
|
||||
simple_backtest(default_conf, contour, numres)
|
||||
|
||||
|
||||
# Test backtest using offline data (testdata directory)
|
||||
def test_backtest_ticks(default_conf):
|
||||
ticks = [1, 5]
|
||||
fun = _BACKTESTING.populate_buy_trend
|
||||
for tick in ticks:
|
||||
backtest_conf = _make_backtest_conf(conf=default_conf)
|
||||
results = _run_backtest_1(fun, backtest_conf)
|
||||
assert not results.empty
|
||||
|
||||
|
||||
def test_backtest_clash_buy_sell(default_conf):
|
||||
# Override the default buy trend function in our DefaultStrategy
|
||||
def fun(dataframe=None):
|
||||
buy_value = 1
|
||||
sell_value = 1
|
||||
return _trend(dataframe, buy_value, sell_value)
|
||||
|
||||
backtest_conf = _make_backtest_conf(conf=default_conf)
|
||||
results = _run_backtest_1(fun, backtest_conf)
|
||||
assert results.empty
|
||||
|
||||
|
||||
def test_backtest_only_sell(default_conf):
|
||||
# Override the default buy trend function in our DefaultStrategy
|
||||
def fun(dataframe=None):
|
||||
buy_value = 0
|
||||
sell_value = 1
|
||||
return _trend(dataframe, buy_value, sell_value)
|
||||
|
||||
backtest_conf = _make_backtest_conf(conf=default_conf)
|
||||
results = _run_backtest_1(fun, backtest_conf)
|
||||
assert results.empty
|
||||
|
||||
|
||||
def test_backtest_alternate_buy_sell(default_conf):
|
||||
backtest_conf = _make_backtest_conf(conf=default_conf, pair='BTC_UNITEST')
|
||||
results = _run_backtest_1(_trend_alternate, backtest_conf)
|
||||
assert len(results) == 3
|
||||
|
||||
|
||||
def test_backtest_record(default_conf, mocker):
|
||||
names = []
|
||||
records = []
|
||||
mocker.patch(
|
||||
'freqtrade.optimize.backtesting.file_dump_json',
|
||||
new=lambda n, r: (names.append(n), records.append(r))
|
||||
)
|
||||
backtest_conf = _make_backtest_conf(
|
||||
conf=default_conf,
|
||||
pair='BTC_UNITEST',
|
||||
record="trades"
|
||||
)
|
||||
results = _run_backtest_1(_trend_alternate, backtest_conf)
|
||||
assert len(results) == 3
|
||||
# Assert file_dump_json was only called once
|
||||
assert names == ['backtest-result.json']
|
||||
records = records[0]
|
||||
# Ensure records are of correct type
|
||||
assert len(records) == 3
|
||||
# ('BTC_UNITEST', 0.00331158, '1510684320', '1510691700', 0, 117)
|
||||
# Below follows just a typecheck of the schema/type of trade-records
|
||||
oix = None
|
||||
for (pair, profit, date_buy, date_sell, buy_index, dur) in records:
|
||||
assert pair == 'BTC_UNITEST'
|
||||
isinstance(profit, float)
|
||||
# FIX: buy/sell should be converted to ints
|
||||
isinstance(date_buy, str)
|
||||
isinstance(date_sell, str)
|
||||
isinstance(buy_index, pd._libs.tslib.Timestamp)
|
||||
if oix:
|
||||
assert buy_index > oix
|
||||
oix = buy_index
|
||||
assert dur > 0
|
||||
|
||||
|
||||
def test_backtest_start_live(default_conf, mocker, caplog):
|
||||
default_conf['exchange']['pair_whitelist'] = ['BTC_UNITEST']
|
||||
mocker.patch('freqtrade.exchange.get_ticker_history',
|
||||
new=lambda n, i: _load_pair_as_ticks(n, i))
|
||||
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest', MagicMock())
|
||||
mocker.patch('freqtrade.optimize.backtesting.Backtesting._generate_text_table', MagicMock())
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = MagicMock()
|
||||
args.ticker_interval = 1
|
||||
args.level = 10
|
||||
args.live = True
|
||||
args.datadir = None
|
||||
args.export = None
|
||||
args.strategy = 'DefaultStrategy'
|
||||
args.timerange = '-100' # needed due to MagicMock malleability
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'backtesting',
|
||||
'--ticker-interval', '1',
|
||||
'--live',
|
||||
'--timerange', '-100'
|
||||
]
|
||||
args = get_args(args)
|
||||
start(args)
|
||||
# check the logs, that will contain the backtest result
|
||||
exists = [
|
||||
'Parameter -i/--ticker-interval detected ...',
|
||||
'Using ticker_interval: 1 ...',
|
||||
'Parameter -l/--live detected ...',
|
||||
'Using max_open_trades: 1 ...',
|
||||
'Parameter --timerange detected: -100 ..',
|
||||
'Parameter --datadir detected: freqtrade/tests/testdata ...',
|
||||
'Using stake_currency: BTC ...',
|
||||
'Using stake_amount: 0.001 ...',
|
||||
'Downloading data for all pairs in whitelist ...',
|
||||
'Measuring data from 2017-11-14T19:32:00+00:00 up to 2017-11-14T22:59:00+00:00 (0 days)..'
|
||||
]
|
||||
|
||||
for line in exists:
|
||||
log_has(line, caplog.record_tuples)
|
||||
534
freqtrade/tests/optimize/test_hyperopt.py
Normal file
534
freqtrade/tests/optimize/test_hyperopt.py
Normal file
@@ -0,0 +1,534 @@
|
||||
# pragma pylint: disable=missing-docstring,W0212,C0103
|
||||
import json
|
||||
import os
|
||||
from copy import deepcopy
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from freqtrade.optimize.__init__ import load_tickerdata_file
|
||||
from freqtrade.optimize.hyperopt import Hyperopt, start
|
||||
from freqtrade.strategy.resolver import StrategyResolver
|
||||
from freqtrade.tests.conftest import default_conf, log_has
|
||||
from freqtrade.tests.optimize.test_backtesting import get_args
|
||||
|
||||
|
||||
# Avoid to reinit the same object again and again
|
||||
_HYPEROPT = Hyperopt(default_conf())
|
||||
|
||||
|
||||
# Functions for recurrent object patching
|
||||
def create_trials(mocker) -> None:
|
||||
"""
|
||||
When creating trials, mock the hyperopt Trials so that *by default*
|
||||
- we don't create any pickle'd files in the filesystem
|
||||
- we might have a pickle'd file so make sure that we return
|
||||
false when looking for it
|
||||
"""
|
||||
_HYPEROPT.trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle')
|
||||
|
||||
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=False)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.os.path.getsize', return_value=1)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.os.remove', return_value=True)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.pickle.dump', return_value=None)
|
||||
|
||||
return mocker.Mock(
|
||||
results=[
|
||||
{
|
||||
'loss': 1,
|
||||
'result': 'foo',
|
||||
'status': 'ok'
|
||||
}
|
||||
],
|
||||
best_trial={'misc': {'vals': {'adx': 999}}}
|
||||
)
|
||||
|
||||
|
||||
# Unit tests
|
||||
def test_start(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test start() function
|
||||
"""
|
||||
start_mock = MagicMock()
|
||||
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'hyperopt',
|
||||
'--epochs', '5'
|
||||
]
|
||||
args = get_args(args)
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
start(args)
|
||||
|
||||
import pprint
|
||||
pprint.pprint(caplog.record_tuples)
|
||||
|
||||
assert log_has(
|
||||
'Starting freqtrade in Hyperopt mode',
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert start_mock.call_count == 1
|
||||
|
||||
|
||||
def test_loss_calculation_prefer_correct_trade_count() -> None:
|
||||
"""
|
||||
Test Hyperopt.calculate_loss()
|
||||
"""
|
||||
hyperopt = _HYPEROPT
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
|
||||
correct = hyperopt.calculate_loss(1, hyperopt.target_trades, 20)
|
||||
over = hyperopt.calculate_loss(1, hyperopt.target_trades + 100, 20)
|
||||
under = hyperopt.calculate_loss(1, hyperopt.target_trades - 100, 20)
|
||||
assert over > correct
|
||||
assert under > correct
|
||||
|
||||
|
||||
def test_loss_calculation_prefer_shorter_trades() -> None:
|
||||
"""
|
||||
Test Hyperopt.calculate_loss()
|
||||
"""
|
||||
hyperopt = _HYPEROPT
|
||||
|
||||
shorter = hyperopt.calculate_loss(1, 100, 20)
|
||||
longer = hyperopt.calculate_loss(1, 100, 30)
|
||||
assert shorter < longer
|
||||
|
||||
|
||||
def test_loss_calculation_has_limited_profit() -> None:
|
||||
hyperopt = _HYPEROPT
|
||||
|
||||
correct = hyperopt.calculate_loss(hyperopt.expected_max_profit, hyperopt.target_trades, 20)
|
||||
over = hyperopt.calculate_loss(hyperopt.expected_max_profit * 2, hyperopt.target_trades, 20)
|
||||
under = hyperopt.calculate_loss(hyperopt.expected_max_profit / 2, hyperopt.target_trades, 20)
|
||||
assert over == correct
|
||||
assert under > correct
|
||||
|
||||
|
||||
def test_log_results_if_loss_improves(capsys) -> None:
|
||||
hyperopt = _HYPEROPT
|
||||
hyperopt.current_best_loss = 2
|
||||
hyperopt.log_results(
|
||||
{
|
||||
'loss': 1,
|
||||
'current_tries': 1,
|
||||
'total_tries': 2,
|
||||
'result': 'foo'
|
||||
}
|
||||
)
|
||||
out, err = capsys.readouterr()
|
||||
assert ' 1/2: foo. Loss 1.00000'in out
|
||||
|
||||
|
||||
def test_no_log_if_loss_does_not_improve(caplog) -> None:
|
||||
hyperopt = _HYPEROPT
|
||||
hyperopt.current_best_loss = 2
|
||||
hyperopt.log_results(
|
||||
{
|
||||
'loss': 3,
|
||||
}
|
||||
)
|
||||
assert caplog.record_tuples == []
|
||||
|
||||
|
||||
def test_fmin_best_results(mocker, default_conf, caplog) -> None:
|
||||
fmin_result = {
|
||||
"macd_below_zero": 0,
|
||||
"adx": 1,
|
||||
"adx-value": 15.0,
|
||||
"fastd": 1,
|
||||
"fastd-value": 40.0,
|
||||
"green_candle": 1,
|
||||
"mfi": 0,
|
||||
"over_sar": 0,
|
||||
"rsi": 1,
|
||||
"rsi-value": 37.0,
|
||||
"trigger": 2,
|
||||
"uptrend_long_ema": 1,
|
||||
"uptrend_short_ema": 0,
|
||||
"uptrend_sma": 0,
|
||||
"stoploss": -0.1,
|
||||
"roi_t1": 1,
|
||||
"roi_t2": 2,
|
||||
"roi_t3": 3,
|
||||
"roi_p1": 1,
|
||||
"roi_p2": 2,
|
||||
"roi_p3": 3,
|
||||
}
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf.update({'config': 'config.json.example'})
|
||||
conf.update({'epochs': 1})
|
||||
conf.update({'timerange': None})
|
||||
conf.update({'spaces': 'all'})
|
||||
|
||||
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
||||
mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value=fmin_result)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
|
||||
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
hyperopt = Hyperopt(conf)
|
||||
hyperopt.trials = create_trials(mocker)
|
||||
hyperopt.tickerdata_to_dataframe = MagicMock()
|
||||
hyperopt.start()
|
||||
|
||||
exists = [
|
||||
'Best parameters:',
|
||||
'"adx": {\n "enabled": true,\n "value": 15.0\n },',
|
||||
'"fastd": {\n "enabled": true,\n "value": 40.0\n },',
|
||||
'"green_candle": {\n "enabled": true\n },',
|
||||
'"macd_below_zero": {\n "enabled": false\n },',
|
||||
'"mfi": {\n "enabled": false\n },',
|
||||
'"over_sar": {\n "enabled": false\n },',
|
||||
'"roi_p1": 1.0,',
|
||||
'"roi_p2": 2.0,',
|
||||
'"roi_p3": 3.0,',
|
||||
'"roi_t1": 1.0,',
|
||||
'"roi_t2": 2.0,',
|
||||
'"roi_t3": 3.0,',
|
||||
'"rsi": {\n "enabled": true,\n "value": 37.0\n },',
|
||||
'"stoploss": -0.1,',
|
||||
'"trigger": {\n "type": "faststoch10"\n },',
|
||||
'"uptrend_long_ema": {\n "enabled": true\n },',
|
||||
'"uptrend_short_ema": {\n "enabled": false\n },',
|
||||
'"uptrend_sma": {\n "enabled": false\n }',
|
||||
'ROI table:\n{0: 6.0, 3.0: 3.0, 5.0: 1.0, 6.0: 0}',
|
||||
'Best Result:\nfoo'
|
||||
]
|
||||
for line in exists:
|
||||
assert line in caplog.text
|
||||
|
||||
|
||||
def test_fmin_throw_value_error(mocker, default_conf, caplog) -> None:
|
||||
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
||||
mocker.patch('freqtrade.optimize.hyperopt.fmin', side_effect=ValueError())
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf.update({'config': 'config.json.example'})
|
||||
conf.update({'epochs': 1})
|
||||
conf.update({'timerange': None})
|
||||
conf.update({'spaces': 'all'})
|
||||
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
hyperopt = Hyperopt(conf)
|
||||
hyperopt.trials = create_trials(mocker)
|
||||
hyperopt.tickerdata_to_dataframe = MagicMock()
|
||||
|
||||
hyperopt.start()
|
||||
|
||||
exists = [
|
||||
'Best Result:',
|
||||
'Sorry, Hyperopt was not able to find good parameters. Please try with more epochs '
|
||||
'(param: -e).',
|
||||
]
|
||||
|
||||
for line in exists:
|
||||
assert line in caplog.text
|
||||
|
||||
|
||||
def test_resuming_previous_hyperopt_results_succeeds(mocker, default_conf) -> None:
|
||||
trials = create_trials(mocker)
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf.update({'config': 'config.json.example'})
|
||||
conf.update({'epochs': 1})
|
||||
conf.update({'mongodb': False})
|
||||
conf.update({'timerange': None})
|
||||
conf.update({'spaces': 'all'})
|
||||
|
||||
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=True)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.len', return_value=len(trials.results))
|
||||
mock_read = mocker.patch(
|
||||
'freqtrade.optimize.hyperopt.Hyperopt.read_trials',
|
||||
return_value=trials
|
||||
)
|
||||
mock_save = mocker.patch(
|
||||
'freqtrade.optimize.hyperopt.Hyperopt.save_trials',
|
||||
return_value=None
|
||||
)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.sorted', return_value=trials.results)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
||||
mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
||||
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
|
||||
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
hyperopt = Hyperopt(conf)
|
||||
hyperopt.trials = trials
|
||||
hyperopt.tickerdata_to_dataframe = MagicMock()
|
||||
|
||||
hyperopt.start()
|
||||
|
||||
mock_read.assert_called_once()
|
||||
mock_save.assert_called_once()
|
||||
|
||||
current_tries = hyperopt.current_tries
|
||||
total_tries = hyperopt.total_tries
|
||||
|
||||
assert current_tries == len(trials.results)
|
||||
assert total_tries == (current_tries + len(trials.results))
|
||||
|
||||
|
||||
def test_save_trials_saves_trials(mocker, caplog) -> None:
|
||||
create_trials(mocker)
|
||||
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.pickle.dump', return_value=None)
|
||||
|
||||
hyperopt = _HYPEROPT
|
||||
mocker.patch('freqtrade.optimize.hyperopt.open', return_value=hyperopt.trials_file)
|
||||
|
||||
hyperopt.save_trials()
|
||||
|
||||
assert log_has(
|
||||
'Saving Trials to \'freqtrade/tests/optimize/ut_trials.pickle\'',
|
||||
caplog.record_tuples
|
||||
)
|
||||
mock_dump.assert_called_once()
|
||||
|
||||
|
||||
def test_read_trials_returns_trials_file(mocker, caplog) -> None:
|
||||
trials = create_trials(mocker)
|
||||
mock_load = mocker.patch('freqtrade.optimize.hyperopt.pickle.load', return_value=trials)
|
||||
mock_open = mocker.patch('freqtrade.optimize.hyperopt.open', return_value=mock_load)
|
||||
|
||||
hyperopt = _HYPEROPT
|
||||
hyperopt_trial = hyperopt.read_trials()
|
||||
assert log_has(
|
||||
'Reading Trials from \'freqtrade/tests/optimize/ut_trials.pickle\'',
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert hyperopt_trial == trials
|
||||
mock_open.assert_called_once()
|
||||
mock_load.assert_called_once()
|
||||
|
||||
|
||||
def test_roi_table_generation() -> None:
|
||||
params = {
|
||||
'roi_t1': 5,
|
||||
'roi_t2': 10,
|
||||
'roi_t3': 15,
|
||||
'roi_p1': 1,
|
||||
'roi_p2': 2,
|
||||
'roi_p3': 3,
|
||||
}
|
||||
|
||||
hyperopt = _HYPEROPT
|
||||
assert hyperopt.generate_roi_table(params) == {0: 6, 15: 3, 25: 1, 30: 0}
|
||||
|
||||
|
||||
def test_start_calls_fmin(mocker, default_conf) -> None:
|
||||
trials = create_trials(mocker)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.sorted', return_value=trials.results)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
||||
mock_fmin = mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf.update({'config': 'config.json.example'})
|
||||
conf.update({'epochs': 1})
|
||||
conf.update({'mongodb': False})
|
||||
conf.update({'timerange': None})
|
||||
conf.update({'spaces': 'all'})
|
||||
|
||||
hyperopt = Hyperopt(conf)
|
||||
hyperopt.trials = trials
|
||||
hyperopt.tickerdata_to_dataframe = MagicMock()
|
||||
|
||||
hyperopt.start()
|
||||
mock_fmin.assert_called_once()
|
||||
|
||||
|
||||
def test_start_uses_mongotrials(mocker, default_conf) -> None:
|
||||
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
||||
mock_fmin = mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
||||
mock_mongotrials = mocker.patch(
|
||||
'freqtrade.optimize.hyperopt.MongoTrials',
|
||||
return_value=create_trials(mocker)
|
||||
)
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf.update({'config': 'config.json.example'})
|
||||
conf.update({'epochs': 1})
|
||||
conf.update({'mongodb': True})
|
||||
conf.update({'timerange': None})
|
||||
conf.update({'spaces': 'all'})
|
||||
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
|
||||
|
||||
hyperopt = Hyperopt(conf)
|
||||
hyperopt.tickerdata_to_dataframe = MagicMock()
|
||||
|
||||
hyperopt.start()
|
||||
mock_mongotrials.assert_called_once()
|
||||
mock_fmin.assert_called_once()
|
||||
|
||||
|
||||
# test log_trials_result
|
||||
# test buy_strategy_generator def populate_buy_trend
|
||||
# test optimizer if 'ro_t1' in params
|
||||
|
||||
def test_format_results():
|
||||
"""
|
||||
Test Hyperopt.format_results()
|
||||
"""
|
||||
trades = [
|
||||
('BTC_ETH', 2, 2, 123),
|
||||
('BTC_LTC', 1, 1, 123),
|
||||
('BTC_XRP', -1, -2, -246)
|
||||
]
|
||||
labels = ['currency', 'profit_percent', 'profit_BTC', 'duration']
|
||||
df = pd.DataFrame.from_records(trades, columns=labels)
|
||||
x = Hyperopt.format_results(df)
|
||||
assert x.find(' 66.67%')
|
||||
|
||||
|
||||
def test_signal_handler(mocker):
|
||||
"""
|
||||
Test Hyperopt.signal_handler()
|
||||
"""
|
||||
m = MagicMock()
|
||||
mocker.patch('sys.exit', m)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.save_trials', m)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.log_trials_result', m)
|
||||
|
||||
hyperopt = _HYPEROPT
|
||||
hyperopt.signal_handler(9, None)
|
||||
assert m.call_count == 3
|
||||
|
||||
|
||||
def test_has_space():
|
||||
"""
|
||||
Test Hyperopt.has_space() method
|
||||
"""
|
||||
_HYPEROPT.config.update({'spaces': ['buy', 'roi']})
|
||||
assert _HYPEROPT.has_space('roi')
|
||||
assert _HYPEROPT.has_space('buy')
|
||||
assert not _HYPEROPT.has_space('stoploss')
|
||||
|
||||
_HYPEROPT.config.update({'spaces': ['all']})
|
||||
assert _HYPEROPT.has_space('buy')
|
||||
|
||||
|
||||
def test_populate_indicators() -> None:
|
||||
"""
|
||||
Test Hyperopt.populate_indicators()
|
||||
"""
|
||||
tick = load_tickerdata_file(None, 'BTC_UNITEST', 1)
|
||||
tickerlist = {'BTC_UNITEST': tick}
|
||||
dataframes = _HYPEROPT.tickerdata_to_dataframe(tickerlist)
|
||||
dataframe = _HYPEROPT.populate_indicators(dataframes['BTC_UNITEST'])
|
||||
|
||||
# Check if some indicators are generated. We will not test all of them
|
||||
assert 'adx' in dataframe
|
||||
assert 'ao' in dataframe
|
||||
assert 'cci' in dataframe
|
||||
|
||||
|
||||
def test_buy_strategy_generator() -> None:
|
||||
"""
|
||||
Test Hyperopt.buy_strategy_generator()
|
||||
"""
|
||||
tick = load_tickerdata_file(None, 'BTC_UNITEST', 1)
|
||||
tickerlist = {'BTC_UNITEST': tick}
|
||||
dataframes = _HYPEROPT.tickerdata_to_dataframe(tickerlist)
|
||||
dataframe = _HYPEROPT.populate_indicators(dataframes['BTC_UNITEST'])
|
||||
|
||||
populate_buy_trend = _HYPEROPT.buy_strategy_generator(
|
||||
{
|
||||
'uptrend_long_ema': {
|
||||
'enabled': True
|
||||
},
|
||||
'macd_below_zero': {
|
||||
'enabled': True
|
||||
},
|
||||
'uptrend_short_ema': {
|
||||
'enabled': True
|
||||
},
|
||||
'mfi': {
|
||||
'enabled': True,
|
||||
'value': 20
|
||||
},
|
||||
'fastd': {
|
||||
'enabled': True,
|
||||
'value': 20
|
||||
},
|
||||
'adx': {
|
||||
'enabled': True,
|
||||
'value': 20
|
||||
},
|
||||
'rsi': {
|
||||
'enabled': True,
|
||||
'value': 20
|
||||
},
|
||||
'over_sar': {
|
||||
'enabled': True,
|
||||
},
|
||||
'green_candle': {
|
||||
'enabled': True,
|
||||
},
|
||||
'uptrend_sma': {
|
||||
'enabled': True,
|
||||
},
|
||||
|
||||
'trigger': {
|
||||
'type': 'lower_bb'
|
||||
}
|
||||
}
|
||||
)
|
||||
result = populate_buy_trend(dataframe)
|
||||
# Check if some indicators are generated. We will not test all of them
|
||||
assert 'buy' in result
|
||||
assert 1 in result['buy']
|
||||
|
||||
|
||||
def test_generate_optimizer(mocker, default_conf) -> None:
|
||||
"""
|
||||
Test Hyperopt.generate_optimizer() function
|
||||
"""
|
||||
conf = deepcopy(default_conf)
|
||||
conf.update({'config': 'config.json.example'})
|
||||
conf.update({'timerange': None})
|
||||
conf.update({'spaces': 'all'})
|
||||
|
||||
trades = [
|
||||
('BTC_POWR', 0.023117, 0.000233, 100)
|
||||
]
|
||||
labels = ['currency', 'profit_percent', 'profit_BTC', 'duration']
|
||||
backtest_result = pd.DataFrame.from_records(trades, columns=labels)
|
||||
|
||||
mocker.patch(
|
||||
'freqtrade.optimize.hyperopt.Hyperopt.backtest',
|
||||
MagicMock(return_value=backtest_result)
|
||||
)
|
||||
|
||||
optimizer_param = {
|
||||
'adx': {'enabled': False},
|
||||
'fastd': {'enabled': True, 'value': 35.0},
|
||||
'green_candle': {'enabled': True},
|
||||
'macd_below_zero': {'enabled': True},
|
||||
'mfi': {'enabled': False},
|
||||
'over_sar': {'enabled': False},
|
||||
'roi_p1': 0.01,
|
||||
'roi_p2': 0.01,
|
||||
'roi_p3': 0.1,
|
||||
'roi_t1': 60.0,
|
||||
'roi_t2': 30.0,
|
||||
'roi_t3': 20.0,
|
||||
'rsi': {'enabled': False},
|
||||
'stoploss': -0.4,
|
||||
'trigger': {'type': 'macd_cross_signal'},
|
||||
'uptrend_long_ema': {'enabled': False},
|
||||
'uptrend_short_ema': {'enabled': True},
|
||||
'uptrend_sma': {'enabled': True}
|
||||
}
|
||||
|
||||
response_expected = {
|
||||
'loss': 1.9840569076926293,
|
||||
'result': ' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC '
|
||||
'(0.0231Σ%). Avg duration 100.0 mins.',
|
||||
'status': 'ok'
|
||||
}
|
||||
|
||||
hyperopt = Hyperopt(conf)
|
||||
generate_optimizer_value = hyperopt.generate_optimizer(optimizer_param)
|
||||
assert generate_optimizer_value == response_expected
|
||||
16
freqtrade/tests/optimize/test_hyperopt_config.py
Normal file
16
freqtrade/tests/optimize/test_hyperopt_config.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# pragma pylint: disable=missing-docstring,W0212
|
||||
|
||||
from user_data.hyperopt_conf import hyperopt_optimize_conf
|
||||
|
||||
|
||||
def test_hyperopt_optimize_conf():
|
||||
hyperopt_conf = hyperopt_optimize_conf()
|
||||
|
||||
assert "max_open_trades" in hyperopt_conf
|
||||
assert "stake_currency" in hyperopt_conf
|
||||
assert "stake_amount" in hyperopt_conf
|
||||
assert "minimal_roi" in hyperopt_conf
|
||||
assert "stoploss" in hyperopt_conf
|
||||
assert "bid_strategy" in hyperopt_conf
|
||||
assert "exchange" in hyperopt_conf
|
||||
assert "pair_whitelist" in hyperopt_conf['exchange']
|
||||
284
freqtrade/tests/optimize/test_optimize.py
Normal file
284
freqtrade/tests/optimize/test_optimize.py
Normal file
@@ -0,0 +1,284 @@
|
||||
# pragma pylint: disable=missing-docstring, protected-access, C0103
|
||||
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
from shutil import copyfile
|
||||
|
||||
from freqtrade import optimize
|
||||
from freqtrade.misc import file_dump_json
|
||||
from freqtrade.optimize.__init__ import make_testdata_path, download_pairs, \
|
||||
download_backtesting_testdata, load_tickerdata_file, trim_tickerlist
|
||||
from freqtrade.tests.conftest import log_has
|
||||
|
||||
# Change this if modifying BTC_UNITEST testdatafile
|
||||
_BTC_UNITTEST_LENGTH = 13681
|
||||
|
||||
|
||||
def _backup_file(file: str, copy_file: bool = False) -> None:
|
||||
"""
|
||||
Backup existing file to avoid deleting the user file
|
||||
:param file: complete path to the file
|
||||
:param touch_file: create an empty file in replacement
|
||||
:return: None
|
||||
"""
|
||||
file_swp = file + '.swp'
|
||||
if os.path.isfile(file):
|
||||
os.rename(file, file_swp)
|
||||
|
||||
if copy_file:
|
||||
copyfile(file_swp, file)
|
||||
|
||||
|
||||
def _clean_test_file(file: str) -> None:
|
||||
"""
|
||||
Backup existing file to avoid deleting the user file
|
||||
:param file: complete path to the file
|
||||
:return: None
|
||||
"""
|
||||
file_swp = file + '.swp'
|
||||
# 1. Delete file from the test
|
||||
if os.path.isfile(file):
|
||||
os.remove(file)
|
||||
|
||||
# 2. Rollback to the initial file
|
||||
if os.path.isfile(file_swp):
|
||||
os.rename(file_swp, file)
|
||||
|
||||
|
||||
def test_load_data_30min_ticker(ticker_history, mocker, caplog) -> None:
|
||||
"""
|
||||
Test load_data() with 30 min ticker
|
||||
"""
|
||||
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
||||
|
||||
file = 'freqtrade/tests/testdata/BTC_UNITTEST-30.json'
|
||||
_backup_file(file, copy_file=True)
|
||||
optimize.load_data(None, pairs=['BTC_UNITTEST'], ticker_interval=30)
|
||||
assert os.path.isfile(file) is True
|
||||
assert not log_has('Download the pair: "BTC_ETH", Interval: 30 min', caplog.record_tuples)
|
||||
_clean_test_file(file)
|
||||
|
||||
|
||||
def test_load_data_5min_ticker(ticker_history, mocker, caplog) -> None:
|
||||
"""
|
||||
Test load_data() with 5 min ticker
|
||||
"""
|
||||
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
||||
|
||||
file = 'freqtrade/tests/testdata/BTC_ETH-5.json'
|
||||
_backup_file(file, copy_file=True)
|
||||
optimize.load_data(None, pairs=['BTC_ETH'], ticker_interval=5)
|
||||
assert os.path.isfile(file) is True
|
||||
assert not log_has('Download the pair: "BTC_ETH", Interval: 5 min', caplog.record_tuples)
|
||||
_clean_test_file(file)
|
||||
|
||||
|
||||
def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None:
|
||||
"""
|
||||
Test load_data() with 1 min ticker
|
||||
"""
|
||||
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
||||
|
||||
file = 'freqtrade/tests/testdata/BTC_ETH-1.json'
|
||||
_backup_file(file, copy_file=True)
|
||||
optimize.load_data(None, ticker_interval=1, pairs=['BTC_ETH'])
|
||||
assert os.path.isfile(file) is True
|
||||
assert not log_has('Download the pair: "BTC_ETH", Interval: 1 min', caplog.record_tuples)
|
||||
_clean_test_file(file)
|
||||
|
||||
|
||||
def test_load_data_with_new_pair_1min(ticker_history, mocker, caplog) -> None:
|
||||
"""
|
||||
Test load_data() with 1 min ticker
|
||||
"""
|
||||
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
||||
|
||||
file = 'freqtrade/tests/testdata/BTC_MEME-1.json'
|
||||
_backup_file(file)
|
||||
optimize.load_data(None, ticker_interval=1, pairs=['BTC_MEME'])
|
||||
assert os.path.isfile(file) is True
|
||||
assert log_has('Download the pair: "BTC_MEME", Interval: 1 min', caplog.record_tuples)
|
||||
_clean_test_file(file)
|
||||
|
||||
|
||||
def test_testdata_path() -> None:
|
||||
assert os.path.join('freqtrade', 'tests', 'testdata') in make_testdata_path(None)
|
||||
|
||||
|
||||
def test_download_pairs(ticker_history, mocker) -> None:
|
||||
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=ticker_history)
|
||||
|
||||
file1_1 = 'freqtrade/tests/testdata/BTC_MEME-1.json'
|
||||
file1_5 = 'freqtrade/tests/testdata/BTC_MEME-5.json'
|
||||
file2_1 = 'freqtrade/tests/testdata/BTC_CFI-1.json'
|
||||
file2_5 = 'freqtrade/tests/testdata/BTC_CFI-5.json'
|
||||
|
||||
_backup_file(file1_1)
|
||||
_backup_file(file1_5)
|
||||
_backup_file(file2_1)
|
||||
_backup_file(file2_5)
|
||||
|
||||
assert os.path.isfile(file1_1) is False
|
||||
assert os.path.isfile(file2_1) is False
|
||||
|
||||
assert download_pairs(None, pairs=['BTC-MEME', 'BTC-CFI'], ticker_interval=1) is True
|
||||
|
||||
assert os.path.isfile(file1_1) is True
|
||||
assert os.path.isfile(file2_1) is True
|
||||
|
||||
# clean files freshly downloaded
|
||||
_clean_test_file(file1_1)
|
||||
_clean_test_file(file2_1)
|
||||
|
||||
assert os.path.isfile(file1_5) is False
|
||||
assert os.path.isfile(file2_5) is False
|
||||
|
||||
assert download_pairs(None, pairs=['BTC-MEME', 'BTC-CFI'], ticker_interval=5) is True
|
||||
|
||||
assert os.path.isfile(file1_5) is True
|
||||
assert os.path.isfile(file2_5) is True
|
||||
|
||||
# clean files freshly downloaded
|
||||
_clean_test_file(file1_5)
|
||||
_clean_test_file(file2_5)
|
||||
|
||||
|
||||
def test_download_pairs_exception(ticker_history, mocker, caplog) -> None:
|
||||
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=ticker_history)
|
||||
mocker.patch('freqtrade.optimize.__init__.download_backtesting_testdata',
|
||||
side_effect=BaseException('File Error'))
|
||||
|
||||
file1_1 = 'freqtrade/tests/testdata/BTC_MEME-1.json'
|
||||
file1_5 = 'freqtrade/tests/testdata/BTC_MEME-5.json'
|
||||
_backup_file(file1_1)
|
||||
_backup_file(file1_5)
|
||||
|
||||
download_pairs(None, pairs=['BTC-MEME'], ticker_interval=1)
|
||||
# clean files freshly downloaded
|
||||
_clean_test_file(file1_1)
|
||||
_clean_test_file(file1_5)
|
||||
assert log_has('Failed to download the pair: "BTC-MEME", Interval: 1 min', caplog.record_tuples)
|
||||
|
||||
|
||||
def test_download_backtesting_testdata(ticker_history, mocker) -> None:
|
||||
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=ticker_history)
|
||||
|
||||
# Download a 1 min ticker file
|
||||
file1 = 'freqtrade/tests/testdata/BTC_XEL-1.json'
|
||||
_backup_file(file1)
|
||||
download_backtesting_testdata(None, pair="BTC-XEL", interval=1)
|
||||
assert os.path.isfile(file1) is True
|
||||
_clean_test_file(file1)
|
||||
|
||||
# Download a 5 min ticker file
|
||||
file2 = 'freqtrade/tests/testdata/BTC_STORJ-5.json'
|
||||
_backup_file(file2)
|
||||
|
||||
download_backtesting_testdata(None, pair="BTC-STORJ", interval=5)
|
||||
assert os.path.isfile(file2) is True
|
||||
_clean_test_file(file2)
|
||||
|
||||
|
||||
def test_download_backtesting_testdata2(mocker) -> None:
|
||||
tick = [{'T': 'bar'}, {'T': 'foo'}]
|
||||
json_dump_mock = mocker.patch('freqtrade.misc.file_dump_json', return_value=None)
|
||||
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=tick)
|
||||
download_backtesting_testdata(None, pair="BTC-UNITEST", interval=1)
|
||||
download_backtesting_testdata(None, pair="BTC-UNITEST", interval=3)
|
||||
assert json_dump_mock.call_count == 2
|
||||
|
||||
|
||||
def test_load_tickerdata_file() -> None:
|
||||
# 7 does not exist in either format.
|
||||
assert not load_tickerdata_file(None, 'BTC_UNITEST', 7)
|
||||
# 1 exists only as a .json
|
||||
tickerdata = load_tickerdata_file(None, 'BTC_UNITEST', 1)
|
||||
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
|
||||
# 8 .json is empty and will fail if it's loaded. .json.gz is a copy of 1.json
|
||||
tickerdata = load_tickerdata_file(None, 'BTC_UNITEST', 8)
|
||||
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
|
||||
|
||||
|
||||
def test_init(default_conf, mocker) -> None:
|
||||
conf = {'exchange': {'pair_whitelist': []}}
|
||||
mocker.patch('freqtrade.optimize.hyperopt_optimize_conf', return_value=conf)
|
||||
assert {} == optimize.load_data(
|
||||
'',
|
||||
pairs=[],
|
||||
refresh_pairs=True,
|
||||
ticker_interval=int(default_conf['ticker_interval'])
|
||||
)
|
||||
|
||||
|
||||
def test_trim_tickerlist() -> None:
|
||||
with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file:
|
||||
ticker_list = json.load(data_file)
|
||||
ticker_list_len = len(ticker_list)
|
||||
|
||||
# Test the pattern ^(-\d+)$
|
||||
# This pattern remove X element from the beginning
|
||||
timerange = ((None, 'line'), None, 5)
|
||||
ticker = trim_tickerlist(ticker_list, timerange)
|
||||
ticker_len = len(ticker)
|
||||
|
||||
assert ticker_list_len == ticker_len + 5
|
||||
assert ticker_list[0] is not ticker[0] # The first element should be different
|
||||
assert ticker_list[-1] is ticker[-1] # The last element must be the same
|
||||
|
||||
# Test the pattern ^(\d+)-$
|
||||
# This pattern keep X element from the end
|
||||
timerange = (('line', None), 5, None)
|
||||
ticker = trim_tickerlist(ticker_list, timerange)
|
||||
ticker_len = len(ticker)
|
||||
|
||||
assert ticker_len == 5
|
||||
assert ticker_list[0] is ticker[0] # The first element must be the same
|
||||
assert ticker_list[-1] is not ticker[-1] # The last element should be different
|
||||
|
||||
# Test the pattern ^(\d+)-(\d+)$
|
||||
# This pattern extract a window
|
||||
timerange = (('index', 'index'), 5, 10)
|
||||
ticker = trim_tickerlist(ticker_list, timerange)
|
||||
ticker_len = len(ticker)
|
||||
|
||||
assert ticker_len == 5
|
||||
assert ticker_list[0] is not ticker[0] # The first element should be different
|
||||
assert ticker_list[5] is ticker[0] # The list starts at the index 5
|
||||
assert ticker_list[9] is ticker[-1] # The list ends at the index 9 (5 elements)
|
||||
|
||||
# Test a wrong pattern
|
||||
# This pattern must return the list unchanged
|
||||
timerange = ((None, None), None, 5)
|
||||
ticker = trim_tickerlist(ticker_list, timerange)
|
||||
ticker_len = len(ticker)
|
||||
|
||||
assert ticker_list_len == ticker_len
|
||||
|
||||
|
||||
def test_file_dump_json() -> None:
|
||||
"""
|
||||
Test file_dump_json()
|
||||
:return: None
|
||||
"""
|
||||
file = 'freqtrade/tests/testdata/test_{id}.json'.format(id=str(uuid.uuid4()))
|
||||
data = {'bar': 'foo'}
|
||||
|
||||
# check the file we will create does not exist
|
||||
assert os.path.isfile(file) is False
|
||||
|
||||
# Create the Json file
|
||||
file_dump_json(file, data)
|
||||
|
||||
# Check the file was create
|
||||
assert os.path.isfile(file) is True
|
||||
|
||||
# Open the Json file created and test the data is in it
|
||||
with open(file) as data_file:
|
||||
json_from_file = json.load(data_file)
|
||||
|
||||
assert 'bar' in json_from_file
|
||||
assert json_from_file['bar'] == 'foo'
|
||||
|
||||
# Remove the file
|
||||
_clean_test_file(file)
|
||||
544
freqtrade/tests/rpc/test_rpc.py
Normal file
544
freqtrade/tests/rpc/test_rpc.py
Normal file
@@ -0,0 +1,544 @@
|
||||
# pragma pylint: disable=invalid-sequence-index, invalid-name, too-many-arguments
|
||||
|
||||
"""
|
||||
Unit test file for rpc/rpc.py
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from freqtrade.freqtradebot import FreqtradeBot
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.rpc.rpc import RPC
|
||||
from freqtrade.state import State
|
||||
from freqtrade.tests.test_freqtradebot import patch_get_signal, patch_coinmarketcap
|
||||
|
||||
|
||||
# Functions for recurrent object patching
|
||||
def prec_satoshi(a, b) -> float:
|
||||
"""
|
||||
:return: True if A and B differs less than one satoshi.
|
||||
"""
|
||||
return abs(a - b) < 0.00000001
|
||||
|
||||
|
||||
# Unit tests
|
||||
def test_rpc_trade_status(default_conf, ticker, mocker) -> None:
|
||||
"""
|
||||
Test rpc_trade_status() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, result) = rpc.rpc_trade_status()
|
||||
assert error
|
||||
assert 'trader is not running' in result
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
(error, result) = rpc.rpc_trade_status()
|
||||
assert error
|
||||
assert 'no active trade' in result
|
||||
|
||||
freqtradebot.create_trade()
|
||||
(error, result) = rpc.rpc_trade_status()
|
||||
assert not error
|
||||
trade = result[0]
|
||||
|
||||
result_message = [
|
||||
'*Trade ID:* `1`\n'
|
||||
'*Current Pair:* '
|
||||
'[BTC_ETH](https://www.bittrex.com/Market/Index?MarketName=BTC-ETH)\n'
|
||||
'*Open Since:* `just now`\n'
|
||||
'*Amount:* `90.99181074`\n'
|
||||
'*Open Rate:* `0.00001099`\n'
|
||||
'*Close Rate:* `None`\n'
|
||||
'*Current Rate:* `0.00001098`\n'
|
||||
'*Close Profit:* `None`\n'
|
||||
'*Current Profit:* `-0.59%`\n'
|
||||
'*Open Order:* `(LIMIT_BUY rem=0.00000000)`'
|
||||
]
|
||||
assert result == result_message
|
||||
assert trade.find('[BTC_ETH]') >= 0
|
||||
|
||||
|
||||
def test_rpc_status_table(default_conf, ticker, mocker) -> None:
|
||||
"""
|
||||
Test rpc_status_table() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, result) = rpc.rpc_status_table()
|
||||
assert error
|
||||
assert '*Status:* `trader is not running`' in result
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
(error, result) = rpc.rpc_status_table()
|
||||
assert error
|
||||
assert '*Status:* `no active order`' in result
|
||||
|
||||
freqtradebot.create_trade()
|
||||
(error, result) = rpc.rpc_status_table()
|
||||
assert 'just now' in result['Since'].all()
|
||||
assert 'BTC_ETH' in result['Pair'].all()
|
||||
assert '-0.59%' in result['Profit'].all()
|
||||
|
||||
|
||||
def test_rpc_daily_profit(default_conf, update, ticker, limit_buy_order, limit_sell_order, mocker)\
|
||||
-> None:
|
||||
"""
|
||||
Test rpc_daily_profit() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
stake_currency = default_conf['stake_currency']
|
||||
fiat_display_currency = default_conf['fiat_display_currency']
|
||||
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
trade = Trade.query.first()
|
||||
assert trade
|
||||
|
||||
# Simulate buy & sell
|
||||
trade.update(limit_buy_order)
|
||||
trade.update(limit_sell_order)
|
||||
trade.close_date = datetime.utcnow()
|
||||
trade.is_open = False
|
||||
|
||||
# Try valid data
|
||||
update.message.text = '/daily 2'
|
||||
(error, days) = rpc.rpc_daily_profit(7, stake_currency, fiat_display_currency)
|
||||
assert not error
|
||||
assert len(days) == 7
|
||||
for day in days:
|
||||
# [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
|
||||
assert (day[1] == '0.00000000 BTC' or
|
||||
day[1] == '0.00006217 BTC')
|
||||
|
||||
assert (day[2] == '0.000 USD' or
|
||||
day[2] == '0.933 USD')
|
||||
# ensure first day is current date
|
||||
assert str(days[0][0]) == str(datetime.utcnow().date())
|
||||
|
||||
# Try invalid data
|
||||
(error, days) = rpc.rpc_daily_profit(0, stake_currency, fiat_display_currency)
|
||||
assert error
|
||||
assert days.find('must be an integer greater than 0') >= 0
|
||||
|
||||
|
||||
def test_rpc_trade_statistics(
|
||||
default_conf, ticker, ticker_sell_up, limit_buy_order, limit_sell_order, mocker) -> None:
|
||||
"""
|
||||
Test rpc_trade_statistics() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.fiat_convert.Market',
|
||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
||||
)
|
||||
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
stake_currency = default_conf['stake_currency']
|
||||
fiat_display_currency = default_conf['fiat_display_currency']
|
||||
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
(error, stats) = rpc.rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert error
|
||||
assert stats.find('no closed trade') >= 0
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
trade = Trade.query.first()
|
||||
# Simulate fulfilled LIMIT_BUY order for trade
|
||||
trade.update(limit_buy_order)
|
||||
|
||||
# Update the ticker with a market going up
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker_sell_up
|
||||
)
|
||||
trade.update(limit_sell_order)
|
||||
trade.close_date = datetime.utcnow()
|
||||
trade.is_open = False
|
||||
|
||||
(error, stats) = rpc.rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert not error
|
||||
assert prec_satoshi(stats['profit_closed_coin'], 6.217e-05)
|
||||
assert prec_satoshi(stats['profit_closed_percent'], 6.2)
|
||||
assert prec_satoshi(stats['profit_closed_fiat'], 0.93255)
|
||||
assert prec_satoshi(stats['profit_all_coin'], 6.217e-05)
|
||||
assert prec_satoshi(stats['profit_all_percent'], 6.2)
|
||||
assert prec_satoshi(stats['profit_all_fiat'], 0.93255)
|
||||
assert stats['trade_count'] == 1
|
||||
assert stats['first_trade_date'] == 'just now'
|
||||
assert stats['latest_trade_date'] == 'just now'
|
||||
assert stats['avg_duration'] == '0:00:00'
|
||||
assert stats['best_pair'] == 'BTC_ETH'
|
||||
assert prec_satoshi(stats['best_rate'], 6.2)
|
||||
|
||||
|
||||
# Test that rpc_trade_statistics can handle trades that lacks
|
||||
# trade.open_rate (it is set to None)
|
||||
def test_rpc_trade_statistics_closed(mocker, default_conf, ticker, ticker_sell_up, limit_buy_order,
|
||||
limit_sell_order):
|
||||
"""
|
||||
Test rpc_trade_statistics() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.fiat_convert.Market',
|
||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
||||
)
|
||||
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
stake_currency = default_conf['stake_currency']
|
||||
fiat_display_currency = default_conf['fiat_display_currency']
|
||||
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
trade = Trade.query.first()
|
||||
# Simulate fulfilled LIMIT_BUY order for trade
|
||||
trade.update(limit_buy_order)
|
||||
# Update the ticker with a market going up
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker_sell_up
|
||||
)
|
||||
trade.update(limit_sell_order)
|
||||
trade.close_date = datetime.utcnow()
|
||||
trade.is_open = False
|
||||
|
||||
for trade in Trade.query.order_by(Trade.id).all():
|
||||
trade.open_rate = None
|
||||
|
||||
(error, stats) = rpc.rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert not error
|
||||
assert prec_satoshi(stats['profit_closed_coin'], 0)
|
||||
assert prec_satoshi(stats['profit_closed_percent'], 0)
|
||||
assert prec_satoshi(stats['profit_closed_fiat'], 0)
|
||||
assert prec_satoshi(stats['profit_all_coin'], 0)
|
||||
assert prec_satoshi(stats['profit_all_percent'], 0)
|
||||
assert prec_satoshi(stats['profit_all_fiat'], 0)
|
||||
assert stats['trade_count'] == 1
|
||||
assert stats['first_trade_date'] == 'just now'
|
||||
assert stats['latest_trade_date'] == 'just now'
|
||||
assert stats['avg_duration'] == '0:00:00'
|
||||
assert stats['best_pair'] == 'BTC_ETH'
|
||||
assert prec_satoshi(stats['best_rate'], 6.2)
|
||||
|
||||
|
||||
def test_rpc_balance_handle(default_conf, mocker):
|
||||
"""
|
||||
Test rpc_balance() method
|
||||
"""
|
||||
mock_balance = [
|
||||
{
|
||||
'Currency': 'BTC',
|
||||
'Balance': 10.0,
|
||||
'Available': 12.0,
|
||||
'Pending': 0.0,
|
||||
'CryptoAddress': 'XXXX',
|
||||
},
|
||||
{
|
||||
'Currency': 'ETH',
|
||||
'Balance': 0.0,
|
||||
'Available': 0.0,
|
||||
'Pending': 0.0,
|
||||
'CryptoAddress': 'XXXX',
|
||||
}
|
||||
]
|
||||
|
||||
patch_get_signal(mocker, (True, False))
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.fiat_convert.Market',
|
||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
||||
)
|
||||
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_balances=MagicMock(return_value=mock_balance)
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
(error, res) = rpc.rpc_balance(default_conf['fiat_display_currency'])
|
||||
assert not error
|
||||
(trade, x, y, z) = res
|
||||
assert prec_satoshi(x, 10)
|
||||
assert prec_satoshi(z, 150000)
|
||||
assert 'USD' in y
|
||||
assert len(trade) == 1
|
||||
assert 'BTC' in trade[0]['currency']
|
||||
assert prec_satoshi(trade[0]['available'], 12)
|
||||
assert prec_satoshi(trade[0]['balance'], 10)
|
||||
assert prec_satoshi(trade[0]['pending'], 0)
|
||||
assert prec_satoshi(trade[0]['est_btc'], 10)
|
||||
|
||||
|
||||
def test_rpc_start(mocker, default_conf) -> None:
|
||||
"""
|
||||
Test rpc_start() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=MagicMock()
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
freqtradebot.state = State.STOPPED
|
||||
|
||||
(error, result) = rpc.rpc_start()
|
||||
assert not error
|
||||
assert '`Starting trader ...`' in result
|
||||
assert freqtradebot.state == State.RUNNING
|
||||
|
||||
(error, result) = rpc.rpc_start()
|
||||
assert error
|
||||
assert '*Status:* `already running`' in result
|
||||
assert freqtradebot.state == State.RUNNING
|
||||
|
||||
|
||||
def test_rpc_stop(mocker, default_conf) -> None:
|
||||
"""
|
||||
Test rpc_stop() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=MagicMock()
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
freqtradebot.state = State.RUNNING
|
||||
|
||||
(error, result) = rpc.rpc_stop()
|
||||
assert not error
|
||||
assert '`Stopping trader ...`' in result
|
||||
assert freqtradebot.state == State.STOPPED
|
||||
|
||||
(error, result) = rpc.rpc_stop()
|
||||
assert error
|
||||
assert '*Status:* `already stopped`' in result
|
||||
assert freqtradebot.state == State.STOPPED
|
||||
|
||||
|
||||
def test_rpc_forcesell(default_conf, ticker, mocker) -> None:
|
||||
"""
|
||||
Test rpc_forcesell() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
|
||||
cancel_order_mock = MagicMock()
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_ticker=ticker,
|
||||
cancel_order=cancel_order_mock,
|
||||
get_order=MagicMock(
|
||||
return_value={
|
||||
'closed': True,
|
||||
'type': 'LIMIT_BUY',
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, res) = rpc.rpc_forcesell(None)
|
||||
assert error
|
||||
assert res == '`trader is not running`'
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
(error, res) = rpc.rpc_forcesell(None)
|
||||
assert error
|
||||
assert res == 'Invalid argument.'
|
||||
|
||||
(error, res) = rpc.rpc_forcesell('all')
|
||||
assert not error
|
||||
assert res == ''
|
||||
|
||||
freqtradebot.create_trade()
|
||||
(error, res) = rpc.rpc_forcesell('all')
|
||||
assert not error
|
||||
assert res == ''
|
||||
|
||||
(error, res) = rpc.rpc_forcesell('1')
|
||||
assert not error
|
||||
assert res == ''
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, res) = rpc.rpc_forcesell(None)
|
||||
assert error
|
||||
assert res == '`trader is not running`'
|
||||
|
||||
(error, res) = rpc.rpc_forcesell('all')
|
||||
assert error
|
||||
assert res == '`trader is not running`'
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
assert cancel_order_mock.call_count == 0
|
||||
# make an limit-buy open trade
|
||||
mocker.patch(
|
||||
'freqtrade.freqtradebot.exchange.get_order',
|
||||
return_value={
|
||||
'closed': None,
|
||||
'type': 'LIMIT_BUY'
|
||||
}
|
||||
)
|
||||
# check that the trade is called, which is done
|
||||
# by ensuring exchange.cancel_order is called
|
||||
(error, res) = rpc.rpc_forcesell('1')
|
||||
assert not error
|
||||
assert res == ''
|
||||
assert cancel_order_mock.call_count == 1
|
||||
|
||||
freqtradebot.create_trade()
|
||||
# make an limit-sell open trade
|
||||
mocker.patch(
|
||||
'freqtrade.freqtradebot.exchange.get_order',
|
||||
return_value={
|
||||
'closed': None,
|
||||
'type': 'LIMIT_SELL'
|
||||
}
|
||||
)
|
||||
(error, res) = rpc.rpc_forcesell('2')
|
||||
assert not error
|
||||
assert res == ''
|
||||
# status quo, no exchange calls
|
||||
assert cancel_order_mock.call_count == 1
|
||||
|
||||
|
||||
def test_performance_handle(default_conf, ticker, limit_buy_order,
|
||||
limit_sell_order, mocker) -> None:
|
||||
"""
|
||||
Test rpc_performance() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_balances=MagicMock(return_value=ticker),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
trade = Trade.query.first()
|
||||
assert trade
|
||||
|
||||
# Simulate fulfilled LIMIT_BUY order for trade
|
||||
trade.update(limit_buy_order)
|
||||
|
||||
# Simulate fulfilled LIMIT_SELL order for trade
|
||||
trade.update(limit_sell_order)
|
||||
|
||||
trade.close_date = datetime.utcnow()
|
||||
trade.is_open = False
|
||||
(error, res) = rpc.rpc_performance()
|
||||
assert not error
|
||||
assert len(res) == 1
|
||||
assert res[0]['pair'] == 'BTC_ETH'
|
||||
assert res[0]['count'] == 1
|
||||
assert prec_satoshi(res[0]['profit'], 6.2)
|
||||
|
||||
|
||||
def test_rpc_count(mocker, default_conf, ticker) -> None:
|
||||
"""
|
||||
Test rpc_count() method
|
||||
"""
|
||||
patch_get_signal(mocker, (True, False))
|
||||
patch_coinmarketcap(mocker)
|
||||
mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
validate_pairs=MagicMock(),
|
||||
get_balances=MagicMock(return_value=ticker),
|
||||
get_ticker=ticker
|
||||
)
|
||||
|
||||
freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
(error, trades) = rpc.rpc_count()
|
||||
nb_trades = len(trades)
|
||||
assert not error
|
||||
assert nb_trades == 0
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
(error, trades) = rpc.rpc_count()
|
||||
nb_trades = len(trades)
|
||||
assert not error
|
||||
assert nb_trades == 1
|
||||
139
freqtrade/tests/rpc/test_rpc_manager.py
Normal file
139
freqtrade/tests/rpc/test_rpc_manager.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""
|
||||
Unit test file for rpc/rpc_manager.py
|
||||
"""
|
||||
|
||||
import logging
|
||||
from copy import deepcopy
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from freqtrade.rpc.rpc_manager import RPCManager
|
||||
from freqtrade.rpc.telegram import Telegram
|
||||
from freqtrade.tests.conftest import log_has, get_patched_freqtradebot
|
||||
|
||||
|
||||
def test_rpc_manager_object() -> None:
|
||||
"""
|
||||
Test the Arguments object has the mandatory methods
|
||||
:return: None
|
||||
"""
|
||||
assert hasattr(RPCManager, '_init')
|
||||
assert hasattr(RPCManager, 'send_msg')
|
||||
assert hasattr(RPCManager, 'cleanup')
|
||||
|
||||
|
||||
def test__init__(mocker, default_conf) -> None:
|
||||
"""
|
||||
Test __init__() method
|
||||
"""
|
||||
init_mock = mocker.patch('freqtrade.rpc.rpc_manager.RPCManager._init', MagicMock())
|
||||
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
||||
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
assert rpc_manager.freqtrade == freqtradebot
|
||||
assert rpc_manager.registered_modules == []
|
||||
assert rpc_manager.telegram is None
|
||||
assert init_mock.call_count == 1
|
||||
|
||||
|
||||
def test_init_telegram_disabled(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test _init() method with Telegram disabled
|
||||
"""
|
||||
caplog.set_level(logging.DEBUG)
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf['telegram']['enabled'] = False
|
||||
|
||||
freqtradebot = get_patched_freqtradebot(mocker, conf)
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
|
||||
assert not log_has('Enabling rpc.telegram ...', caplog.record_tuples)
|
||||
assert rpc_manager.registered_modules == []
|
||||
assert rpc_manager.telegram is None
|
||||
|
||||
|
||||
def test_init_telegram_enabled(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test _init() method with Telegram enabled
|
||||
"""
|
||||
caplog.set_level(logging.DEBUG)
|
||||
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
||||
|
||||
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
|
||||
assert log_has('Enabling rpc.telegram ...', caplog.record_tuples)
|
||||
len_modules = len(rpc_manager.registered_modules)
|
||||
assert len_modules == 1
|
||||
assert 'telegram' in rpc_manager.registered_modules
|
||||
assert isinstance(rpc_manager.telegram, Telegram)
|
||||
|
||||
|
||||
def test_cleanup_telegram_disabled(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test cleanup() method with Telegram disabled
|
||||
"""
|
||||
caplog.set_level(logging.DEBUG)
|
||||
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.cleanup', MagicMock())
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf['telegram']['enabled'] = False
|
||||
|
||||
freqtradebot = get_patched_freqtradebot(mocker, conf)
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
rpc_manager.cleanup()
|
||||
|
||||
assert not log_has('Cleaning up rpc.telegram ...', caplog.record_tuples)
|
||||
assert telegram_mock.call_count == 0
|
||||
|
||||
|
||||
def test_cleanup_telegram_enabled(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test cleanup() method with Telegram enabled
|
||||
"""
|
||||
caplog.set_level(logging.DEBUG)
|
||||
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
||||
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.cleanup', MagicMock())
|
||||
|
||||
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
|
||||
# Check we have Telegram as a registered modules
|
||||
assert 'telegram' in rpc_manager.registered_modules
|
||||
|
||||
rpc_manager.cleanup()
|
||||
assert log_has('Cleaning up rpc.telegram ...', caplog.record_tuples)
|
||||
assert 'telegram' not in rpc_manager.registered_modules
|
||||
assert telegram_mock.call_count == 1
|
||||
|
||||
|
||||
def test_send_msg_telegram_disabled(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test send_msg() method with Telegram disabled
|
||||
"""
|
||||
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
||||
|
||||
conf = deepcopy(default_conf)
|
||||
conf['telegram']['enabled'] = False
|
||||
|
||||
freqtradebot = get_patched_freqtradebot(mocker, conf)
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
rpc_manager.send_msg('test')
|
||||
|
||||
assert log_has('test', caplog.record_tuples)
|
||||
assert telegram_mock.call_count == 0
|
||||
|
||||
|
||||
def test_send_msg_telegram_enabled(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test send_msg() method with Telegram disabled
|
||||
"""
|
||||
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
||||
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
||||
|
||||
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
rpc_manager.send_msg('test')
|
||||
|
||||
assert log_has('test', caplog.record_tuples)
|
||||
assert telegram_mock.call_count == 1
|
||||
1096
freqtrade/tests/rpc/test_rpc_telegram.py
Normal file
1096
freqtrade/tests/rpc/test_rpc_telegram.py
Normal file
File diff suppressed because it is too large
Load Diff
34
freqtrade/tests/strategy/test_default_strategy.py
Normal file
34
freqtrade/tests/strategy/test_default_strategy.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.strategy.default_strategy import DefaultStrategy
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def result():
|
||||
with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file:
|
||||
return Analyze.parse_ticker_dataframe(json.load(data_file))
|
||||
|
||||
|
||||
def test_default_strategy_structure():
|
||||
assert hasattr(DefaultStrategy, 'minimal_roi')
|
||||
assert hasattr(DefaultStrategy, 'stoploss')
|
||||
assert hasattr(DefaultStrategy, 'ticker_interval')
|
||||
assert hasattr(DefaultStrategy, 'populate_indicators')
|
||||
assert hasattr(DefaultStrategy, 'populate_buy_trend')
|
||||
assert hasattr(DefaultStrategy, 'populate_sell_trend')
|
||||
|
||||
|
||||
def test_default_strategy(result):
|
||||
strategy = DefaultStrategy()
|
||||
|
||||
assert type(strategy.minimal_roi) is dict
|
||||
assert type(strategy.stoploss) is float
|
||||
assert type(strategy.ticker_interval) is int
|
||||
indicators = strategy.populate_indicators(result)
|
||||
assert type(indicators) is DataFrame
|
||||
assert type(strategy.populate_buy_trend(indicators)) is DataFrame
|
||||
assert type(strategy.populate_sell_trend(indicators)) is DataFrame
|
||||
118
freqtrade/tests/strategy/test_strategy.py
Normal file
118
freqtrade/tests/strategy/test_strategy.py
Normal file
@@ -0,0 +1,118 @@
|
||||
# pragma pylint: disable=missing-docstring, protected-access, C0103
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from freqtrade.strategy.interface import IStrategy
|
||||
from freqtrade.strategy.resolver import StrategyResolver
|
||||
|
||||
|
||||
def test_search_strategy():
|
||||
default_location = os.path.join(os.path.dirname(
|
||||
os.path.realpath(__file__)), '..', '..', 'strategy'
|
||||
)
|
||||
assert isinstance(
|
||||
StrategyResolver._search_strategy(default_location, 'DefaultStrategy'), IStrategy
|
||||
)
|
||||
assert StrategyResolver._search_strategy(default_location, 'NotFoundStrategy') is None
|
||||
|
||||
|
||||
def test_load_strategy(result):
|
||||
resolver = StrategyResolver()
|
||||
resolver._load_strategy('TestStrategy')
|
||||
assert hasattr(resolver.strategy, 'populate_indicators')
|
||||
assert 'adx' in resolver.strategy.populate_indicators(result)
|
||||
|
||||
|
||||
def test_load_strategy_custom_directory(result):
|
||||
resolver = StrategyResolver()
|
||||
extra_dir = os.path.join('some', 'path')
|
||||
with pytest.raises(
|
||||
FileNotFoundError,
|
||||
match=r".*No such file or directory: '{}'".format(extra_dir)):
|
||||
resolver._load_strategy('TestStrategy', extra_dir)
|
||||
|
||||
assert hasattr(resolver.strategy, 'populate_indicators')
|
||||
assert 'adx' in resolver.strategy.populate_indicators(result)
|
||||
|
||||
|
||||
def test_load_not_found_strategy():
|
||||
strategy = StrategyResolver()
|
||||
with pytest.raises(ImportError,
|
||||
match=r'Impossible to load Strategy \'NotFoundStrategy\'.'
|
||||
r' This class does not exist or contains Python code errors'):
|
||||
strategy._load_strategy('NotFoundStrategy')
|
||||
|
||||
|
||||
def test_strategy(result):
|
||||
resolver = StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
|
||||
assert hasattr(resolver.strategy, 'minimal_roi')
|
||||
assert resolver.strategy.minimal_roi[0] == 0.04
|
||||
|
||||
assert hasattr(resolver.strategy, 'stoploss')
|
||||
assert resolver.strategy.stoploss == -0.10
|
||||
|
||||
assert hasattr(resolver.strategy, 'populate_indicators')
|
||||
assert 'adx' in resolver.strategy.populate_indicators(result)
|
||||
|
||||
assert hasattr(resolver.strategy, 'populate_buy_trend')
|
||||
dataframe = resolver.strategy.populate_buy_trend(resolver.strategy.populate_indicators(result))
|
||||
assert 'buy' in dataframe.columns
|
||||
|
||||
assert hasattr(resolver.strategy, 'populate_sell_trend')
|
||||
dataframe = resolver.strategy.populate_sell_trend(resolver.strategy.populate_indicators(result))
|
||||
assert 'sell' in dataframe.columns
|
||||
|
||||
|
||||
def test_strategy_override_minimal_roi(caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
config = {
|
||||
'strategy': 'DefaultStrategy',
|
||||
'minimal_roi': {
|
||||
"0": 0.5
|
||||
}
|
||||
}
|
||||
resolver = StrategyResolver(config)
|
||||
|
||||
assert hasattr(resolver.strategy, 'minimal_roi')
|
||||
assert resolver.strategy.minimal_roi[0] == 0.5
|
||||
assert ('freqtrade.strategy.resolver',
|
||||
logging.INFO,
|
||||
'Override strategy \'minimal_roi\' with value in config file.'
|
||||
) in caplog.record_tuples
|
||||
|
||||
|
||||
def test_strategy_override_stoploss(caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
config = {
|
||||
'strategy': 'DefaultStrategy',
|
||||
'stoploss': -0.5
|
||||
}
|
||||
resolver = StrategyResolver(config)
|
||||
|
||||
assert hasattr(resolver.strategy, 'stoploss')
|
||||
assert resolver.strategy.stoploss == -0.5
|
||||
assert ('freqtrade.strategy.resolver',
|
||||
logging.INFO,
|
||||
'Override strategy \'stoploss\' with value in config file: -0.5.'
|
||||
) in caplog.record_tuples
|
||||
|
||||
|
||||
def test_strategy_override_ticker_interval(caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
|
||||
config = {
|
||||
'strategy': 'DefaultStrategy',
|
||||
'ticker_interval': 60
|
||||
}
|
||||
resolver = StrategyResolver(config)
|
||||
|
||||
assert hasattr(resolver.strategy, 'ticker_interval')
|
||||
assert resolver.strategy.ticker_interval == 60
|
||||
assert ('freqtrade.strategy.resolver',
|
||||
logging.INFO,
|
||||
'Override strategy \'ticker_interval\' with value in config file: 60.'
|
||||
) in caplog.record_tuples
|
||||
146
freqtrade/tests/test_acl_pair.py
Normal file
146
freqtrade/tests/test_acl_pair.py
Normal file
@@ -0,0 +1,146 @@
|
||||
# pragma pylint: disable=missing-docstring,C0103,protected-access
|
||||
|
||||
import freqtrade.tests.conftest as tt # test tools
|
||||
|
||||
# whitelist, blacklist, filtering, all of that will
|
||||
# eventually become some rules to run on a generic ACL engine
|
||||
# perhaps try to anticipate that by using some python package
|
||||
|
||||
|
||||
def whitelist_conf():
|
||||
config = tt.default_conf()
|
||||
|
||||
config['stake_currency'] = 'BTC'
|
||||
config['exchange']['pair_whitelist'] = [
|
||||
'BTC_ETH',
|
||||
'BTC_TKN',
|
||||
'BTC_TRST',
|
||||
'BTC_SWT',
|
||||
'BTC_BCC'
|
||||
]
|
||||
|
||||
config['exchange']['pair_blacklist'] = [
|
||||
'BTC_BLK'
|
||||
]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_market_summaries():
|
||||
return [{
|
||||
'MarketName': 'BTC-TKN',
|
||||
'High': 0.00000919,
|
||||
'Low': 0.00000820,
|
||||
'Volume': 74339.61396015,
|
||||
'Last': 0.00000820,
|
||||
'BaseVolume': 1664,
|
||||
'TimeStamp': '2014-07-09T07:19:30.15',
|
||||
'Bid': 0.00000820,
|
||||
'Ask': 0.00000831,
|
||||
'OpenBuyOrders': 15,
|
||||
'OpenSellOrders': 15,
|
||||
'PrevDay': 0.00000821,
|
||||
'Created': '2014-03-20T06:00:00',
|
||||
'DisplayMarketName': ''
|
||||
}, {
|
||||
'MarketName': 'BTC-ETH',
|
||||
'High': 0.00000072,
|
||||
'Low': 0.00000001,
|
||||
'Volume': 166340678.42280999,
|
||||
'Last': 0.00000005,
|
||||
'BaseVolume': 42,
|
||||
'TimeStamp': '2014-07-09T07:21:40.51',
|
||||
'Bid': 0.00000004,
|
||||
'Ask': 0.00000005,
|
||||
'OpenBuyOrders': 18,
|
||||
'OpenSellOrders': 18,
|
||||
'PrevDay': 0.00000002,
|
||||
'Created': '2014-05-30T07:57:49.637',
|
||||
'DisplayMarketName': ''
|
||||
}, {
|
||||
'MarketName': 'BTC-BLK',
|
||||
'High': 0.00000072,
|
||||
'Low': 0.00000001,
|
||||
'Volume': 166340678.42280999,
|
||||
'Last': 0.00000005,
|
||||
'BaseVolume': 3,
|
||||
'TimeStamp': '2014-07-09T07:21:40.51',
|
||||
'Bid': 0.00000004,
|
||||
'Ask': 0.00000005,
|
||||
'OpenBuyOrders': 18,
|
||||
'OpenSellOrders': 18,
|
||||
'PrevDay': 0.00000002,
|
||||
'Created': '2014-05-30T07:57:49.637',
|
||||
'DisplayMarketName': ''
|
||||
}]
|
||||
|
||||
|
||||
def get_health():
|
||||
return [{'Currency': 'ETH', 'IsActive': True},
|
||||
{'Currency': 'TKN', 'IsActive': True},
|
||||
{'Currency': 'BLK', 'IsActive': True}]
|
||||
|
||||
|
||||
def get_health_empty():
|
||||
return []
|
||||
|
||||
|
||||
def test_refresh_market_pair_not_in_whitelist(mocker):
|
||||
conf = whitelist_conf()
|
||||
|
||||
freqtradebot = tt.get_patched_freqtradebot(mocker, conf)
|
||||
|
||||
mocker.patch('freqtrade.freqtradebot.exchange.get_wallet_health', get_health)
|
||||
refreshedwhitelist = freqtradebot._refresh_whitelist(
|
||||
conf['exchange']['pair_whitelist'] + ['BTC_XXX']
|
||||
)
|
||||
# List ordered by BaseVolume
|
||||
whitelist = ['BTC_ETH', 'BTC_TKN']
|
||||
# Ensure all except those in whitelist are removed
|
||||
assert whitelist == refreshedwhitelist
|
||||
|
||||
|
||||
def test_refresh_whitelist(mocker):
|
||||
conf = whitelist_conf()
|
||||
freqtradebot = tt.get_patched_freqtradebot(mocker, conf)
|
||||
|
||||
mocker.patch('freqtrade.freqtradebot.exchange.get_wallet_health', get_health)
|
||||
refreshedwhitelist = freqtradebot._refresh_whitelist(conf['exchange']['pair_whitelist'])
|
||||
|
||||
# List ordered by BaseVolume
|
||||
whitelist = ['BTC_ETH', 'BTC_TKN']
|
||||
# Ensure all except those in whitelist are removed
|
||||
assert whitelist == refreshedwhitelist
|
||||
|
||||
|
||||
def test_refresh_whitelist_dynamic(mocker):
|
||||
conf = whitelist_conf()
|
||||
freqtradebot = tt.get_patched_freqtradebot(mocker, conf)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.exchange',
|
||||
get_wallet_health=get_health,
|
||||
get_market_summaries=get_market_summaries
|
||||
)
|
||||
|
||||
# argument: use the whitelist dynamically by exchange-volume
|
||||
whitelist = ['BTC_TKN', 'BTC_ETH']
|
||||
|
||||
refreshedwhitelist = freqtradebot._refresh_whitelist(
|
||||
freqtradebot._gen_pair_whitelist(conf['stake_currency'])
|
||||
)
|
||||
|
||||
assert whitelist == refreshedwhitelist
|
||||
|
||||
|
||||
def test_refresh_whitelist_dynamic_empty(mocker):
|
||||
conf = whitelist_conf()
|
||||
freqtradebot = tt.get_patched_freqtradebot(mocker, conf)
|
||||
mocker.patch('freqtrade.freqtradebot.exchange.get_wallet_health', get_health_empty)
|
||||
|
||||
# argument: use the whitelist dynamically by exchange-volume
|
||||
whitelist = []
|
||||
conf['exchange']['pair_whitelist'] = []
|
||||
freqtradebot._refresh_whitelist(whitelist)
|
||||
pairslist = conf['exchange']['pair_whitelist']
|
||||
|
||||
assert set(whitelist) == set(pairslist)
|
||||
194
freqtrade/tests/test_analyze.py
Normal file
194
freqtrade/tests/test_analyze.py
Normal file
@@ -0,0 +1,194 @@
|
||||
# pragma pylint: disable=missing-docstring, C0103
|
||||
|
||||
"""
|
||||
Unit test file for analyse.py
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import arrow
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.analyze import Analyze, SignalType
|
||||
from freqtrade.optimize.__init__ import load_tickerdata_file
|
||||
from freqtrade.tests.conftest import log_has
|
||||
|
||||
# Avoid to reinit the same object again and again
|
||||
_ANALYZE = Analyze({'strategy': 'DefaultStrategy'})
|
||||
|
||||
|
||||
def test_signaltype_object() -> None:
|
||||
"""
|
||||
Test the SignalType object has the mandatory Constants
|
||||
:return: None
|
||||
"""
|
||||
assert hasattr(SignalType, 'BUY')
|
||||
assert hasattr(SignalType, 'SELL')
|
||||
|
||||
|
||||
def test_analyze_object() -> None:
|
||||
"""
|
||||
Test the Analyze object has the mandatory methods
|
||||
:return: None
|
||||
"""
|
||||
assert hasattr(Analyze, 'parse_ticker_dataframe')
|
||||
assert hasattr(Analyze, 'populate_indicators')
|
||||
assert hasattr(Analyze, 'populate_buy_trend')
|
||||
assert hasattr(Analyze, 'populate_sell_trend')
|
||||
assert hasattr(Analyze, 'analyze_ticker')
|
||||
assert hasattr(Analyze, 'get_signal')
|
||||
assert hasattr(Analyze, 'should_sell')
|
||||
assert hasattr(Analyze, 'min_roi_reached')
|
||||
|
||||
|
||||
def test_dataframe_correct_length(result):
|
||||
dataframe = Analyze.parse_ticker_dataframe(result)
|
||||
assert len(result.index) == len(dataframe.index)
|
||||
|
||||
|
||||
def test_dataframe_correct_columns(result):
|
||||
assert result.columns.tolist() == \
|
||||
['date', 'close', 'high', 'low', 'open', 'volume']
|
||||
|
||||
|
||||
def test_populates_buy_trend(result):
|
||||
# Load the default strategy for the unit test, because this logic is done in main.py
|
||||
dataframe = _ANALYZE.populate_buy_trend(_ANALYZE.populate_indicators(result))
|
||||
assert 'buy' in dataframe.columns
|
||||
|
||||
|
||||
def test_populates_sell_trend(result):
|
||||
# Load the default strategy for the unit test, because this logic is done in main.py
|
||||
dataframe = _ANALYZE.populate_sell_trend(_ANALYZE.populate_indicators(result))
|
||||
assert 'sell' in dataframe.columns
|
||||
|
||||
|
||||
def test_returns_latest_buy_signal(mocker):
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
||||
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
return_value=DataFrame([{'buy': 1, 'sell': 0, 'date': arrow.utcnow()}])
|
||||
)
|
||||
)
|
||||
assert _ANALYZE.get_signal('BTC-ETH', 5) == (True, False)
|
||||
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
return_value=DataFrame([{'buy': 0, 'sell': 1, 'date': arrow.utcnow()}])
|
||||
)
|
||||
)
|
||||
assert _ANALYZE.get_signal('BTC-ETH', 5) == (False, True)
|
||||
|
||||
|
||||
def test_returns_latest_sell_signal(mocker):
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
return_value=DataFrame([{'sell': 1, 'buy': 0, 'date': arrow.utcnow()}])
|
||||
)
|
||||
)
|
||||
|
||||
assert _ANALYZE.get_signal('BTC-ETH', 5) == (False, True)
|
||||
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
return_value=DataFrame([{'sell': 0, 'buy': 1, 'date': arrow.utcnow()}])
|
||||
)
|
||||
)
|
||||
assert _ANALYZE.get_signal('BTC-ETH', 5) == (True, False)
|
||||
|
||||
|
||||
def test_get_signal_empty(default_conf, mocker, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=None)
|
||||
assert (False, False) == _ANALYZE.get_signal('foo', int(default_conf['ticker_interval']))
|
||||
assert log_has('Empty ticker history for pair foo', caplog.record_tuples)
|
||||
|
||||
|
||||
def test_get_signal_exception_valueerror(default_conf, mocker, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
side_effect=ValueError('xyz')
|
||||
)
|
||||
)
|
||||
assert (False, False) == _ANALYZE.get_signal('foo', int(default_conf['ticker_interval']))
|
||||
assert log_has('Unable to analyze ticker for pair foo: xyz', caplog.record_tuples)
|
||||
|
||||
|
||||
def test_get_signal_empty_dataframe(default_conf, mocker, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
return_value=DataFrame([])
|
||||
)
|
||||
)
|
||||
assert (False, False) == _ANALYZE.get_signal('xyz', int(default_conf['ticker_interval']))
|
||||
assert log_has('Empty dataframe for pair xyz', caplog.record_tuples)
|
||||
|
||||
|
||||
def test_get_signal_old_dataframe(default_conf, mocker, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
||||
# FIX: The get_signal function has hardcoded 10, which we must inturn hardcode
|
||||
oldtime = arrow.utcnow() - datetime.timedelta(minutes=11)
|
||||
ticks = DataFrame([{'buy': 1, 'date': oldtime}])
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
return_value=DataFrame(ticks)
|
||||
)
|
||||
)
|
||||
assert (False, False) == _ANALYZE.get_signal('xyz', int(default_conf['ticker_interval']))
|
||||
assert log_has(
|
||||
'Outdated history for pair xyz. Last tick is 11 minutes old',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
def test_get_signal_handles_exceptions(mocker):
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.analyze.Analyze',
|
||||
analyze_ticker=MagicMock(
|
||||
side_effect=Exception('invalid ticker history ')
|
||||
)
|
||||
)
|
||||
|
||||
assert _ANALYZE.get_signal('BTC-ETH', 5) == (False, False)
|
||||
|
||||
|
||||
def test_parse_ticker_dataframe(ticker_history, ticker_history_without_bv):
|
||||
columns = ['date', 'close', 'high', 'low', 'open', 'volume']
|
||||
|
||||
# Test file with BV data
|
||||
dataframe = Analyze.parse_ticker_dataframe(ticker_history)
|
||||
assert dataframe.columns.tolist() == columns
|
||||
|
||||
# Test file without BV data
|
||||
dataframe = Analyze.parse_ticker_dataframe(ticker_history_without_bv)
|
||||
assert dataframe.columns.tolist() == columns
|
||||
|
||||
|
||||
def test_tickerdata_to_dataframe(default_conf) -> None:
|
||||
"""
|
||||
Test Analyze.tickerdata_to_dataframe() method
|
||||
"""
|
||||
analyze = Analyze(default_conf)
|
||||
|
||||
timerange = ((None, 'line'), None, -100)
|
||||
tick = load_tickerdata_file(None, 'BTC_UNITEST', 1, timerange=timerange)
|
||||
tickerlist = {'BTC_UNITEST': tick}
|
||||
data = analyze.tickerdata_to_dataframe(tickerlist)
|
||||
assert len(data['BTC_UNITEST']) == 100
|
||||
154
freqtrade/tests/test_arguments.py
Normal file
154
freqtrade/tests/test_arguments.py
Normal file
@@ -0,0 +1,154 @@
|
||||
# pragma pylint: disable=missing-docstring, C0103
|
||||
|
||||
"""
|
||||
Unit test file for arguments.py
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from freqtrade.arguments import Arguments
|
||||
|
||||
|
||||
def test_arguments_object() -> None:
|
||||
"""
|
||||
Test the Arguments object has the mandatory methods
|
||||
:return: None
|
||||
"""
|
||||
assert hasattr(Arguments, 'get_parsed_arg')
|
||||
assert hasattr(Arguments, 'parse_args')
|
||||
assert hasattr(Arguments, 'parse_timerange')
|
||||
assert hasattr(Arguments, 'scripts_options')
|
||||
|
||||
|
||||
# Parse common command-line-arguments. Used for all tools
|
||||
def test_parse_args_none() -> None:
|
||||
arguments = Arguments([], '')
|
||||
assert isinstance(arguments, Arguments)
|
||||
assert isinstance(arguments.parser, argparse.ArgumentParser)
|
||||
assert isinstance(arguments.parser, argparse.ArgumentParser)
|
||||
|
||||
|
||||
def test_parse_args_defaults() -> None:
|
||||
args = Arguments([], '').get_parsed_arg()
|
||||
assert args.config == 'config.json'
|
||||
assert args.dynamic_whitelist is None
|
||||
assert args.loglevel == logging.INFO
|
||||
|
||||
|
||||
def test_parse_args_config() -> None:
|
||||
args = Arguments(['-c', '/dev/null'], '').get_parsed_arg()
|
||||
assert args.config == '/dev/null'
|
||||
|
||||
args = Arguments(['--config', '/dev/null'], '').get_parsed_arg()
|
||||
assert args.config == '/dev/null'
|
||||
|
||||
|
||||
def test_parse_args_verbose() -> None:
|
||||
args = Arguments(['-v'], '').get_parsed_arg()
|
||||
assert args.loglevel == logging.DEBUG
|
||||
|
||||
args = Arguments(['--verbose'], '').get_parsed_arg()
|
||||
assert args.loglevel == logging.DEBUG
|
||||
|
||||
|
||||
def test_scripts_options() -> None:
|
||||
arguments = Arguments(['-p', 'BTC_ETH'], '')
|
||||
arguments.scripts_options()
|
||||
args = arguments.get_parsed_arg()
|
||||
assert args.pair == 'BTC_ETH'
|
||||
|
||||
|
||||
def test_parse_args_version() -> None:
|
||||
with pytest.raises(SystemExit, match=r'0'):
|
||||
Arguments(['--version'], '').get_parsed_arg()
|
||||
|
||||
|
||||
def test_parse_args_invalid() -> None:
|
||||
with pytest.raises(SystemExit, match=r'2'):
|
||||
Arguments(['-c'], '').get_parsed_arg()
|
||||
|
||||
|
||||
def test_parse_args_strategy() -> None:
|
||||
args = Arguments(['--strategy', 'SomeStrategy'], '').get_parsed_arg()
|
||||
assert args.strategy == 'SomeStrategy'
|
||||
|
||||
|
||||
def test_parse_args_strategy_invalid() -> None:
|
||||
with pytest.raises(SystemExit, match=r'2'):
|
||||
Arguments(['--strategy'], '').get_parsed_arg()
|
||||
|
||||
|
||||
def test_parse_args_strategy_path() -> None:
|
||||
args = Arguments(['--strategy-path', '/some/path'], '').get_parsed_arg()
|
||||
assert args.strategy_path == '/some/path'
|
||||
|
||||
|
||||
def test_parse_args_strategy_path_invalid() -> None:
|
||||
with pytest.raises(SystemExit, match=r'2'):
|
||||
Arguments(['--strategy-path'], '').get_parsed_arg()
|
||||
|
||||
|
||||
def test_parse_args_dynamic_whitelist() -> None:
|
||||
args = Arguments(['--dynamic-whitelist'], '').get_parsed_arg()
|
||||
assert args.dynamic_whitelist == 20
|
||||
|
||||
|
||||
def test_parse_args_dynamic_whitelist_10() -> None:
|
||||
args = Arguments(['--dynamic-whitelist', '10'], '').get_parsed_arg()
|
||||
assert args.dynamic_whitelist == 10
|
||||
|
||||
|
||||
def test_parse_args_dynamic_whitelist_invalid_values() -> None:
|
||||
with pytest.raises(SystemExit, match=r'2'):
|
||||
Arguments(['--dynamic-whitelist', 'abc'], '').get_parsed_arg()
|
||||
|
||||
|
||||
def test_parse_timerange_incorrect() -> None:
|
||||
assert ((None, 'line'), None, -200) == Arguments.parse_timerange('-200')
|
||||
assert (('line', None), 200, None) == Arguments.parse_timerange('200-')
|
||||
with pytest.raises(Exception, match=r'Incorrect syntax.*'):
|
||||
Arguments.parse_timerange('-')
|
||||
|
||||
|
||||
def test_parse_args_backtesting_invalid() -> None:
|
||||
with pytest.raises(SystemExit, match=r'2'):
|
||||
Arguments(['backtesting --ticker-interval'], '').get_parsed_arg()
|
||||
|
||||
with pytest.raises(SystemExit, match=r'2'):
|
||||
Arguments(['backtesting --ticker-interval', 'abc'], '').get_parsed_arg()
|
||||
|
||||
|
||||
def test_parse_args_backtesting_custom() -> None:
|
||||
args = [
|
||||
'-c', 'test_conf.json',
|
||||
'backtesting',
|
||||
'--live',
|
||||
'--ticker-interval', '1',
|
||||
'--refresh-pairs-cached']
|
||||
call_args = Arguments(args, '').get_parsed_arg()
|
||||
assert call_args.config == 'test_conf.json'
|
||||
assert call_args.live is True
|
||||
assert call_args.loglevel == logging.INFO
|
||||
assert call_args.subparser == 'backtesting'
|
||||
assert call_args.func is not None
|
||||
assert call_args.ticker_interval == 1
|
||||
assert call_args.refresh_pairs is True
|
||||
|
||||
|
||||
def test_parse_args_hyperopt_custom() -> None:
|
||||
args = [
|
||||
'-c', 'test_conf.json',
|
||||
'hyperopt',
|
||||
'--epochs', '20',
|
||||
'--spaces', 'buy'
|
||||
]
|
||||
call_args = Arguments(args, '').get_parsed_arg()
|
||||
assert call_args.config == 'test_conf.json'
|
||||
assert call_args.epochs == 20
|
||||
assert call_args.loglevel == logging.INFO
|
||||
assert call_args.subparser == 'hyperopt'
|
||||
assert call_args.spaces == ['buy']
|
||||
assert call_args.func is not None
|
||||
336
freqtrade/tests/test_configuration.py
Normal file
336
freqtrade/tests/test_configuration.py
Normal file
@@ -0,0 +1,336 @@
|
||||
# pragma pylint: disable=protected-access, invalid-name
|
||||
|
||||
"""
|
||||
Unit test file for configuration.py
|
||||
"""
|
||||
import json
|
||||
from copy import deepcopy
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from jsonschema import ValidationError
|
||||
|
||||
from freqtrade.arguments import Arguments
|
||||
from freqtrade.configuration import Configuration
|
||||
from freqtrade.tests.conftest import log_has
|
||||
|
||||
|
||||
def test_configuration_object() -> None:
|
||||
"""
|
||||
Test the Constants object has the mandatory Constants
|
||||
"""
|
||||
assert hasattr(Configuration, 'load_config')
|
||||
assert hasattr(Configuration, '_load_config_file')
|
||||
assert hasattr(Configuration, '_validate_config')
|
||||
assert hasattr(Configuration, '_load_common_config')
|
||||
assert hasattr(Configuration, '_load_backtesting_config')
|
||||
assert hasattr(Configuration, '_load_hyperopt_config')
|
||||
assert hasattr(Configuration, 'get_config')
|
||||
|
||||
|
||||
def test_load_config_invalid_pair(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test the configuration validator with an invalid PAIR format
|
||||
"""
|
||||
conf = deepcopy(default_conf)
|
||||
conf['exchange']['pair_whitelist'].append('BTC-ETH')
|
||||
|
||||
with pytest.raises(ValidationError, match=r'.*does not match.*'):
|
||||
configuration = Configuration([])
|
||||
configuration._validate_config(conf)
|
||||
|
||||
|
||||
def test_load_config_missing_attributes(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test the configuration validator with a missing attribute
|
||||
"""
|
||||
conf = deepcopy(default_conf)
|
||||
conf.pop('exchange')
|
||||
|
||||
with pytest.raises(ValidationError, match=r'.*\'exchange\' is a required property.*'):
|
||||
configuration = Configuration([])
|
||||
configuration._validate_config(conf)
|
||||
|
||||
|
||||
def test_load_config_file(default_conf, mocker, caplog) -> None:
|
||||
"""
|
||||
Test Configuration._load_config_file() method
|
||||
"""
|
||||
file_mock = mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
configuration = Configuration([])
|
||||
validated_conf = configuration._load_config_file('somefile')
|
||||
assert file_mock.call_count == 1
|
||||
assert validated_conf.items() >= default_conf.items()
|
||||
assert 'internals' in validated_conf
|
||||
assert log_has('Validating configuration ...', caplog.record_tuples)
|
||||
|
||||
|
||||
def test_load_config_file_exception(mocker, caplog) -> None:
|
||||
"""
|
||||
Test Configuration._load_config_file() method
|
||||
"""
|
||||
mocker.patch(
|
||||
'freqtrade.configuration.open',
|
||||
MagicMock(side_effect=FileNotFoundError('File not found'))
|
||||
)
|
||||
configuration = Configuration([])
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
configuration._load_config_file('somefile')
|
||||
assert log_has(
|
||||
'Config file "somefile" not found. Please create your config file',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
def test_load_config(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test Configuration.load_config() without any cli params
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = Arguments([], '').get_parsed_arg()
|
||||
configuration = Configuration(args)
|
||||
validated_conf = configuration.load_config()
|
||||
|
||||
assert validated_conf.get('strategy') == 'DefaultStrategy'
|
||||
assert validated_conf.get('strategy_path') is None
|
||||
assert 'dynamic_whitelist' not in validated_conf
|
||||
assert 'dry_run_db' not in validated_conf
|
||||
|
||||
|
||||
def test_load_config_with_params(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test Configuration.load_config() with cli params used
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--dynamic-whitelist', '10',
|
||||
'--strategy', 'TestStrategy',
|
||||
'--strategy-path', '/some/path',
|
||||
'--dry-run-db',
|
||||
]
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
validated_conf = configuration.load_config()
|
||||
|
||||
assert validated_conf.get('dynamic_whitelist') == 10
|
||||
assert validated_conf.get('strategy') == 'TestStrategy'
|
||||
assert validated_conf.get('strategy_path') == '/some/path'
|
||||
assert validated_conf.get('dry_run_db') is True
|
||||
|
||||
|
||||
def test_load_custom_strategy(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test Configuration.load_config() without any cli params
|
||||
"""
|
||||
custom_conf = deepcopy(default_conf)
|
||||
custom_conf.update({
|
||||
'strategy': 'CustomStrategy',
|
||||
'strategy_path': '/tmp/strategies',
|
||||
})
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(custom_conf)
|
||||
))
|
||||
|
||||
args = Arguments([], '').get_parsed_arg()
|
||||
configuration = Configuration(args)
|
||||
validated_conf = configuration.load_config()
|
||||
|
||||
assert validated_conf.get('strategy') == 'CustomStrategy'
|
||||
assert validated_conf.get('strategy_path') == '/tmp/strategies'
|
||||
|
||||
|
||||
def test_show_info(default_conf, mocker, caplog) -> None:
|
||||
"""
|
||||
Test Configuration.show_info()
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--dynamic-whitelist', '10',
|
||||
'--strategy', 'TestStrategy',
|
||||
'--dry-run-db'
|
||||
]
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
configuration.get_config()
|
||||
|
||||
assert log_has(
|
||||
'Parameter --dynamic-whitelist detected. '
|
||||
'Using dynamically generated whitelist. '
|
||||
'(not applicable with Backtesting and Hyperopt)',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert log_has(
|
||||
'Parameter --dry-run-db detected ...',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert log_has(
|
||||
'Dry_run will use the DB file: "tradesv3.dry_run.sqlite"',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
# Test the Dry run condition
|
||||
configuration.config.update({'dry_run': False})
|
||||
configuration._load_common_config(configuration.config)
|
||||
assert log_has(
|
||||
'Dry run is disabled. (--dry_run_db ignored)',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'backtesting'
|
||||
]
|
||||
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
config = configuration.get_config()
|
||||
assert 'max_open_trades' in config
|
||||
assert 'stake_currency' in config
|
||||
assert 'stake_amount' in config
|
||||
assert 'exchange' in config
|
||||
assert 'pair_whitelist' in config['exchange']
|
||||
assert 'datadir' in config
|
||||
assert log_has(
|
||||
'Parameter --datadir detected: {} ...'.format(config['datadir']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert 'ticker_interval' in config
|
||||
assert not log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'live' not in config
|
||||
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'realistic_simulation' not in config
|
||||
assert not log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'refresh_pairs' not in config
|
||||
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'timerange' not in config
|
||||
assert 'export' not in config
|
||||
|
||||
|
||||
def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
'--datadir', '/foo/bar',
|
||||
'backtesting',
|
||||
'--ticker-interval', '1',
|
||||
'--live',
|
||||
'--realistic-simulation',
|
||||
'--refresh-pairs-cached',
|
||||
'--timerange', ':100',
|
||||
'--export', '/bar/foo'
|
||||
]
|
||||
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
config = configuration.get_config()
|
||||
assert 'max_open_trades' in config
|
||||
assert 'stake_currency' in config
|
||||
assert 'stake_amount' in config
|
||||
assert 'exchange' in config
|
||||
assert 'pair_whitelist' in config['exchange']
|
||||
assert 'datadir' in config
|
||||
assert log_has(
|
||||
'Parameter --datadir detected: {} ...'.format(config['datadir']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert 'ticker_interval' in config
|
||||
assert log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
||||
assert log_has(
|
||||
'Using ticker_interval: 1 ...',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert 'live' in config
|
||||
assert log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'realistic_simulation'in config
|
||||
assert log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
||||
assert log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
|
||||
|
||||
assert 'refresh_pairs'in config
|
||||
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
||||
assert 'timerange' in config
|
||||
assert log_has(
|
||||
'Parameter --timerange detected: {} ...'.format(config['timerange']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert 'export' in config
|
||||
assert log_has(
|
||||
'Parameter --export detected: {} ...'.format(config['export']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
def test_hyperopt_with_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'hyperopt',
|
||||
'--epochs', '10',
|
||||
'--use-mongodb',
|
||||
'--spaces', 'all',
|
||||
]
|
||||
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
config = configuration.get_config()
|
||||
|
||||
assert 'epochs' in config
|
||||
assert int(config['epochs']) == 10
|
||||
assert log_has('Parameter --epochs detected ...', caplog.record_tuples)
|
||||
assert log_has('Will run Hyperopt with for 10 epochs ...', caplog.record_tuples)
|
||||
|
||||
assert 'mongodb' in config
|
||||
assert config['mongodb'] is True
|
||||
assert log_has('Parameter --use-mongodb detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'spaces' in config
|
||||
assert config['spaces'] == ['all']
|
||||
assert log_has('Parameter -s/--spaces detected: [\'all\']', caplog.record_tuples)
|
||||
25
freqtrade/tests/test_constants.py
Normal file
25
freqtrade/tests/test_constants.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
Unit test file for constants.py
|
||||
"""
|
||||
|
||||
from freqtrade import constants
|
||||
|
||||
|
||||
def test_constant_object() -> None:
|
||||
"""
|
||||
Test the Constants object has the mandatory Constants
|
||||
"""
|
||||
assert hasattr(constants, 'CONF_SCHEMA')
|
||||
assert hasattr(constants, 'DYNAMIC_WHITELIST')
|
||||
assert hasattr(constants, 'PROCESS_THROTTLE_SECS')
|
||||
assert hasattr(constants, 'TICKER_INTERVAL')
|
||||
assert hasattr(constants, 'HYPEROPT_EPOCH')
|
||||
assert hasattr(constants, 'RETRY_TIMEOUT')
|
||||
assert hasattr(constants, 'DEFAULT_STRATEGY')
|
||||
|
||||
|
||||
def test_conf_schema() -> None:
|
||||
"""
|
||||
Test the CONF_SCHEMA is from the right type
|
||||
"""
|
||||
assert isinstance(constants.CONF_SCHEMA, dict)
|
||||
34
freqtrade/tests/test_dataframe.py
Normal file
34
freqtrade/tests/test_dataframe.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# pragma pylint: disable=missing-docstring, C0103
|
||||
|
||||
import pandas
|
||||
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.optimize import load_data
|
||||
from freqtrade.strategy.resolver import StrategyResolver
|
||||
|
||||
_pairs = ['BTC_ETH']
|
||||
|
||||
|
||||
def load_dataframe_pair(pairs):
|
||||
ld = load_data(None, ticker_interval=5, pairs=pairs)
|
||||
assert isinstance(ld, dict)
|
||||
assert isinstance(pairs[0], str)
|
||||
dataframe = ld[pairs[0]]
|
||||
|
||||
analyze = Analyze({'strategy': 'DefaultStrategy'})
|
||||
dataframe = analyze.analyze_ticker(dataframe)
|
||||
return dataframe
|
||||
|
||||
|
||||
def test_dataframe_load():
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
dataframe = load_dataframe_pair(_pairs)
|
||||
assert isinstance(dataframe, pandas.core.frame.DataFrame)
|
||||
|
||||
|
||||
def test_dataframe_columns_exists():
|
||||
StrategyResolver({'strategy': 'DefaultStrategy'})
|
||||
dataframe = load_dataframe_pair(_pairs)
|
||||
assert 'high' in dataframe.columns
|
||||
assert 'low' in dataframe.columns
|
||||
assert 'close' in dataframe.columns
|
||||
135
freqtrade/tests/test_fiat_convert.py
Normal file
135
freqtrade/tests/test_fiat_convert.py
Normal file
@@ -0,0 +1,135 @@
|
||||
# pragma pylint: disable=missing-docstring, too-many-arguments, too-many-ancestors,
|
||||
# pragma pylint: disable=protected-access, C0103
|
||||
|
||||
import time
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from freqtrade.fiat_convert import CryptoFiat, CryptoToFiatConverter
|
||||
|
||||
|
||||
def test_pair_convertion_object():
|
||||
pair_convertion = CryptoFiat(
|
||||
crypto_symbol='btc',
|
||||
fiat_symbol='usd',
|
||||
price=12345.0
|
||||
)
|
||||
|
||||
# Check the cache duration is 6 hours
|
||||
assert pair_convertion.CACHE_DURATION == 6 * 60 * 60
|
||||
|
||||
# Check a regular usage
|
||||
assert pair_convertion.crypto_symbol == 'BTC'
|
||||
assert pair_convertion.fiat_symbol == 'USD'
|
||||
assert pair_convertion.price == 12345.0
|
||||
assert pair_convertion.is_expired() is False
|
||||
|
||||
# Update the expiration time (- 2 hours) and check the behavior
|
||||
pair_convertion._expiration = time.time() - 2 * 60 * 60
|
||||
assert pair_convertion.is_expired() is True
|
||||
|
||||
# Check set price behaviour
|
||||
time_reference = time.time() + pair_convertion.CACHE_DURATION
|
||||
pair_convertion.set_price(price=30000.123)
|
||||
assert pair_convertion.is_expired() is False
|
||||
assert pair_convertion._expiration >= time_reference
|
||||
assert pair_convertion.price == 30000.123
|
||||
|
||||
|
||||
def test_fiat_convert_is_supported():
|
||||
fiat_convert = CryptoToFiatConverter()
|
||||
assert fiat_convert._is_supported_fiat(fiat='USD') is True
|
||||
assert fiat_convert._is_supported_fiat(fiat='usd') is True
|
||||
assert fiat_convert._is_supported_fiat(fiat='abc') is False
|
||||
assert fiat_convert._is_supported_fiat(fiat='ABC') is False
|
||||
|
||||
|
||||
def test_fiat_convert_add_pair():
|
||||
fiat_convert = CryptoToFiatConverter()
|
||||
|
||||
pair_len = len(fiat_convert._pairs)
|
||||
assert pair_len == 0
|
||||
|
||||
fiat_convert._add_pair(crypto_symbol='btc', fiat_symbol='usd', price=12345.0)
|
||||
pair_len = len(fiat_convert._pairs)
|
||||
assert pair_len == 1
|
||||
assert fiat_convert._pairs[0].crypto_symbol == 'BTC'
|
||||
assert fiat_convert._pairs[0].fiat_symbol == 'USD'
|
||||
assert fiat_convert._pairs[0].price == 12345.0
|
||||
|
||||
fiat_convert._add_pair(crypto_symbol='btc', fiat_symbol='Eur', price=13000.2)
|
||||
pair_len = len(fiat_convert._pairs)
|
||||
assert pair_len == 2
|
||||
assert fiat_convert._pairs[1].crypto_symbol == 'BTC'
|
||||
assert fiat_convert._pairs[1].fiat_symbol == 'EUR'
|
||||
assert fiat_convert._pairs[1].price == 13000.2
|
||||
|
||||
|
||||
def test_fiat_convert_find_price(mocker):
|
||||
api_mock = MagicMock(return_value={
|
||||
'price_usd': 12345.0,
|
||||
'price_eur': 13000.2
|
||||
})
|
||||
mocker.patch('freqtrade.fiat_convert.Market.ticker', api_mock)
|
||||
fiat_convert = CryptoToFiatConverter()
|
||||
|
||||
with pytest.raises(ValueError, match=r'The fiat ABC is not supported.'):
|
||||
fiat_convert._find_price(crypto_symbol='BTC', fiat_symbol='ABC')
|
||||
|
||||
with pytest.raises(ValueError, match=r'The crypto symbol XRP is not supported.'):
|
||||
fiat_convert.get_price(crypto_symbol='XRP', fiat_symbol='USD')
|
||||
|
||||
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=12345.0)
|
||||
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 12345.0
|
||||
assert fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='usd') == 12345.0
|
||||
|
||||
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=13000.2)
|
||||
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='EUR') == 13000.2
|
||||
|
||||
|
||||
def test_fiat_convert_get_price(mocker):
|
||||
api_mock = MagicMock(return_value={
|
||||
'price_usd': 28000.0,
|
||||
'price_eur': 15000.0
|
||||
})
|
||||
mocker.patch('freqtrade.fiat_convert.Market.ticker', api_mock)
|
||||
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=28000.0)
|
||||
|
||||
fiat_convert = CryptoToFiatConverter()
|
||||
|
||||
with pytest.raises(ValueError, match=r'The fiat US DOLLAR is not supported.'):
|
||||
fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='US Dollar')
|
||||
|
||||
# Check the value return by the method
|
||||
pair_len = len(fiat_convert._pairs)
|
||||
assert pair_len == 0
|
||||
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 28000.0
|
||||
assert fiat_convert._pairs[0].crypto_symbol == 'BTC'
|
||||
assert fiat_convert._pairs[0].fiat_symbol == 'USD'
|
||||
assert fiat_convert._pairs[0].price == 28000.0
|
||||
assert fiat_convert._pairs[0]._expiration is not 0
|
||||
assert len(fiat_convert._pairs) == 1
|
||||
|
||||
# Verify the cached is used
|
||||
fiat_convert._pairs[0].price = 9867.543
|
||||
expiration = fiat_convert._pairs[0]._expiration
|
||||
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 9867.543
|
||||
assert fiat_convert._pairs[0]._expiration == expiration
|
||||
|
||||
# Verify the cache expiration
|
||||
expiration = time.time() - 2 * 60 * 60
|
||||
fiat_convert._pairs[0]._expiration = expiration
|
||||
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 28000.0
|
||||
assert fiat_convert._pairs[0]._expiration is not expiration
|
||||
|
||||
|
||||
def test_fiat_convert_without_network():
|
||||
# Because CryptoToFiatConverter is a Singleton we reset the value of _coinmarketcap
|
||||
|
||||
fiat_convert = CryptoToFiatConverter()
|
||||
|
||||
CryptoToFiatConverter._coinmarketcap = None
|
||||
|
||||
assert fiat_convert._coinmarketcap is None
|
||||
assert fiat_convert._find_price(crypto_symbol='BTC', fiat_symbol='USD') == 0.0
|
||||
1276
freqtrade/tests/test_freqtradebot.py
Normal file
1276
freqtrade/tests/test_freqtradebot.py
Normal file
File diff suppressed because it is too large
Load Diff
13
freqtrade/tests/test_indicator_helpers.py
Normal file
13
freqtrade/tests/test_indicator_helpers.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import pandas as pd
|
||||
|
||||
from freqtrade.indicator_helpers import went_up, went_down
|
||||
|
||||
|
||||
def test_went_up():
|
||||
series = pd.Series([1, 2, 3, 1])
|
||||
assert went_up(series).equals(pd.Series([False, True, True, False]))
|
||||
|
||||
|
||||
def test_went_down():
|
||||
series = pd.Series([1, 2, 3, 1])
|
||||
assert went_down(series).equals(pd.Series([False, False, False, True]))
|
||||
93
freqtrade/tests/test_main.py
Normal file
93
freqtrade/tests/test_main.py
Normal file
@@ -0,0 +1,93 @@
|
||||
"""
|
||||
Unit test file for main.py
|
||||
"""
|
||||
|
||||
import logging
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from freqtrade.main import main, set_loggers
|
||||
from freqtrade.tests.conftest import log_has
|
||||
|
||||
|
||||
def test_parse_args_backtesting(mocker) -> None:
|
||||
"""
|
||||
Test that main() can start backtesting and also ensure we can pass some specific arguments
|
||||
further argument parsing is done in test_arguments.py
|
||||
"""
|
||||
backtesting_mock = mocker.patch('freqtrade.optimize.backtesting.start', MagicMock())
|
||||
main(['backtesting'])
|
||||
assert backtesting_mock.call_count == 1
|
||||
call_args = backtesting_mock.call_args[0][0]
|
||||
assert call_args.config == 'config.json'
|
||||
assert call_args.live is False
|
||||
assert call_args.loglevel == 20
|
||||
assert call_args.subparser == 'backtesting'
|
||||
assert call_args.func is not None
|
||||
assert call_args.ticker_interval is None
|
||||
|
||||
|
||||
def test_main_start_hyperopt(mocker) -> None:
|
||||
"""
|
||||
Test that main() can start hyperopt
|
||||
"""
|
||||
hyperopt_mock = mocker.patch('freqtrade.optimize.hyperopt.start', MagicMock())
|
||||
main(['hyperopt'])
|
||||
assert hyperopt_mock.call_count == 1
|
||||
call_args = hyperopt_mock.call_args[0][0]
|
||||
assert call_args.config == 'config.json'
|
||||
assert call_args.loglevel == 20
|
||||
assert call_args.subparser == 'hyperopt'
|
||||
assert call_args.func is not None
|
||||
|
||||
|
||||
def test_set_loggers() -> None:
|
||||
"""
|
||||
Test set_loggers() update the logger level for third-party libraries
|
||||
"""
|
||||
previous_value1 = logging.getLogger('requests.packages.urllib3').level
|
||||
previous_value2 = logging.getLogger('telegram').level
|
||||
|
||||
set_loggers()
|
||||
|
||||
value1 = logging.getLogger('requests.packages.urllib3').level
|
||||
assert previous_value1 is not value1
|
||||
assert value1 is logging.INFO
|
||||
|
||||
value2 = logging.getLogger('telegram').level
|
||||
assert previous_value2 is not value2
|
||||
assert value2 is logging.INFO
|
||||
|
||||
|
||||
def test_main(mocker, caplog) -> None:
|
||||
"""
|
||||
Test main() function
|
||||
In this test we are skipping the while True loop by throwing an exception.
|
||||
"""
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.freqtradebot.FreqtradeBot',
|
||||
_init_modules=MagicMock(),
|
||||
worker=MagicMock(
|
||||
side_effect=KeyboardInterrupt
|
||||
),
|
||||
clean=MagicMock(),
|
||||
)
|
||||
args = ['-c', 'config.json.example']
|
||||
|
||||
# Test Main + the KeyboardInterrupt exception
|
||||
with pytest.raises(SystemExit) as pytest_wrapped_e:
|
||||
main(args)
|
||||
log_has('Starting freqtrade', caplog.record_tuples)
|
||||
log_has('Got SIGINT, aborting ...', caplog.record_tuples)
|
||||
assert pytest_wrapped_e.type == SystemExit
|
||||
assert pytest_wrapped_e.value.code == 42
|
||||
|
||||
# Test the BaseException case
|
||||
mocker.patch(
|
||||
'freqtrade.freqtradebot.FreqtradeBot.worker',
|
||||
MagicMock(side_effect=BaseException)
|
||||
)
|
||||
with pytest.raises(SystemExit):
|
||||
main(args)
|
||||
log_has('Got fatal exception!', caplog.record_tuples)
|
||||
71
freqtrade/tests/test_misc.py
Normal file
71
freqtrade/tests/test_misc.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# pragma pylint: disable=missing-docstring,C0103
|
||||
|
||||
"""
|
||||
Unit test file for misc.py
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.misc import (shorten_date, datesarray_to_datetimearray,
|
||||
common_datearray, file_dump_json)
|
||||
from freqtrade.optimize.__init__ import load_tickerdata_file
|
||||
|
||||
|
||||
def test_shorten_date() -> None:
|
||||
"""
|
||||
Test shorten_date() function
|
||||
:return: None
|
||||
"""
|
||||
str_data = '1 day, 2 hours, 3 minutes, 4 seconds ago'
|
||||
str_shorten_data = '1 d, 2 h, 3 min, 4 sec ago'
|
||||
assert shorten_date(str_data) == str_shorten_data
|
||||
|
||||
|
||||
def test_datesarray_to_datetimearray(ticker_history):
|
||||
"""
|
||||
Test datesarray_to_datetimearray() function
|
||||
:return: None
|
||||
"""
|
||||
dataframes = Analyze.parse_ticker_dataframe(ticker_history)
|
||||
dates = datesarray_to_datetimearray(dataframes['date'])
|
||||
|
||||
assert isinstance(dates[0], datetime.datetime)
|
||||
assert dates[0].year == 2017
|
||||
assert dates[0].month == 11
|
||||
assert dates[0].day == 26
|
||||
assert dates[0].hour == 8
|
||||
assert dates[0].minute == 50
|
||||
|
||||
date_len = len(dates)
|
||||
assert date_len == 3
|
||||
|
||||
|
||||
def test_common_datearray(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test common_datearray()
|
||||
:return: None
|
||||
"""
|
||||
analyze = Analyze(default_conf)
|
||||
tick = load_tickerdata_file(None, 'BTC_UNITEST', 1)
|
||||
tickerlist = {'BTC_UNITEST': tick}
|
||||
dataframes = analyze.tickerdata_to_dataframe(tickerlist)
|
||||
|
||||
dates = common_datearray(dataframes)
|
||||
|
||||
assert dates.size == dataframes['BTC_UNITEST']['date'].size
|
||||
assert dates[0] == dataframes['BTC_UNITEST']['date'][0]
|
||||
assert dates[-1] == dataframes['BTC_UNITEST']['date'][-1]
|
||||
|
||||
|
||||
def test_file_dump_json(mocker) -> None:
|
||||
"""
|
||||
Test file_dump_json()
|
||||
:return: None
|
||||
"""
|
||||
file_open = mocker.patch('freqtrade.misc.open', MagicMock())
|
||||
json_dump = mocker.patch('json.dump', MagicMock())
|
||||
file_dump_json('somefile', [1, 2, 3])
|
||||
assert file_open.call_count == 1
|
||||
assert json_dump.call_count == 1
|
||||
366
freqtrade/tests/test_persistence.py
Normal file
366
freqtrade/tests/test_persistence.py
Normal file
@@ -0,0 +1,366 @@
|
||||
# pragma pylint: disable=missing-docstring, C0103
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from freqtrade.exchange import Exchanges
|
||||
from freqtrade.persistence import Trade, init, clean_dry_run_db
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def init_persistence(default_conf):
|
||||
init(default_conf)
|
||||
|
||||
|
||||
def test_init_create_session(default_conf, mocker):
|
||||
mocker.patch.dict('freqtrade.persistence._CONF', default_conf)
|
||||
|
||||
# Check if init create a session
|
||||
init(default_conf)
|
||||
assert hasattr(Trade, 'session')
|
||||
assert 'Session' in type(Trade.session).__name__
|
||||
|
||||
|
||||
def test_init_dry_run_db(default_conf, mocker):
|
||||
default_conf.update({'dry_run_db': True})
|
||||
mocker.patch.dict('freqtrade.persistence._CONF', default_conf)
|
||||
|
||||
# First, protect the existing 'tradesv3.dry_run.sqlite' (Do not delete user data)
|
||||
dry_run_db = 'tradesv3.dry_run.sqlite'
|
||||
dry_run_db_swp = dry_run_db + '.swp'
|
||||
|
||||
if os.path.isfile(dry_run_db):
|
||||
os.rename(dry_run_db, dry_run_db_swp)
|
||||
|
||||
# Check if the new tradesv3.dry_run.sqlite was created
|
||||
init(default_conf)
|
||||
assert os.path.isfile(dry_run_db) is True
|
||||
|
||||
# Delete the file made for this unitest and rollback to the previous
|
||||
# tradesv3.dry_run.sqlite file
|
||||
|
||||
# 1. Delete file from the test
|
||||
if os.path.isfile(dry_run_db):
|
||||
os.remove(dry_run_db)
|
||||
|
||||
# 2. Rollback to the initial file
|
||||
if os.path.isfile(dry_run_db_swp):
|
||||
os.rename(dry_run_db_swp, dry_run_db)
|
||||
|
||||
|
||||
def test_init_dry_run_without_db(default_conf, mocker):
|
||||
default_conf.update({'dry_run_db': False})
|
||||
mocker.patch.dict('freqtrade.persistence._CONF', default_conf)
|
||||
|
||||
# First, protect the existing 'tradesv3.dry_run.sqlite' (Do not delete user data)
|
||||
dry_run_db = 'tradesv3.dry_run.sqlite'
|
||||
dry_run_db_swp = dry_run_db + '.swp'
|
||||
|
||||
if os.path.isfile(dry_run_db):
|
||||
os.rename(dry_run_db, dry_run_db_swp)
|
||||
|
||||
# Check if the new tradesv3.dry_run.sqlite was created
|
||||
init(default_conf)
|
||||
assert os.path.isfile(dry_run_db) is False
|
||||
|
||||
# Rollback to the initial 'tradesv3.dry_run.sqlite' file
|
||||
if os.path.isfile(dry_run_db_swp):
|
||||
os.rename(dry_run_db_swp, dry_run_db)
|
||||
|
||||
|
||||
def test_init_prod_db(default_conf, mocker):
|
||||
default_conf.update({'dry_run': False})
|
||||
mocker.patch.dict('freqtrade.persistence._CONF', default_conf)
|
||||
|
||||
# First, protect the existing 'tradesv3.sqlite' (Do not delete user data)
|
||||
prod_db = 'tradesv3.sqlite'
|
||||
prod_db_swp = prod_db + '.swp'
|
||||
|
||||
if os.path.isfile(prod_db):
|
||||
os.rename(prod_db, prod_db_swp)
|
||||
|
||||
# Check if the new tradesv3.sqlite was created
|
||||
init(default_conf)
|
||||
assert os.path.isfile(prod_db) is True
|
||||
|
||||
# Delete the file made for this unitest and rollback to the previous tradesv3.sqlite file
|
||||
|
||||
# 1. Delete file from the test
|
||||
if os.path.isfile(prod_db):
|
||||
os.remove(prod_db)
|
||||
|
||||
# Rollback to the initial 'tradesv3.sqlite' file
|
||||
if os.path.isfile(prod_db_swp):
|
||||
os.rename(prod_db_swp, prod_db)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_update_with_bittrex(limit_buy_order, limit_sell_order):
|
||||
"""
|
||||
On this test we will buy and sell a crypto currency.
|
||||
|
||||
Buy
|
||||
- Buy: 90.99181073 Crypto at 0.00001099 BTC
|
||||
(90.99181073*0.00001099 = 0.0009999 BTC)
|
||||
- Buying fee: 0.25%
|
||||
- Total cost of buy trade: 0.001002500 BTC
|
||||
((90.99181073*0.00001099) + ((90.99181073*0.00001099)*0.0025))
|
||||
|
||||
Sell
|
||||
- Sell: 90.99181073 Crypto at 0.00001173 BTC
|
||||
(90.99181073*0.00001173 = 0,00106733394 BTC)
|
||||
- Selling fee: 0.25%
|
||||
- Total cost of sell trade: 0.001064666 BTC
|
||||
((90.99181073*0.00001173) - ((90.99181073*0.00001173)*0.0025))
|
||||
|
||||
Profit/Loss: +0.000062166 BTC
|
||||
(Sell:0.001064666 - Buy:0.001002500)
|
||||
Profit/Loss percentage: 0.0620
|
||||
((0.001064666/0.001002500)-1 = 6.20%)
|
||||
|
||||
:param limit_buy_order:
|
||||
:param limit_sell_order:
|
||||
:return:
|
||||
"""
|
||||
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
assert trade.open_order_id is None
|
||||
assert trade.open_rate is None
|
||||
assert trade.close_profit is None
|
||||
assert trade.close_date is None
|
||||
|
||||
trade.open_order_id = 'something'
|
||||
trade.update(limit_buy_order)
|
||||
assert trade.open_order_id is None
|
||||
assert trade.open_rate == 0.00001099
|
||||
assert trade.close_profit is None
|
||||
assert trade.close_date is None
|
||||
|
||||
trade.open_order_id = 'something'
|
||||
trade.update(limit_sell_order)
|
||||
assert trade.open_order_id is None
|
||||
assert trade.close_rate == 0.00001173
|
||||
assert trade.close_profit == 0.06201057
|
||||
assert trade.close_date is not None
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
|
||||
trade.open_order_id = 'something'
|
||||
trade.update(limit_buy_order)
|
||||
assert trade.calc_open_trade_price() == 0.001002500
|
||||
|
||||
trade.update(limit_sell_order)
|
||||
assert trade.calc_close_trade_price() == 0.0010646656
|
||||
|
||||
# Profit in BTC
|
||||
assert trade.calc_profit() == 0.00006217
|
||||
|
||||
# Profit in percent
|
||||
assert trade.calc_profit_percent() == 0.06201057
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_calc_close_trade_price_exception(limit_buy_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
|
||||
trade.open_order_id = 'something'
|
||||
trade.update(limit_buy_order)
|
||||
assert trade.calc_close_trade_price() == 0.0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_update_open_order(limit_buy_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=1.00,
|
||||
fee=0.1,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
|
||||
assert trade.open_order_id is None
|
||||
assert trade.open_rate is None
|
||||
assert trade.close_profit is None
|
||||
assert trade.close_date is None
|
||||
|
||||
limit_buy_order['closed'] = False
|
||||
trade.update(limit_buy_order)
|
||||
|
||||
assert trade.open_order_id is None
|
||||
assert trade.open_rate is None
|
||||
assert trade.close_profit is None
|
||||
assert trade.close_date is None
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_update_invalid_order(limit_buy_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=1.00,
|
||||
fee=0.1,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
limit_buy_order['type'] = 'invalid'
|
||||
with pytest.raises(ValueError, match=r'Unknown order type'):
|
||||
trade.update(limit_buy_order)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_calc_open_trade_price(limit_buy_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
trade.open_order_id = 'open_trade'
|
||||
trade.update(limit_buy_order) # Buy @ 0.00001099
|
||||
|
||||
# Get the open rate price with the standard fee rate
|
||||
assert trade.calc_open_trade_price() == 0.001002500
|
||||
|
||||
# Get the open rate price with a custom fee rate
|
||||
assert trade.calc_open_trade_price(fee=0.003) == 0.001003000
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_calc_close_trade_price(limit_buy_order, limit_sell_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
trade.open_order_id = 'close_trade'
|
||||
trade.update(limit_buy_order) # Buy @ 0.00001099
|
||||
|
||||
# Get the close rate price with a custom close rate and a regular fee rate
|
||||
assert trade.calc_close_trade_price(rate=0.00001234) == 0.0011200318
|
||||
|
||||
# Get the close rate price with a custom close rate and a custom fee rate
|
||||
assert trade.calc_close_trade_price(rate=0.00001234, fee=0.003) == 0.0011194704
|
||||
|
||||
# Test when we apply a Sell order, and ask price with a custom fee rate
|
||||
trade.update(limit_sell_order)
|
||||
assert trade.calc_close_trade_price(fee=0.005) == 0.0010619972
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_calc_profit(limit_buy_order, limit_sell_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
trade.open_order_id = 'profit_percent'
|
||||
trade.update(limit_buy_order) # Buy @ 0.00001099
|
||||
|
||||
# Custom closing rate and regular fee rate
|
||||
# Higher than open rate
|
||||
assert trade.calc_profit(rate=0.00001234) == 0.00011753
|
||||
# Lower than open rate
|
||||
assert trade.calc_profit(rate=0.00000123) == -0.00089086
|
||||
|
||||
# Custom closing rate and custom fee rate
|
||||
# Higher than open rate
|
||||
assert trade.calc_profit(rate=0.00001234, fee=0.003) == 0.00011697
|
||||
# Lower than open rate
|
||||
assert trade.calc_profit(rate=0.00000123, fee=0.003) == -0.00089092
|
||||
|
||||
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
|
||||
trade.update(limit_sell_order)
|
||||
assert trade.calc_profit() == 0.00006217
|
||||
|
||||
# Test with a custom fee rate on the close trade
|
||||
assert trade.calc_profit(fee=0.003) == 0.00006163
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_calc_profit_percent(limit_buy_order, limit_sell_order):
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
fee=0.0025,
|
||||
exchange=Exchanges.BITTREX,
|
||||
)
|
||||
trade.open_order_id = 'profit_percent'
|
||||
trade.update(limit_buy_order) # Buy @ 0.00001099
|
||||
|
||||
# Get percent of profit with a custom rate (Higher than open rate)
|
||||
assert trade.calc_profit_percent(rate=0.00001234) == 0.1172387
|
||||
|
||||
# Get percent of profit with a custom rate (Lower than open rate)
|
||||
assert trade.calc_profit_percent(rate=0.00000123) == -0.88863827
|
||||
|
||||
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
|
||||
trade.update(limit_sell_order)
|
||||
assert trade.calc_profit_percent() == 0.06201057
|
||||
|
||||
# Test with a custom fee rate on the close trade
|
||||
assert trade.calc_profit_percent(fee=0.003) == 0.0614782
|
||||
|
||||
|
||||
def test_clean_dry_run_db(default_conf):
|
||||
init(default_conf, create_engine('sqlite://'))
|
||||
|
||||
# Simulate dry_run entries
|
||||
trade = Trade(
|
||||
pair='BTC_ETH',
|
||||
stake_amount=0.001,
|
||||
amount=123.0,
|
||||
fee=0.0025,
|
||||
open_rate=0.123,
|
||||
exchange='BITTREX',
|
||||
open_order_id='dry_run_buy_12345'
|
||||
)
|
||||
Trade.session.add(trade)
|
||||
|
||||
trade = Trade(
|
||||
pair='BTC_ETC',
|
||||
stake_amount=0.001,
|
||||
amount=123.0,
|
||||
fee=0.0025,
|
||||
open_rate=0.123,
|
||||
exchange='BITTREX',
|
||||
open_order_id='dry_run_sell_12345'
|
||||
)
|
||||
Trade.session.add(trade)
|
||||
|
||||
# Simulate prod entry
|
||||
trade = Trade(
|
||||
pair='BTC_ETC',
|
||||
stake_amount=0.001,
|
||||
amount=123.0,
|
||||
fee=0.0025,
|
||||
open_rate=0.123,
|
||||
exchange='BITTREX',
|
||||
open_order_id='prod_buy_12345'
|
||||
)
|
||||
Trade.session.add(trade)
|
||||
|
||||
# We have 3 entries: 2 dry_run, 1 prod
|
||||
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 3
|
||||
|
||||
clean_dry_run_db()
|
||||
|
||||
# We have now only the prod
|
||||
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 1
|
||||
14
freqtrade/tests/test_state.py
Normal file
14
freqtrade/tests/test_state.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""
|
||||
Unit test file for constants.py
|
||||
"""
|
||||
|
||||
from freqtrade.state import State
|
||||
|
||||
|
||||
def test_state_object() -> None:
|
||||
"""
|
||||
Test the State object has the mandatory states
|
||||
:return: None
|
||||
"""
|
||||
assert hasattr(State, 'RUNNING')
|
||||
assert hasattr(State, 'STOPPED')
|
||||
1
freqtrade/tests/testdata/BTC_ADA-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_ADA-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_ADA-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_ADA-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_DASH-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_DASH-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_DASH-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_DASH-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_ETC-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_ETC-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_ETC-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_ETC-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_ETH-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_ETH-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_ETH-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_ETH-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_LTC-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_LTC-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_LTC-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_LTC-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_NXT-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_NXT-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_NXT-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_NXT-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_POWR-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_POWR-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_POWR-5.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_POWR-5.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_UNITEST-1.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_UNITEST-1.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
freqtrade/tests/testdata/BTC_UNITEST-30.json
vendored
Normal file
1
freqtrade/tests/testdata/BTC_UNITEST-30.json
vendored
Normal file
File diff suppressed because one or more lines are too long
3
freqtrade/tests/testdata/BTC_UNITEST-8.json
vendored
Normal file
3
freqtrade/tests/testdata/BTC_UNITEST-8.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[
|
||||
{"O": 0.00162008, "H": 0.00162008, "L": 0.00162008, "C": 0.00162008, "V": 108.14853839, "T": "2017-11-04T23:02:00", "BV": 0.17520927}
|
||||
]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user