I get the following data from a stock API:
0, 0, 0, 1, 0, 4, 71, 79, 79, 71, 0, 0, 0, 0, 4, 68, 93, -17, -65, -67, 82, 68, 95, 5, 31, >68, 93, 64, 0, 68, 93, -17, -65, -67, -17, -65, -67, 70, -17, -65, -67, 52, -17, -65, -67, >0, 0, 1, 63, -17, -65, -67, 99, 30, 0, 68, 92, -17, -65, -67, -17, -65, -67, 68, 94, -17, >-65, -67, 0, 68, 91, 81, 72, 68, 94, -17, -65, -67, -17, -65, -67, 70, -17, -65, -67, -17, >-65, -67, 87, 0, 0, 1, 63, -17, -65, -67, -17, -65, -67, 122, 0, 68, 93, -17, -65, -67, >-17, -65, -67, 68, 94, 74, -17, -65, -67, 68, 91, -17, -65, -67, 0, 68, 91, -17, -65, -67, >70, 34, 107, 10, 0, 0, 1, 63, -17, -65, -67, -17, -65, -67, -17, -65, -67, 0, 68, 95, 95, >92, 68, 95, -17, -65, -67, 61, 68, 93, -17, -65, -67, -17, -65, -67, 68, 94, -17, -65, >-67, 0, 70, 127, -17, -65, -67, 0, 0, 1, 63, -17, -65, -67, -17, -65, -67
This is the piece of binary read succesfully by the code below which returns the array {error=0.0, symbol=GOOG, count=1, bars=4, length=4, bar=[]}:
0, 0, 0, 1, 0, 4, 71, 79, 79, 71, 0, 0, 0, 0, 4
It is from this point forward I'm struggling:
68, 93, -17, -65, -67, 82, 68, 95, 5, 31, >68, 93, 64, 0, 68, 93, -17, -65, -67, -17, -65, >-67, 70, -17, -65, -67, 52, -17, -65, -67, >0, 0, 1, 63, -17, -65, -67, 99, 30, 0, 68, 92, >-17, -65, -67, -17, -65, -67, 68, 94, -17, >-65, -67, 0, 68, 91, 81, 72, 68, 94, -17, -65, >-67, -17, -65, -67, 70, -17, -65, -67, -17, >-65, -67, 87, 0, 0, 1, 63, -17, -65, -67, >-17, -65, -67, 122, 0, 68, 93, -17, -65, -67, >-17, -65, -67, 68, 94, 74, -17, -65, -67, >68, 91, -17, -65, -67, 0, 68, 91, -17, -65, -67, >70, 34, 107, 10, 0, 0, 1, 63, -17, -65, >-67, -17, -65, -67, -17, -65, -67, 0, 68, 95, 95, >92, 68, 95, -17, -65, -67, 61, 68, 93, >-17, -65, -67, -17, -65, -67, 68, 94, -17, -65, >-67, 0, 70, 127, -17, -65, -67, 0, 0, 1, >63, -17, -65, -67, -17, -65, -67
The above is four "bars" of code including close, high, low, open, volume and timestmap. All of which are 4 byte Floats except Volume which is a Long with length of 8. Length described by the API as 8-bit bytes.
I'm not having issues parsing the first bit of the code (though it may not be perfect). I'm having issues though with the rest. From the API website, this is how the data is structured:
Field Type Length(8 bit bytes) Description
Symbol Count Integer 4 Number of symbols for which data is being returned. The subsequent sections are repeated this many times
REPEATING SYMBOL DATA
Symbol Length Short 2 Length of the Symbol field
Symbol String Variable The symbol for which the historical data is returned
Error Code Byte 1 0=OK, 1=ERROR
Error Length Short 2 Only returned if Error Code=1. Length of the Error string
Error Text String Variable Only returned if Error Code=1. The string describing the error
Bar Count Integer 4 # of chart bars; only if error code=0
REPEATING PRICE DATA
close Float 4 high Float 4 low Float 4 open Float 4 volume Float 4 (in 100's) timestamp Long 8 time in milliseconds from 00:00:00 UTC on January 1, 1970
END OF REPEATING PRICE DATA
Terminator Bytes 2 0xFF, 0XFF
END OF REPEATING SYMBOL DATA The PriceHistory response is in binary format represented by a symbol, number of chart
END OF REPEATING SYMBOL DATA
I'm able to extract count, length, symbol, error code and bar count. But it's parsing the bars I'm having issues. I'm not getting any data back but rather gibberish such as D]�R9.
Code is available here if you want to see how crappy I've gotten so far :) : http://pastebin.com/5eq9XPjT
for (i=0;i<=dataArray.length;i++) {
if (i<=5) {
symbolDetails['count'] = Utilities.newBlob(dataArray[0] + dataArray[1] + dataArray[2] + dataArray[3]).getDataAsString(); //Symbol count
symbolDetails['length'] = Utilities.newBlob(dataArray[4] + dataArray[5]).getDataAsString(); // Length of record
i=5; // jump ahead
} else if (i>5 && i<6+Number(symbolDetails['length'])) {
for (j=0;j<Number(symbolDetails['length']);j++) {
symbolDetails['symbol'][j] = dataArray[i];
i++
}
i = 10;
Logger.log(symbolDetails['symbol']);
symbolDetails['symbol'] = Utilities.newBlob(symbolDetails['symbol']).getDataAsString();
// partTwo is the sequence of data after the symbol. As symbol can be of varying length, this will help us know where we are.
var partTwo = 5 + Number(symbolDetails['length']);
} else {
// Get Error Code
if (i == partTwo) {
symbolDetails['error'] = Utilities.newBlob(dataArray[i]).getDataAsString();
} else if (i>= Number(partTwo + 1) && i<=Number(partTwo+5)) {
// Get Bar Count
for (j=0;j<4;j++) {
symbolDetails['bars'] =+ dataArray[i];
i++;
}
symbolDetails['bars'] = Utilities.newBlob(symbolDetails['bars']).getDataAsString();
}
}
}
Hoping someone can assist with this. Thank you very much.