Open dinhduongha opened 8 months ago
There is a workaround. You may copy a state object via Load/Save functions which work with FILE. But you may open a virtual FILE handle to a buffer allocated in RAM to speed up the process. This would work for a buffer in a stack memory:
char buf[1024];
FILE* f = fmemopen(buf, 1024, "w+");
assert(f != NULL);
TA_MACD_StateSave(state, f);
assert(fseek(f, 0, SEEK_SET) == 0); // seek to the beginning of file
struct TA_MACD_State *state_copy = NULL;
TA_MACD_StateLoad(&state_copy, f);
fclose(f);
or in a heap memory:
void * buf = malloc(1024);
FILE* f = fmemopen(buf, 1024, "w+");
assert(f != NULL);
TA_MACD_StateSave(state, f);
assert(fseek(f, 0, SEEK_SET) == 0); // seek to the beginning of file
struct TA_MACD_State *state_copy = NULL;
TA_MACD_StateLoad(&state_copy, f);
fclose(f);
... work with a state_copy here
free(buf);
Note that as state_copy
wasn't allocated with TA_MACD_StateInit
it shouldn't be destroyed with TA_MACD_StateFree()
. The state_copy
object will exists while buf
exists. So it will exists till the end of the function (in fact end of the buf
variable visibility) in 1st case or till free(buf)
called in 2nd.
The size 1024 is chosen manually and it's overshoot. Perhaps I'll need to add a function that return a state size in addition to one that copy it.
I am using TA_SaveState and it takes a buffer. How can I determine the size of the buffer? Here you say that 1024 sufficiently large, is that for sure, irrespective say rolling window size?
The functions
int TA_[MYINDICATOR]_State(void* _state, dataArguments, outArguments)
will change _state. Sometimes, we want to keep _state for real-time data.For example: If we calculate SUM volume or EMA of time-frame 4h, we must wait 4h for next call. But we also want to calc these values at 1h, 2h, 3h (when the candle not closed), how to do?
TA_[MYINDICATOR]_StateRT(void* _state, dataArguments, outArguments)
will fill outArguments without change _stateor
TA_[MYINDICATOR]_StateCopy(const void* _state, dataArguments, void* _copiedStateOuput)
to copy current state to new variable, then users can callTA_[MYINDICATOR]_State(_copiedStateOuput , ....)
to calc TA values of not-yet-close-candles.