Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Optimising and reusing code Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
Onereason
Posted : Tuesday, October 1, 2013 1:20:28 PM
Registered User
Joined: 10/7/2004
Posts: 56

Hey Bruce,

I keep banding my head on computer resource limits, and also recently I found a need to use a previous bar qualifier for a setup.

With that said, hopefully both issues can be solved with the same solution if I understand "static" variables correctly.

First, the previous bar's variable requirement -

I have a bunch of code to create a qualifying setup that includes previous opens, highs, closes, volume etc.. that gives me a value. I will call this value varSETUP. I also want a value from similar variables and I will call this varConfirmation

I want the previous bar's close to be greater than varSETUP and IF that happens, I want the current bar's high/last to be greater than varConfirmation in order to "PASS" to signal a trade opportunity.

Currently, I use code that checks the previous bar's data along with the current bar's data to perform the "pass" IF THEN inside a condition.

I am wondering if I can store the bar's data for it to be used as the previous bar's data upon a new bar (I'm using intraday data).

I want the same the compariable indicator.

Also, along the same lines, I use volume moving averages that are quite long (over 500 bars), I am wondering if I can use static variables to lower the processing power needed. I understand each bar makes a static variable increasingly "stale" when it's based on a moving average, so I would like to check after each bar and maybe recalculate it after X number of bars. 

I'm not really sure how to make it happen or if using static variables are the path, but I'm open to all ideas.

Thanks

Bob

 

Bruce_L
Posted : Tuesday, October 1, 2013 1:46:40 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

Static variables keep their values between bars while other variables do not. Having a variable keep its values is just a matter of declaring it using Static instead of Dim.

A Static variable will have the same value when you first start calculating a new bar (or symbol for that matter) as it had at the end of the calculations for the previous bar.

So you can use:

Static varSETUP As Single
Static varConfirmation As Single

And then you could use the values calculated for varSETUP and varConfirmation at the end of the previous bar unless or until your code made changes to varSETUP and varConfirmation. Once you have made any changes to these values, then you could no longer access the previously calculated values.

To have both past and current values of varSETUP and varConfirmation available, you would need to actually have static variables for buth past and current values. You could do this by having multiple variables with different names or by using a static list or static array.

There are algorithms for moving averages which do not require calculating the moving average from scratch at each bar. For example, if you were calculating a 500-period simple moving average, you would add up the first 500 bars to get a sum and divide the results by 500. But on the next bar, you can just add the most recent value and subtract the value from 500 bars ago from the sum instead of adding together 500 values. It doesn't take any longer to do this for a 500-bar moving average than for a 5-bar moving moving average.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Onereason
Posted : Tuesday, October 1, 2013 2:42:31 PM
Registered User
Joined: 10/7/2004
Posts: 56

Thanks Bruce,

How do I code that it's a new bar as apposed to just another tick?

Which reminds me of another feature that would be nice. I created the code to have a popup and sound when a condition is met, but it continues to pop up multiple times in a single bar.

It would be nice if it only popped once per bar and I'm guessing the code to limit that is what I need to have the other static variables refresh upon each new bar.

Can you provide a code example that tests if its a new bar?

Bruce_L
Posted : Tuesday, October 1, 2013 3:26:54 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

My original response was about using static variables to access previous values of an indicator within a loop through all of the symbols during the calculation of that indicator. This is what I thought you were asking, but I no longer think this is the case.

What you appear to be asking is for a way to not loop through the calculations at all when a new bar is created. You want to just use the old value of the indicator and the values for the new bar to calculate the value of the indicator for the new bar without looping through the rest of the values at all.

If I am now understanding what you are attempting to do, then I really can't help you. This is not the default way indicator calculations occur in StockFinder. When a new bar forms, the entire indicator is recalculated.

There are ways around this, but I am not a programmer and doing so is beyond my abilities to assist you.

The Audible alert help and showmessage topics explore how to get a notification only once, but I think the calculations still loop through all of the data every time a new bar forms in both topics.

It may be possible to use the New SharedHash object to store the results for each symbol and only calculate the results for a single bar whenever a new bar forms, but I do not have any specific suggestions as to how you might do so.

Another possibility would be to create the values you want to use as indicators and then occassionaly export them manually (there isn't an automated method of exporting data). You could then read the previously calculated exported data so you only had to calculate values past the end of the export. The Export/Import topic explores this in a very basic way. I don't know if it would actually be practical in your case and any advanced implementation of this are also beyond my abilities as a non-programmer to provide any meaningful help.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.