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

Notification

Icon
Error

Options
Go to last post Go to first unread
sunman4008  
#1 Posted : Wednesday, April 19, 2017 1:59:03 PM(UTC)
sunman4008

Rank: Member

Groups: Registered Users, Subscribers, Unverified Users
Joined: 3/7/2017(UTC)
Posts: 10

Hello,

Sorry..new to MetaStock formula.   I am writing code.   When I plot the indicator, I see 10 plots from name of Value.

How do I change the plot name from Value to something else?  How do I make sure some of the variables don't get plotted?

mstt  
#2 Posted : Wednesday, April 19, 2017 10:28:59 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 sunman

Here's an example of an indicator that creates a number of variables but has only one output (the last line of code). The output line for this indicator is the last line of code. Variables are created before the output line but will not be plotted on a chart unless an additional output is created by, let's say, Start; .

 {Date Filter}  {Roy Larsen, 2003-2015, 12/5/15} Sd:=Input("Start day"  ,1,31,1); Sm:=Input("Start month",1,12,1); Sy:=Input("Start year" ,1980,2015,2011); Ed:=Input("End day"    ,1,31,31); Em:=Input("End month"  ,1,12,12); Ey:=Input("End year"   ,1980,2020,2013); Start:=(DayOfMonth()>=Sd AND Month()=Sm AND Year()=Sy) OR Year()>Sy OR (Year()=Sy AND Month()>Sm); End:=(DayOfMonth()<=Ed AND Month()=Em AND Year()=Ey) OR Year()<Ey OR (Year()=Ey AND Month()<Em); Start AND (End OR (Start AND Alert(Start=0,2)));

 {Date Filter + Start}  {Roy Larsen, 2003-2015, 12/5/15} Sd:=Input("Start day"  ,1,31,1); Sm:=Input("Start month",1,12,1); Sy:=Input("Start year" ,1980,2015,2011); Ed:=Input("End day"    ,1,31,31); Em:=Input("End month"  ,1,12,12); Ey:=Input("End year"   ,1980,2020,2013); Start:=(DayOfMonth()>=Sd AND Month()=Sm AND Year()=Sy) OR Year()>Sy OR (Year()=Sy AND Month()>Sm); End:=(DayOfMonth()<=Ed AND Month()=Em AND Year()=Ey) OR Year()<Ey OR (Year()=Ey AND Month()<Em); Start AND (End OR (Start AND Alert(Start=0,2))); Start;

So you can output as many variables as you want on a chart, but usually indicators only have one output, which is probably the last line of code, and it often won't have a name. If the intended output does have a name then that name can be used to create the output.

At any rate what you need to do is study a few of the standard indicators that come with MetaStock.

Roy

sunman4008  
#3 Posted : Wednesday, April 19, 2017 10:49:03 PM(UTC)
sunman4008

Rank: Member

Groups: Registered Users, Subscribers, Unverified Users
Joined: 3/7/2017(UTC)
Posts: 10

