Picking up echo_req responses
Hi there,
Another noob question. would like to get Tick_history_update with the subscribe function. msg1 arrives as msg_type : 'candles' with history and I can successfully map this to a new HTML table no problem. I want to merge the original JSON and ohlc JSON's. The table creates no problem but defining the data for 'ohlc' is the difficulty. I keep getting 'undefined' errors. I have been going round in circles for days. I am sure it will be the simplest oversight on my behalf, but if someone could kindly point out the error of my ways it would be much appreciated, please. Code I am using so far is below:
ws.onopen = function(evt) { ws.send(JSON.stringify({ "ticks_history": "frxGBPUSD","end": "latest","count": 86400,"style":"candles","granularity": 14400,"subscribe":1})); } ws.onmessage = function(msg) { var data = JSON.parse(msg.data); console.log('ticks_history update: %o', data); for (var i = 0; i < data["msg_type"].length; i ++){ var arr1 = data.candles.map(function(candles){ return { day : moment(candles.epoch*1000).format('DD/MM/YYYY'), time : moment(candles.epoch*1000).format('HH:MM:SS'), open : candles.open, high : candles.high, low : candles.low, close : candles.close}; });} console.log('can_Array: %o', arr1); if( typeof data.candles !== 'undefined' ){ var echo = ["ohlc"]; for (var i = 0; i < echo.length; i ++){ var arr2 = echo.map(function(echo){ return { day : moment(echo.epoch*1000).format('DD/MM/YYYY'), time : moment(echo.open_time*1000).format('HH:MM:SS'), open : echo.open, high : echo.high, low : echo.low, close : echo.close};});}} console.log('tic_Array: %o', arr2); var liv = arr1.concat(arr2);
Many thanks.
Comments
@Middleend why not do something like this
Thanks @Raunak unfortunately this has no helped, but I have learned a lot from it thank you. The code works for the map funcrion to create the arrays, I can define elements of the very first JSON message and produce the table, can create the second array but am unable to define the components. The tick_history_update continues but ignores the typeof catch and get the errors. I have checked the JSON in the API playground and it is "ohlc", {"ohlc" [...... please see below screen shot.
of the console log. if use if and if else the results are the same...many thanks once again.
The problem with your code is that you are checking
msg_type
lengthfor (var i = 0; i < data["msg_type"].length; i++) {
rather than type of it.When you request
ticks_history
with subscribe first it returns candles from time to current time (if no end_time) withmsg_type: candles
then it will start streamingohlc
from there on hence change of msg_type