onBar and Functions
onBar and Functions
12 Apr 2023, 14:35
I am new to coding cBots, though not to coding, and wondering if I am missing something and making things to complicated for myself.
Functions like Minimum/Maximum/HasCrossedAbove/Below etc. take a DataSeries and a period.
OnBar is triggered on the opening of a Bar not the close.
If I call HasCrossedXXX in OnBar it will include the current unclosed Bar, so whatever the result I get I cannot know for sure whether in fact it really is correct because the current bar is not closed so it may not be crossed by the end of the bar, neither can I use it for looking back at the last closed bar because there isn't a way to call HasCrossed that won't include the current unclosed bar.
Therefore the only solution seems to be to have to code up my own version of HasCrossed etc.. that excludes the current bar?
This is what I have started to do but it seems odd that I need to code up a bunch of replacements for the existing cTrader API just to get usable data and I wonder if I am missing something obvious.
Thanks
Rob
Replies
robert.john.edgar
13 Apr 2023, 11:51
RE:
firemyst said:
What do you have for your "Period" setting?
According to the API documentation, the "HasCrossed" should look back the number of bars equal to "period" and see if the values have crossed during that time:
So if you have a period of 1, it should look back over the last 1 bars to see if the values have crossed. If you set period to 2, it should look over the last 2 bars to see if the values have crossed, etc etc.
If not, then please post sample code showing the issue so everyone can see what you're doing and how.
Thank you.
Thanks for the response but I think you slightly misunderstand the API....
A period of 0 looks at only the current bar whilst a period of 1 looks at BOTH the current and the previous bars, and therefore a period of 2 looks at a total of 3 Bars, the current, the previous and the one before that.
Whatever period you use it will ALWAYS include the current unclosed bar in its calculation.
The documentation actually explicitly warns about the problem of these functions when it states:
It is not uncommon that the function will return true and by the end of the bar the two series will uncross.
It about as strong a warning as you can get to not use these functions........
Rob
@robert.john.edgar
robert.john.edgar
13 Apr 2023, 13:15
RE:
firemyst said:
Then why not just create your own function that does the same thing?
Should be straight forward.
Just a simple for-loop going through each bar in the data series except the current bar, and compare the values?
Well if you read my original post you will see that's exactly what I am doing, but I had wondered whether I was missing something obvious and was just reinventing the wheel, hence why I posted on this forum to hear from people with more experience at coding cBots about how they were dealing with what should be a common issue.
Rob
@robert.john.edgar
firemyst
13 Apr 2023, 14:24
I understand what you mean.
While the API is a good start, there's definitely lots of things that can be improved.
What annoys me the most so far is the API's for the indicators aren't standardized. For instance, there's a lot of indicators that do not accept a data series as a parameter; or other indicators like the MA's where some have the "shift" parameter, and others that use MA's don't.
And we still don't have an option to programmatically turn on/off any "cloud" attribute.
Go figure. :-/
@firemyst
daniel.podrazka
14 Apr 2023, 20:41
RE: RE:
robert.john.edgar said:
firemyst said:
Then why not just create your own function that does the same thing?
Should be straight forward.
Just a simple for-loop going through each bar in the data series except the current bar, and compare the values?
Well if you read my original post you will see that's exactly what I am doing, but I had wondered whether I was missing something obvious and was just reinventing the wheel, hence why I posted on this forum to hear from people with more experience at coding cBots about how they were dealing with what should be a common issue.
Rob
There is a simple solution to that although a bit ugly:
Call the function twice, once for period=0 and once for period=1 and combine the result:
where period=1 == True and period=0 == False
@daniel.podrazka
firemyst
13 Apr 2023, 03:49
What do you have for your "Period" setting?
According to the API documentation, the "HasCrossed" should look back the number of bars equal to "period" and see if the values have crossed during that time:
So if you have a period of 1, it should look back over the last 1 bars to see if the values have crossed. If you set period to 2, it should look over the last 2 bars to see if the values have crossed, etc etc.
If not, then please post sample code showing the issue so everyone can see what you're doing and how.
Thank you.
@firemyst