Array Question (probably a dumb one)

Questions about MultiCharts and user contributed studies.
WarEagle
Posts: 36
Joined: 07 Nov 2006
Has thanked: 7 times

Array Question (probably a dumb one)

Postby WarEagle » 16 Sep 2010

Hi All,

here is a snippet of code taken from the trendlines automatic study that comes with MC.

Code: Select all

begin

for Value1 = 9 downto 0
begin
arr0[ Value1 + 1 ] = arr0[Value1] ;
arr1[ Value1 + 1 ] = arr1[Value1] ;
arr2[ Value1 + 1 ] = arr2[Value1] ;
end ;


arr0[0] = Date[SwHiStrength] ;
arr1[0] = Time[SwHiStrength] ;
arr2[0] = High[SwHiStrength] ;

for Value2 = 1 to 10
begin
if arr2[Value2] > arr2[0] then
begin
var8 = Value2 ;
Value2 = 11 ;
print(value2);
end ;
end ;

if Value2 = 12 then
begin
if var0 >= 0 then
begin
condition1 = var10 and var2 = false ;
if condition1 then
my question is this - Value2 is set up as a counter to go through the array. Once it finds an appropriate value per the condition Value2 is set to 11 so it will stop counting. the next section starts if Value2=12....how does Value2 ever get to 12?? I have been through all of the code and cant figure this out - I was hoping someone could share some enlightenment on this.

Thanks in advance!

bowlesj3
Posts: 2110
Joined: 21 Jul 2007
Has thanked: 202 times
Been thanked: 420 times

Re: Array Question (probably a dumb one)

Postby bowlesj3 » 16 Sep 2010

I think that the for loop is likely adding 1 to value2 before it actually tests to see if it is greater than 10. So if it was set to 11 then it will become 12 then the test occurs and it exits the loop so it will not do the next run through. It is pretty easy to test the theory.

If "if SwHiVal[Value2] > SwHiVal[0] then" did not occur, then value2 would be left at value 11 instead. It is not really a good self documenting programming technique. It is better to create a true/false variable the has a name that describes the situation. However based upon the comment it appears that if value2 = 11 on the way out then the next-most recent swhi does not exist because it did the full 10 element search without finding it.

WarEagle
Posts: 36
Joined: 07 Nov 2006
Has thanked: 7 times

Re: Array Question (probably a dumb one)

Postby WarEagle » 16 Sep 2010

Hi bowlesj3 - thanks for your reply. I was thinking along the same lines as you. I do like your idea of testing the values. As I swim deeper in the powerlanguage waters, I am finding the documenting that you mentioned essential. I guess this indicator is not documented since it comes with the MC package. Thanks again.

bowlesj3
Posts: 2110
Joined: 21 Jul 2007
Has thanked: 202 times
Been thanked: 420 times

Re: Array Question (probably a dumb one)

Postby bowlesj3 » 17 Sep 2010

Useually things are not documented due to programmer habits and programmer rushing due to pressure. Sometimes they think it is smart to hide things in sneaky ways but if they are a programmer who is using their own code for trading this is not overly wise. I was thinking that the test for value1 = 12 should be something like this.

IntraBarPersist LastSwingWasFoundBeforeArraysEnd (false),

Then an if test of
if LastSwingWasFoundBeforeArraysEnd = true then

So the value would be set to false just before the for loop then where the value1 is set to 12 instead the code would execute "LastSwingWasFoundBeforeArraysEnd = true;"

That describes it very clearly. They refer to it as "Self documenting programming".

The programmer needs their thinking cap on , needs to be thinking "long term" and like I said it takes a little longer.

WarEagle
Posts: 36
Joined: 07 Nov 2006
Has thanked: 7 times

Re: Array Question (probably a dumb one)

Postby WarEagle » 19 Sep 2010

Thanks for the clarifican bowlesj3. I am working on your test now. I do see the value of your self-documenting programming. that something that i never really considered. it takes a bit if thought on the front-end but certainly will help in the future! thanks again for the tips. as you can tell i am a bit of a newbie here -

bowlesj3
Posts: 2110
Joined: 21 Jul 2007
Has thanked: 202 times
Been thanked: 420 times

Re: Array Question (probably a dumb one)

Postby bowlesj3 » 19 Sep 2010

No Problem.

I forgot to mention something that you probably noticed anyway. The meaningful variable name has a capital to separate every word so that they are a little easier/faster to understand while at the same time saving some space.

User avatar
TJ
Posts: 7210
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2052 times

Re: Array Question (probably a dumb one)

Postby TJ » 19 Sep 2010

for your perusal...

Code: Select all

