BullCharts Forum Homepage
Forum Home Forum Home > BullCharts > BullScript
  New Posts New Posts RSS Feed: Problem averaging only positive values
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Problem averaging only positive values

 Post Reply Post Reply
Author
Message Reverse Sort Order / View First Unread Post
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Topic: Problem averaging only positive values
    Posted: 11 Jan 2018 at 10:54pm
Thanks, they work well!


I had a similar issue of 'only positive values' with calculating monthly returns in a scan. Min value fixed it.

((O-Ref(O,-21)) / min(O,Ref(O,-21)))*100;









Edited by maximo - 13 Jan 2018 at 6:16pm
Back to Top
cudderbean View Drop Down
Regular
Regular
Avatar

Joined: 02 Oct 2006
Location: Thailand
Posts: 40
Post Options Post Options   Quote cudderbean Quote  Post ReplyReply Direct Link To This Post Posted: 25 Jul 2017 at 5:27pm
Thanks for your earlier help, admin.

As always, not the Holy Grail, but perhaps just a warning sign to help make a decision if you feel you are in an overbought or oversold position.

Play with the parameters to suit your trading style. Let me know coding errors or improvements. Thanks.

LINE CHART FORMAT

[description=" Drifter. Measures how far closing price tends to drift away before reverting to mean"]
[target=default]

cn := input("MA close time periods",10,1);
cma := ma(c,cn,simple);
drift := c-cma;
n := input("MA drift time periods",5,1);
method := inputma("Driftma method",SIMPLE);
driftma := hist(ma(drift,n,method),1);
driftup := if(c>=cma,c-cma,undefined);
driftdwn := if(c<cma,cma-c,undefined)*-1;
sdfactor := input("Multiple of stdev",1.0,0);

avedriftup := hist(ma(driftup,250,s),1); {average amount drifts up over last 250 periods}
sd_driftup := hist(stdev(driftup,250),1); {st dev amount drifts up over last 250 periods}
extremdriftup :=avedriftup+(sd_driftup*sdfactor);

avedriftdwn := hist(ma(driftdwn,250,s),1); {average amount drifts down over last 250 periods}
sd_driftdwn := hist(stdev(driftdwn,250),1); {st dev amount drifts down over last 250 periods}
extremdriftdwn :=avedriftdwn+(sd_driftdwn*sdfactor)*-1;

[horzline=0]

[color=blue;name=drift]
drift;

[color=red;name=driftma]
driftma;

[color=purple;name=extremdriftup]
extremdriftup;

[color=purple;name=extremdriftdwn]
extremdriftdwn;

...............................

RIBBON FORMAT

[description=" Drifter. Measures how far closing price tends to drift away before reverting to mean"]
[target=ribbon]

cn := input("MA close time periods",10,1);
cma := ma(c,cn,simple);
drift := c-cma;
n := input("MA drift time periods",5,1);
method := inputma("Driftma method",SIMPLE);
driftma := hist(ma(drift,n,method),1);
driftup := if(c>=cma,c-cma,undefined);
driftdwn := if(c<cma,cma-c,undefined)*-1;
sdfactor := input("Multiple of stdev",1.0,0);

avedriftup := hist(ma(driftup,250,s),1); {average amount drifts up over last 250 periods}
sd_driftup := hist(stdev(driftup,250),1); {st dev amount drifts up over last 250 periods}
extremdriftup :=avedriftup+(sd_driftup*sdfactor);

avedriftdwn := hist(ma(driftdwn,250,s),1); {average amount drifts down over last 250 periods}
sd_driftdwn := hist(stdev(driftdwn,250),1); {st dev amount drifts down over last 250 periods}
extremdriftdwn :=avedriftdwn+(sd_driftdwn*sdfactor)*-1;

[color=darkgreen]
[fillstyle=solid]
drift>=driftma;

[color=red]
[fillstyle=solid]
drift<driftma;

[color=white]
Back to Top
cudderbean View Drop Down
Regular
Regular
Avatar

Joined: 02 Oct 2006
Location: Thailand
Posts: 40
Post Options Post Options   Quote cudderbean Quote  Post ReplyReply Direct Link To This Post Posted: 20 Jun 2017 at 9:07pm
Thank you very kindly for your help.

That neat little function "undefined" does the trick. I have a feeling I have come across this problem before in other coding, so with your help solving it, it opens up more coding possibilities for me.

I am travelling at the moment, but when I get back to Aus, I will post the completed code for my indicator above.

Thanks again.
Back to Top
administrator View Drop Down
BullCharts Staff
BullCharts Staff


Joined: 09 Sep 2004
Location: Australia
Posts: 2
Post Options Post Options   Quote administrator Quote  Post ReplyReply Direct Link To This Post Posted: 20 Jun 2017 at 10:54am

Please try undefined.

drift := if(C>cma,c-cma,undefined);

Back to Top
cudderbean View Drop Down
Regular
Regular
Avatar

Joined: 02 Oct 2006
Location: Thailand
Posts: 40
Post Options Post Options   Quote cudderbean Quote  Post ReplyReply Direct Link To This Post Posted: 01 Jun 2017 at 10:02pm
I am trying to create an indicator that measures how far the close is drifting away from the simple 10 day moving average of the close. So that I can see when the close may (?!) be drifting too far away from the mean.

[target=default]

cma:= ma(c,10,simple);
drift := c-cma;
avedrift := ma(drift,250,s); {average amount it drifts away over last 250 periods}

[horzline=0]

[color=blue]
drift;
[color=red]
avedrift;

Try this coding out on WPL for previous 250 days up until 19 May 2017.

The average drift away appears as .098

The PROBLEM is that the code is also including in its calculations negative values where the close also drifts away below the average...where c-cma returns a value less than zero.

I need the code to calculate how far the bull drift tends to move upwards away ONLY when the close is positive above the average. 

When I use Excel to calculate ONLY the positive values >= the average, the average becomes quite different     .482   [and .89 when you add a standard deviation]

So I substituted these lines:
drift := if(c>=cma,c-cma,cma-c);
drift := if(c>=cma,c-cma,0);
drift := if(c>=cma,c-cma,abs(cma-c));

No good.

The problem is that the IF function in BC demands 3 arguments..if ( a, then b, otherwise c)
Whereas Excel technically may only demand 2 arguments:
if      close>cma,   then close-cma,   otherwise ""[i.e. blank which is numerically different from zero]

How can I get BC to calculate when averaging only the positive values above the MA

[I do plan to add refinements like standard deviations to find true extremes, but the basic average is proving tricky...will post end result if successful with coding]

Am I missing the obvious. Anyone with more coding experience, your help would be much appreciated.
Thank you.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.69
Copyright ©2001-2010 Web Wiz