From f26c20423cb01538121fd952c2b3263aa31d07d1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 11 Jan 2020 18:38:54 +0100 Subject: [PATCH] fitfuction and refactoring --- Candles.js | 50 +++++++++++++++++++++++++++++++------------------- Volumes.js | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/Candles.js b/Candles.js index bebebe7d0..3d4abcc90 100644 --- a/Candles.js +++ b/Candles.js @@ -13,6 +13,7 @@ initialize: initialize, finalize: finalize, container: undefined, + fitFunction: undefined, getContainer: getContainer, setTimeFrame: setTimeFrame, setDatetime: setDatetime, @@ -34,7 +35,7 @@ thisObject.container = newContainer() thisObject.container.initialize(MODULE_NAME) - let timeLineCoordinateSystem = newTimeLineCoordinateSystem(); // Needed to be able to plot on the timeline, otherwise not. + let coordinateSystem = newCoordinateSystem(); // Needed to be able to plot on the timeline, otherwise not. let timeFrame; // This will hold the current Time Frame the user is at. let datetime; // This will hold the current Datetime the user is at. @@ -81,6 +82,7 @@ marketFile = undefined; fileCursor = undefined; + thisObject.fitFunction = undefined } catch (err) { if (ERROR_LOG === true) { logger.write("[ERROR] finalize -> err = " + err.stack); } @@ -242,7 +244,7 @@ y: candles[i].open }; - targetPoint = timeLineCoordinateSystem.transformThisPoint(targetPoint); + targetPoint = coordinateSystem.transformThisPoint(targetPoint); targetPoint = transformThisPoint(targetPoint, thisObject.container); let targetMax = { @@ -250,7 +252,7 @@ y: candles[i].max }; - targetMax = timeLineCoordinateSystem.transformThisPoint(targetMax); + targetMax = coordinateSystem.transformThisPoint(targetMax); targetMax = transformThisPoint(targetMax, thisObject.container); let targetMin = { @@ -258,7 +260,7 @@ y: candles[i].min }; - targetMin = timeLineCoordinateSystem.transformThisPoint(targetMin); + targetMin = coordinateSystem.transformThisPoint(targetMin); targetMin = transformThisPoint(targetMin, thisObject.container); let center = { @@ -295,7 +297,7 @@ y: lastClose }; - targetPoint = timeLineCoordinateSystem.transformThisPoint(targetPoint); + targetPoint = coordinateSystem.transformThisPoint(targetPoint); targetPoint = transformThisPoint(targetPoint, thisObject.container); let center = { @@ -385,8 +387,8 @@ let daysOnSides = getSideDays(timeFrame); - let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, timeLineCoordinateSystem); - let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, timeLineCoordinateSystem); + let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, coordinateSystem); + let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, coordinateSystem); let dateDiff = rightDate.valueOf() - leftDate.valueOf(); @@ -471,8 +473,8 @@ let daysOnSides = getSideDays(timeFrame); - let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, timeLineCoordinateSystem); - let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, timeLineCoordinateSystem); + let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, coordinateSystem); + let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, coordinateSystem); let dateDiff = rightDate.valueOf() - leftDate.valueOf(); @@ -529,7 +531,7 @@ try { - if (timeLineCoordinateSystem.maxValue > 0) { return; } // Already calculated. + if (coordinateSystem.maxValue > 0) { return; } // Already calculated. let minValue = { x: MIN_PLOTABLE_DATE.valueOf(), @@ -542,7 +544,7 @@ }; - timeLineCoordinateSystem.initialize( + coordinateSystem.initialize( minValue, maxValue, thisObject.container.frame.width, @@ -589,10 +591,10 @@ y: candle.close }; - candle.candlePoint1 = timeLineCoordinateSystem.transformThisPoint(candle.candlePoint1); - candle.candlePoint2 = timeLineCoordinateSystem.transformThisPoint(candle.candlePoint2); - candle.candlePoint3 = timeLineCoordinateSystem.transformThisPoint(candle.candlePoint3); - candle.candlePoint4 = timeLineCoordinateSystem.transformThisPoint(candle.candlePoint4); + candle.candlePoint1 = coordinateSystem.transformThisPoint(candle.candlePoint1); + candle.candlePoint2 = coordinateSystem.transformThisPoint(candle.candlePoint2); + candle.candlePoint3 = coordinateSystem.transformThisPoint(candle.candlePoint3); + candle.candlePoint4 = coordinateSystem.transformThisPoint(candle.candlePoint4); candle.candlePoint1 = transformThisPoint(candle.candlePoint1, thisObject.container); candle.candlePoint2 = transformThisPoint(candle.candlePoint2, thisObject.container); @@ -604,6 +606,11 @@ candle.candlePoint3 = viewPort.fitIntoVisibleArea(candle.candlePoint3); candle.candlePoint4 = viewPort.fitIntoVisibleArea(candle.candlePoint4); + candle.candlePoint1 = thisObject.fitFunction(candle.candlePoint1); + candle.candlePoint2 = thisObject.fitFunction(candle.candlePoint2); + candle.candlePoint3 = thisObject.fitFunction(candle.candlePoint3); + candle.candlePoint4 = thisObject.fitFunction(candle.candlePoint4); + candle.stickPoint1 = { x: candle.begin + timeFrame / 7 * 3.2, y: candle.max @@ -624,10 +631,10 @@ y: candle.min }; - candle.stickPoint1 = timeLineCoordinateSystem.transformThisPoint(candle.stickPoint1); - candle.stickPoint2 = timeLineCoordinateSystem.transformThisPoint(candle.stickPoint2); - candle.stickPoint3 = timeLineCoordinateSystem.transformThisPoint(candle.stickPoint3); - candle.stickPoint4 = timeLineCoordinateSystem.transformThisPoint(candle.stickPoint4); + candle.stickPoint1 = coordinateSystem.transformThisPoint(candle.stickPoint1); + candle.stickPoint2 = coordinateSystem.transformThisPoint(candle.stickPoint2); + candle.stickPoint3 = coordinateSystem.transformThisPoint(candle.stickPoint3); + candle.stickPoint4 = coordinateSystem.transformThisPoint(candle.stickPoint4); candle.stickPoint1 = transformThisPoint(candle.stickPoint1, thisObject.container); candle.stickPoint2 = transformThisPoint(candle.stickPoint2, thisObject.container); @@ -639,6 +646,11 @@ candle.stickPoint3 = viewPort.fitIntoVisibleArea(candle.stickPoint3); candle.stickPoint4 = viewPort.fitIntoVisibleArea(candle.stickPoint4); + candle.stickPoint1 = thisObject.fitFunction(candle.stickPoint1); + candle.stickPoint2 = thisObject.fitFunction(candle.stickPoint2); + candle.stickPoint3 = thisObject.fitFunction(candle.stickPoint3); + candle.stickPoint4 = thisObject.fitFunction(candle.stickPoint4); + } /* On screen candles */ diff --git a/Volumes.js b/Volumes.js index 83c35f53c..5b9e8327b 100644 --- a/Volumes.js +++ b/Volumes.js @@ -17,6 +17,7 @@ initialize: initialize, finalize: finalize, container: undefined, + fitFunction: undefined, getContainer: getContainer, setTimeFrame: setTimeFrame, setDatetime: setDatetime, @@ -29,8 +30,8 @@ thisObject.container = newContainer() thisObject.container.initialize(MODULE_NAME) - let timeLineCoordinateSystem = newTimeLineCoordinateSystem(); // Needed to be able to plot on the timeline, otherwise not. - let plotAreaFrame = newTimeLineCoordinateSystem(); // This chart uses this extra object. + let coordinateSystem = newCoordinateSystem(); // Needed to be able to plot on the timeline, otherwise not. + let plotAreaFrame = newCoordinateSystem(); // This chart uses this extra object. let timeFrame; // This will hold the current Time Frame the user is at. let datetime; // This will hold the current Datetime the user is at. @@ -79,6 +80,7 @@ marketFile = undefined; fileCursor = undefined; + thisObject.fitFunction = undefined } catch (err) { if (ERROR_LOG === true) { logger.write("[ERROR] finalize -> err = " + err.stack); } @@ -303,8 +305,8 @@ if (fileCursor.files.size === 0) { return; } // We need to wait until there are files in the cursor - let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, timeLineCoordinateSystem); - let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, timeLineCoordinateSystem); + let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, coordinateSystem); + let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, coordinateSystem); let dateDiff = rightDate.valueOf() - leftDate.valueOf(); @@ -384,8 +386,8 @@ if (marketFile === undefined) { return; } // Initialization not complete yet. - let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, timeLineCoordinateSystem); - let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, timeLineCoordinateSystem); + let leftDate = getDateFromPoint(viewPort.visibleArea.topLeft, thisObject.container, coordinateSystem); + let rightDate = getDateFromPoint(viewPort.visibleArea.topRight, thisObject.container, coordinateSystem); let dateDiff = rightDate.valueOf() - leftDate.valueOf(); @@ -441,7 +443,7 @@ x: MAX_PLOTABLE_DATE.valueOf() }; - timeLineCoordinateSystem.initializeX( + coordinateSystem.initializeX( minValue, maxValue, thisObject.container.frame.width @@ -475,7 +477,7 @@ maxValue.y = getMaxVolume() / (timeFrameRatio / 2.5); - timeLineCoordinateSystem.initializeY( + coordinateSystem.initializeY( minValue, maxValue, viewPort.visibleArea.bottomRight.y - viewPort.visibleArea.topLeft.y @@ -601,13 +603,13 @@ if (volumePointA1.y > viewPort.visibleArea.bottomLeft.y && frameHeightInViewPort > visibleHeight * 2 / 3) { - if (calculateBuys(timeLineCoordinateSystem, visibleHeight) === false) { continue; } // We snap t to the view port. + if (calculateBuys(coordinateSystem, visibleHeight) === false) { continue; } // We snap t to the view port. /* Now we set the real value of y. */ volumePointA1.y = viewPort.visibleArea.bottomRight.y; - volumePointA2.y = viewPort.visibleArea.bottomRight.y - volume.amountBuy * timeLineCoordinateSystem.scale.y; - volumePointA3.y = viewPort.visibleArea.bottomRight.y - volume.amountBuy * timeLineCoordinateSystem.scale.y; + volumePointA2.y = viewPort.visibleArea.bottomRight.y - volume.amountBuy * coordinateSystem.scale.y; + volumePointA3.y = viewPort.visibleArea.bottomRight.y - volume.amountBuy * coordinateSystem.scale.y; volumePointA4.y = viewPort.visibleArea.bottomRight.y; } @@ -655,13 +657,13 @@ if (volumePointB1.y < viewPort.visibleArea.topLeft.y && frameHeightInViewPort > visibleHeight * 2 / 3) { - calculateSells(timeLineCoordinateSystem, visibleHeight); // We snap it to the view port. + calculateSells(coordinateSystem, visibleHeight); // We snap it to the view port. /* Now we set the real value of y. */ volumePointB1.y = viewPort.visibleArea.topLeft.y; - volumePointB2.y = viewPort.visibleArea.topLeft.y + volume.amountSell * timeLineCoordinateSystem.scale.y; - volumePointB3.y = viewPort.visibleArea.topLeft.y + volume.amountSell * timeLineCoordinateSystem.scale.y; + volumePointB2.y = viewPort.visibleArea.topLeft.y + volume.amountSell * coordinateSystem.scale.y; + volumePointB3.y = viewPort.visibleArea.topLeft.y + volume.amountSell * coordinateSystem.scale.y; volumePointB4.y = viewPort.visibleArea.topLeft.y; } @@ -696,6 +698,16 @@ volumePointB3 = viewPort.fitIntoVisibleArea(volumePointB3); volumePointB4 = viewPort.fitIntoVisibleArea(volumePointB4); + volumePointA1 = thisObject.fitFunction(volumePointA1); + volumePointA2 = thisObject.fitFunction(volumePointA2); + volumePointA3 = thisObject.fitFunction(volumePointA3); + volumePointA4 = thisObject.fitFunction(volumePointA4); + + volumePointB1 = thisObject.fitFunction(volumePointB1); + volumePointB2 = thisObject.fitFunction(volumePointB2); + volumePointB3 = thisObject.fitFunction(volumePointB3); + volumePointB4 = thisObject.fitFunction(volumePointB4); + /* Everything must fit within the Frame. We know that that is equivalent to say that each bar con not be higher than the base of the opposite . */ if (volumePointA2.y < volumePointB1.y) {