{
This indicator:
(1) INSERTS DnTL's/UpTL's connecting each new SwingHi/SwingLo with the next most
recent higher/lower SwingHi/SwingLo within the last 10 SwingHi's/SwingLo's;
(2) EXTENDS each new TL to the right and SETS it's color and alert type;
(3) TRUNCATES a DnTL/UpTL on the right when the next new DnTL/UpTL is drawn (if the
History input is set to "Yes"), or BarsPast bars after it has been breached,
whichever comes first.
(4) DELETES a DnTL/UpTL when the next new DnTL/UpTL is drawn, if the History input
is set to "No".

NOTE: This indicator may not work well with low-count tick bars, such as 10-tick bars,
etc., because the time resolution of the bars may not be high enough for each bar to
have a distinct time stamp.
}

inputs:
SwHiStrength( 4 ),
SwLoStrength( 4 ),
BarsPast( 10 ),
History( "Yes" ),
DnTLColor( Red ),
UpTLColor( Cyan ),
AlertType( "IntraBar" ) ;

variables:
DnTLRef( -1 ),
DnTLEndBar( 0 ),
DnTLBreak( false ),
DnTLColorNum( DnTLColor ),

UpTLRef( -1 ),
UpTLEndBar( 0 ),
UpTLBreak( false ),
UpTLColorNum( UpTLColor ),

Index( 0 ),
BarNum( 0 ),
HistoryTF( false ),
AlertTypeCAPS( UpperStr( AlertType ) ) ;

arrays:
SwHiDate[10]( 0 ),
SwHiTime[10]( 0 ),
SwHiVal[10]( -1000000 ),
SwLoDate[10]( 0 ),
SwLoTime[10]( 0 ),
SwLoVal[10]( 1000000 ) ;

{========== end of variables ==========}

if CurrentBar = 1 then
HistoryTF = UpperStr( History ) = "YES" or UpperStr( History ) = "Y" ;
{ should also be able to do this via declaration above }

BarNum = BarNumber ;

if SwingHighBar( 1, High, SwHiStrength, SwHiStrength + 1 ) = SwHiStrength then
{ ie, if just confirmed SwHi }
begin

{ push arrays back }
for Value1 = 9 downto 0
begin
SwHiDate[ Value1 + 1 ] = SwHiDate[Value1] ;
SwHiTime[ Value1 + 1 ] = SwHiTime[Value1] ;
SwHiVal[ Value1 + 1 ] = SwHiVal[Value1] ;
end ;

{ read in parameters of new SwHi into 0-elements of arrays }
SwHiDate[0] = Date[SwHiStrength] ;
SwHiTime[0] = Time[SwHiStrength] ;
SwHiVal[0] = High[SwHiStrength] ;

{ find and save the index of the next-most-recent higher SwHi if it exists }
for Value2 = 1 to 10
begin
if SwHiVal[Value2] > SwHiVal[0] then
begin
Index = Value2 ;
Value2 = 11 ; { short circuit the looping with 11 instead of 10; the 11
will become 12 in the final pass }
end ;
end ;

if Value2 = 12 then { ie, if next-most-recent higher SwHi exists }
begin
if DnTLRef >= 0 then { ie, if previous DnTL exists }
begin
if HistoryTF and DnTLBreak = false then
{ if history reqd and most recent DnTL not already truncated
elsewhere, truncate it now }
begin
TL_SetEnd( DnTLRef, Date, Time, TL_GetValue( DnTLRef, Date, Time ) ) ;
TL_SetExtRight( DnTLRef, false ) ;
end
else
if HistoryTF = false then
{ if history not reqd, delete most recent DnTL }
TL_Delete( DnTLRef ) ;
end ;

{ draw new DnTL, reset break flag, save endbar, set extents/color/alert }
DnTLRef = TL_New( SwHiDate[Index], SwHiTime[Index], SwHiVal[Index],
SwHiDate[0], SwHiTime[0], SwHiVal[0] ) ;

if DnTLBreak = true then
DnTLBreak = false ;

DnTLEndBar = BarNum - SwHiStrength ;
TL_SetExtLeft( DnTLRef, false ) ;
TL_SetExtRight( DnTLRef, true ) ;

if DnTLColorNum <> 99 then
TL_SetColor( DnTLRef, DnTLColorNum ) ;

if AlertTypeCAPS = "ONCLOSE" then
TL_SetAlert( DnTLRef, 2 )
else
if AlertTypeCAPS = "INTRABAR" then
TL_SetAlert( DnTLRef, 1 )
else
TL_SetAlert( DnTLRef, 0 ) ;
end ;
end ;

if SwingLowBar( 1, Low, SwLoStrength, SwLoStrength + 1 ) = SwLoStrength then
{ ie, if just confirmed SwLo }
begin

{ push arrays back }
for Value1 = 9 downto 0
begin
SwLoDate[Value1+1] = SwLoDate[Value1] ;
SwLoTime[Value1+1] = SwLoTime[Value1] ;
SwLoVal[Value1+1] = SwLoVal[Value1] ;
end ;

