Beispiel #1
0
function getSMACombined($company, $from = "1900-01-01 00:00:00", $to = null, $dataorg = "json", $periodShort = 20, $periodMedium = 50, $periodLong = 120, $enSignals = false, $host, $db, $user, $pass)
{
    $smaShort = getSMA($company, $from, $to, $dataorg, $periodShort, $enSignals, false, $host, $db, $user, $pass);
    $smaMedium = getSMA($company, $from, $to, $dataorg, $periodMedium, $enSignals, false, $host, $db, $user, $pass);
    $smaLong = getSMA($company, $from, $to, $dataorg, $periodLong, $enSignals, false, $host, $db, $user, $pass);
    $real = getSMA_sub_real($company, $from, $to, $dataorg, $periodShort, $enSignals, $host, $db, $user, $pass);
    if ($smaShort == 0 || $smaMedium == 0 || $smaLong == 0 || $real == 0) {
        return 0;
    }
    $allData = [];
    if (strcasecmp($enSignals, "latest") == 0) {
        //return only the latest signal
        // [timestamp,trade signal]
        $lastSignal = codesword_smaBuySellSignalCombinedLatests($real, $smaShort, $smaMedium, $smaLong, $dataorg);
        return $lastSignal;
    } elseif ($enSignals) {
        $allData = codesword_smaBuySellSignalCombined($real, $smaShort, $smaMedium, $smaLong, $dataorg);
        echo json_encode($allData);
    } else {
        $allData[0] = codesword_smaConsolidate($real, $smaShort, $smaMedium, $smaLong);
        $allData[1] = 0;
        echo json_encode($allData);
    }
}
Beispiel #2
0
function codesword_smaBuySellSignalCombined($real, $smaShort, $smaMedium, $smaLong, $dataorg = "json")
{
    $smaConsolidated = codesword_smaConsolidate($real, $smaShort, $smaMedium, $smaLong);
    $tradeSignals = [];
    if ($smaConsolidated == 0) {
        //echo "codesword_smaBuySellSignalCombined: stock has low chance of being traded<Br>";
        return 0;
    }
    $ctr = 0;
    for ($i = 0; $i < count($smaConsolidated); $i++) {
        $curDate = $smaConsolidated[$i][0];
        $curPrice = isset($smaConsolidated[$i][1]) ? $smaConsolidated[$i][1] : -1;
        $curSmaShort = isset($smaConsolidated[$i][2]) ? $smaConsolidated[$i][2] : -1;
        $curSmaMedium = isset($smaConsolidated[$i][3]) ? $smaConsolidated[$i][3] : -1;
        $curSmaLong = isset($smaConsolidated[$i][4]) ? $smaConsolidated[$i][4] : -1;
        if ($i == 0) {
            $prevPrice = $smaConsolidated[$i][1];
            $prevSmaShort = $smaConsolidated[$i][2];
            continue;
        }
        //Only make decisions for cases where all SMAs have values
        if ($curSmaShort > 0 && $curSmaMedium > 0 && $curSmaLong > 0) {
            //Segregate into uptrend, downtrend and sideways
            //Uptrend
            // SMA short > SMA medium > SMA long
            if ($curSmaShort >= $curSmaMedium && $curSmaMedium >= $curSmaLong) {
                //RoT #1: Buy. Price > s20 > s50 > s120
                if ($curPrice > $curSmaShort && $curPrice > $curSmaMedium && $curPrice > $curSmaLong) {
                    $prevPrevPrice = isset($smaConsolidated[$i - 2][1]) ? $smaConsolidated[$i - 2][1] : 100000;
                    if ($curPrice > $prevPrice && $curPrice > $prevPrevPrice) {
                        //Buy only if price increase wrt to the sma short is 5% higher
                        if ($curPrice / $curSmaShort - 1 > 0.05) {
                            if ($ctr > 0) {
                                //to filter out redundant buy signals
                                if ($tradeSignals[$ctr - 1][1] == "sell") {
                                    $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                    $tradeSignals[$ctr][1] = "buy";
                                    $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                    $ctr++;
                                }
                            } else {
                                $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                $tradeSignals[$ctr][1] = "buy";
                                $tradeSignals[$ctr][2] = 0;
                                $ctr++;
                            }
                        }
                        /*							if ($ctr > 0) {
                        								//to filter out redundant buy signals
                        								if ($tradeSignals[$ctr-1][1] == "sell") {
                        									$tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                        									$tradeSignals[$ctr][1] = "buy";
                        									$tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr-1][0], 
                        																				$tradeSignals[$ctr][0], 
                        																				$dataorg);
                        									$ctr++;
                        								}
                        							}
                        							else {
                        								$tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                        								$tradeSignals[$ctr][1] = "buy";
                        								$tradeSignals[$ctr][2] = 0;
                        								$ctr++;
                        							}*/
                    }
                }
                //RoT #2.2: Sell. Price < s20 (if Uptrending)
                if ($curPrice < $curSmaShort) {
                    if ($ctr > 0) {
                        //generate a sell signal only if last one was a buy signal
                        if ($tradeSignals[$ctr - 1][1] == "buy") {
                            //different thresholds depending on current price
                            if ($curPrice <= 35) {
                                //check if |curPrice/prevPrice - 1| > 2.5%
                                if ($prevPrice / $curPrice - 1 > 0.025) {
                                    //sell if s20/curPrice - 1 > 0.025
                                    if ($curSmaShort / $curPrice - 1 > 0.025) {
                                        $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                        $tradeSignals[$ctr][1] = "sell";
                                        $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                        $ctr++;
                                    }
                                } elseif ($curSmaShort / $curPrice - 1 > 0.03) {
                                    $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                    $tradeSignals[$ctr][1] = "sell";
                                    $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                    $ctr++;
                                }
                            } else {
                                //check if |curPrice/prevPrice - 1| > 1%
                                if ($prevPrice / $curPrice - 1 > 0.01) {
                                    $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                    $tradeSignals[$ctr][1] = "sell";
                                    $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                    $ctr++;
                                }
                                //TODO: if s20/curPrice - 1 > [arbitrary number for bigger prices]
                            }
                        }
                    } else {
                        $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                        $tradeSignals[$ctr][1] = "sell";
                        $tradeSignals[$ctr][2] = 0;
                        $ctr++;
                    }
                }
            } elseif ($curSmaShort <= $curSmaLong && $curSmaMedium <= $curSmaLong) {
                //new RoT (unlabeled)
                //you need at least 5 samples to make this conclusion
                //price is greater than sma short and sma medium
                //and the difference between sma short and sma medium is
                //getting smaller
                if ($i >= 4) {
                    $curTempSma = 0;
                    if ($curSmaShort >= $curSmaMedium) {
                        $curTempSma = $curSmaShort;
                    } else {
                        $curTempSma = $curSmaMedium;
                    }
                    //Price should at least be 5% greater than both SMAs
                    if ($curPrice > $curSmaShort && $curPrice > $curSmaMedium && $curPrice / $curTempSma - 1 > 0.05) {
                        $divergencePoints = 0;
                        $prevDiff = 100;
                        //check the differences of sma short and sma medium
                        // to see if its getting smaller
                        for ($k = 4; $k >= 0; $k--) {
                            $tempSmaShort = isset($smaConsolidated[$i - $k][2]) ? $smaConsolidated[$i - $k][2] : -1;
                            $tempSmaMedium = isset($smaConsolidated[$i - $k][3]) ? $smaConsolidated[$i - $k][3] : -1;
                            $tempDiff = abs($tempSmaShort - $tempSmaMedium);
                            if ($tempDiff <= $prevDiff) {
                                $divergencePoints++;
                            }
                            $prevDiff = $tempDiff;
                        }
                        if ($divergencePoints >= 3) {
                            if ($ctr > 0) {
                                //to filter out redundant buy signals
                                if ($tradeSignals[$ctr - 1][1] == "sell") {
                                    $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                    $tradeSignals[$ctr][1] = "buy";
                                    $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                    $ctr++;
                                }
                            } else {
                                $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                $tradeSignals[$ctr][1] = "buy";
                                $tradeSignals[$ctr][2] = 0;
                                $ctr++;
                            }
                        }
                    }
                }
            } else {
            }
            //RoT #2.1: Sell automatically when Price < sma medium
            if ($curPrice < $curSmaMedium) {
                if ($ctr > 0) {
                    //generate a sell signal only if last one was a buy signal
                    if ($tradeSignals[$ctr - 1][1] == "buy") {
                        //different thresholds depending on current price
                        if ($curPrice <= 35) {
                            //check if |curSmaMedium/prevPrice - 1| > 2.5%
                            if ($curSmaMedium / $curPrice - 1 > 0.025) {
                                $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                $tradeSignals[$ctr][1] = "sell";
                                $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                $ctr++;
                            }
                        } else {
                            //check if |curSmaMedium/prevPrice - 1| > 1.3%
                            if ($curSmaMedium / $curPrice - 1 > 0.013) {
                                $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                                $tradeSignals[$ctr][1] = "sell";
                                $tradeSignals[$ctr][2] = codesword_dateDiff($tradeSignals[$ctr - 1][0], $tradeSignals[$ctr][0], $dataorg);
                                $ctr++;
                            }
                        }
                    }
                } else {
                    $tradeSignals[$ctr][0] = $smaConsolidated[$i][0];
                    $tradeSignals[$ctr][1] = "sell";
                    $tradeSignals[$ctr][2] = 0;
                    $ctr++;
                }
            }
        }
        //fix for "undefined offset" notice
        $prevPrice = isset($smaConsolidated[$i][1]) ? $smaConsolidated[$i][1] : -1;
        $prevSmaShort = isset($smaConsolidated[$i][2]) ? $smaConsolidated[$i][2] : -1;
    }
    $allData = [];
    $allData[0] = $smaConsolidated;
    $allData[1] = $tradeSignals;
    return $allData;
}