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

Notification

Icon
Error

Options
Go to last post Go to first unread
theghost  
#1 Posted : Saturday, October 29, 2011 3:29:42 PM(UTC)
theghost

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/4/2005(UTC)
Posts: 63
Location: Poole Dorset England

Hello All.
I have a formula that displays a dual coloured Moving Average (If it's displayed as large dots)
Code:

{Plot MA in two colors}
A:=Mov(C,5,E);
If(A>=Ref(A,-1),A,0);
If(A<Ref(A,-1),A,0);
ValueWhen(1,0,A);


What I would like to be able to do is incorporate a"% shift" function to this also,, so I can shift the MA by say 1.5%
(Actually if this is possible,, I would then plot this again, so as to create a banding around the price bars,).

Not sure how to do it.
I did look here;
http://trader.online.pl/MSZ/e-w-Moving_Average_Shifted_Bands.html
& found a formula with a %shift function,,,but I just wasn't able to do it. :-(

Hope somebody can help me with this please, I think it would be quite useful,, a MA band that changes colour also.

Many thanks for all your time
The Ghost
jjstein  
#2 Posted : Saturday, October 29, 2011 5:24:44 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Try this:

{Plot MA in two colors}
A:=Mov(C,5,E);
BandPct:=Input("Shift %",1,10,1.5)/100;
Band:=A*BandPct;
BandHi:=A+Band;
BandLo:=A-Band;

BandHi;
BandLo;


You'll have to click on each band separately to change its color.

theghost  
#3 Posted : Sunday, October 30, 2011 3:32:22 AM(UTC)
theghost

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/4/2005(UTC)
Posts: 63
Location: Poole Dorset England

Hi jjstein,, many many thanks for your swift reply,, actually it's not quite what I'm looking for. :-(
Let me try and explain a bit more as I don't think I was too clear before.

1st,, a mistake by me in my original formula (Copy & paste went hey-wire I think,, it's when you copy text out of Metastock into texpad it seems to slightly go wrong ) :-)
Original code for #2 Colour MA is as follows;
Code:

{Plot MA in two colors}
A:=Mov(C,21,E);
If(A>=Ref(A,-1),A,0);
If(A<Ref(A,-1),A,0);
ValueWhen(1,0,A);

***ACTUALLY,,It's done it again,, copying into Firefox,, Meant to be If(A,,THEN THE GREATER THAN SYMBOL on line 2,, & line 3 If(A LESS THAN SYMBOL,,,NOT>,,,&Lt,, Not sure how tyo get over this,, tried editing a few times,, couldn't change it)*** Sorry
2nd,, "A picture paints a 1000 words!",, I'll insert a picture to try and illustrate 1st jjstein.
UserPostedImage

Hopefully you can see what I'm really trying to achieve jjstein. My original formula lets me create a MA that can be dual coloured. What I would really like to do is be able to plot 2 MA's on the same chart,, say two 5 period MA's that are dual coloured. By using the vertical shift formula,, I can create a banding around the price (IE say 1.5 as a setting for vertical shift).

The formula that you wrote is 50-70% there,, but not in a way that the MA's can be dual coloured.
Maybe my above formula needs to be written to incorporate the vertical shift as well (Which I don't know how do do, hence this post :-),, then I just plot this twice on the chart.
Or, might be better/ easier to create 2 MA's dual coloured with vertical shift all within 1 formula?

I hope this kind explains it a bit better jjstein.

Again,, many many thanks for your help.
Much appreciated.

Many thanks
TheGhost
jjstein  
#4 Posted : Sunday, October 30, 2011 10:37:36 AM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
The purpose and logic of your code is not clear -- you'll have to go through it line-by-line. Please note that if you're trying to make a dual-colored line, it cannot be done, insofar as I know (you're not the first to try/want it).

Usually, the closest you can come is to set the Style to Vertical Line, plot the value conditionally twice, thicken the lines and get an area graph, like a MACD Histogram. You can be a little creative, like setting the condition for numbers above zero in green, below in red, or set conditions for a rising slope to green, declining slope in red. This usually needs to be plotted in its own sub-window.

HOWEVER, after thinkout about it some, here is something that might be what you want; note that it will take some fancy formatting:

{ Indicator ABOVE }
MA:=Mov(C,21,E);
BandPct:=Input("Shift %",1,10,1.5)/100;
Band:=MA*BandPct;
BandHi:=MA+Band;

BandHi;
If(C>MA,MA,BandHi);

Drag the ABOVE indicator into the graph; on the Color/Style tab, set color to green, style to dotted and click OK. Now, click the bottom line and set the color to WHITE.

{ Indicator BELOW }
MA:=Mov(C,21,E);
BandPct:=Input("Shift %",1,10,1.5)/100;
Band:=MA*BandPct;
BandLo:=MA-Band;

BandLo;
If(C<MA,MA,BandLo);

Do the same, but set color to RED.


Should look like this: UserPostedImage


theghost  
#5 Posted : Sunday, October 30, 2011 11:06:12 AM(UTC)
theghost

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/4/2005(UTC)
Posts: 63
Location: Poole Dorset England

I'm still here! :-)
Many thanks again for your reply jjstein,,
Sorry,, but again,, maybe I've confused the issue more with the images! :-(
Here is what I have; (Another image which I hope doesn't confuse things).
UserPostedImage
If possible,, all I'm looking to do is add a vertical shift function to this formula in the image above. Then,, I can set this shift to say +2.
THEN,, I add the formula again to the same price chart,, but with a shift of -2,, hence creating banding around the price that is dual coloured. :-)

Possible jjstein?
I just can't get it.
I hope this makes sense this time jjstein.
Many thanks again for trying though.
It's a tough 1 !! ;-)

Best regards jjstein
TheGhost :-)
PS,, as a noye,, The MA would be a lot smaller,, IE 5 or 8 period,, not 21 :-),, just thought I'd mention it.
jjstein  
#6 Posted : Sunday, October 30, 2011 1:35:20 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Is this it?

UserPostedImage


{Plot MA in two colors}
Band:=Mov(C,21,E);
BandPct:=Input("Shift %",-10,10,2.0)/100;
Band:=Band+Band*BandPct;

If(Band>=Ref(Band,-1),Band,0);
If(Band<Ref(Band,-1),Band,0);
ValueWhen(1,0,Band);


If so, once one band is colored & styled correctly, just click on it, Copy & Paste, click and select one of them, then double-click to change "Shift" to "-2".


theghost  
#7 Posted : Sunday, October 30, 2011 3:37:12 PM(UTC)
theghost

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/4/2005(UTC)
Posts: 63
Location: Poole Dorset England

Yes! That's more like it jjstein! :-)
As it happens I've just received a formula that does a similar job,, but,, well is different.
(I haven't wrote it, (Not clever enough! ) :-)
The code is as follows;
Code:

{Plot MA in two colors}
N:=Input("Moving Average Periods",1,99,5);
D:=Input("Vertical Shift %",-100,+100,0)/100;
A:=Mov(C,N,E)*(1+D);
If(A>=Ref(A,-1),A,0);
If(Ref(A,-1)>A,A,0);
ValueWhen(1,0,0);


Any thoughts on this code jjstein?
Many thanks for trying again,,,I must look at the 2 together now,, see how they line up.
many thanks again jjstein.
Kind regards
TheGhost

jjstein  
#8 Posted : Sunday, October 30, 2011 6:04:02 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
The code is similar; the 1st INPUT() lets you adjust the moving average, while the "A" variable simplifies what I did. I prefer named variables, as they're easier to understand.

Actually, I'm not quite sure how this code produces the plot it does -- I don't usually use the "dotted" style. I think I understand the two IF() statements, but not why the VALUEWHEN() is there.

Roy, feel free to chime in here.

mstt  
#9 Posted : Sunday, October 30, 2011 6:38:37 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)
Hi Johnathan

And I thought I could stay below the radar. I didn't want to draw attention but you got me anyway. I guess my style is as good as (or as bad as) a signature.

The purpose of ValueWhen(1,0,A), for example, is to create an N/A result as the final output of the indicator. This has the effect of allowing properly functioning indicator plots to move beyond the borders of the chart window while remaining scaled to the price plot. Dotted lines are used for a multi-coloured plot because dotted lines leave no trace as they move between one bar and the next. Dashed and solid lines, on the other hand, do leave a trace, and this is particularly unwanted when switching from value to zero or vice versa.

Roy
jjstein  
#10 Posted : Monday, October 31, 2011 12:40:20 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Roy -- That begs two questions, in my mind:

1. "The purpose of ValueWhen(1,0,A), for example, is to create an N/A result as the final output of the indicator."

Any reason that the ValueWhen() can't be generic, then?

ie: ValueWhen(1,0,0); rather than: ValueWhen(1,0,whatever);


2. "This has the effect of allowing properly functioning indicator plots to move beyond the borders of the chart window while remaining scaled to the price plot."

Most indicators don't seem to have a problem with this, that I've noticed. What is so special about the code for this one? (Taking out the ValueWhen() makes the plot go wonky, but why?)


mstt  
#11 Posted : Monday, October 31, 2011 3:03:01 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi Johnathan

There's no reason why ValueWhen(1,0,0) can't be used, and it was in the formula that I sent to TG. Any expression that generates an N/A output across all bars would be fine. I like ValueWhen() because of it's known ability to generate an N/A until the "Expression" parameter is TRUE. The "Data Array" parameter is irrelevant for this situation since it will never display anyway.

I'm not sure that I get your meaning about "most indicators" so I may be about to try and answer the wrong question(s). I've noticed that indicators generating a valid result as the last (or only) output tend to fill a window's vertical space except for top and bottom bands of around 10%. The one notable exception is an unscaled flat-line indicator that shares the window with a non-flat-line indicator.

Once an indicator has a scale applied to it will plot within the 80% vertical range that I just mentioned. In the "flat-line off the bottom of the chart" scenario, that indicator can be located, marked and scaled by drawing the cursor slowly through the bottom of the window (thicker flat lines will be visible but lines with the default thin setting will not be).

What having an N/A result as the last output appears to do is remove the restriction that normally forces a valid plot to stay within the 80% range of a window's upper and lower extremes (ignoring the unscaled flat line which is able to sit at the extreme lower edge of the window). The price chart and other "normal" indicators will continue to display within the 10%-90% vertical range of a window while an "N/A modified" indicator apparently ignores the 10% and 90% limits that apply to normal indicators. Why? I have no idea. In the case of a dual-color moving average, it should be scaled to the same scale as the price bars. That prevents the required dotted lines from reaching for the stars (plotting somewhere above the upper limit of the window).

So the short answer to your second question is that we don't know why an N/A result has the effect it does, but we do know what that effect is. It's presence allows the visible output(s) of an indicator to "float" out of view (above or below) the normal vertical extremities of an inner window.

Roy

jjstein  
#12 Posted : Monday, October 31, 2011 3:47:02 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Roy -- Thanks for the detail. Are there other expressions which generate an N/A? I'm a bit curious now, as I would not have guessed that would solve a plotting problem.

Just notice something: When the indicator is right-clicked for SCALING OPTIONS, it is set to "Overlay", and even if one of the other options is checked, it does not change -- right-clicking again still shows "Overlay".

I assume this is related to the Indicator's final value of N/A -- but is there any way to make sure price and indicator are on the same scale? They appear to be -- or to be close -- but the "Overlay" setting bothers me.


mstt  
#13 Posted : Monday, October 31, 2011 4:50:38 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi Johnathan

I'd guess that almost any "Nth" and "Condition" parameter function would substitute for ValueWhen(). The only requirement is that the "Condition" parameter never comes TRUE. If it does come TRUE the indicator will bring the unwanted portion of the plot back into the viewable area and distort the price plot that it's scaled to. I imagine that almost any code that generates an N/A result in an exploration might might be a candidate for the same result when used in an indicator. I'm sure you're aware that ValueWhen() and similar functions should never be used in a "Minimum Periods" exploration because of this tendency to generate an N/A result if the Condition parameter remains FALSE.

To be honest I've never used (or even noticed as far as I can remember) the "overlay" option. My way of making sure that the "N/A" indicator is scaled to what I want is to disable that output {with braces} and then set a normal output to the appropriate scale. Typically this has been "left" in my default template since the days of MS version 6.5. With that done the N/A element can be made active again.

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