{ read in parameters of new SwLo into 0-elements of arrays }
SwLoDate[0] = Date[SwLoStrength] ;
SwLoTime[0] = Time[SwLoStrength] ;
SwLoVal[0] = Low[SwLoStrength] ;

{ find and save the index of the next-most-recent lower SwLo if it exists }
for Value2 = 1 to 10
begin
if SwLoVal[Value2] < SwLoVal[0] then
begin
Index = Value2 ;
Value2 = 11 ;{ short circuit the looping with 11 instead of 10; the 11
will become 12 in the final pass }
end ;
end ;

if Value2 = 12 then { ie, if next-most-recent lower SwLo exists }
begin
if UpTLRef >= 0 then { ie, if previous UpTL exists }
begin
if HistoryTF and UpTLBreak = false then
{ if history reqd and most recent UpTL not already truncated
elsewhere, truncate it now }
begin
TL_SetEnd( UpTLRef, Date, Time, TL_GetValue( UpTLRef, Date, Time ) ) ;
TL_SetExtRight( UpTLRef, false ) ;
end
else
if HistoryTF = false then
{ if history not reqd, delete most recent UpTL }
TL_Delete( UpTLRef ) ;
end ;

{ draw new UpTL, reset break flag, save endbar, set extents/color/alert }
UpTLRef = TL_New( SwLoDate[Index], SwLoTime[Index], SwLoVal[Index],
SwLoDate[0], SwLoTime[0], SwLoVal[0] ) ;

if UpTLBreak = true then
UpTLBreak = false ;

UpTLEndBar = BarNum - SwLoStrength ;
TL_SetExtLeft( UpTLRef, false ) ;
TL_SetExtRight( UpTLRef, true ) ;

if UpTLColorNum <> 99 then
TL_SetColor( UpTLRef, UpTLColorNum ) ;

if AlertTypeCAPS = "ONCLOSE" then
TL_SetAlert( UpTLRef, 2 )
else if AlertTypeCAPS = "INTRABAR" then
TL_SetAlert( UpTLRef, 1 )
else
TL_SetAlert( UpTLRef, 0 ) ;
end ;
end ;

{ if most recent DnTL/UpTL exists AND has not yet been truncated here AND was drawn
at least BarsPast ago AND was breached BarsPast bars ago THEN truncate it here and
set break flag }

if DnTLRef >= 0
and DnTLBreak = false
and BarNum > DnTLEndBar + SwHiStrength + BarsPast
and ( Close > TL_GetValue( DnTLRef, Date, Time ) )[BarsPast]
then
begin
TL_SetEnd( DnTLRef, Date, Time, TL_GetValue( DnTLRef, Date, Time ) ) ;
TL_SetExtRight( DnTLRef, false ) ;
DnTLBreak = true ;
end ;

if UpTLRef >= 0
and UpTLBreak = false
and BarNum > UpTLEndBar + SwLoStrength + BarsPast
and ( Close < TL_GetValue( UpTLRef, Date, Time ) )[BarsPast]
then
begin
TL_SetEnd( UpTLRef, Date, Time, TL_GetValue( UpTLRef, Date, Time ) ) ;
TL_SetExtRight( UpTLRef, false ) ;
UpTLBreak = true ;
end ;


{ ** Copyright (c) 1991-2003 TS Technologies, Inc. All rights reserved. **
** TS reserves the right to modify or overwrite this analysis technique
with each release. ** }

WarEagle
Posts: 36
Joined: 07 Nov 2006
Has thanked: 7 times

Re: Array Question (probably a dumb one)

Postby WarEagle » 21 Sep 2010

No Problem.

The meaningful variable name has a capital to separate every word so that they are a little easier/faster to understand while at the same time saving some space.
heh, you are correct - I did notice that and have adopted that for my own work.

Thanks for the code TJ. That helps a great deal. Pretty clever how they "trick" the loop to add a value on the last pass...

WarEagle
Posts: 36
Joined: 07 Nov 2006
Has thanked: 7 times

Re: Array Question (probably a dumb one)

Postby WarEagle » 30 Sep 2010

Hi guys I am pleased to announce that i finally got my project working the way I intended it. After many long nights and many many <dumb> mistakes its working. I think I can fill a couple of notebooks on the wrong way to do stuff.

Thanks again for your tips and assistance.

User avatar
TJ
Posts: 7210
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2052 times

Re: Array Question (probably a dumb one)

Postby TJ » 30 Sep 2010

Hi guys I am pleased to announce that i finally got my project working the way I intended it. After many long nights and many many <dumb> mistakes its working. I think I can fill a couple of notebooks on the wrong way to do stuff.

Thanks again for your tips and assistance.
it is good to know you have it working.


Return to “MultiCharts”