From 61ceb0feed2f8be3185dfceb7736b2100f06592c Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 25 Oct 2020 12:18:31 +0100 Subject: [PATCH] Calculations Ready --- LowFrequencyTrading/OrdersCalculations.js | 45 ++++++++++++++------- LowFrequencyTrading/OrdersSimulations.js | 49 +++++++++++++++-------- LowFrequencyTrading/TradingOrders.js | 8 ++-- 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/LowFrequencyTrading/OrdersCalculations.js b/LowFrequencyTrading/OrdersCalculations.js index 4c985113c..d4f8e13d2 100644 --- a/LowFrequencyTrading/OrdersCalculations.js +++ b/LowFrequencyTrading/OrdersCalculations.js @@ -8,7 +8,7 @@ exports.newOrdersCalculations = function newOrdersCalculations(bot, logger) { actualSizeCalculation: actualSizeCalculation, actualRateCalculation: actualRateCalculation, feesToBePaidCalculation: feesToBePaidCalculation, - sizeToBeFilledCalculation: sizeToBeFilledCalculation, + amountReceivedCalculation: amountReceivedCalculation, finapercentageFilledCalculationlize: percentageFilledCalculation, feesPaidCalculation: feesPaidCalculation, sizeFilledCalculation: sizeFilledCalculation, @@ -205,19 +205,6 @@ exports.newOrdersCalculations = function newOrdersCalculations(bot, logger) { } } - async function sizeToBeFilledCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) { - /* - The Size to Be Filled is the Actual Size minus the Fees to be Paid. - */ - tradingEngineOrder.orderBaseAsset.sizeToBeFilled.value = - tradingEngineOrder.orderBaseAsset.actualSize.value - - tradingEngineOrder.orderBaseAsset.feesToBePaid.value - - tradingEngineOrder.orderQuotedAsset.sizeToBeFilled.value = - tradingEngineOrder.orderQuotedAsset.actualSize.value - - tradingEngineOrder.orderQuotedAsset.feesToBePaid.value - } - async function percentageFilledCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) { /* Percentage Filled Calculation: The only relevant information we get from the exchange is the order.filled. @@ -278,4 +265,34 @@ exports.newOrdersCalculations = function newOrdersCalculations(bot, logger) { tradingEngineOrder.orderQuotedAsset.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderQuotedAsset.sizeFilled.value, 10) } + + async function amountReceivedCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) { + /* + This calculation is for informational purposes, so that users do not have to calculate + it by themselves. The Amount Received out of the trade depends if we are Buying or Selling + the Base Asset. If we are Buying, then the Amount Receiced will be in Base Asset. If we + are selling the it will be in Quoted Asset. + */ + + switch (true) { + case tradingSystemOrder.type === 'Market Buy Order' || tradingSystemOrder.type === 'Limit Buy Order': { + /* + In this case the Amount Received is in Base Asset. + */ + tradingEngineOrder.orderBaseAsset.amountReceived.value.value = + tradingEngineOrder.orderBaseAsset.sizeFilled.value - + tradingEngineOrder.orderBaseAsset.feesPaid.value + break + } + case tradingSystemOrder.type === 'Market Sell Order' || tradingSystemOrder.type === 'Limit Sell Order': { + /* + In this case the Amount Received is in Quoted Asset. + */ + tradingEngineOrder.orderQuotedAsset.amountReceived.value.value = + tradingEngineOrder.orderQuotedAsset.sizeFilled.value - + tradingEngineOrder.orderQuotedAsset.feesPaid.value + break + } + } + } } \ No newline at end of file diff --git a/LowFrequencyTrading/OrdersSimulations.js b/LowFrequencyTrading/OrdersSimulations.js index 410f4d330..5e931f5f3 100644 --- a/LowFrequencyTrading/OrdersSimulations.js +++ b/LowFrequencyTrading/OrdersSimulations.js @@ -8,7 +8,7 @@ exports.newOrdersSimulations = function newOrdersSimulations(bot, logger) { actualSizeSimulation: actualSizeSimulation, actualRateSimulation: actualRateSimulation, feesToBePaidSimulation: feesToBePaidSimulation, - sizeToBeFilledSimulation: sizeToBeFilledSimulation, + amountReceivedSimulation: amountReceivedSimulation, percentageFilledSimulation: percentageFilledSimulation, feesPaidSimulation: feesPaidSimulation, sizeFilledSimulation: sizeFilledSimulation, @@ -184,19 +184,6 @@ exports.newOrdersSimulations = function newOrdersSimulations(bot, logger) { } } - async function sizeToBeFilledSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) { - /* - The Size to Be Filled is the Actual Size minus the Fees to be Paid. - */ - tradingEngineOrder.orderBaseAsset.sizeToBeFilled.value = - tradingEngineOrder.orderBaseAsset.actualSize.value - - tradingEngineOrder.orderBaseAsset.feesToBePaid.value - - tradingEngineOrder.orderQuotedAsset.sizeToBeFilled.value = - tradingEngineOrder.orderQuotedAsset.actualSize.value - - tradingEngineOrder.orderQuotedAsset.feesToBePaid.value - } - function percentageFilledSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) { /* When we are live, it is the exchange who knows which order was filled and in @@ -324,14 +311,44 @@ exports.newOrdersSimulations = function newOrdersSimulations(bot, logger) { */ tradingEngineOrder.orderBaseAsset.sizeFilled.value = - tradingEngineOrder.orderBaseAsset.sizeToBeFilled.value * + tradingEngineOrder.orderBaseAsset.amountReceived.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 tradingEngineOrder.orderQuotedAsset.sizeFilled.value = - tradingEngineOrder.orderQuotedAsset.sizeToBeFilled.value * + tradingEngineOrder.orderQuotedAsset.amountReceived.value * tradingEngineOrder.orderStatistics.percentageFilled.value / 100 tradingEngineOrder.orderBaseAsset.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderBaseAsset.sizeFilled.value, 10) tradingEngineOrder.orderQuotedAsset.sizeFilled.value = global.PRECISE(tradingEngineOrder.orderQuotedAsset.sizeFilled.value, 10) } + + async function amountReceivedSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) { + /* + This simulation is for informational purposes, so that users do not have to calculate + it by themselves. The Amount Received out of the trade depends if we are Buying or Selling + the Base Asset. If we are Buying, then the Amount Receiced will be in Base Asset. If we + are selling the it will be in Quoted Asset. + */ + + switch (true) { + case tradingSystemOrder.type === 'Market Buy Order' || tradingSystemOrder.type === 'Limit Buy Order': { + /* + In this case the Amount Received is in Base Asset. + */ + tradingEngineOrder.orderBaseAsset.amountReceived.value.value = + tradingEngineOrder.orderBaseAsset.sizeFilled.value - + tradingEngineOrder.orderBaseAsset.feesPaid.value + break + } + case tradingSystemOrder.type === 'Market Sell Order' || tradingSystemOrder.type === 'Limit Sell Order': { + /* + In this case the Amount Received is in Quoted Asset. + */ + tradingEngineOrder.orderQuotedAsset.amountReceived.value.value = + tradingEngineOrder.orderQuotedAsset.sizeFilled.value - + tradingEngineOrder.orderQuotedAsset.feesPaid.value + break + } + } + } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingOrders.js b/LowFrequencyTrading/TradingOrders.js index ec3eb78a2..47c2044c8 100644 --- a/LowFrequencyTrading/TradingOrders.js +++ b/LowFrequencyTrading/TradingOrders.js @@ -237,14 +237,14 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM if (tradingEngineOrder.orderBaseAsset.sizeFilled === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderBaseAsset.sizeFilled === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderBaseAsset.feesPaid === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderBaseAsset.feesPaid === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderBaseAsset.actualSize === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderBaseAsset.actualSize === undefined', tradingEngineOrder) } - if (tradingEngineOrder.orderBaseAsset.sizeToBeFilled === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderBaseAsset.sizeToBeFilled === undefined', tradingEngineOrder) } + if (tradingEngineOrder.orderBaseAsset.amountReceived === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderBaseAsset.amountReceived === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderBaseAsset.feesToBePaid === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderBaseAsset.feesToBePaid === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderQuotedAsset.size === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.size === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderQuotedAsset.sizeFilled === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.sizeFilled === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderQuotedAsset.feesPaid === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.feesPaid === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderQuotedAsset.actualSize === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.actualSize === undefined', tradingEngineOrder) } - if (tradingEngineOrder.orderQuotedAsset.sizeToBeFilled === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.sizeToBeFilled === undefined', tradingEngineOrder) } + if (tradingEngineOrder.orderQuotedAsset.amountReceived === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.amountReceived === undefined', tradingEngineOrder) } if (tradingEngineOrder.orderQuotedAsset.feesToBePaid === undefined) { badDefinitionUnhandledException(undefined, 'tradingEngineOrder.orderQuotedAsset.feesToBePaid === undefined', tradingEngineOrder) } } @@ -554,10 +554,10 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM ordersSimulationsModule.actualSizeSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) ordersSimulationsModule.actualRateSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) ordersSimulationsModule.feesToBePaidSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) - ordersSimulationsModule.sizeToBeFilledSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) ordersSimulationsModule.percentageFilledSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) ordersSimulationsModule.feesPaidSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) ordersSimulationsModule.sizeFilledSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) + ordersSimulationsModule.amountReceivedSimulation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, applyFeePercentage) ordersSimulationsModule.finalize() @@ -586,10 +586,10 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM await ordersCalculationsModule.actualSizeCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) await ordersCalculationsModule.actualRateCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) await ordersCalculationsModule.feesToBePaidCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) - await ordersCalculationsModule.sizeToBeFilledCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) await ordersCalculationsModule.percentageFilledCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) await ordersCalculationsModule.feesPaidCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) await ordersCalculationsModule.sizeFilledCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) + await ordersCalculationsModule.amountReceivedCalculation(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, order, applyFeePercentage) ordersCalculationsModule.finalize()