From e3624984bac07d8da4abd4e0cfa41464c96f4aae Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 1 Apr 2020 13:59:51 +0200 Subject: [PATCH] base asset available --- Session.js | 14 ++++++++------ TradingSimulation.js | 46 ++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Session.js b/Session.js index 87756700e..f671d62aa 100644 --- a/Session.js +++ b/Session.js @@ -513,12 +513,14 @@ let socialBot = bot.SESSION.socialBots.bots[i] try { let code = announcement.code - if (socialBot.id === announcement.referenceParent.id) { - if (socialBot.type === "Telegram Bot") { - if (announcement.formulaValue !== undefined) { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) - } else { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, code.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) + if (announcement.referenceParent !== undefined) { + if (socialBot.id === announcement.referenceParent.id) { + if (socialBot.type === "Telegram Bot") { + if (announcement.formulaValue !== undefined) { + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) + } else { + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, code.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) + } } } } diff --git a/TradingSimulation.js b/TradingSimulation.js index 9e92d6502..9de9a0e51 100644 --- a/TradingSimulation.js +++ b/TradingSimulation.js @@ -173,6 +173,10 @@ let orderId = 0; let messageId = 0; + /* Allowing these to be accesible at formulas */ + baseAsset = bot.VALUES_TO_USE.baseAsset + quotedAsset = bot.VALUES_TO_USE.quotedAsset + let yesterday = {}; /* Initialization */ @@ -714,7 +718,7 @@ if (openStage !== undefined) { /* Default Values*/ - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { positionSize = balanceBaseAsset; positionRate = candle.close; } else { @@ -737,13 +741,13 @@ initialDefinition.positionSize.formula.error = err.message } if (isNaN(positionSize)) { - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { positionSize = balanceBaseAsset; } else { positionSize = balanceQuotedAsset; } } else { - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { if (positionSize > balanceBaseAsset) { positionSize = balanceBaseAsset } } else { if (positionSize > balanceQuotedAsset) { positionSize = balanceQuotedAsset } @@ -764,7 +768,7 @@ initialDefinition.positionRate.formula.error = err.message } if (isNaN(positionRate)) { - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { positionRate = candle.close; } else { positionRate = candle.close; @@ -1101,7 +1105,7 @@ let maximumBalance let balance - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { balance = balanceBaseAsset minimumBalance = bot.VALUES_TO_USE.minimumBalanceA maximumBalance = bot.VALUES_TO_USE.maximumBalanceA @@ -1695,7 +1699,7 @@ /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - if ((bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset && candle.max >= stopLoss) || (bot.VALUES_TO_USE.baseAsset !== bot.market.baseAsset && candle.min <= stopLoss)) { + if ((baseAsset === bot.market.baseAsset && candle.max >= stopLoss) || (baseAsset !== bot.market.baseAsset && candle.min <= stopLoss)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> loop -> Stop Loss was hit."); } /* @@ -1705,7 +1709,7 @@ If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { if (stopLoss < candle.min) { stopLoss = candle.min if (processingDailyFiles) { @@ -1730,7 +1734,7 @@ /* Apply the Slippage */ let slippageAmount = slippedStopLoss * bot.VALUES_TO_USE.slippage.stopLoss / 100 - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { slippedStopLoss = slippedStopLoss + slippageAmount } else { slippedStopLoss = slippedStopLoss - slippageAmount @@ -1765,7 +1769,7 @@ /* Take Profit condition: Here we verify if the Take Profit was hit or not. */ - if ((bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset && candle.min <= takeProfit) || (bot.VALUES_TO_USE.baseAsset !== bot.market.baseAsset && candle.max >= takeProfit)) { + if ((baseAsset === bot.market.baseAsset && candle.min <= takeProfit) || (baseAsset !== bot.market.baseAsset && candle.max >= takeProfit)) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> loop -> Take Profit was hit."); } /* @@ -1775,7 +1779,7 @@ If we take the stop loss value at those situation would be a huge distortion of facts. */ - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { if (takeProfit > candle.max) { takeProfit = candle.max if (processingDailyFiles) { @@ -1799,7 +1803,7 @@ /* Apply the Slippage */ let slippageAmount = slippedTakeProfit * bot.VALUES_TO_USE.slippage.takeProfit / 100 - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { slippedTakeProfit = slippedTakeProfit + slippageAmount } else { slippedTakeProfit = slippedTakeProfit - slippageAmount @@ -1860,7 +1864,7 @@ /* We take what was calculated at the formula and apply the slippage. */ let slippageAmount = tradePositionRate * bot.VALUES_TO_USE.slippage.positionRate / 100 - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { tradePositionRate = tradePositionRate - slippageAmount } else { tradePositionRate = tradePositionRate + slippageAmount @@ -1988,7 +1992,7 @@ let orderSide - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { orderSide = "sell" orderPrice = tradePositionRate - 100 // This is going to be ingnored at the Exchange API for now since we only put market orders. @@ -2082,7 +2086,7 @@ let feePaid = 0 - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { feePaid = tradePositionSize * tradePositionRate * bot.VALUES_TO_USE.feeStructure.taker / 100 @@ -2194,7 +2198,7 @@ let amountB let orderSide - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { orderSide = "buy" orderPrice = ticker.last + 100; // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. @@ -2276,7 +2280,7 @@ let feePaid = 0 - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { strategy.positionSize = balanceQuotedAsset / closeRate; strategy.positionRate = closeRate; @@ -2302,7 +2306,7 @@ } } - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { lastTradeProfitLoss = balanceBaseAsset - previousBalanceBaseAsset; lastTradeROI = lastTradeProfitLoss * 100 / tradePositionSize; if (isNaN(lastTradeROI)) { lastTradeROI = 0; } @@ -2349,7 +2353,7 @@ } } - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { ROI = (bot.VALUES_TO_USE.initialBalanceA + profit) / bot.VALUES_TO_USE.initialBalanceA - 1; hitRatio = hits / roundtrips; anualizedRateOfReturn = ROI / days * 365; @@ -2507,8 +2511,8 @@ balanceQuotedAsset = Number.MAX_SAFE_INTEGER } - let quotedBaseAsset = '"' + bot.VALUES_TO_USE.baseAsset + '"' - let quotedQuotedAsset = '"' + bot.VALUES_TO_USE.quotedAsset + '"' + let quotedBaseAsset = '"' + baseAsset + '"' + let quotedQuotedAsset = '"' + quotedAsset + '"' simulationRecord = { begin: candle.begin, @@ -2619,7 +2623,7 @@ currentTrade.endRate = candle.close /* Here we will calculate the ongoing ROI */ - if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) { + if (baseAsset === bot.market.baseAsset) { currentTrade.lastTradeROI = (tradePositionRate - candle.close) / tradePositionRate * 100 } else { currentTrade.lastTradeROI = (candle.close - tradePositionRate) / tradePositionRate * 100