- ago
I am developing a strategy that uses the Broker query functionality. I declared the broker account number as a global private string variable and set the value in the BacktestBegin() method. I want to use this account number variable for Broker queries in the PostExecute() method.

I find that the variable is null in the PE method unless I declare the variable as static. I was wondering was this is, since BB is only run once and normally, I would not think about having to set a global variable.as static so the value can be accessed from PE.

I've found this specific behavior is not limited to string variables.
0
414
Solved
6 Replies

Reply

Bookmark

Sort
- ago
#1
There is an instance of your Strategy for every symbol.

Only static variables exist once for all symbols.
3
Best Answer
- ago
#2
QUOTE:
I find that the variable is null in the PE method unless I declare the variable as static.

Well, the static variable approach is one solution. If the account variable is only used in PostExecute, then why not simply code it as a local variable within the PostExecute block?
0
- ago
#3
Thanks, @DrKoch, for the reply and info. If I understand your reply correctly, a backtest instance is created for each symbol in the dataset and WL runs these simultaneously, not sequentially. Therefore, a non-static variable's value assigned in BacktestBegin() would be lost after the first instance is created if it is not declared as static. This is because BB only executes once, not once for each symbol. Or is it more about the static keyword allowing the variable to be shared across multiple instances of the same class?

0
- ago
#4
Both.
An Instance is created for each symbol.
BB() os run once (for the first symbol).

A static variable exists just once for all instances.
1
- ago
#5
Thanks, @superticker, for the response. The only reason I moved it to BacktestBegin() is because i thought it was a more correct coding approach, i.e., avoiding reassigning the same value to the same variable for each symbol in the dataset. Probably a negligible improvement in performance, but it helps me keep track which method code is looping by bar, by symbol, or not at all.

I initially had it the way you suggested and didn't understand the implications of reorganizing the code. But it has definitely been an opportunity to learn more about WL internals, which can be helpful going forward.
0
- ago
#6
@DrKoch, that helps. My debugging output now makes sense as it looked like it was working for the first symbol only. Thanks for your help!
0

Reply

Bookmark

Sort