Login Page - Create Account

Support Board


Date/Time: Tue, 26 Nov 2024 06:33:48 +0000



[Programming Help] - [shared] persistent variables vs study inputs vs direct sc methods (for 2+ studies)

View Count: 363

[2023-12-20 22:06:15]
User133994 - Posts: 80
Support,

The goal: share state between 2 bots each on it's own chart and own account. For example:
chart 1: bot1study, trading on account1
chart 2: bot2study, trading on account2

Currently I'm using various combinations (whatever I can get to work) to share state between bot1/bot2 studies. Bot1 needs to know about bot2's position, phase (custom logic), and other variable. Similarly, Bot2 also depends on Bot1's account position status, phase (custom logic), and other variables.

Right now I'm having issues sharing bot1's account positiondata (i.e. just an integer) with bot2. The update is 3 bars behind reality? It took about 2 minutes for 3 bars to complete...so there was plenty of price updates to allow for the persistent shared variables to update. I use autoloop=1 on both studies. I can see the position on the top chart, but the bottom chart wouldn't register it until 3 bars/2 minutes later.

I was using Chart Study Persistent Variable Functions and had to switch to Automated Trading From an Advanced Custom Study: sc.GetTradePositionForSymbolAndAccount() to get "real-time" responsiveness that the persistent variables approach didn't have. Don't know why the direct method worked better?

There is a 3rd method to share data between the 2 studies: sc.GetStudyArraysFromChartUsingID() (or one of the variants). From my experience this method seems most reliable--works as expected most of the time.

All 3 seem to accomplish the same task in various ways, however, I'd prefer persistent shared variables, except the performance is lacking.

Has anyone else has the same problem?

It seems like I'll have to revert back to using sc.GetStudyArraysFromChartUsingID() as this method seems to "always" work as expected.

And, since bot1 refers to bot2 and bot2 refers to bot1 to make decisions there can be the appearance of an endless loop that never catches up; however, I've not designed it that way. There are discrete phases that interact with each other and do not cause any race conditions. Nonetheless, perhaps someone has another idea/method that would better suited for this goal of sharing state across 2 different charts/accounts/studies.

Would someone please share your ideas on why persistent shared variables work sometimes as expected; and, other times appear to be underperformant?

Also, would someone recommend the proper context for each method (a) shared persistent variables vs (b) shared study arrays vs (c) direct sc methods--or should they all work equally well when sharing states of variables (i.e. float/int, etc.) between 2 different studies on 2 different charts using 2 different trading accounts?

Thanks in advance.
Date Time Of Last Edit: 2023-12-20 23:19:08
[2023-12-20 22:34:51]
User133994 - Posts: 80
PART 2: My 2nd example is on the same 2 studies, 2 charts, 2 accounts.

This time I'm sharing a persistent int between the 2 studies; but, the exact same info is also stored in an output of the top chart. And I watch it in realtime and the output switches values, but the 2nd bottom chart doesn't see the updated shared persistent int until a bar later (not as long as the above example), but still underperforming expectations!?

So, now you understand why I prefer using sc.GetStudyArraysFromChartUsingID() >> appears to work much quicker than persistent shared variables.

Again, I'm providing this as more context for the above questions (I'm not using account info this time, just a basic int from the logic space of chart 1). Yet, there is still a delay.

No, I'm not using 'fast persistent' because I don't have hundreds of persistent variables being shared...I have about 20 shared persistent variables between the 2 charts.

Has anyone else has the same problem?

Would someone please share your ideas on why persistent shared variables work sometimes as expected; and, other times appear to be underperformant?

Also, would someone recommend the proper context for each method (a) shared persistent variables vs (b) shared study arrays vs (c) direct sc methods--or should they all work equally well when sharing states of variables (i.e. float/int, etc.) between 2 different studies on 2 different charts using 2 different trading accounts?

Thanks again
[2023-12-21 15:48:03]
User133994 - Posts: 80
Well, I wrote a custom solution using a flat file using sc.OpenFile() and the other file commands with SC. And....it worked?

Performance is perfect. I see the change with my eyes the same time the 2nd chart sees it.

Now, I'm really confused as to why I can write a flat file and have better performance sharing state information between 2 studies and 2 charts?

I must really be missing something or am using the shared persistent variables wrong? Nonetheless...I'll read any responses. For now, flat file is the way to go; I'm abandoning persistent variables in favor of a flat file for this part of the design (and likely any future parts).

No, I'm not writing arrays to the flat file, just state information for various bot algorithms I have. So the data requirements aren't huge like it would be for saving an array.

Any comments welcome. Thanks
[2023-12-21 19:55:09]
ertrader - Posts: 672
My experience has been the following: (I have not used sc.GetPersistentFloatFromChartStudy):

1) Within the same study: Persistent variables work without issue. Persistence meaning that the value is held regardless of the chart Index within the study and until you change it programmatically.

2) Within the same chartbook but between charts: sc.GetStudyArraysFromChartUsingID() works but you must use study/price price overlay first to get information from the other chart, then reference the study/price overlay. Ensure that your study comes AFTER the study/price overlay in the Chart Studies Window. Your study has to get information AFTER the study/price overlay so it must be below in the sequence. Study Sequence is vital to get the timing right. Persistent variables work fine but again, only within the study. My bots use this method reliably all the time. I have not had success for some reason referencing the other chart directly without using study/price overlay first. Probably something I am missing or doing wrong but this is working.

3) Within or Between chartbooks, studies and SC instances: read/write text files. This works well under nearly any condition and you have complete control. If you want to improve performance, you can use a ramdisk. Trigger timing can be an issue but you can put logic in place to check for conditions.

There may be other ways but this has been my experience. Frederick with https://www.simplesystemtrading.com/ wrote a custom trade copier that moves data within and between charts and instances on the same computer. I'm not sure how he does it but you may want to contact him also.
Date Time Of Last Edit: 2023-12-21 21:02:20

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account