라이브러리

[PHP] trader_ma - 이동 평균




트레이딩 모멘텀 지표 (Trading Momentum Indicator)

트레이딩 모멘텀 지표는 주식 시장에서 가격의 변동성을 분석하는 데 사용되는 지표입니다. 모멘텀 지표는 가격의 변동성을 측정하여 투자자들이 시장의 흐름을 예측하고 투자 결정을 내릴 수 있도록 도와줍니다.

PHP에서 Trader_MA 사용하기

PHP에서 Trader_MA를 사용하려면 PHP의 수학적 함수와 배열을 사용하여 모멘텀 지표를 계산할 수 있습니다. Trader_MA는 단순 이동 평균(Simple Moving Average, SMA)와 지지 저항 이동 평균(Relative Strength Index, RSI)로 구성됩니다.

Trader_MA 클래스


#hostingforum.kr
php

class Trader_MA {

    private $prices;

    private $sma;

    private $rsi;



    public function __construct($prices) {

        $this->prices = $prices;

        $this->sma = array();

        $this->rsi = array();

    }



    public function calculateSMA($period) {

        for ($i = 0; $i < count($this->prices); $i++) {

            if ($i >= $period - 1) {

                $sum = 0;

                for ($j = $i - $period + 1; $j <= $i; $j++) {

                    $sum += $this->prices[$j];

                }

                $this->sma[] = $sum / $period;

            } else {

                $this->sma[] = null;

            }

        }

    }



    public function calculateRSI($period) {

        for ($i = 0; $i < count($this->prices); $i++) {

            if ($i >= $period - 1) {

                $up = 0;

                $down = 0;

                for ($j = $i - $period + 1; $j <= $i; $j++) {

                    if ($this->prices[$j] > $this->prices[$j - 1]) {

                        $up += $this->prices[$j] - $this->prices[$j - 1];

                    } elseif ($this->prices[$j] < $this->prices[$j - 1]) {

                        $down += $this->prices[$j - 1] - $this->prices[$j];

                    }

                }

                $avgGain = $up / $period;

                $avgLoss = $down / $period;

                if ($avgLoss == 0) {

                    $this->rsi[] = 100;

                } else {

                    $rs = $avgGain / $avgLoss;

                    $this->rsi[] = 100 - (100 / (1 + $rs));

                }

            } else {

                $this->rsi[] = null;

            }

        }

    }



    public function getMA($period) {

        $this->calculateSMA($period);

        return $this->sma;

    }



    public function getRSI($period) {

        $this->calculateRSI($period);

        return $this->rsi;

    }

}



예제


#hostingforum.kr
php

$prices = array(10, 12, 15, 18, 20, 22, 25, 28, 30, 32, 35, 38, 40, 42, 45);

$ma = new Trader_MA($prices);

$maPeriod = 3;

$maValues = $ma->getMA($maPeriod);

$rsiPeriod = 3;

$rsiValues = $ma->getRSI($rsiPeriod);



echo "MA($maPeriod) : ";

print_r($maValues);

echo "
";

echo "RSI($rsiPeriod) : ";

print_r($rsiValues);



이 예제에서는 Trader_MA 클래스를 사용하여 단순 이동 평균(SMA)과 지지 저항 이동 평균(RSI)를 계산합니다. `getMA` 메서드는 SMA를 계산하고 `getRSI` 메서드는 RSI를 계산합니다. `print_r` 함수를 사용하여 계산된 SMA와 RSI 값을 출력합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 73 페이지

검색

게시물 검색