Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill output with realtime data, without change _state. #28

Open
dinhduongha opened this issue Feb 15, 2024 · 2 comments
Open

Fill output with realtime data, without change _state. #28

dinhduongha opened this issue Feb 15, 2024 · 2 comments

Comments

@dinhduongha
Copy link

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?

  • Add TA_[MYINDICATOR]_StateRT(void* _state, dataArguments, outArguments) will fill outArguments without change _state

or

  • Add TA_[MYINDICATOR]_StateCopy(const void* _state, dataArguments, void* _copiedStateOuput) to copy current state to new variable, then users can call TA_[MYINDICATOR]_State(_copiedStateOuput , ....) to calc TA values of not-yet-close-candles.
@trufanov-nok
Copy link
Owner

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.

@sunpavlik
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants