UNPKG

sql-synergy

Version:

Synergy Wave TA

129 lines (122 loc) 5.82 kB
"use strict"; /* In cases of moderate trends A rule of thumb is to BUY - when the oscillator dips below 30 and crosses back up. SELL - when the oscillator penetraes above 70 and croses down again. In cases of strong trends. In cases of strong trends, up or down, an extreme oscillator is produced very fast. Claims of an overbought or oversold market can be premature and can lead to an earlyh exit from a profitable trend. In strong uptrends - overbought markets can remain overbought for some time. Just because an oscillator has moved into the upper region is not enough reason to liquidate a long position ( or short into a strong trend ). The signal to pay close attention to is the second more into new highs ( forming a double top or a lower high on the oscillator ), a possible bear divergence exits. At that point, some defensive action can be taken to protect existing positions. If, further, the oscillator moves in the opposite direction, breaking a previous support or reaction low, then a divergence or failure swing is confirmed. Even then, a market exit might prove premature until the price trend itself shows signs of reversing. The moral is not to jump out of a profitable trade just because the oscillator reaches an extreme. Watch for the second move into the danger zone. In strong downtrends - The same rule applies before exiting a short position. A move by the oscillator into the oversold region is not sufficient reason to cover the short position ( or buy into a strong downtrend ). Wait for the second move by the oscillator into the danger zone ( below 30 ). If the second move fails to confirm price moves into new lows ( forming a double bottom or a higher low on the oscillator ) a bull divergence is imminent. At this point, one may choose to cover shorts. Only when the oscillator rebounds and breaks its reachtion high, confirming a failure swing, does one establish fresh long positions. Bull Divergence is when price makes a new low, rebounds and subsequently moves down to make lower low, while the corresponding troughs of RSI make a low, then a higher low. When you have a BULL DIVERGENCE or simply, CONVERGENCE, a buy signals awaits you. Bear Divergence is when price has made a new high, reacts and subsequently moves up to make a higher high, while the corresponding peaks on RSI make a high, then a lower high. A BEARISH DIVERGENCE signals a weaker market ahead. The concept of divergences is applicable with all oscillators including momentum, rate-of change, RSI, stochastics, MACD, Williams %R, etc. although not all oscillators emit divergences at the same time. A spotting of a divergence in any one oscillator is sufficient to warn a trager of an imminent trend change. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RSI = void 0; class RSI { // public getRSI ( fPrice:Array<number>, nPeriod:number, iIndex:number ):number // { // if ( iIndex <= nPeriod || iIndex >= fPrice.length ) // return Number.MIN_VALUE ; // var fUps = 0, fDowns = 0, f ; // var nUps = 0, nDowns = 0 ; // var i = iIndex - nPeriod ; // for ( ; i < iIndex ; i++ ) // { // f = fPrice [ i ] - fPrice [ i -1 ] ; // //System.out.println ( f ) ; // if ( f >= 0 ) // { // fUps += f ; // nUps++ ; // } // else // { // fDowns += f ; // nDowns++ ; // } // } // fDowns = -fDowns ; // if ( nUps == 0 ) // nUps = 1 ; // var fAverageUps = fUps / nUps ; // // System.out.println ( fUps + " " + fDowns ) ; // if ( nDowns == 0 ) // nDowns = 1 ; // var fAverageDowns = fDowns / nDowns ; // var fRelativeStrength = fAverageUps / fAverageDowns ; // var fRSI = 100 - ( 100 / ( 1 + fRelativeStrength ) ) ; // return fRSI ; // } //Matches MetaStock Computations getRSI(fPrice, nPeriods) { if (fPrice.length == 0) return []; let fRSI = new Array(fPrice.length); var i = 1; var fUps = 0, fDowns = 0, f = 0; var fT = Math.min(nPeriods, fPrice.length); for (; i < fT; i++) { f = fPrice[i] - fPrice[i - 1]; if (f > 0) fUps += f; else fDowns += f; fRSI[i] = Number.MIN_VALUE; } var fAvgUps = fUps / (nPeriods - 1); var fAvgDowns = -fDowns / (nPeriods - 1); var fFirstRS = fAvgUps / fAvgDowns; // System.out.println ( "Ups: " + fAvgUps + " Total Dn: " + fDowns + " Dns: " + fAvgDowns ) ; // System.out.println ( "First RS: " + fFirstRS ) ; var fFirstRSI = 100 - (100 / (1 + fFirstRS)); // System.out.println ( "RS: " + fFirstRS ) ; fUps = fDowns = 0.0; if (f >= 0.0) fUps = f; else fDowns = -f; fRSI[i - 1] = fFirstRSI; var fSmoothedRS; for (; i < fPrice.length; i++) { f = fPrice[i] - fPrice[i - 1]; fUps = fDowns = 0.0; if (f > 0.0) fUps = f; else fDowns = -f; fAvgUps = (fAvgUps * (nPeriods - 1) + fUps) / nPeriods; fAvgDowns = (fAvgDowns * (nPeriods - 1) + fDowns) / nPeriods; // System.out.println ( f + " AvgUp: " + fAvgUps + " AvgDn: " + fAvgDowns ) ; fSmoothedRS = fAvgUps / fAvgDowns; // System.out.println ( "Smoothed RS = " + fSmoothedRS ) ; fRSI[i] = (100 - (100 / (1 + fSmoothedRS))); } return fRSI; } } exports.RSI = RSI;