Hello, Ok...If I understand it correctly, I modified my code and it looks like the following. It should only print test which is hard code right now to 2 for testing. Instead, it shows 11 variables. strat4:=0; test:=6; var1 :=0; var2 :=0; var3 :=0; var4 :=0; BUFFER :=0; BUFFER2 :=0; BEARISHSTOP :=0; BULLISHSTOP :=0; var1 = HHV(H,10); var2 = HHV(H,30); var3 = HHV(H,200); var4 = (var1 + var2)/1.612; BUFFER = ATR(26)*0.26; BUFFER2 = ATR(26)*0.69; BEARISHSTOP = var2 + BUFFER2; BULLISHSTOP = var2 - BUFFER2; pvtbull:= If(C>Ref(var4,-25) AND (C>Ref(var3,-25)) AND (C >= var2) AND ((Ref(C,-1)<Ref(var4,-26)) OR (Ref(C,-1)<Ref(var3,-26))) AND ((Ref(C,-2)<Ref(var4,-27)) OR (Ref(C,-2)<Ref(var3,-27))),1,0); pvtbear:= If(C<Ref(var4,-25) AND (C<Ref(var3,-25)) AND (C <= var2) AND ((Ref(C,-1)>Ref(var4,-26)) OR (Ref(C,-1)>Ref(var3,-26))) AND ((Ref(C,-2)>Ref(var4,-27)) OR (Ref(C,-2)>Ref(var3,-27))),-1,0); strat4= If( (Ref(strat4,-1)=1) OR ((Ref(pvtbull,-1)=1) AND (Ref(strat4,-1)<>-2)) AND ((C>=var2) OR (var1<var2)) AND (H < Ref(H,-1)),2,0); strat42:= If( (Ref(pvtbull,-1)=1) AND ((C>=var2) OR (var1<var2)) AND (H>=Ref(H,-1)),1,0); strat43:= If( (Ref(strat4,-1)=-1) OR ((Ref(pvtbear,-1)=-1) AND (Ref(strat4,-1)<>2)) AND ((C<=var2) OR (var1>var2)) AND (L > Ref(L,-1)),-2,0); strat44:= If( (Ref(pvtbear,-1)=-1) AND ((C<=var2) OR (var1>var2)) AND (L<=Ref(L,-1)),-1,0); test = 2; test
mstt  
#4 Posted : Thursday, April 20, 2017 2:24:39 AM(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)
You do not need (or want) to set variables to zero or some other value before defining the actual variable required. There can be situations where a defined variable can be assigned a different value but in your case the initial definitions are superfluous and just complicate (and mess up) things. Keep in mind that the MetaStock Formula Language is purely sequential with no Jumps or Branches etc. (with the possible exception of the PREV function). Also keep in mind that MetaStock only allows up to 20 different variables in any given indicator, Exploration column or Expert segment. What you can do to get around that particular difficulty is to re-use a variable name once its initial task has been completed. I have formulas that run into 60+ variable functions while just using a set of only 19 or 20 variable names. Forget the setting of variable names such var1,var2 etc. and just add a semicolon after var1 and before = and you'll eliminate all the spurious outputs var1 = HHV(H,10); var2 = HHV(H,30); var3 = HHV(H,200); var4 = (var1 + var2)/1.612; BUFFER = ATR(26)*0.26; BUFFER2 = ATR(26)*0.69; BEARISHSTOP = var2 + BUFFER2; BULLISHSTOP = var2 - BUFFER2; Roy
mstt  
#5 Posted : Thursday, April 20, 2017 2:34:41 AM(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)
For what it's worth here's a "Trade Equity" indicator formula that uses external indicators as well as Input()functions and the odd constant. Remember, MetaStock is a purely sequential language and works from top to bottom without loops or branches etc. (not counting PREV as I mentioned earlier). {Trade Equity GV LE} {Roy Larsen, 20/12/15, v8.6} {} A:=Input("Trade Equity LE Options",0,127,0); B:=Input("Entry, 1=O 2=C 3=H 4=L 5=Stop",1,5,1); Z:=Input("Exit, 1=O 2=C 3=H 4=L 5=Stop",1,5,1); G:=Input("Entry $",0,999,5); J:=Input("Exit $",0,999,5); D:=Input("Entry/Exit Delays 00-55",0,55,11); K:=1000; F:=1;{Equity/Factor} {} I:=Fml("Green-Red DWF System"); N:= I>0; Ns:=0; X:= I<0; Xs:=0; {} F:=ExtFml("GV.SetVar","Cf",If(F=0,1,F)); I:=ExtFml("GV.SetVar","Le",A); Xd:=LastValue(Int(Frac(D/9.9)*10)); D:=LastValue(Int(D/10));Ns:=(B=5)*Ns; F:=ValueWhen(1+D,1,If(Abs(F)>1,Abs(F),-F)); M:=If(Ns>0,Min(H,Max(L,Ns)),If(B=1,O,If(B=3,H,If(B=4,L,C)))); N:=N*(Alert(N=0,2)+(Cum(N>-1)=1)); X:=X*Alert(X=0,2)*(Z<5); N:=ValueWhen(1+D,1,N);N:=If(B<5,N,Ns>0); N:=If((F<=1)*(F>0)*(F*K<M),(K=0)*N,N); Xs:=(Xs>0)*Min(H,Max(L,Xs)); Y:=If(Xs>0,Xs,If(Z=1,O,If(Z=3,H,If(Z=4,L,C)))); X:=ValueWhen(1+Xd,1,X);X:=X+Xs>0; Y:=If((Z<5)*(X=0),C{Y},Y);Y:=If((Xs>0)*N*X,Xs,Y); I:=Cum(Abs(F)+K+N+X>-1)=1;N:=(I>-1)*N; Y:=If((N+X=0)*Alert(N*X,2),ValueWhen(2,1,Y),Y); R:=BarsSince(I+N)<(BarsSince(I+X)+(Cum(N)=1 AND Cum(I+X)=1)); R:=If((N+X>1)*(Alert(R,2)+((D+Xd<1) *(B<>2)*(Z>1)*(Max(B,Z)>4 OR B<>Z))),1,R); U:=R*Alert(R=0,2)+I;{M:=If(I*(N=0),C,M);} Rx:=Alert(R,2);Q:=Rx*(LastValue(Cum(1))=Cum(1)); Z:={Q+}(R=0)*Rx;D:=A<99;A:=ValueWhen(1,U,If(M,M,1)); F:=ValueWhen(1,U,If(F<0,K*Abs(F), If(F<=1,Int((F*K-G)/A)*A+G,F*A+G)))*(K>0); B:=Rx*(1+BarsSince(U)); I:=(F-G)*Y/A-F;U:=(F-G)*(A-Y)/A-G; N:=(Z+Q>0)*If(D,If(K,I-J*Z,Y-A),If(K,U-J*Z,A-Y)); Xs:=Rx*If(D,If(K,I-Z*J,Y-A),If(K,U-Z*J,A-Y)); X:=Cum((Z+Q>0)*(N>0)*(B-1));Xd:=Cum((Z+Q>0)*(N<=0)*(B-1)); M:=Cum(N)+K+R*(Q=0)*If(D,If(K,I,Y-A),If(K,U,A-Y)); I:=ExtFml("GV.SetVar","Mx",Rx*If(D,If(K,(F-G)* ((H/A)-1)-G-J*Z,H-A),If(K,(F-G)*((A-L)/A)-G-J*Z,A-L))); I:=ExtFml("GV.SetVar","Mn",RX*If(D,If(K,(F-G)* ((L/A)-1)-G-J*Z,L-A),If(K,(F-G)*((A-H)/A)-G-J*Z,A-H))); I:=ExtFml("GV.SetVar","A",A);I:=ExtFml("GV.SetVar","Cn",G); I:=ExtFml("GV.SetVar","Cp",K);I:=ExtFml("GV.SetVar","Cx",J); I:=ExtFml("GV.SetVar","Eq",M);I:=ExtFml("GV.SetVar","F",F); I:=ExtFml("GV.SetVar","N",N);I:=ExtFml("GV.SetVar","Tr",R); I:=ExtFml("GV.SetVar","X",X);I:=ExtFml("GV.SetVar","Xd",Xd); I:=ExtFml("GV.SetVar","Xs",Xs);I:=ExtFml("GV.SetVar","Y",Y); M; {100*(M-K)/K;} Roy
sunman4008  
#6 Posted : Thursday, April 20, 2017 2:47:07 AM(UTC)
sunman4008

