Merge changes

This commit is contained in:
Matias Benitez committed 2018-08-16 15:10:47 -03:00
1 parent 0c7df9e08b
commit f5135b8df4
11 files changed
+224 -68

No files matched your search

+12 -12
View File
@@ -487,7 +487,12 @@
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> positionFound -> Entering function."); }
/*
/* We update the final amountA based on what we got from the exchange.
This is needed since the values could be different from what we calculate.
*/
position.amountA = exchangePosition.amountA;
/*
We need to know if the order was partially executed. To know that, we compare the amounts on file with the ones
received from the exchange. If they are the same, then the order is intact. Otherwise, to confirm a partial execution,
@@ -497,8 +502,7 @@
if (position.amountB === exchangePosition.amountB) {
/* Position is still there, untouched. We update the final amountA on exchange. */
position.amountA = exchangePosition.amountA;
/* Position is still there, untouched. Nothing to do here. */
next();
return;
@@ -558,10 +562,7 @@
if (thisPosition.id === pPositionId) {
let feeRate = 0.0025; // Default fee
if (Math.random(1) < 0.5)
feeRate = 0.0015; // Some times we will pay less fee
let feeRate = 0.002; // Default backtesting fee simulation
let trade = {
id: Math.trunc(Math.random(1) * 1000000),
@@ -630,7 +631,6 @@
callBack(global.DEFAULT_FAIL_RESPONSE);
}
}
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] ordersExecutionCheck -> loopBody -> getPositionTradesAtExchange -> err = " + err.message);
callBack(global.DEFAULT_FAIL_RESPONSE);
@@ -749,9 +749,9 @@
let exchangeParam = exchangeAPI.getMaxDecimalPositions();
let minValue = '0.' + (1).toPrecision(exchangeParam-1).split('.').reverse().join('');
let decimalPositions = parseFloat(parseFloat(minValue).toFixed(exchangeParam));
let exchangePrecision = parseFloat(parseFloat(minValue).toFixed(exchangeParam));
if (Math.abs(position.amountA - sumAssetA) > decimalPositions || Math.abs(position.amountB - sumAssetB) > decimalPositions) {
if (Math.abs(position.amountA - sumAssetA) > exchangePrecision || Math.abs(position.amountB - sumAssetB) > exchangePrecision) {
logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> position.amountA = " + position.amountA);
logger.write(MODULE_NAME, "[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasPartiallyExecuted -> sumAssetA = " + sumAssetA);
@@ -857,8 +857,8 @@
context.executionContext.balance.assetA = thisObject.truncDecimals(context.executionContext.balance.assetA + assetA);
context.executionContext.balance.assetB = thisObject.truncDecimals(context.executionContext.balance.assetB - assetB);
let calculatedBalance =
context.executionContext.availableBalance.assetA = thisObject.truncDecimals(context.executionContext.availableBalance.assetA + assetA);
context.executionContext.availableBalance.assetA = thisObject.truncDecimals(context.executionContext.availableBalance.assetA + assetA);
/* Not the available balance for asset B is not affected since it was already reduced when the order was placed. */
+9 -5
View File
@@ -70,15 +70,18 @@
let runIndex; // This is the index for this run and depends on the startMode.
let totalAlgobots;
return thisObject;
function initialize(pStatusDependencies, callBackFunction) {
function initialize(pStatusDependencies, pTotalAlgobots, callBackFunction) {
try {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
statusDependencies = pStatusDependencies;
totalAlgobots = pTotalAlgobots;
/*
@@ -114,8 +117,6 @@
case global.DEFAULT_OK_RESPONSE.result: {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> onDone -> Execution finished well."); }
bot.hasTheBotJustStarted = false;
callBackFunction(global.DEFAULT_OK_RESPONSE);
return;
}
@@ -328,8 +329,8 @@
once we advance into multiple competitions scheme.
*/
const INITIAL_INVESTMENT_A = 0; // This is just for this release of the platform.
const INITIAL_INVESTMENT_B = .002; // This is just for this release of the platform.
const INITIAL_INVESTMENT_A = 0; // This is just for this release of the platform.
const INITIAL_INVESTMENT_B = .002 / totalAlgobots; // This is just for this release of the platform.
thisObject.executionContext = {
investment: { // This is used to calculate profits.
@@ -384,6 +385,9 @@
switch (err.result) {
case global.DEFAULT_OK_RESPONSE.result: {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] saveThemAll -> onDone -> Execution finished well."); }
bot.hasTheBotJustStarted = false;
callBackFunction(global.DEFAULT_OK_RESPONSE);
return;
}
+13 -2
View File
@@ -39,6 +39,12 @@
<Compile Include="DataSet.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="exchange\exchangeUtils.js" />
<Compile Include="exchange\wrappers\beautifier.js" />
<Compile Include="exchange\wrappers\binance.js" />
<Compile Include="exchange\wrappers\binanceClient.js" />
<Compile Include="exchange\wrappers\poloniex.js" />
<Compile Include="exchange\wrappers\poloniexClient.js" />
<Compile Include="Root.js">
<SubType>Code</SubType>
</Compile>
@@ -52,13 +58,12 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="DebugLog.js" />
<Compile Include="ExchangeAPI.js">
<Compile Include="exchange\ExchangeAPI.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="IndicatorBotProcessMainLoop.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="PoloniexAPIClient.js" />
<Compile Include="Run.js" />
<Compile Include="StatusReport.js">
<SubType>Code</SubType>
@@ -70,8 +75,14 @@
</ItemGroup>
<ItemGroup>
<Content Include=".gitignore" />
<Content Include="exchange\wrappers\binanceProperties.json" />
<Content Include="exchange\wrappers\poloniexProperties.json" />
<Content Include="this.config.json" />
</ItemGroup>
<ItemGroup>
<Folder Include="exchange\" />
<Folder Include="exchange\wrappers\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<!--Do not delete the following Import Project. While this appears to do nothing it is a marker for setting TypeScript properties before our import that depends on them.-->
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="False" />
+138 -32
View File
@@ -386,7 +386,7 @@ exports.newRoot = function newRoot() {
}
/* We tesst each type of start Mode to get what to run and how. */
/* We test each type of start Mode to get what to run and how. */
if (processConfig.startMode.allMonths !== undefined) {
@@ -756,57 +756,163 @@ exports.newRoot = function newRoot() {
function runTradingBot(pBotConfig, pProcessConfig) {
try {
const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog');
let logger;
logger = DEBUG_MODULE.newDebugLog();
logger.bot = pBotConfig;
if (FULL_LOG === true) { console.log(logDisplace + "[INFO] start -> findProcess -> runTradingBot -> Entering function."); }
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> findProcess -> runTradingBot -> Entering function."); }
if (pBotConfig.genes !== undefined) {
let tradingBotMainLoop = TRADING_BOT_MAIN_LOOP_MODULE.newTradingBotProcessMainLoop(pBotConfig, logger);
tradingBotMainLoop.initialize(UI_COMMANDS, pProcessConfig, onInitializeReady);
/* Here, based on the gene we will create one instance for each combination of genes */
function onInitializeReady(err) {
let valueMatrix = [];
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
for (let i = 0; i < pBotConfig.genes.length; i++) {
tradingBotMainLoop.run(whenRunFinishes);
let gene = pBotConfig.genes[i];
function whenRunFinishes(err) {
let possibleValues = [];
pBotConfig.loopCounter = 0;
for (let j = gene.lowerLimit; j <= gene.upperLimit; j++) {
let botId;
possibleValues.push(j);
botId = pBotConfig.devTeam + "." + pBotConfig.codeName + "." + pBotConfig.process;
}
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
valueMatrix.push(possibleValues);
logger.write(MODULE_NAME, "[INFO] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Bot execution finished sucessfully.");
logger.write(MODULE_NAME, "[INFO] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Bot Id = " + botId);
}
console.log(logDisplace + "Root : [INFO] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> botId = " + botId);
console.log(logDisplace + "Root : [INFO] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Bot execution finished sucessfully.");
logger.persist();
/*
Now we have all the possible genes values in a multi-dimensional matrix.
We will go thorugh each of the elements of each dimension and get each combination possible with the other dimmensions.
*/
let combinations = [];
let combination = [];
let dimensionIndex = 0;
calculateCombinations(dimensionIndex, combination);
function calculateCombinations(pDimensionIndex, pCombination) {
let dimension = valueMatrix[pDimensionIndex];
for (let j = 0; j < dimension.length; j++) {
let copy = JSON.parse(JSON.stringify(pCombination));
let value = dimension[j];
copy.push(value);
if (pDimensionIndex < valueMatrix.length - 1) {
calculateCombinations(pDimensionIndex + 1, copy);
} else {
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> err = " + err.message);
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Execution will be stopped. ");
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Bye.");
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Bot Id = " + botId);
console.log(logDisplace + "Root : [ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> whenStartFinishes -> Bot execution finished with errors. Please check the logs.");
logger.persist();
combinations.push(copy);
}
}
}
} else {
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> err = " + err.message);
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> Bot will not be started. ");
console.log(logDisplace + "Root : [ERROR] start -> findProcess -> runTradingBot -> onInitializeReady -> err = " + err.message);
/* At this point we have an array with all the possible combinations, we just need to create an instance for each one and set the genes according to that. */
logger.persist();
for (let i = 0; i < combinations.length; i++) {
let botConfig = JSON.parse(JSON.stringify(pBotConfig));
let combination = combinations[i];
let genes = {};
let clonKey = "";
for (let j = 0; j < botConfig.genes.length; j++) {
genes[botConfig.genes[j].name] = combination[j];
clonKey = clonKey + "." + combination[j];
}
let clonName = "Clon" + clonKey;
botConfig.filePathRoot = botConfig.devTeam + "/" + botConfig.codeName + "-" + clonName + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + botConfig.dataSetVersion;
botConfig.instance = "Clon" + clonKey;
botConfig.instanceIndex = i;
setTimeout(execute, i * Math.random() * 10 * 1000);
function execute() {
createBotInstance(genes, combinations.length, botConfig);
}
}
} else {
/* If the bot does not have any genes at all */
let genes = {};
pBotConfig.instance = "Master";
pBotConfig.instanceIndex = 0;
createBotInstance(genes, 1, pBotConfig);
}
function createBotInstance(pGenes, pTotalInstances, pBotConfig) {
const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog');
let logger;
logger = DEBUG_MODULE.newDebugLog();
logger.bot = pBotConfig;
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> findProcess -> runTradingBot -> createBotInstance -> Entering function."); }
let tradingBotMainLoop = TRADING_BOT_MAIN_LOOP_MODULE.newTradingBotProcessMainLoop(pBotConfig, logger);
tradingBotMainLoop.initialize(UI_COMMANDS, pProcessConfig, onInitializeReady);
function onInitializeReady(err) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
tradingBotMainLoop.run(pGenes, pTotalInstances, whenRunFinishes);
function whenRunFinishes(err) {
pBotConfig.loopCounter = 0;
let botId;
botId = pBotConfig.devTeam + "." + pBotConfig.codeName + "." + pBotConfig.instance + "." + pBotConfig.process;
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[INFO] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Bot execution finished sucessfully.");
logger.write(MODULE_NAME, "[INFO] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Bot Id = " + botId);
console.log(logDisplace + "Root : [INFO] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> botId = " + botId);
console.log(logDisplace + "Root : [INFO] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Bot execution finished sucessfully.");
logger.persist();
} else {
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> err = " + err.message);
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Execution will be stopped. ");
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Bye.");
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Bot Id = " + botId);
console.log(logDisplace + "Root : [ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> whenStartFinishes -> Bot execution finished with errors. Please check the logs.");
logger.persist();
}
}
} else {
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> err = " + err.message);
logger.write(MODULE_NAME, "[ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> Bot will not be started. ");
console.log(logDisplace + "Root : [ERROR] start -> findProcess -> runTradingBot -> createBotInstance -> onInitializeReady -> err = " + err.message);
logger.persist();
}
}
}
}
+2 -2
View File
@@ -97,10 +97,10 @@ function readExchangeAPIKey() {
console.log("[HINT] You need to have a file at this path -> " + filePath);
global.EXCHANGE_KEYS = {
Poloniex: {
Poloniex: [{
Key: "",
Secret: ""
}
}]
}
startRoot();
+17 -3
View File
@@ -88,10 +88,24 @@
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> load -> Entering function."); }
let rootPath = owner.devTeam + "/" + owner.bot + "." + owner.botVersion.major + "." + owner.botVersion.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + owner.dataSetVersion;
let fileName = "Status.Report." + global.MARKET.assetA + '_' + global.MARKET.assetB + ".json";
let filePath = rootPath + "/Reports/" + owner.process + timePath;
let filePath;
let ownerId = owner.devTeam + "-" + owner.bot + "-" + owner.botVersion.major + "-" + owner.botVersion.minor + "-" + owner.process + "-" + owner.dataSetVersion;
let botId = bot.devTeam + "-" + bot.codeName + "-" + bot.version.major + "-" + bot.version.minor + "-" + bot.process + "-" + bot.dataSetVersion;
if (ownerId !== botId) {
let rootPath = owner.devTeam + "/" + owner.bot + "." + owner.botVersion.major + "." + owner.botVersion.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + owner.dataSetVersion;
filePath = rootPath + "/Reports/" + owner.process + timePath;
} else {
filePath = bot.filePathRoot + "/Reports/" + owner.process + timePath;
}
if (bot.type === 'Trading') {
fileName = bot.startMode + "." + fileName;
}
+6 -6
View File
@@ -132,7 +132,7 @@
}
}
function run(callBackFunction) {
function run(pGenes, pTotalAlgobots, callBackFunction) {
try {
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] run -> Entering function."); }
@@ -160,7 +160,7 @@
return str.length < max ? pad(" " + str, max) : str;
}
console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Entered into Main Loop # " + pad(Number(bot.loopCounter) + 1, 8));
console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.instance, 20) + " " + pad(bot.process, 30) + " Entered into Main Loop # " + pad(Number(bot.loopCounter) + 1, 8));
/* For each loop we want to create a new log file. */
@@ -535,7 +535,7 @@
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> initializeContext -> Entering function."); }
context = CONTEXT.newContext(bot, logger, BLOB_STORAGE, UTILITIES, STATUS_REPORT);
context.initialize(statusDependencies, onInizialized);
context.initialize(statusDependencies, pTotalAlgobots, onInizialized);
function onInizialized(err) {
@@ -756,7 +756,7 @@
usertBot = USER_BOT_MODULE.newUserBot(bot, logger, COMMONS_MODULE);
usertBot.initialize(assistant, onInizialized);
usertBot.initialize(assistant, pGenes, onInizialized);
function onInizialized(err) {
@@ -1058,7 +1058,7 @@
if (now - bot.loopStartTime > processConfig.normalWaitTime) {
logger.write(MODULE_NAME, "[ERROR] run -> loop -> checkLoopHealth -> Dead loop found -> pLastLoop = " + pLastLoop);
console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now."));
console.log((new Date().toISOString() + " [ERROR] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.instance, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found dead. Resurrecting it now."));
logger.persist(); // We persist the logs of the failed execution.
clearTimeout(nextLoopTimeoutHandle); // We cancel the timeout in case the original loop was still running and schedulled to reexecute.
@@ -1066,7 +1066,7 @@
} else {
console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken."));
console.log((new Date().toISOString() + " [WARN] run -> loop -> checkLoopHealth -> " + pad(bot.codeName, 20) + " " + pad(bot.instance, 20) + " " + pad(bot.process, 30) + " Loop # " + pad(Number(bot.loopCounter), 5) + " found delayed but still alive. No action taken."));
}
}
+4 -3
View File
@@ -31,7 +31,8 @@
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
let exchange = bot.products[0].exchangeList[0].name.toLowerCase() + 'Client.js'; //TODO Improve
//let exchange = bot.products[0].exchangeList[0].name.toLowerCase() + 'Client.js'; //TODO Define
let exchange = 'binanceClient.js'; //TODO Define
let api = require('./wrappers/' + exchange);
apiClient = api.newAPIClient(global.EXCHANGE_KEYS[global.EXCHANGE_NAME].Key, global.EXCHANGE_KEYS[global.EXCHANGE_NAME].Secret, logger);
@@ -168,8 +169,8 @@
let check = isValidOrder({
market: getMarketConfig(),
api: apiClient,
pAmountB: truncDecimals(pAmountB),
pRate: truncDecimals(pRate)
amount: truncDecimals(pAmountB),
price: truncDecimals(pRate)
});
if (check.valid) {
+10 -1
View File
@@ -83,9 +83,18 @@ const includes = function(str, list) {
return _.some(list, item => str.includes(item));
}
const getMarketConfig = function(exchangeProperties) {
return _.find(exchangeProperties.markets, (p) => {
return _.first(p.pair) === global.MARKET.assetA.toUpperCase() &&
_.last(p.pair) === global.MARKET.assetB.toUpperCase();
});
}
module.exports = {
retry: retryInstance,
includes: includes,
isValidOrder
isValidOrder,
getMarketConfig
}
+11 -1
View File
@@ -44,7 +44,9 @@
sell: sell,
movePosition: movePosition,
getPublicTradeHistory: getPublicTradeHistory,
getExchangeProperties: getExchangeProperties
getExchangeProperties: getExchangeProperties,
isValidPrice: isValidPrice,
isValidLot: isValidLot
};
return thisObject;
@@ -65,6 +67,14 @@
return exchangeProperties;
}
function truncDecimals(pFloatValue) {
let decimals = 6;
return parseFloat(parseFloat(pFloatValue).toFixed(decimals));
}
/*
* Returns the price for a given pair of assets.
* The object returned is an array of trades:
+2 -1
View File
@@ -58,7 +58,8 @@
sell: sell,
movePosition: movePosition,
getPublicTradeHistory: getPublicTradeHistory,
getExchangeProperties: getExchangeProperties
getExchangeProperties: getExchangeProperties,
isValidLot: isValidLot
};
return thisObject;