Powerlanguage Indirect() Equivalent?

Questions about MultiCharts and user contributed studies.
WHM
Posts: 7
Joined: 28 May 2015
Location: Australia
Has thanked: 13 times

Powerlanguage Indirect() Equivalent?

Postby WHM » 23 Jul 2015

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!

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Powerlanguage Indirect() Equivalent?

Postby TJ » 23 Jul 2015

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.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Powerlanguage Indirect() Equivalent?

Postby JoshM » 23 Jul 2015

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?
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.

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).

WHM
Posts: 7
Joined: 28 May 2015
Location: Australia
Has thanked: 13 times

Re: Powerlanguage Indirect() Equivalent?

Postby WHM » 23 Jul 2015

Your "example" is incomprehensible.
Please use a real life example.
Thanks JoshM, I will look into this.

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}
In a pyramid position there will be >1 open entry, so this process is repeated for _OpenEntryPrice_1, _OpenEntryPrice_2 etc. up to the max allowable entry orders.

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?


Return to “MultiCharts”