This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
forked from KennyYou/Stock_API_Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmz_functions.php
248 lines (208 loc) · 8 KB
/
dmz_functions.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/php
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
//INITIATE API CLIENT
use AlphaVantage\Client;
$alpha_vantage = new Client('9J4N8FA67HVHYZG0');
//This is a rough edited version of testRabbitMQServer.php only including the functions done at the DMZ. Extra comments are included for context
//FUNCTION LIST
//Doing RSS from google news source
function RSS() {
$xml=("https://news.google.com/rss/search?q=stocks&hl=en-US&gl=US&ceid=US:en");
return $xml;
}
//END RSS FEATURE
function doSearch($search) {
$ch = curl_init("https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=" . $search . "&apikey=9J4N8FA67HVHYZG0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_results = curl_exec($ch);
curl_close($ch);
$jsonarray = json_decode($curl_results, true);
foreach($jsonarray['bestMatches'] as $variable) {
$symbol = $variable['1. symbol'];
$name = $variable['2. name'];
$output .= "<td>".$symbol." </td>";
$output .= "<td>".$name." </td>";
$output .= "</tr>";
}
return $output;
//send output back to backend
}
function doDetailSearch($search) {
$ch = curl_init("https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=" . $search . "&apikey=9J4N8FA67HVHYZG0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_results = curl_exec($ch);
curl_close($ch);
$jsonarray = json_decode($curl_results, true);
$output .= "<th>Symbol</th>";
$output .= "<th>Name</th>";
$output .= "<th>Open</th>";
$output .= "<th>High</th>";
$output .= "<th>Low</th>";
$output .= "<th>Close</th>";
$output .= "<th>Volume</th>";
$output .= "</tr>";
$symbol = $jsonarray['bestMatches'][0]['1. symbol'];
$name = $jsonarray['bestMatches'][0]['2. name'];
//Curl for each symbol and get open, high, low, and close.
$ch2 = curl_init("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=".$symbol."&interval=1min&apikey=9J4N8FA67HVHYZG0");
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
$curl_results2 = curl_exec($ch2);
curl_close($ch2);
$jsonarray2 = json_decode($curl_results2, true);
$time = $jsonarray2['Meta Data']['3. Last Refreshed'];
$open = $jsonarray2['Time Series (1min)'][$time]['1. open'];
$high = $jsonarray2['Time Series (1min)'][$time]['2. high'];
$low = $jsonarray2['Time Series (1min)'][$time]['3. low'];
$close = $jsonarray2['Time Series (1min)'][$time]['4. close'];
$volume = $jsonarray2['Time Series (1min)'][$time]['5. volume'];
$output .= "<td>".$symbol." </td>";
$output .= "<td>".$name." </td>";
$output .= "<td>".$open." </td>";
$output .= "<td>".$high." </td>";
$output .= "<td>".$low." </td>";
$output .= "<td>".$close." </td>";
$output .= "<td>".$volume." </td>";
$output .= "</tr>";
return $output;
//send output back to backend
}
function doPortfolioCron($stockArray) {
//$fullPriceArray = array();
foreach ($stockArray as $stock) {
echo $stock;
echo "\n";
$ch = curl_init("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" .$stock. "&apikey=9J4N8FA67HVHYZG0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_results = curl_exec($ch);
curl_close($ch);
$today = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime("-1 days"));
echo $today;
echo "\n";
echo $yesterday;
$jsonarray = json_decode($curl_results, true);
$os_unround = floatval($jsonarray['Time Series (Daily)'][$yesterday]['4. close']);
$cs_unround = floatval($jsonarray['Time Series (Daily)'][$today]['4. close']);
$old_stock = round($os_unround,2);
$cur_stock = round($cs_unround,2);
echo $old_stock;
echo "\n";
//Get stock CLOSE as old_stock and cur_stock
$priceArray = array("old"=>$old_stock, "cur"=>$cur_stock);
$fullPriceArray[$stock] = $priceArray;
}
return $fullPriceArray;
}
function doGraphSearch2($searchgraph){
$key = '9J4N8FA67HVHYZG0';
$alpha = "https://www.alphavantage.co/query?function=";
$grapharray = array($key, $alpha);
echo "\nRecieved graph info!";
return $grapharray;
}
function doBuyStock($symbol, $amount) {
//Fetch current stock info from API
$alpha_vantage = new Client('9J4N8FA67HVHYZG0');
$stockinfo = $alpha_vantage
->stock()
->intraday($symbol, AlphaVantage\Resources\Stock::INTERVAL_1MIN);
$fetchedsymbol = $stockinfo["Meta Data"]["2. Symbol"];
//Check if symbol exists
if ($symbol != $fetchedsymbol)
return "ERROR! Invalid symbol detected!";
//Get stock LOW as buy price (NBBO)
$stocktime = $stockinfo["Meta Data"]["3. Last Refreshed"];
$stockpricestring = $stockinfo["Time Series (1min)"][$stocktime]["3. low"];
//convert price and amount to float, multiply stock by amount, then round.
$stockprice = floatval($stockpricestring);
$i_amount = intval($amount);
$t = $stockprice * $i_amount;
$total = round($t,2);
//Now cURL for the portfolio values
$ch = curl_init("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" .$symbol. "&apikey=9J4N8FA67HVHYZG0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_results = curl_exec($ch);
curl_close($ch);
$today = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime("-1 days"));
echo $today;
echo "\n";
echo $yesterday;
$jsonarray = json_decode($curl_results, true);
$os_unround = floatval($jsonarray['Time Series (Daily)'][$yesterday]['4. close']);
$cs_unround = floatval($jsonarray['Time Series (Daily)'][$today]['4. close']);
$old_stock = round($os_unround,2);
$cur_stock = round($cs_unround,2);
echo $old_stock;
echo "\n";
$buyarray = array($i_amount, $total, $old_stock, $cur_stock);
echo "Sent buying info.";
//print_r($response);
echo"\n";
return $buyarray;
//At this point, you are done with collecting API data and need database data, so idealy the needed values would be sent back to the backend through RabbitMQ. The rest of this function would be done on the backend and is unnedded here.
}
function doSellStock($symbol, $amount) {
//The beginning of this function starts on the backend, and if successful, would pass on to a dmz function
//Fetch current stock info from API
$alpha_vantage = new Client('9J4N8FA67HVHYZG0');
$stockinfo = $alpha_vantage
->stock()
->intraday($symbol, AlphaVantage\Resources\Stock::INTERVAL_1MIN);
//Get stock HIGH as sell price (NBBO)
$stocktime = $stockinfo["Meta Data"]["3. Last Refreshed"];
$stockpricestring = $stockinfo["Time Series (1min)"][$stocktime]["2. high"];
//convert price and amount to float, multiply stock by amount, then round.
$stockprice = floatval($stockpricestring);
$i_amount = intval($amount);
$t = $stockprice * $i_amount;
$total = round($t,2);
$sellarray = array($i_amount, $total);
return $sellarray;
//Send needed values to backend. Rounding can happen either beforehand or afterwards.
}
function doSendMail($address, $subject, $body){
mail($address, $subject, $body);
return "Mail sent!\n";
}
//CODE "STARTS" HERE (server recieves requests then chooses function)
function requestProcessor($request) {
$alpha_vantage = new Client('9J4N8FA67HVHYZG0');
echo "Received a request".PHP_EOL;
var_dump($request);
if(!isset($request['type'])) {
return "ERROR: Unsupported message type!";
}
switch ($request['type']) {
case "RSS":
return RSS($request['RSS']);
case "search2S":
return doSearch($request['search2S']);
case "search2N":
return doDetailSearch($request['search2N']);
case "portfolioCron":
return doPortfolioCron($request['stockArray']);
case "buyStock2":
return doBuyStock($request['symbol'], $request['amount']);
case "sellStock2":
return doSellStock($request['symbol'], $request['amount']);
case "graph2":
return doGraphSearch2($request['searchgraph']);
case "mail":
return doSendMail($request['address'], $request['subject'], $request['body']);
}
return array("returnCode" => '0', 'message'=>"Server received request and processed.");
}
$server = new rabbitMQServer("DMZRabbitMQ.ini","testServer");
$server->process_requests('requestProcessor');
exit();
?>