logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
zigzag  
#1 Posted : Wednesday, February 18, 2009 11:14:45 AM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Hello Wabbit

Your formulas and comments are excellent - thanks.

I was wondering if you could take a quick peek at the following code I wrote. My goal is to display:

1. total number of bars since last high (looking back to first displayed data) was achieved, excluding current bar

2. peak-to-trough % change [defined as (high-low)/high], being high as per above and low=lowest value since high

I have looked around in the Forum but couldn't quite find what I wanted.

plot:=Input("1=Bars Since Last Hi,2=% Peak to Tr Since Prev Hi",1,2,1);
{is current bar highest value?}
test1:=P>Ref(Highest(P),-1);
{bars since last high}
test2:=HighestSinceBars(1,test1=1,P);
{lowest since prev high}
test3:=If(test1=1,P,LowestSince(1,test2=1,P));
{highest since prev high}
test4:=HighestSince(1,test2=0,P);
{% peak to trough}
ptt:=(test4-test3)/test4*100;
{plot}
If(plot=1,test2,ptt)

I think my code achieves this but would like a reality check from a pro.

Many thanks.

ZigZag

wabbit  
#2 Posted : Wednesday, February 18, 2009 8:08:04 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Hi zz,

The code appears to do what you want it to do; I do have some suggestions for your coding style though:

1) good of use comments, but I believe good code requires less comments if you use better variable names and descriptions, and match these to any comments that you do use;

2) think carefully about what each line of code does, logically, and try to discover if there is a "better" way to achieve the result;

3) try to make the code adaptable to changes. In your code, if you changed the "event", would the comments and code still make sense?

4) depending on how you define your "drawdown" it might make more sense to use HIGH and LOW as appropriate instead of P?

5) try to handle as many possible sources of MS errors as you can. This will make the code more applicable to different data sets.

6) consider the ramifications of N/A bars at the LHS or RHS of the chart. Do they make a difference to you and what you are trying to achieve? Do you need to perform some sort of initialisation to avoid these? Are there other functions which would avoid these N/A bars?

------------

plot:=Input("1=Bars Since Event,2=% Drawdown Since Event",1,2,1);

{the "event"}
{the current bar is a new highest value}
event:=P>Ref(Highest(P),-1);

{lowest data value since the event}
lowestSinceEvent:=If(event,P,LowestSince(1,event,P));

{highest data value since the event}
{this is just the highest charted value, so can be computed simply as:}
highestSinceEvent:=Highest(P);

{but, just in case you change the definition of the event, it is safer to use:}
highestSinceEvent:=If(event,P,HighestSince(1,event,P));

{% peak to trough, includes DBZ error handling}
drawdown:=100*(highestSinceEvent-lowestSinceEvent)/Max(highestSinceEvent,Power(10,-8));

{plot in new inner window}
If(plot=1,BarsSince(event),drawdown);

------------

That's all I have time for now. I may take another look later?



Hope this helps.

wabbit [:D]


P.S. Where possible, please don't address forum posts to individuals. If you want someone in particular to have a look at your work, please send them an email or PM (be cognisant that Administrators and Moderators receive a lot messages and may not necessarily answer all of them, in the timeframe you expect or at all!). The forum is a place for everyone to share; if you ask an individual for assistance and that person is away, your post may not get answered because other members might think it is not their place to post a response when the original post was not intended for them. It can cause confusion, and as such, should be avoided.


zigzag  
#3 Posted : Thursday, February 19, 2009 7:46:02 AM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Wabbit

Many thanks for your prompt and valuable reply. Your code achieves intended goals with more efficiency.

Also, your PS is well taken.

Best

ZigZag

Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.