-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
check_entry_exit.cpp
109 lines (91 loc) · 3.53 KB
/
check_entry_exit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <sstream>
#include <stdlib.h>
#include <iomanip>
#include "check_entry_exit.h"
std::string percToStr(double perc) {
std::ostringstream s;
if (perc < 0.0) {
s << std::fixed << std::setprecision(2) << perc * 100.0 << "%";
} else {
s << " " << std::fixed << std::setprecision(2) << perc * 100.0 << "%";
}
return s.str();
}
bool checkEntry(Bitcoin *btcLong, Bitcoin *btcShort, Result &res, Parameters params) {
double priceLong;
double priceShort;
int longId = btcLong->getId();
int shortId = btcShort->getId();
// compute limit (fees + spreadEntry)
double limit = 2 * btcLong->getFees() + 2 * btcShort->getFees() + params.spreadEntry;
// check if btcLong is below btcShort
if (btcShort->getHasShort()) {
// btcLong: buy looking to match ask
// btcShort: sell looking to match bid
priceLong = btcLong->getLastAsk();
priceShort = btcShort->getLastBid();
res.spreadIn = (priceShort - priceLong) / priceLong;
// maxSpread and minSpread
if (res.spreadIn > res.maxSpread[longId][shortId]) {
res.maxSpread[longId][shortId] = res.spreadIn;
}
if (res.spreadIn < res.minSpread[longId][shortId]) {
res.minSpread[longId][shortId] = res.spreadIn;
}
if (params.verbose) {
std::cout << " " << btcLong->getExchName() << "/" << btcShort->getExchName() << ":\t" << percToStr(res.spreadIn);
std::cout << " [target " << percToStr(limit) << ", min " << percToStr(res.minSpread[longId][shortId]) << ", max " << percToStr(res.maxSpread[longId][shortId]) << "]" << std::endl;
}
// TODO Kraken (id 3) is not ready to be traded on
if (res.spreadIn >= limit && priceLong > 0.0 && priceShort > 0.0 && longId != 3 && shortId != 3) {
// opportunity found
res.idExchLong = longId;
res.idExchShort = shortId;
res.feesLong = btcLong->getFees();
res.feesShort = btcShort->getFees();
res.exchNameLong = btcLong->getExchName();
res.exchNameShort = btcShort->getExchName();
res.priceLongIn = priceLong;
res.priceShortIn = priceShort;
return true;
}
}
// no opportunity
return false;
}
bool checkExit(Bitcoin *btcLong, Bitcoin *btcShort, Result &res, Parameters params, time_t period) {
double priceLong;
double priceShort;
int longId = btcLong->getId();
int shortId = btcShort->getId();
// close btcLong: sell looking to match bid
// close btcShort: buy looking to match ask
priceLong = btcLong->getLastBid();
priceShort = btcShort->getLastAsk();
res.spreadOut = (priceShort - priceLong) / priceLong;
// maxSpread and minSpread
if (res.spreadOut > res.maxSpread[longId][shortId]) {
res.maxSpread[longId][shortId] = res.spreadOut;
}
if (res.spreadOut < res.minSpread[longId][shortId]) {
res.minSpread[longId][shortId] = res.spreadOut;
}
if (params.verbose) {
std::cout << " " << btcLong->getExchName() << "/" << btcShort->getExchName() << ":\t" << percToStr(res.spreadOut);
std::cout << " [target " << percToStr(params.spreadExit) << ", min " << percToStr(res.minSpread[longId][shortId]) << ", max " << percToStr(res.maxSpread[longId][shortId]) << "]" << std::endl;
}
// check length
if (period - res.entryTime >= params.maxLength) {
res.priceLongOut = priceLong;
res.priceShortOut = priceShort;
return true;
}
if (res.spreadOut <= params.spreadExit && priceLong > 0.0 && priceShort > 0.0) {
// exit opportunity found
res.priceLongOut = priceLong;
res.priceShortOut = priceShort;
return true;
}
// no exit opportunity
return false;
}