Mida
Mida copied to clipboard
objects are null in console.log
This is a funny thing I've noticed in different places. Might be the fact that I have your library in Nrwl NX, or maybe have an older node version?
account.on("order", (event) => {
const order = event.descriptor.order;
console.log(order);
});
console.log outputs CTraderOrder {} when I place an order.
But if I go for instance console.log(order.id, order.direction); i get what is there.
How is this possible? I did not see anything like this ever. :D
It's because all Mida objects are immutable ES6 class instances, and all properties are actually getters (apart from methods). Node.js is currently unable to log getters since they are not enumerable so you will see only something like MidaBrokerOrder {}. I'm already working on this, it's good you also noticed, so I know now that I have to give priority to this issue. I will keep this issue open until it's fixed.
We have just released Pro Inspector which is going to improve the console logs. This works with any Node.js project and is not connected to Mida.
Installation
npm i @reiryoku-Technologies/pro-inspector
Usage
Use the following code in your program entry point
import { ProInspector, } from "@reiryoku/pro-inspector";
ProInspector.activateGlobally();
Let me know if it works as expected
Unfortunately I get,
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/dbv/tg/node_modules/@reiryoku/pro-inspector/entry/node/main.js
require() of ES modules is not supported.
require() of /home/dbv/tg/node_modules/@reiryoku/pro-inspector/entry/node/main.js from /home/dbv/tg/dist/apps/autotrade/main.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /home/dbv/tg/node_modules/@reiryoku/pro-inspector/entry/node/main.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/dbv/tg/node_modules/@reiryoku/pro-inspector/package.json.
at new NodeError (internal/errors.js:322:7)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.Module._load (/home/dbv/tg/packages/node/src/executors/node/node-with-require-overrides.ts:14:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:93:18)
at Object.@reiryoku/pro-inspector (/home/dbv/tg/dist/apps/autotrade/webpack:/tradeguardian/external commonjs "@reiryoku/pro-inspector":1:1)
at __webpack_require__ (/home/dbv/tg/dist/apps/autotrade/webpack:/tradeguardian/webpack/bootstrap:19:1)
at Object../apps/autotrade/src/app/autotrade.ts (/home/dbv/tg/dist/apps/autotrade/webpack:/tradeguardian/apps/autotrade/src/app/autotrade.ts:5:1)
I can confirm that @reiryoku/[email protected] works!
However, there is still the issue that I don't get the object itself.
I do get the info in the console and it looks great but it's not a JSON object.
If I try to do Object.keys(anyMidaObject) it is undefined still.
This is good for debugging but in production for an order/position/trade I want to create an object of my own with the relevant data, but also have a field of originalObject which can be a JSON.stringify(midaObject).
Maybe a good idea would be to have a setting when loading the inspector that it either outputs a JSON or formatted data?
The console.log() and JSON representation of an object are two different things. What you see in the console is just a message useful for debugging. Even with the native console.log() you are not actually seeing a valid JSON formatted message.
Most of Mida objects are complex JavaScript objects (class instances) so most of them are not going to work as you expect with JSON.stringify() and Object.keys(). That's because class members are not enumerable by default.
We can consider creating a custom toJSON() method for every Mida class, in this way we can easily use JSON.stringify() and Object.keys() with any Mida object, please read here.
For now you can just convert a Mida object to a simple JavaScript object by implementing some custom functions
function orderToJson (order) {
return {
id: order.id,
status: order.status,
executionPrice: order.executionPrice,
executionDate: order.executionDate.iso,
// ... more data
};
}
In this way you can call this function on any Mida order and then use JSON.stringify() or Object.keys() on it.
The issue is that when I call a function that returns an array.
I cannot loop thru it and I can not take an element out of the array.
.getOpenPositions() is such function.
When I do const pos=(await account.getOpenPositions())[0]; this is undefined.
And there are open positions there. If I do console.log of the whole array I see them.
The problem is they cannot be accessed.