Rank: Member

Groups: Registered Users, Subscribers, Unverified Users
Joined: 3/7/2017(UTC)
Posts: 10

Hello, I did that because pvtbull is going to access Ref(strat4) variable which is declared on the second line. If I don't initialize it......when it gets to the pvtbull calculation....it gives me an error because as you mentioned the code goes from top to bottom.
sunman4008  
#7 Posted : Thursday, April 20, 2017 2:47:37 AM(UTC)
sunman4008

Rank: Member

Groups: Registered Users, Subscribers, Unverified Users
Joined: 3/7/2017(UTC)
Posts: 10

Hello, I did that because pvtbull is going to access Ref(strat4) variable which is declared on the second line. If I don't initialize it......when it gets to the pvtbull calculation....it gives me an error because as you mentioned the code goes from top to bottom. strat4 also uses data from pvtball
wabbit  
#8 Posted : Thursday, April 20, 2017 3:49:09 AM(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)

The reason you're getting a lot of "values" returned from your codes is that you're using the equal comparison "=" in places where you need to be using the assignment operator ":="

You were initialising variables to zero at the top of the code, then you performed a comparison instead of reassigning a value.

Code:

var1 := 0; {assignment, unnecessary}

...

var1 = HHV(H,10); {comparison, Boolean, returns TRUE (1) or FALSE (0)}

sunman4008  
#9 Posted : Thursday, April 20, 2017 12:46:42 PM(UTC)
sunman4008

Rank: Member

Groups: Registered Users, Subscribers, Unverified Users
Joined: 3/7/2017(UTC)
Posts: 10

Hellp Yeah..noticed that. Strange. Seems like if I keep on changING the variable over aND over....I will get a new plot too. Most language you initialize....change variables and then you choose which one to plot. If one ploy allowed...the last line is it but not here
wabbit  
#10 Posted : Friday, April 21, 2017 12:26:51 AM(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)

Perhaps if you gave us a full description of what you're trying to achieve, we'd get a better understanding of the problem: what you're trying to do might not even be achievable in the Metastock Formula Language, or it might require so much self-referencing (PREV) that it will be too slow to actually be of any use.  From reading the snippets of code you've posted, I still have no idea what you're trying to do.

