SetContext inside a class definition
Author: superticker
Creation Date: 5/6/2018 3:25 PM
profile picture

superticker

#1
In the code below, WSObj.SetContext() returns a native Bars object instead of the external symbol Bars object requested by the idxSymbol variable. What's weird is Visual Studio can find the reference to WSObj.SetContext just fine in the WealthScript definition, and this code executes fine when included inside a strategy (i.e. not inside a class definition). How is that possible? Does it have something to do with the Bars object SetContext is operating on being in stack storage (as a parameter) instead of dynamic storage? Do I need a "ref" on the parameter line?
CODE:
Please log in to see this code.


Anyway, how does one get WSObj.SetContext to function inside a class definition like this?
CODE:
Please log in to see this code.
profile picture

Eugene

#2
Could be the usage of class level variable.
profile picture

superticker

#3
QUOTE:
Could be the usage of class level variable.
Which class-level variable are you talking about? SetContext only operates on the Bars "bars" object, which isn't a class-level variable. Right? The Bars object is passed as a parameter, but because of that, a copy is made from dynamic storage to stack storage. Is SetContext operating on the stack storage copy (which would be okay, I think) or the dynamic storage original?

I'm assuming if I include the "ref" directive in the Bars parameter specification, that forces the compiler to not make the stack copy in the first place.

Typically, one tries to design code to avoid using the "ref" directive so classes don't create unexpected side effects for the calling program's variables. That's why the stack copy is make in the first place, so there are no unexpected side effects passed back. With the "ref" directive, that protection is lost.
profile picture

Eugene

#4
You could help us reproduce what you're seeing by providing a step by step procedure with more details like context (S. window or SM), DataSets and symbols involved, etc.
profile picture

superticker

#5
Right. What you need is a bare bone example of the problem you can execute. Here it is.
CODE:
Please log in to see this code.
So the PlotSeries call should plot .SPX in all cases, but it doesn't. My fear is correct. The bars.Close variable points to the stack copy of the original; whereas, the SetContext call operates on the dynamic heap original, not the stack copy. There are two possible solutions: 1) get SetContext to operate on the stack copy (safest approach), or 2) provide the dynamic original to the class. I tried doing the latter by including the "ref" directive as discussed in Post# 1, but the compiler disallows using a "ref" directive on a dynamic original. (I didn't know that.) Method 2 might still be possible but not with a "ref" directive.

I would prefer not to escape the C# language as "reflection" does. Is there another way to accomplish Method 1 or 2?

Is SetContext operating by escaping the language? If not, then how is it granted access of the dynamic (heap) original within a class like this?
profile picture

Eugene

#6
Your code does not look complete to me. After you set context to an external symbol...
CODE:
Please log in to see this code.

...to plot that external symbol you would want to reflect that change in code below accordingly:
CODE:
Please log in to see this code.
profile picture

superticker

#7
Thanks a million. That's the fix. Bottom line, WSObj.SetContext operates on WSObj.Bars and not local bars, so you must address the former instead.
profile picture

Eugene

#8
At your service.