Hi there,
I've been trying to work out a way to reference variables in a strategy via merging a string and a number (rather than a variable directly) such that the variable is dynamic.
For example, using "var"&numtostr(OpenEntries) to refer to var1/ var2/ var3 etc which would all be declared for the strategy.
In Excel the Indirect() function would work, but I can't find an equivalent. Is there anything in Powerlanguage that works the same way?
Thanks for any help!
Powerlanguage Indirect() Equivalent?
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2222 times
Re: Powerlanguage Indirect() Equivalent?
Hi there,
I've been trying to work out a way to reference variables in a strategy via merging a string and a number (rather than a variable directly) such that the variable is dynamic.
For example, using "var"&numtostr(OpenEntries) to refer to var1/ var2/ var3 etc which would all be declared for the strategy.
In Excel the Indirect() function would work, but I can't find an equivalent. Is there anything in Powerlanguage that works the same way?
Thanks for any help!
Your "example" is incomprehensible.
Please use a real life example.
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Powerlanguage Indirect() Equivalent?
That's not possible in PowerLanguage. What you can do is use an array and then use the `OpenEntries` keyword to refer to a specific index (instead of using the var1, var2, var3, etc variables) of the array.I've been trying to work out a way to reference variables in a strategy via merging a string and a number (rather than a variable directly) such that the variable is dynamic.
For example, using "var"&numtostr(OpenEntries) to refer to var1/ var2/ var3 etc which would all be declared for the strategy.
In Excel the Indirect() function would work, but I can't find an equivalent. Is there anything in Powerlanguage that works the same way?
For example, when `OpenEntries` is 0, `myArray[OpenEntries]` would get or set the array's first index (like var1). With `OpenEntries` being 1, `myArray[OpenEntries]` returns the array's second index (like var2 otherwise would).
Re: Powerlanguage Indirect() Equivalent?
Thanks JoshM, I will look into this.Your "example" is incomprehensible.
Please use a real life example.
TJ, as requested:
I use OpenEntryPrice(OpenEntriesCount - 1) in a strategy, but if the strategy is disconnected then re-started all open trades are consolidated into 1 position. (url: viewtopic.php?f=1&t=48733)
As such I have used Global Variables to restore the open trade properties to the strategy upon a re-start. In order to do this I have had to use variables in place of reserved words. For example:
Code: Select all
Once
Begin
INITOpenEntryPrice = OpenEntryPrice( 0 ) ; { where OpenEntryPrice( 0 ) will be the average OpenEntryPrice of all open positions when the strategy is re-started}
GV_OpenEntryPrice_0 = GVGetNamedFloat( "...", -999.99 ) ;
End ;
// Reset Open Trade Properties
IF OpenEntryPrice( 0 ) = INITOpenEntryPrice THEN {i.e. no change in the strategy's position}
_OpenEntryPrice_0 = GV_OpenEntryPrice_0
ELSE
_OpenEntryPrice_0 = OpenEntryPrice( 0 ) ; {resume use of reserved word}
The issue is that I can no longer use these variables in the same way I was previously using the reserved words i.e. OpenEntryPrice(OpenEntriesCount - 1) was dynamic, so I'm trying to work out the most efficient way to achieve the same outcome using the _OpenEntryPrice_0 variables (which by the sound of it is via using an array).
Hope that makes sense?