Remember too: MS Formula Language is a scripting language, not a programming language like anything you've encountered before.  It's designed for traders (not programmers) to write basic functions to aid their trading; it's not a full-functioning high-level programming environment for programmers who happen to trade.  It's very primitive; you can achieve a lot, but not everything.

[... edit ...]

And another thing.... please don't post code snippets.  Post the ENTIRE code.  If you have the holy grail of trading systems, well done, but people are more willing to give you their time when they can see all of the code and all of the issues, not small parts of "I've coded something like..."  Often, it's the bits left out of the posted code which are the bits which cause the problems.

The forum code tags are working again (for now) so please put the word "code" in square brackets "[" and "]" above the code section in your post, and close the tag with "/code" in square brackets "[" and "]" at the end of the code section in your post.

Edited by user Friday, April 21, 2017 12:35:43 AM(UTC)  | Reason: Added more words

mstt  
#11 Posted : Saturday, April 22, 2017 5:05:54 AM(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 wabbit Thanks for the reminder on use of code for marking the beginning and end of any formula. I'd completely forgotten there was such a thing. It makes life a little easier. Here's hoping it makes the TE indicator a little easier for anyone wanting to figure out what it does a little easier. For the uninitiated the "Fml("Green-Red DWF System")" is not disclosed but can be swapped out in favor of any system formula that generates a +1 (or higher) for a Buy and a -1 (or lower) for a Sell. The "I" variables toward the end of the indicator can be discarded (or commented out with braces {}) as they generally relate to cumulative results (of a parcel of securities) across Explorations and Expert Advisor summaries. Anyone wanting more information about my TE tools can email me at rlarsen_99@yahoo.co.uk. Roy

Code:

{Trade Equity GV LE}
{Roy Larsen, 20/12/15, v8.6}
{}
A:=Input("Trade Equity LE Options",0,127,0);
B:=Input("Entry, 1=O 2=C 3=H 4=L 5=Stop",1,5,1);
Z:=Input("Exit, 1=O 2=C 3=H 4=L 5=Stop",1,5,1);
G:=Input("Entry $",0,999,5);
J:=Input("Exit $",0,999,5);
D:=Input("Entry/Exit Delays 00-55",0,55,11);
K:=1000; F:=1;{Equity/Factor}
{}
I:=Fml("Green-Red DWF System");
N:= I>0;
Ns:=0;
X:= I<0;
Xs:=0;
{}
F:=ExtFml("GV.SetVar","Cf",If(F=0,1,F));
I:=ExtFml("GV.SetVar","Le",A);
Xd:=LastValue(Int(Frac(D/9.9)*10));
D:=LastValue(Int(D/10));Ns:=(B=5)*Ns;
F:=ValueWhen(1+D,1,If(Abs(F)>1,Abs(F),-F));
M:=If(Ns>0,Min(H,Max(L,Ns)),If(B=1,O,If(B=3,H,If(B=4,L,C))));
N:=N*(Alert(N=0,2)+(Cum(N>-1)=1));
X:=X*Alert(X=0,2)*(Z<5);
N:=ValueWhen(1+D,1,N);N:=If(B<5,N,Ns>0);
N:=If((F<=1)*(F>0)*(F*K

Edited by user Tuesday, April 25, 2017 8:52:20 AM(UTC)  | Reason: Not specified

mstt  
#12 Posted : Saturday, April 22, 2017 5:11:12 AM(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)
I see that went well. Still, I guess most people are capable of adding a new-line character in the appropriate places so I'll leave it at that. Roy
wabbit  
#13 Posted : Saturday, April 22, 2017 11:34:23 AM(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)
Using code tags
Users browsing this topic
Guest (Hidden)
Similar Topics
A custom indicator that plots the number of stocks above 50 DMA (Advanced Coding Techniques)
by anirban81 9/18/2013 9:37:10 PM(UTC)
Better Candlestick Price Plots (Product Wish List)
by nasdaqtrader 2/20/2013 8:24:40 PM(UTC)
Variable moving average - anyone knows what it plots in the null area? (MetaStock Developer's Kit (MDK) Assistance)
by exito100 10/17/2009 11:31:10 PM(UTC)
Can anybody tell me if I can create scatterplots in Metastock Pro 9.0? (Formula Assistance)
by SAILBEARS1 10/22/2007 1:05:08 PM(UTC)
removing Null (N/A) plots the easy way (Advanced Coding Techniques)
by Jose 4/24/2007 11:22:05 AM(UTC)
MA overlay plots values incorrectly (MetaStock)
by alexj 1/29/2006 6:15:43 PM(UTC)
How to reference data arrays in multiple security plots (Formula Assistance)
by drobato 8/19/2005 3:21:02 AM(UTC)
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.