sql-synergy
Version:
Synergy Wave TA
351 lines (350 loc) • 15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WaveOrganizer = void 0;
const WaveList_1 = require("./WaveList");
const Wave_1 = require("./Wave");
// import { IWave } from './IWave' ;
// import { LinkedList } from 'linked-list-typescript';
class WaveOrganizer {
constructor(oWS) {
this.vRemove = new Array();
this.oWaveChanged = null;
this.oWS = oWS;
}
static setWaveList(oWL) {
WaveOrganizer.oWL = oWL;
}
recalculateMinMaxTgt(oWave0) {
// let oLI:IterableIterator<Wave> | undefined = this.oWS?.getIterator ( ) ;
let bFound = false;
let oWaves = this.oWS.oLL.toArray();
let n = oWaves.length;
for (var c = 0; c < n; c++) {
// let oW:Wave = oLI?.next ( ).value ;
let oW = oWaves[c];
if (oW == oWave0 || bFound == true) {
// let oWave2:Wave = oLI?.previous ( ) ;
let oWave2 = (c > 0) ? oWaves[c - 1] : null;
if (c > 0) {
// let oWave1:Wave = oLI?.previous ( ) ;
let oWave1 = oWaves[c - 1];
this.setMinMax(oWave0, oWave1, oWave2);
bFound = true;
c++;
// oLI?.next ( ) ;
}
c++;
// oLI?.next ( ) ;
}
}
}
getPreviousWave(oWave0) {
if (oWave0 == null)
return null;
let oPW = null;
let sParent = oWave0.getParent();
if (sParent == null)
return null;
//let oWave:Wave = WaveOrganizer.oWL.get ( sParent ) ;
let s = WaveOrganizer.oWL.getWavesString();
oPW = null;
for (var i = 0; i < s.length; i++) {
let oW = WaveOrganizer.oWL.get(s[i]);
if (oW?.getName() === sParent)
break;
oPW = oW;
}
if (oPW == null)
return null;
if (oPW.getName().length != oWave0.getName().length) {
console.log("Invalid Previous Wave for: [" + oWave0.getName() + "] Got: [" + oPW.getName() + "]");
return null;
}
console.log("LastWave: ");
oPW.print();
return oPW;
}
setMinMax(oWave0, oWave1, oWave2) {
if (oWave1 == null) {
console.log("Change of Wave: " + oWave0.getName());
let oPrevSameRankWave = 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");
}
}
}
recomputeMinMax() {
// let oLI:IterableIterator<Wave> | undefined = this.oWS?.getIterator ( ) ;
let oWave0 = null, oWave1 = null, oWave2 = null;
// for (var z = this.oWS?.size(); z !== undefined && z > 0; z--)
let oWaves = this.oWS.oLL.toArray();
let n = oWaves.length;
for (var z = 0; z < n; z++) {
oWave2 = oWave1;
oWave1 = oWave0;
oWave0 = oWaves[z];
this.setMinMax(oWave0, oWave1, oWave2);
}
}
adjustWaves() {
let n = this.vRemove.length;
// let oI:IterableIterator<Wave>|undefined = this.oWS?.getIterator ( ) ;
let iWL = -1;
let oWaves = this.oWS.oLL.toArray();
// let n = oWaves.length ;
for (var z = 0; z < n; z++)
// for (var z = this.oWS?.size(); z !== undefined && z > 0; z--)
{
let oFirstWave = oWaves[z];
if (iWL == -1)
iWL = oFirstWave.getName().length - 1;
this.oWS.setWaveNameSize(iWL);
}
for (var i = 0; i < n; i++) {
let oWWW = this.vRemove[i];
this.oWS.remove(oWWW);
console.log("Removing: ");
oWWW?.print();
}
this.vRemove = [];
if (this.oWaveChanged != null)
this.recalculateMinMaxTgt(this.oWaveChanged);
this.oWaveChanged = null;
}
adjustMinMaxValues() {
let oWave0 = null, oWave1 = null, oWave2 = null;
let oWaves = this.oWS.oLL.toArray();
let n = oWaves.length;
for (var i = 0; i < n; i++) {
oWave2 = oWave1;
oWave1 = oWave0;
oWave0 = oWaves[i];
this.setMinMax(oWave0, oWave1, oWave2);
}
}
reorganize() {
let oWave0 = null, oWave1 = null, oWave2 = null;
let oWaves = this.oWS.oLL.toArray();
let n = oWaves.length;
for (var i = 0; i < n; i++) {
oWave2 = oWave1;
oWave1 = oWave0;
oWave0 = oWaves[i];
this.setMinMax(oWave0, oWave1, oWave2);
if (oWave1 == null && oWave0 != null) {
//Step1 Neutralization Policy
//Waves that ends with 1 Step1 need not be met
//Waves that ends with 2 & 4 Step1 need not be met
if (oWave0.wasStep1Met() == false) {
if ((i + 1) < n) {
oWave0.addSmallerWave(Wave_1.Wave.copy(oWave0));
i++;
let oTWave = oWaves[i];
oWave0.addSmallerWave(oTWave);
this.vRemove.push(oTWave);
}
if ((i + 1) < n) {
i++;
let oTWave = oWaves[i];
oWave0.addSmallerWave(oTWave);
if (oWave0.isUp()) {
if (oWave0.getHigh() < oTWave.getHigh())
oWave0.setHighest(oTWave.getHighIndex(), oTWave.getHigh());
}
else {
if (oWave0.getLow() > oTWave.getLow())
oWave0.setLowest(oTWave.getLowIndex(), oTWave.getLow());
}
this.vRemove.push(oTWave);
}
if (this.vRemove.length == 0)
return true;
this.oWaveChanged = oWave0;
return false;
}
}
if (oWave2 == null || oWave1 == null || oWave0 == null) {
continue;
}
if (oWave1.wasStep1Met() == false) {
if (oWave0.isUp() && oWave0?.isImpulsive()) {
if (oWave1.isPerfect() == false) {
if (oWave0.getHigh() > oWave1.getHigh()) {
//oWave2 Resumes
oWave2.addSmallerWave(Wave_1.Wave.copy(oWave2));
oWave2.addSmallerWave(oWave1);
oWave2.addSmallerWave(oWave0);
oWave2.setHighest(oWave0.getHighIndex(), oWave0.getHigh());
this.oWaveChanged = oWave2;
this.vRemove.push(oWave0);
this.vRemove.push(oWave1);
return false;
}
else { // Rename to smaller wave
oWave2.addSmallerWave(Wave_1.Wave.copy(oWave2));
oWave2.addSmallerWave(oWave1);
oWave2.addSmallerWave(oWave0);
this.vRemove.push(oWave0);
this.vRemove.push(oWave1);
oWave0.bZigZag = true;
return false;
}
}
}
if (oWave0.isDown() && oWave0.isImpulsive()) {
if (oWave1.isPerfect() == false) {
if (oWave0.getLow() < oWave1.getLow()) {
oWave2.addSmallerWave(Wave_1.Wave.copy(oWave2));
oWave2.addSmallerWave(oWave1);
oWave2.addSmallerWave(oWave0);
oWave2.setLowest(oWave0.getLowIndex(), oWave0.getLow());
this.oWaveChanged = oWave2;
this.vRemove.push(oWave0);
this.vRemove.push(oWave1);
return false;
}
else {
this.oWaveChanged = oWave2;
oWave2.addSmallerWave(Wave_1.Wave.copy(oWave2));
oWave2.addSmallerWave(oWave1);
oWave2.addSmallerWave(oWave0);
this.vRemove.push(oWave0);
this.vRemove.push(oWave1);
oWave0.bZigZag = true;
return false;
}
}
}
if (oWave0.isUp() && oWave0.isCorrective()) {
if ((i + 1) < n) {
i++;
let oNextWave = oWaves[i];
oWave1.addSmallerWave(Wave_1.Wave.copy(oWave1));
oWave1.addSmallerWave(oWave0);
oWave1.addSmallerWave(oNextWave);
// console.log ( "*** B4M Wave1: " ) ;
// oWave1.print ( ) ;
if (oWave1.getLow() > oNextWave.getLow()) {
oWave1.setLowest(oNextWave.getLowIndex(), oNextWave.getLow());
// console.log ( "*** AFR Wave1: " ) ;
// oWave1.print ( ) ;
this.oWaveChanged = oWave1;
this.vRemove.push(oWave0);
this.vRemove.push(oNextWave);
return false;
}
this.oWaveChanged = oWave1;
this.vRemove.push(oWave0);
this.vRemove.push(oNextWave);
return false;
}
//TODO Last ??
// console.log ( "TODO: Prev Wave Min Not Met and Current wave is Corrective Up Wave. This is the last wave" ) ;
oWave1.addSmallerWave(Wave_1.Wave.copy(oWave1));
oWave1.addSmallerWave(oWave0);
this.vRemove.push(oWave0);
// oWave1.setLowest ( oWave0.getLowIndex ( ), oWave0.getLow ( ) ) ;
return false;
}
if (oWave0.isDown() && oWave0.isCorrective()) {
if ((i + 1) < n) {
i++;
let oNextWave = oWaves[i];
oWave1.addSmallerWave(Wave_1.Wave.copy(oWave1));
oWave1.addSmallerWave(oWave0);
oWave1.addSmallerWave(oNextWave);
//console.log ( "*** B4M Wave1: " ) ;
//oWave1.print ( ) ;
if (oWave1.getHigh() < oNextWave.getHigh()) {
oWave1.setHighest(oNextWave.getHighIndex(), oNextWave.getHigh());
//console.log ( "*** AFR Wave1: " ) ;
//oWave1.print ( ) ;
this.oWaveChanged = oWave1;
this.vRemove.push(oWave0);
this.vRemove.push(oNextWave);
return false;
}
this.oWaveChanged = oWave1;
this.vRemove.push(oWave0);
this.vRemove.push(oNextWave);
return false;
}
//TODO Last ??
// console.log ( "TODO: Prev Wave Min Not Met and Current wave is Corrective Down Wave. This is the last wave" ) ;
oWave1.addSmallerWave(Wave_1.Wave.copy(oWave1));
oWave1.addSmallerWave(oWave0);
this.vRemove.push(oWave0);
return false;
}
}
}
return true;
}
}
exports.WaveOrganizer = WaveOrganizer;
// main ( )
// {
// let oWR:WaveReader = new WaveReader ( "e:/dev1/ScanOK/src/synergy/wavetheory/70001123Normal.csv" ) ;
// let oWS:WaveSequence = oWR.getWaveSequence ( ) ;
// oWS.print ( ) ;
// console.log ( "**********" ) ;
// let oWO:WaveOrganizer = new WaveOrganizer ( oWS ) ;
// while ( oWO.reorganize ( ) == false )
// {
// oWO.adjustWaves ( ) ;
// oWS.print ( ) ;
// console.log ( "**********" ) ;
// }
// oWS.print ( ) ;
// }
WaveOrganizer.oWL = new WaveList_1.WaveList();