-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrevenue.php
150 lines (121 loc) · 4.12 KB
/
revenue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* @author Ebo Eppenga
* @copyright 2022
*
* GoldStar Buy and Sell bot based on signals from for example TradeView
* or any other platform using PHP Bybit API from zhouaini528.
*
* revenue.php
* Calculates revenue of trades and outputs a CSV file
**/
// Set error reporting
error_reporting(E_ALL & ~E_NOTICE);
// Set log file
$logfile = "data/log_revenue.csv";
// Load the latest revenue from log files
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
$url = $protocol . $host . $uri;
// Get the path and remove the filename
$parts = parse_url($url);
$path = $parts['path'];
$lastSlashPos = strrpos($path, '/');
$basePath = substr($path, 0, $lastSlashPos);
// Construct the base URL with folders
$baseUrlWithFolders = $parts['scheme'] . '://' . $parts['host'] . $basePath . '/';
// Actually load the latest revenue
$content = file_get_contents($baseUrlWithFolders . "log_combine.php?files=revenue");
// Loop through revenue file for the first time and make a list of BUY orders
$handle = fopen($logfile, "r");
while (($line = fgetcsv($handle)) !== false) {
// Get some basic variables
$orderId = $line[2];
$side = $line[5];
$orderStatus = $line[7];
// Only create a list with BUY orders
if (($side == "Buy") && (strpos($orderStatus, "Filled") !== false)) {
if (empty($revenue[$orderId])) {
$revenue[$orderId] = [
'createdTime' => $line[0],
'updatedTime' => $line[1],
'orderId' => $line[2],
'orderLinkId' => $line[3],
'symbol' => $line[4],
'side' => $line[5],
'orderType' => $line[6],
'orderStatus' => $line[7],
'revStatus' => 'Open',
'qty' => $line[9]
];
} else {
// Sum up qty
$revenue[$orderId]['qty'] = $revenue[$orderId]['qty'] + $line[9];
// Determine oldest createTime
if ($line[0] < $revenue[$orderId]['createdTime']) {
$revenue[$orderId]['createdTime'] = $line[0];
}
// Determine newest updatedTime
if ($line[1] > $revenue[$orderId]['updatedTime']) {
$revenue[$orderId]['updatedTime'] = $line[1];
}
}
}
}
fclose($handle);
//print_r($revenue);
//exit();
// Loop through revenue file for the second time and determine if orders are closed
$handle = fopen($logfile, "r");
while (($line = fgetcsv($handle)) !== false) {
// Get some basic variables
$orderId = $line[2];
$orderLinkId = $line[3];
$side = $line[5];
$orderStatus = $line[7];
// Match the list with SELL orders
if (($side == "Sell") && (strpos($orderStatus, "Filled") !== false)) {
// Check if we can match a BUY order to a SELL order
if (!empty($revenue[$orderLinkId])) {
// Sum up qty
$revenue[$orderLinkId]['qty'] = $revenue[$orderLinkId]['qty'] + $line[9];
// Determine oldest createTime
if ($line[0] < $revenue[$orderLinkId]['createdTime']) {
$revenue[$orderLinkId]['createdTime'] = $line[0];
}
// Determine newest updatedTime
if ($line[1] > $revenue[$orderLinkId]['updatedTime']) {
$revenue[$orderLinkId]['updatedTime'] = $line[1];
}
// Set order to Closed
$revenue[$orderLinkId]['revStatus'] = 'Closed';
} else {
// Orders that only have a SELL without a BUY
$revenue[$orderId] = [
'createdTime' => $line[0],
'updatedTime' => $line[1],
'orderId' => $line[2],
'orderLinkId' => $line[3],
'symbol' => $line[4],
'side' => $line[5],
'orderType' => $line[6],
'orderStatus' => $line[7],
'revStatus' => 'Open',
'qty' => ($line[9] + $revenue[$orderId]['qty'])
];
}
}
}
//print_r($revenue);
//exit();
// Output the array as CSV file
foreach ($revenue as $orderId => $orderDetails) {
$line = "";
foreach ($orderDetails as $key => $value) {
$line .= $value . ",";
}
$line = substr($line, 0, -1) . "\n";
echo $line;
}
?>