/
dataAccountManagement.php
149 lines (120 loc) · 4.02 KB
/
dataAccountManagement.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
<?php
require_once('dataBasicPlots.php');
require_once('codesword_trade_signals.php');
require_once('codesword_sma.php');
function tableExists($con, $table) {
$sql = "SHOW TABLES LIKE '$table'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0) {
return true;
}
else {
return false;
}
}
// generate the names of all the companies
function importHoldingsData ($host, $db, $user, $pass, $data) {
// Create connection
$con=mysqli_connect($host, $user, $pass, $db);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
//create table if it doesn't exist yet
$tableExists = tableExists($con, "current_holdings");
if (!$tableExists) {
$createTable = "CREATE TABLE `pse_data`.`current_holdings` (
`quote` VARCHAR(16) NOT NULL,
`datebuy` DATE NOT NULL,
`pricebuy` FLOAT NULL,
`volume` INT NULL,
`pricestoploss` FLOAT NULL,
PRIMARY KEY (`quote`))
COMMENT = 'contains the current stocks being held and the stop loss selling price'";
$result = mysqli_query($con, $createTable);
echo "importHoldingsData: Created table 'current_holdings' <Br><Br>";
}
$sql = "REPLACE INTO current_holdings (datebuy, quote, pricebuy, volume)";
$holdingValues = " VALUES";
$dataSize = count($data);
$ctr = 0;
foreach ($data as $holdings) {
$holdingValues = $holdingValues . "('".$holdings['date']."','".$holdings['company']."','".$holdings['pricebuy']."','".$holdings['volume']."')";
$ctr++;
if ($ctr < $dataSize) {
$holdingValues = $holdingValues . ", ";
}
}
$sql = $sql . $holdingValues;
echo "$sql";
$result = mysqli_query($con, $sql);
if (mysqli_affected_rows($con) < 1) {
echo "importHoldingsData: Failed Value Insertion!<Br>";
}
mysqli_close($con);
}
// returns OHLC for a candlestick chart
function getStoMACD ($company, $from="1900-01-01 00:00:00", $to=null,
$dataorg="json", $ensig=false,
$host, $db, $user, $pass) {
$thlc = []; //[timestamp,high,low,close]
$stoch = []; //[timestamp,%K,%D]
$stochSignals = []; //[timestamp,signal,desc]
$stochSmaSignals = []; //[timestamp,signal,desc]
$isCloseHigherThanSMA = []; //[timestamp,boolean]
$dataOhlc = [];
//OHLC data format [timestamp,open,high,low,close,volume]
if ($dataorg == "highchart") {
$dataOhlc = getOHLC ($company, $from, $to, "array2", $host, $db, $user, $pass);
} else {
$dataOhlc = getOHLC ($company, $from, $to, "array", $host, $db, $user, $pass);
}
//echo json_encode($dataOhlc);
//Return if $dataOhlc is null
if ( ($dataOhlc == []) || ($dataOhlc == 0) ) {
return 0;
}
//Input for stochastic convestion should be [timestamp,high,low,close]
$ctr = 0;
foreach ((array)$dataOhlc as $ohlc) {
$thlc[$ctr][0] = $ohlc[0]; //timestamp
$thlc[$ctr][1] = $ohlc[2]; //high
$thlc[$ctr][2] = $ohlc[3]; //low
$thlc[$ctr++][3] = $ohlc[4]; //close
}
//echo json_encode($thlc);
//generate stochastic values
$stoch = codesword_stochastic($thlc);
//echo json_encode($stoch);
//find out if the close/current price > sma
$isCloseHigherThanSMA = codesword_isHigherThanSMA($thlc);
//echo json_encode($isCloseHigherThanSMA);
//generate trade signals from stochastic values
$stochSignals = codesword_stochTradeDetector($stoch);
//do some more filtering
$stochSmaSignals = codesword_stochSmaTradeDetector($stochSignals,
$isCloseHigherThanSMA);
if (strcasecmp($ensig, "latest") == 0) {
//return only the latest signal
// [timestamp,trade signal,... other info...]
if (count($stochSmaSignals) > 0) {
$lastSignal = $stochSmaSignals[count($stochSmaSignals)-1];
//echo json_encode($lastSignal);
return $lastSignal;
}
else {
return 0;
}
}
elseif ($ensig) {
//echo json_encode($stochSignals);
$allData[0] = $stoch;
$allData[1] = $stochSmaSignals;
echo json_encode($allData);
}
else {
echo json_encode($stoch);
}
}
?>