UNPKG

sql-synergy

Version:

Synergy Wave TA

922 lines (919 loc) 37.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WaveTerminator = void 0; const Step1D1_1 = require("./Step1D1"); const Step2_1 = require("./Step2"); const Step3_1 = require("./Step3"); const Step4_1 = require("./Step4"); const Step5_1 = require("./Step5"); const Step6_1 = require("./Step6"); const IConstants_1 = require("./IConstants"); const Wave_1 = require("./Wave"); //import ThreeWhiteSoldiers from "technicalindicators/declarations/candlestick/ThreeWhiteSoldiers"; const fs = require("fs"); class WaveTerminator { // constructor(oMSQuote:MSQuote, oPC:Technicals, oIC:Technicals) { constructor(oMSQuote, iPC, iIC) { this.iPC = 0; this.iIC = 0; this.iaWave = []; this.iWaveName = 0; this.bPerfect = false; // public hourly3P(iDay: number): number { // if (this.oStep2.hasBuySignal(iDay)) { // this.bPerfect = true; // return IConstants.BUY_SIGNAL; // } // if (this.oStep2.hasBuySignal(iDay)) { // this.bPerfect = true; // return IConstants.SELL_SIGNAL; // } // return IConstants.NONE; // } this.bN = []; this.add = (a, b) => a + b; this.sWaveName = '@'; this.iBiggerChannelDir = 0; this.iPC = iPC; this.iIC = iIC; this.oPC = oMSQuote.getData(this.iPC); this.oIC = oMSQuote.getData(this.iIC); this.oBStep2 = this.oBStep2; // if no big chart this.oBStep3 = this.oBStep3; // if no big chart if (iIC < IConstants_1.IConstants.MONTHLY) // Monthly Indicator should not be neutralized { this.oBIC = oMSQuote.getData(this.iIC + 1); this.oBStep4 = new Step4_1.Step4(this.oBIC); this.oBStep5 = new Step5_1.Step5(this.oBIC); this.oBStep6 = new Step6_1.Step6(this.oBIC); } if (iPC < IConstants_1.IConstants.MONTHLY) // Monthly Indicator should not be neutralized { this.oBPC = oMSQuote.getData(this.iPC + 1); this.oBStep2 = new Step2_1.Step2(this.oBPC); this.oBStep3 = new Step3_1.Step3(this.oBPC); } if (this.oIC.getPeriod() == IConstants_1.IConstants.HOURLY) { this.oStep1D1 = new Step1D1_1.Step1D1(this.oIC); } this.oStep2 = new Step2_1.Step2(this.oPC); this.oStep3 = new Step3_1.Step3(this.oPC); this.oStep4 = new Step4_1.Step4(this.oIC); this.oStep5 = new Step5_1.Step5(this.oIC); this.oStep6 = new Step6_1.Step6(this.oIC); } getJSON(a = null) { if (a === null) a = this.iaWave; var s = ''; console.log('['); for (var i = 0; i < this.iaWave.length; i++) { console.log(s + this.iaWave[i].getJSON()); s = ','; } console.log(']'); } generateWavesNOTUSED(iStartIndex, iEndIndex = -1) { if (iEndIndex == -1) iEndIndex = this.oPC.getLastDay(); let iSignal = IConstants_1.IConstants.NONE; let iSignalDay = []; for (var i = iStartIndex; i <= iEndIndex; i++) { if (iSignal != IConstants_1.IConstants.BUY_SIGNAL && this.hasBuySignal(i)) { iSignal = IConstants_1.IConstants.BUY_SIGNAL; let oWave = new Wave_1.Wave(); oWave.setDirection(iSignal); oWave.set5StepsDateIndex(i); oWave.setSignalDate(this.oPC.fD[i]); oWave.setCharts(this.oPC, this.oIC); oWave.setPerfect(this.bN.reduce(this.add) == 0); oWave.iPC = this.iPC; oWave.iIC = this.iIC; this.iaWave.push(oWave); iSignalDay.push([i, this.oPC.fD[i], this.oPC.fO[i], this.oPC.fH[i], this.oPC.fL[i], this.oPC.fC[i]]); // console.log("BUY Signal: ", iSignal, this.bN); continue; } if (iSignal != IConstants_1.IConstants.SELL_SIGNAL && this.hasSellSignal(i)) { iSignal = IConstants_1.IConstants.SELL_SIGNAL; let oWave = new Wave_1.Wave(); oWave.setDirection(iSignal); oWave.set5StepsDateIndex(i); oWave.setSignalDate(this.oPC.fD[i]); oWave.setCharts(this.oPC, this.oIC); oWave.setPerfect(this.bN.reduce(this.add) == 0); oWave.iPC = this.iPC; oWave.iIC = this.iIC; this.iaWave.push(oWave); iSignalDay.push([-i, this.oPC.fD[i], this.oPC.fO[i], this.oPC.fH[i], this.oPC.fL[i], this.oPC.fC[i]]); // console.log("SELL Signal: ", iSignal, this.bN); } } console.log("Done."); console.log(iSignalDay); // this.convertArray2Waves(iSignalDay); this.updateMinMax(); this.updateFirstAndLastWaves(); // this.getJSON(); while (this.removeZigZag()) { this.updateMinMax(); } console.log("Removed ZigZag"); // console.log("B4MinMax") ; this.recalculateMinMaxTgt(); // console.log("AftMinMax") ; // this.getJSON(); // console.log("Step1 Check") ; while (this.checkStep1()) { this.adjustMinMax(); this.recalculateMinMaxTgt(); // this.getJSON() ; } // console.log("AftMinMax") ; // this.getJSON(); } // public setMinMax ( oWave0:any, oWave1:any, oWave2:any ) // { // if ( oWave1 == null ) // { // console.log ( "Change of Wave: " + oWave0.getName ( ) ) ; // let oPrevSameRankWave:any = this.getPreviousWave ( oWave0 ) ; // if ( oPrevSameRankWave == null ) // { // return ; // } // if ( oWave0.isDown ( ) ) // { // oWave0.fMinimum = oWave0.getHigh ( ) - oPrevSameRankWave.getLength ( ) / 2 ; // oWave0.fMaximum = oWave0.getHigh ( ) - oPrevSameRankWave.getLength ( ) ; // return ; // } // if ( oWave0 != null ) // { // oWave0.fMinimum = oWave0?.getLow ( ) + oPrevSameRankWave.getLength ( ) / 2 ; // oWave0.fMaximum = oWave0?.getLow ( ) + oPrevSameRankWave.getLength ( ) ; // } // return ; // } // if ( oWave0.isDown ( ) ) // { // if ( oWave0.isCorrective ( ) ) // { // oWave0.fMinimum = oWave0.getHigh ( ) - oWave1.getLength ( ) / 2 ; // oWave0.fMaximum = oWave0.getHigh ( ) - oWave1.getLength ( ) ; // } // else // { // if ( oWave2 != null ) // { // oWave0.fMinimum = oWave0.getHigh ( ) - oWave2.getLength ( ) ; // oWave0.fMaximum = oWave0.getHigh ( ) - oWave2.getLength ( ) * 2 ; // } // // oWave0.fMinimum = oWave0.getHigh ( ) - oWave1.getLength ( ) ; // // oWave0.fMaximum = oWave0.getHigh ( ) - oWave1.getLength ( ) * 2 ; // } // } // else // { // if ( oWave0.isCorrective ( ) ) // { // oWave0.fMinimum = oWave0.getLow ( ) + oWave1.getLength ( ) / 2 ; // oWave0.fMaximum = oWave0.getLow ( ) + oWave1.getLength ( ) ; // } // else // { // if ( oWave2 != null ) // { // if ( oWave0 != null ) // { // oWave0.fMinimum = oWave0?.getLow ( ) + oWave2.getLength ( ) ; // oWave0.fMaximum = oWave0?.getLow ( ) + oWave2.getLength ( ) * 2 ; // } // } // // oWave0.fMinimum = oWave0.getLow ( ) + oWave1.getLength ( ) ; // // oWave0.fMaximum = oWave0.getLow ( ) + oWave1.getLength ( ) * 2 ; // } // } // if ( oWave2 == null ) // { // First wave is B and the next wave is C // // Minimum Target is atleast "B's" High // if ( oWave0.getName() == "C" ) // { // if ( oWave1.getName ( ) == "B" ) // { // oWave0.setMinimum ( oWave1.getHigh ( ) ) ; // } // else // console.error ( this + " Fatal: Check Logic Previuos wave of C is not B" ) ; // } // } // } recalculateMinMaxTgt() { // let oLI:IterableIterator<Wave> | undefined = this.oWS?.getIterator ( ) ; let bFound = false; let n = this.iaWave.length; let oWave0 = this.iaWave[0]; let oWaveP = this.iaWave[0]; let isPImpulsive = true; // Just treat all as corrective wave for (var c = 1; c < n; c++) { //isPImpulsive = oWave0.isImpulsive() ; if (isPImpulsive) { //This is Corrective Wave if (this.iaWave[c].isUp()) { this.iaWave[c].setMinimum(this.iaWave[c].fLowest + (oWave0.fHighest + oWave0.fLowest) / 2); this.iaWave[c].setMaximum(this.iaWave[c].fLowest + oWave0.fHighest + oWave0.fLowest); } else { this.iaWave[c].setMinimum(this.iaWave[c].fHighest - (oWave0.fHighest + oWave0.fLowest) / 2); this.iaWave[c].setMaximum(this.iaWave[c].fHighest - (oWave0.fHighest + oWave0.fLowest)); } } else { //This is Impulsive Wave let fLength = oWaveP.fHighest - oWaveP.fLowest; if (this.iaWave[c].isUp()) { this.iaWave[c].setMinimum(this.iaWave[c].fLowest + fLength); this.iaWave[c].setMaximum(this.iaWave[c].fLowest + fLength * 2); } else { this.iaWave[c].setMinimum(this.iaWave[c].fHighest - fLength); this.iaWave[c].setMaximum(this.iaWave[c].fHighest - fLength * 2); } } oWaveP = oWave0; oWave0 = this.iaWave[c]; } } checkStep1() { var n = this.iaWave.length; if (n < 3) return false; var oWave1 = this.iaWave[0]; var oWave2 = this.iaWave[1]; var i = 2; while (i < n) { var oWave3 = this.iaWave[i]; console.log(oWave2.wasStep1Met()); if (oWave2.wasStep1Met() == false) { if (oWave1.isDown()) oWave1.iLowIndex = oWave3.iLowIndex; else oWave1.iHighIndex = oWave3.iHighIndex; this.iaWave.splice(i, 2); return true; } i++; oWave1 = oWave2; oWave2 = oWave3; } return false; } removeZigZag() { /* [1507507200000,1524441600000,"A-Up",0.00008,0.00244,"I","N",0,0,1,"W","W",1517788800000] [1524441600000,1537747200000,"B-Dn",0.00244,0.0007911,"C","N",0,0,-1,"W","W",1537747200000] [1537747200000,1538956800000,"C-Up",0.0007911,0.0009013,"I","N",0,0,1,"W","W",1538352000000] [1538956800000,1539561600000,"D-Dn",0.0009013,0.0007955,"C","N",0,0,-1,"W","W",1538956800000] [1539561600000,1554076800000,"E-Up",0.0007955,0.0011478,"I","N",0,0,1,"W","W",1539561600000] [1554076800000,1561334400000,"F-Dn",0.0011478,0.0004936,"I","N",0,0,-1,"W","W",1560729600000] === > REMOVES This [1561334400000,1561334400000,"G-Up",0.0004936,0.0006718,"I","N",0,0,1,"W","W",1561334400000] [1561334400000,1610323200000,"H-Dn",0.0006718,0.0000694,"I","N",0,0,-1,"W","W",1561939200000] */ var n = this.iaWave.length; if (n < 3) return false; var oWave1 = this.iaWave[0]; var oWave2 = this.iaWave[1]; var i = 2; while (i < n) { var oWave3 = this.iaWave[i]; if (oWave2.getStartIndex() == oWave2.getEndIndex()) { if (oWave1.isDown()) oWave1.iLowIndex = oWave3.iLowIndex; else oWave1.iHighIndex = oWave3.iHighIndex; this.iaWave.splice(i, 2); return true; } i++; oWave1 = oWave2; oWave2 = oWave3; } return false; // for ( var i = 0 ; i < n ; i++ ) // { // if ( this.iaWave[i].getStartIndex() == this.iaWave[i].getEndIndex()) // { // this.iaWave.splice(i, 2); // n -= 2 ; // i -= 2 ; // } // } // this.updateMinMax(); } removeZigZagNU() { /* [1507507200000,1524441600000,"A-Up",0.00008,0.00244,"I","N",0,0,1,"W","W",1517788800000] [1524441600000,1537747200000,"B-Dn",0.00244,0.0007911,"C","N",0,0,-1,"W","W",1537747200000] [1537747200000,1538956800000,"C-Up",0.0007911,0.0009013,"I","N",0,0,1,"W","W",1538352000000] [1538956800000,1539561600000,"D-Dn",0.0009013,0.0007955,"C","N",0,0,-1,"W","W",1538956800000] [1539561600000,1554076800000,"E-Up",0.0007955,0.0011478,"I","N",0,0,1,"W","W",1539561600000] [1554076800000,1561334400000,"F-Dn",0.0011478,0.0004936,"I","N",0,0,-1,"W","W",1560729600000] === > REMOVES This [1561334400000,1561334400000,"G-Up",0.0004936,0.0006718,"I","N",0,0,1,"W","W",1561334400000] [1561334400000,1610323200000,"H-Dn",0.0006718,0.0000694,"I","N",0,0,-1,"W","W",1561939200000] */ var n = this.iaWave.length; if (n < 3) return this.iaWave; var oWave1 = this.iaWave[0]; var oWave2 = this.iaWave[1]; var i = 2; while (i < n) { var oWave3 = this.iaWave[i]; if (oWave2.getStartIndex() == oWave2.getEndIndex()) { if (oWave1.isDown()) oWave1.iLowIndex = oWave3.iLowIndex; else oWave1.iHighIndex = oWave3.iHighIndex; if (++i == n) break; oWave2 = this.iaWave[i]; if (++i == n) break; oWave3 = this.iaWave[i]; continue; } i++; oWave1 = oWave2; oWave2 = oWave3; } for (var i = 0; i < n; i++) { if (this.iaWave[i].getStartIndex() == this.iaWave[i].getEndIndex()) { this.iaWave.splice(i, 2); n -= 2; i -= 2; } } this.updateMinMax(); // this.getJSON(); } updateFirstAndLastWaves() { var n = this.iaWave.length; if (n == 0) return; let oWave = this.iaWave[0]; if (oWave.getDirection() == IConstants_1.IConstants.BUY_SIGNAL) { let iLL = this.getLowestLow(this.oPC.fL, 0, oWave.get5StepsDateIndex()); oWave.setLowest(iLL, this.oPC.fL[iLL]); } else { let iHH = this.getHighestHigh(this.oPC.fH, 0, oWave.get5StepsDateIndex()); oWave.setHighest(iHH, this.oPC.fH[iHH]); } if (n == 1) return; oWave = this.iaWave[n - 1]; // if (oWave.getDirection() == IConstants.BUY_SIGNAL) { // let iHH = this.getHighestHigh(this.oPC.fH, oWave.get5StepsDateIndex(), this.oPC.fH.length - 1) // oWave.setHighest(iHH, this.oPC.fH[iHH]) // } // else { // let iLL = this.getLowestLow(this.oPC.fL, oWave.get5StepsDateIndex(), this.oPC.fL.length - 1) // oWave.setLowest(iLL, this.oPC.fL[iLL]) // } } getNextName(sName) { return "" + this.iWaveName++; // return String.fromCharCode(this.ascii(sName.charAt(0)) + 1) } ascii(a) { return a.charCodeAt(0); } updateMinMax() { let sName = "@"; var n = this.iaWave.length; var i = 0; for (; i < n; i++) { let oWave = this.iaWave[i]; var iEndIndex = this.oPC.getLastDay(); let oNextWave = null; if (i < n - 1) { oNextWave = this.iaWave[i + 1]; iEndIndex = oNextWave.get5StepsDateIndex(); } oWave.setName(sName = this.getNextName(sName)); if (oWave.getDirection() == IConstants_1.IConstants.BUY_SIGNAL) { let iHH = this.getHighestHigh(this.oPC.fH, oWave.get5StepsDateIndex(), iEndIndex); oWave.setHighest(iHH, this.oPC.fH[iHH]); if (oNextWave != null) oNextWave.setHighest(iHH, this.oPC.fH[iHH]); continue; } let iLL = this.getLowestLow(this.oPC.fL, oWave.get5StepsDateIndex(), iEndIndex); oWave.setLowest(iLL, this.oPC.fL[iLL]); if (oNextWave != null) oNextWave.setLowest(iLL, this.oPC.fL[iLL]); continue; } // if (n > 0) // { // let oWave:Wave = this.iaWave[n-1] ; // if (oWave.getDirection() == IConstants.BUY_SIGNAL) { // let iHH = this.getHighestHigh(this.oPC.fH, oWave.get5StepsDateIndex(), this.oPC.getLastDay()) // oWave.setHighest(iHH, this.oPC.fH[iHH]) ; // } // else // { // let iLL = this.getLowestLow(this.oPC.fL, oWave.get5StepsDateIndex(), this.oPC.getLastDay()) // oWave.setLowest(iLL, this.oPC.fL[iLL]) // } // } } // convertArray2Waves(iaSignals: any) { // var idx: number = 0; // for (var i = iaSignals.length - 1; i > 0; i--) { // idx = iaSignals[i][0]; // if (idx < 0) // Down Wave // console.log(this.getHighestHigh(this.oPC.fH, iaSignals[i - 1][0], -idx)); // else // console.log(this.getLowestLow(this.oPC.fL, -iaSignals[i - 1][0], idx)); // } // // First Wave // if (iaSignals.length > 0) { // idx = iaSignals[0][0]; // if (idx < 0) // console.log(this.getHighestHigh(this.oPC.fH, 0, -idx)); // else // console.log(this.getLowestLow(this.oPC.fL, 0, idx)); // } // } getHighestHigh(oP, iStartIndex, iEndIndex) { var p = Number.MIN_VALUE; var idx = iStartIndex; for (var i = iStartIndex; i <= iEndIndex; i++) { if (oP[i] > p) { p = oP[i]; idx = i; } } return idx; } getLowestLow(oP, iStartIndex, iEndIndex) { var p = Number.MAX_VALUE; var idx = iStartIndex; for (var i = iStartIndex; i <= iEndIndex; i++) { if (oP[i] < p) { p = oP[i]; idx = i; } } return idx; } check3IHourlyBuySignal(iDay) { if (this.oStep1D1.hasBuySignal(iDay)) { return true; } // if (this.oBStep5.hasBuySignal(this.getBigIndicatorIndex(iDay))) { // this.bN[5] = 1; // return true; // } return false; } check3IHourlySellSignal(iDay) { if (this.oStep1D1.hasSellSignal(iDay)) { return true; } // if (this.oBStep5.hasSellSignal(this.getBigIndicatorIndex(iDay))) { // this.bN[5] = 1; // return true; // } return false; } checkBRF(iDay) { this.bN[2] = 0; if (this.oStep2.hasBuySignal(iDay)) { // console.log("Step2 ", iDay, this.oPC.fD[iDay], this.oPC.isWhiteAndNonReversal(iDay)) return true; } // if (this.oBStep2.hasBuySignal(this.getBigPriceIndex(iDay))) { // this.bN[2] = 1; // return true; // } // if (this.wasBiggerCHBroken(iDay)) { // this.bN[2] = 1; // return true; // } return false; } checkTRF(iDay) { this.bN[2] = 0; if (this.oStep2.hasSellSignal(iDay)) { return true; } // if (this.oBStep2.hasSellSignal(this.getBigPriceIndex(iDay))) { // this.bN[2] = 1; // return true; // } // if (this.wasBiggerCLBroken(iDay)) { // this.bN[2] = 1; // return true; // } return false; } checkCH(iDay) { this.bN[3] = 0; if (this.wasBiggerCHBroken(iDay)) { this.iBiggerChannelDir = 1; } if (this.oStep3.hasBuySignal(iDay)) { // console.log("Checking checkCH on ", this.iPC, iDay, this.oPC.fC.length ) // console.log("Has BuySignal: ", iDay, this.oPC.fC[iDay], this.oStep3.fCH[iDay]) return true; } // if (this.wasBiggerCHBroken(iDay)) { // this.bN[3] = 1; // return true; // } return false; } checkCL(iDay) { this.bN[3] = 0; if (this.wasBiggerCLBroken(iDay)) { this.iBiggerChannelDir = -1; } if (this.oStep3.hasSellSignal(iDay)) { return true; } // if (this.wasBiggerCLBroken(iDay)) { // this.bN[3] = 1; // return true; // } return false; } // public checkWaveNeutralization(iDay: number, bUpWave: boolean): boolean { // //FIXME if waves minimum target is not met // this.bN[3] = 0; // if (bUpWave) //Neutralization of Up Wave // { // if (this.oStep3.hasSellSignal(iDay) // && this.oStep3.oPC.isWhiteAndNonReversal(iDay) // && this.oStep3.fCL[iDay] > this.oStep3.fC[iDay]) //FIXME One Price Bid Up // { // this.bN[3] = 1; // return true; // } // } // if (!bUpWave) //Neutralization // { // if (this.oStep3.hasSellSignal(iDay) // && this.oStep3.oPC.isBlackAndNonReversal(iDay) // && this.oStep3.fCH[iDay] < this.oStep3.fC[iDay]) //FIXME One Price Bid Up // { // this.bN[3] = 1; // return true; // } // } // return false; // } check3IBuySignal(iDay) { //this.bN[0] = 0; //Neturalization of Indicators by CHCL this.bN[4] = 0; this.bN[5] = 0; this.bN[6] = 0; if (this.iIC == IConstants_1.IConstants.HOURLY) { return this.check3IHourlyBuySignal(iDay); } var s4 = this.oStep4.hasBuySignal(iDay); var s5 = this.oStep5.hasBuySignal(iDay); var s6 = this.oStep6.hasBuySignal(iDay); if (s4 && s5 && s6) { return true; } if (this.oBStep4 == undefined) return false; var sB4 = this.oBStep4.hasBuySignal(this.getBigIndicatorIndex(iDay)); var sB5 = this.oBStep5.hasBuySignal(this.getBigIndicatorIndex(iDay)); var sB6 = this.oBStep6.hasBuySignal(this.getBigIndicatorIndex(iDay)); if ((s4 || sB4) && (s5 || sB5) && (s6 && sB6)) { if (!s4) this.bN[4] = 1; if (!s5) this.bN[5] = 1; if (!s6) this.bN[6] = 1; return true; } // var bNeutralized = this.wasBiggerCHBroken(iDay); // if (bNeutralized) { // this.bN[0] = 1; // return true; // } return false; } check3ISellSignal(iDay) { //this.bN[0] = 0; //Neturalization of Indicators this.bN[4] = 0; this.bN[5] = 0; this.bN[6] = 0; if (this.iIC == IConstants_1.IConstants.HOURLY) { return this.check3IHourlySellSignal(iDay); } var s4 = this.oStep4.hasSellSignal(iDay); var s5 = this.oStep5.hasSellSignal(iDay); var s6 = this.oStep6.hasSellSignal(iDay); if (s4 && s5 && s6) { return true; } if (this.oBStep4 == undefined) return false; var sB4 = this.oBStep4.hasSellSignal(this.getBigIndicatorIndex(iDay)); var sB5 = this.oBStep5.hasSellSignal(this.getBigIndicatorIndex(iDay)); var sB6 = this.oBStep6.hasSellSignal(this.getBigIndicatorIndex(iDay)); if ((s4 || sB4) && (s5 || sB5) && (s6 && sB6)) { if (!s4) this.bN[4] = 1; if (!s5) this.bN[5] = 1; if (!s6) this.bN[6] = 1; return true; } // var bNeutralized = this.wasBiggerCLBroken(iDay); // if (bNeutralized) { // this.bN[0] = 1; // return true; // } return false; } hasBuySignal(iDay) { this.bN = [0, 0, 0, 0, 0, 0, 0]; this.bN[1] = 0; //Wave if (this.check3IBuySignal(iDay) && this.checkBRF(iDay) && this.checkCH(iDay)) { return true; } return false; } // public getBuySignals(iDay: number):Array<number> // { // var iAllSignals = [0,0,-1,-1,-1,-1,-1] ; // if ( this.checkBRF(iDay) == true) // iAllSignals[2] = 0 ; // if ( this.checkCH(iDay) == true) // iAllSignals[3] = 0 ; // if ( this.check3IBuySignal(iDay) == true) // iAllSignals[2] = 0 ; // return iAllSignals ; // } hasSellSignal(iDay) { this.bN = [0, 0, 0, 0, 0, 0, 0]; this.bN[1] = 0; //Wave if (this.check3ISellSignal(iDay) && this.checkTRF(iDay) && this.checkCL(iDay)) { return true; } return false; } // public getSmallerChartDateIndex(iDay: number): number { // try { // let oSPC = this.oMSQuote.get(this.iPC - 1); // return oSPC.getSmallerChartDate(this.oPC.getDate()[iDay]); // } // catch (err) { // console.error(err); // } // return -1; // } getBigPriceIndex(iDay) { return this.oBPC.getChartDate(this.oPC.getDate()[iDay]); } getBigIndicatorIndex(iDay) { if (this.oBIC === undefined) // Monthly Indicators cannot be neutralized return -1; return this.oBIC.getChartDate(this.oIC.getDate()[iDay]); } wasBiggerCHBroken(iDay) { if (this.oBStep3 === undefined) return false; return this.oBStep3.hasBuySignal(this.getBigPriceIndex(iDay)); } wasBiggerCLBroken(iDay) { if (this.oBStep3 === undefined) return false; return this.oBStep3.hasSellSignal(this.getBigPriceIndex(iDay)); } adjustMinMax() { var n = this.iaWave.length; var i = 1; for (; i < n - 1; i++) { let oWave = this.iaWave[i]; // console.log("Wave Start and NExt End:", oWave.getStartIndex(), this.iaWave[i+1].getEndIndex()); if (oWave.isDown()) { let iLL = this.getLowestLow(this.oPC.fL, oWave.getStartIndex(), this.iaWave[i + 1].getEndIndex()); oWave.setLowest(iLL, this.oPC.fL[iLL]); this.iaWave[i + 1].setLowest(iLL, this.oPC.fL[iLL]); } else { let iHH = this.getHighestHigh(this.oPC.fH, oWave.getStartIndex(), this.iaWave[i + 1].getEndIndex()); oWave.setHighest(iHH, this.oPC.fH[iHH]); this.iaWave[i + 1].setHighest(iHH, this.oPC.fH[iHH]); } } } newWave(i) { let oWave = new Wave_1.Wave(); oWave.set5StepsDateIndex(i); oWave.setSignalDate(this.oPC.fD[i]); oWave.setCharts(this.oPC, this.oIC); oWave.biggerChannel = this.iBiggerChannelDir; oWave.iPC = this.iPC; oWave.iIC = this.iIC; return oWave; } setStartIndexFirstTime(oWave, iDir, iStartPos, isPrefect = false) { oWave.setDirection(iDir); oWave.setPerfect(isPrefect); if (oWave.isUp()) { let v = this.getLowestLow(this.oPC.fL, iStartPos, oWave.get5StepsDateIndex()); oWave.setLowest(v, this.oPC.fL[v]); } else { let v = this.getHighestHigh(this.oPC.fH, iStartPos, oWave.get5StepsDateIndex()); oWave.setHighest(v, this.oPC.fH[v]); } } setStartIndex(oWave, iDir, iStartPos, isPrefect = false) { oWave.setDirection(iDir); oWave.setPerfect(isPrefect); if (oWave.isUp()) { let v = this.getLowestLow(this.oPC.fL, iStartPos, oWave.get5StepsDateIndex()); oWave.setLowest(v, this.oPC.fL[v]); v = this.getHighestHigh(this.oPC.fH, v, oWave.get5StepsDateIndex()); oWave.setHighest(v, this.oPC.fH[v]); } else { let v = this.getHighestHigh(this.oPC.fH, iStartPos, oWave.get5StepsDateIndex()); oWave.setHighest(v, this.oPC.fH[v]); v = this.getLowestLow(this.oPC.fL, v, oWave.get5StepsDateIndex()); oWave.setLowest(v, this.oPC.fL[v]); } } setEndIndex(oWave, iEndPos = -1) { if (iEndPos == -1) iEndPos = this.oPC.getLastDay(); if (oWave.isDown()) { let v = this.getLowestLow(this.oPC.fL, oWave.get5StepsDateIndex(), iEndPos); oWave.setLowest(v, this.oPC.fL[v]); } else { let v = this.getHighestHigh(this.oPC.fH, oWave.get5StepsDateIndex(), iEndPos); oWave.setHighest(v, this.oPC.fH[v]); } } generateWaves(iStartIndex, iEndIndex = -1) { if (iEndIndex == -1) iEndIndex = this.oPC.getLastDay(); let iP = -1; let iStartPos = iStartIndex; // let iW = 0; for (var i = iStartIndex; i <= iEndIndex //&& this.iaWave.length < 10 ; i++) { let bHasBS = this.hasBuySignal(i); let bPerfectBS = this.bN.reduce(this.add) == 0; let bHasSS = this.hasSellSignal(i); let bPerfectSS = this.bN.reduce(this.add) == 0; // if (!bHasBS && !bHasSS) continue; // if ( !bPerfectBS && !bPerfectBS ) continue ; if (iP == -1 && (bHasBS || bHasSS)) { // first wave let oWave = this.newWave(i); if (bPerfectBS) { this.setStartIndexFirstTime(oWave, Wave_1.Wave.UP, iStartPos, true); oWave.setMinimum(this.oPC.fH[i]); // To cheat later that min target was met this.iaWave.push(oWave); } else if (bPerfectSS) { this.setStartIndexFirstTime(oWave, Wave_1.Wave.DN, iStartPos, true); oWave.setMinimum(this.oPC.fL[i]); // To cheat later that min target was met this.iaWave.push(oWave); } oWave.setName(this.sWaveName = this.getNextName(this.sWaveName)); iP = i; iStartPos = i; continue; } if (this.iaWave.length == 0) continue; let oPrev = this.iaWave[this.iaWave.length - 1]; this.setEndIndex(oPrev, i); // if ( oPrev.wasStep1Met() ) { let oWave = this.newWave(i); // console.log(this.oPC.fD[oPrev.getEndIndex()]); // console.log(oPrev.getJSON()); if (oPrev.isUp()) { if (bHasSS && bPerfectSS) { // console.log("PREV> ", oPrev.getJSON()); // console.log("Up Wave Previous:", oPrev.isUp(), bHasSS, bHasBS); oWave.setDirection(Wave_1.Wave.DN); oWave.setHighest(oPrev.getEndIndex(), oPrev.fHighest); this.setStartIndex(oWave, Wave_1.Wave.DN, oPrev.get5StepsDateIndex(), bPerfectSS); // console.log(oWave.getJSON()) this.iaWave.push(oWave); oWave.setName(this.sWaveName = this.getNextName(this.sWaveName)); iP = i; iStartPos = i; this.recalculateMinMaxTgt(); } } else { if (bHasBS && bPerfectBS) { // console.log("PREV> ", oPrev.getJSON()); // console.log("Up Wave Previous:", oPrev.isUp(), bHasSS, bHasBS); // this.setStartIndex(oWave, Wave.DN, oPrev.get5StepsDateIndex(), bPerfectSS); oWave.setDirection(Wave_1.Wave.UP); oWave.setLowest(oPrev.getEndIndex(), this.oPC.fL[oPrev.getEndIndex()]); this.setStartIndex(oWave, Wave_1.Wave.UP, oPrev.get5StepsDateIndex(), bPerfectBS); // console.log(oWave.getJSON()) this.iaWave.push(oWave); oWave.setName(this.sWaveName = this.getNextName(this.sWaveName)); iP = i; iStartPos = i; this.recalculateMinMaxTgt(); } } } let oLast = this.iaWave[this.iaWave.length - 1]; // if ( oLast.isUp() ) // { // if ( oLast.fLowest > this.oPC.fL[i]) // { // let oP:Wave = this.iaWave[this.iaWave.length - 2 ] ; // oP.setLowest(i, this.oPC.fL[i]) ; // // this.sWaveName = this.iaWave[this.iaWave.length - 1].reduceWaveNumber(); // // this.iaWave.splice(this.iaWave.length - 1); // this.recalculateMinMaxTgt(); // } // } // if ( oLast.isDown()) // { // if ( oLast.fHighest < this.oPC.fH[i]) // { // let oP:Wave = this.iaWave[this.iaWave.length - 2 ] ; // oP.setHighest(i, this.oPC.fH[i]) ; // // this.sWaveName = this.iaWave[this.iaWave.length - 1].reduceWaveNumber(); // // this.iaWave.splice(this.iaWave.length - 1); // this.recalculateMinMaxTgt(); // } // } // else { // if (this.iaWave.length > 2) { // // check for wave continuation // // in case of up wave, if current low is less than start low // // then the previous wave resumes // console.log(this.iaWave.length) ; // let oT: Wave = this.iaWave[this.iaWave.length - 2]; // console.log("** CHecking ", oT.getJSON(), this.oPC.fL[i], oT.fLowest, this.oPC.fH[i], oT.fHighest) // if (oT.isDown() && this.oPC.fL[i] < oT.fLowest) { // console.log("***** CHECK Resumption of Wave: ", this.iaWave.length); // oT.setLowest(i, oT.fLowest); // this.sWaveName = this.iaWave[this.iaWave.length - 1].reduceWaveNumber(); // this.iaWave.splice(this.iaWave.length - 1); // this.recalculateMinMaxTgt(); // } // if (oT.isUp() && this.oPC.fH[i] > oT.fHighest) { // console.log("***** CHECK Resumption of Wave: ", this.iaWave.length); // oT.setLowest(i, oT.fLowest); // this.sWaveName = this.iaWave[this.iaWave.length - 1].reduceWaveNumber(); // this.iaWave.splice(this.iaWave.length - 1); // this.recalculateMinMaxTgt(); // } // } // } // // } // if (this.iaWave.length > 1) { // let oPrev: Wave = this.iaWave[this.iaWave.length - 1]; // this.setEndIndex(oPrev) // } } console.log("Done."); this.writeJSON(); } writeJSON() { var sFileName = "./data/" + this.oPC.getSymbol() + "/" + IConstants_1.IConstants.sChartPeriods[this.iPC] + ".WAVES"; var oPW = fs.createWriteStream(sFileName); var s = ''; oPW.write('['); for (var i = 0; i < this.iaWave.length; i++) { oPW.write(s + this.iaWave[i].getJSON()); s = ','; } oPW.write(']'); oPW.close(); console.log("Created File:", sFileName); } } exports.WaveTerminator = WaveTerminator;