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

Notification

Icon
Error

Options
Go to last post Go to first unread
supremeinfo  
#1 Posted : Wednesday, August 8, 2012 4:23:22 AM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

hello everybody ,i am new to metastock although i have gone through metastock formula book once . i want to test a system which will give a buy signal if the price rises by 3% after a crossover of 2 ema & 36sma . cross(mov(c,2,e),mov(c,36,s) and roc(c,5,%) >3 i am not getting the desired result . request you all to correct me
jjstein  
#2 Posted : Wednesday, August 8, 2012 3:20: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)
Hi Supreme! Welcome to the forum!

First: The short answer is that you need to learn the VALUEWHEN() function.

Second: FYI, use "code tags" in your msg when posting a formula (see below), otherwise the forum software can leave out formula code -- very annoying!

Third: Your explanation is not complete -- it does not mention the "5" in rate of change, and you haven't specified which "price" -- is it the CLOSE, HIGH, TYPICAL, MEDIAN or what?

Fourth: You have not specified a SELL or RESET condition.

What your CODE says is: Signal if at the time of crossover, the 5-day rate-of-change is greater than three percent.

What your EXPLANATION says (I think) is: After a crossover, signal when the current price (CLOSE or HIGH?) rises three percent from the price when it crossed.

Assuming you meant the CLOSE, and the percent difference between the current and at-time-of-crossover, what you want might be something like this:
Code:

Condition:=Cross(Mov(C,2,E),Mov(C,36,S));
CloseAtCrossover:=ValueWhen(1,Condition,C);
Value:=(C-CloseAtCrossover)/CloseAtCrossover;
Buy:=Value>0.03;
Buy;


NOTE: Once you "flesh out" the above with a sell/cancel/reset, the above can have the signal be filtered, and/or made adjustable, using the INPUT() function.

Click the link below for the PRIMER, if you haven't already got it.
supremeinfo  
#3 Posted : Wednesday, August 8, 2012 7:28:38 PM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

Hi johnathan Thanks for your help...... what i am actually trying to do is code a simple pyramid strategy which will give me a first buy signal at the crossover of the 2 MA. second buy signal will come at price rising by 3 % from the crossover price (high) . third buy comes at 4.2% from the crossover price (high) .i do not want to make things complex so i want to test the strategy seperately with 3% & 4.2 % me using the ROC function was wong .My sell signal will be reverse of all the above Warm regards supreme
jjstein  
#4 Posted : Wednesday, August 8, 2012 9:55:29 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)
OK. What's unclear is the sell signal -- would any one of the conditions do it, or all three, or what? The following assumes a reverse crossover would do it -- otherwise, are you saying a 3% drop and 4.2% drop -- from where, the high/low/close?

Anyway, the following code which will "filter" redundant signals, and you will need to understand the FMLVAR() function. There are three separate indicators; each formula name has an underscore "_".
Code:

_Pyramid1
Entry:=Cross(Mov(C,2,E),Mov(C,36,S));
Exit:=Cross(Mov(C,36,E),Mov(C,2,S));

{ REMOVE EXTRA SIGNALS, +1=Buy -1=Sell }
Signal:=Cum(IsDefined(Entry+Exit))=1; { Enough data ? }
Signal:=ValueWhen(1,Entry-Exit<>0 OR Signal,Entry); { Current }
Entry:=Signal*(Alert(Signal=0,2) OR Entry*Cum(Entry)=1); { Current or 1st }
Exit:=(Signal=0)*(Alert(Signal,2) OR Exit*Cum(Exit)=1); { Current or 1st }
Entry:=(Exit=0)*entry; { favor EXIT }
Signal:=Entry-Exit; { Signal }
Season:=ValueWhen(1,Signal<>0,Entry)*2-1; { Signal active }

{ PLOT }
Signal;





_Pyramid2
Condition:=FmlVar("_Pyramid1","Entry");
PriceAtCrossover:=ValueWhen(1,Condition,H);
Value:=(H-PriceAtCrossover)/PriceAtCrossover;

Entry:=Value>0.03 and FmlVar("_Pyramid1","Season")=1;
Exit:=FmlVar("_Pyramid1","Exit");

{ REMOVE EXTRA SIGNALS, +1=Buy -1=Sell }
Signal:=Cum(IsDefined(Entry+Exit))=1; { Enough data ? }
Signal:=ValueWhen(1,Entry-Exit<>0 OR Signal,Entry); { Current }
Entry:=Signal*(Alert(Signal=0,2) OR Entry*Cum(Entry)=1); { Current or 1st }
Exit:=(Signal=0)*(Alert(Signal,2) OR Exit*Cum(Exit)=1); { Current or 1st }
Entry:=(Exit=0)*entry; { favor EXIT }
Signal:=Entry-Exit; { Signal }
Season:=ValueWhen(1,Signal<>0,Entry)*2-1; { Signal active }

{ PLOT }
Signal;





_Pyramid3
Condition:=FmlVar("_Pyramid1","Entry");
PriceAtCrossover:=ValueWhen(1,Condition,C);
Value:=(C-PriceAtCrossover)/PriceAtCrossover;

Entry:=Value>0.042 and FmlVar("_Pyramid2","Season")=1;;
Exit:=FmlVar("_Pyramid1","Exit");

{ REMOVE EXTRA SIGNALS, +1=Buy -1=Sell }
Signal:=Cum(IsDefined(Entry+Exit))=1; { Enough data ? }
Signal:=ValueWhen(1,Entry-Exit<>0 OR Signal,Entry); { Current }
Entry:=Signal*(Alert(Signal=0,2) OR Entry*Cum(Entry)=1); { Current or 1st }
Exit:=(Signal=0)*(Alert(Signal,2) OR Exit*Cum(Exit)=1); { Current or 1st }
Entry:=(Exit=0)*entry; { favor EXIT }
Signal:=Entry-Exit; { Signal }
Season:=ValueWhen(1,Signal<>0,Entry)*2-1; { Signal active }

{ PLOT }
Signal;


supremeinfo  
#5 Posted : Wednesday, August 8, 2012 11:48:00 PM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

Thanks johnathan for your valuable time & effort The sell signal will be close below 36 SMA cross(mov(c,36,s),c) or a 15 period trailing stop loss (if long the TSL will be the high of 15bars behind or if short tsl will be low of 15bars behind..) If either of stoploss is hit all the 3 pyramid positions will exit.... Signal for selling short cross(mov(c,36,s),mov(c,2,e)) pyramid 1 : sell when the price drops by 3% from the price on the crossover day pyramid 2 : sell when the price drops by 4.2% from the price on the crossover day signal to cover short position cross(c,mov(c,36,s)) or price touching low of previous 15 th bar... To make things simple i was going to test the entire strategy in 3 parts seperately..... Warm regards & thanks again Supreme
supremeinfo  
#6 Posted : Thursday, August 9, 2012 5:24:32 AM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

Hello sir
jjstein  
#7 Posted : Thursday, August 9, 2012 2:00: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)
supremeinfo wrote:
if long the TSL will be the high of 15bars behind

So, there will be no stop for the first 14 bars of a trade?

supremeinfo wrote:
If either of stoploss is hit all the 3 pyramid positions will exit

What triggers the stop -- LOW, CLOSE, etc.?

supremeinfo  
#8 Posted : Thursday, August 9, 2012 7:17:59 PM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

sir, if long stop loss will be the highest high price of the 15th bars behind...... if short stop loss will be the lowest low price of the 15th bars behind.... so in all the previous 14 bars are ignored.....the above conditions will hold true only if todays price is higher then the high of the previous 15th bar if long.....or todays price is lower then the low of the previous 15th bar if position is short low will trigger the stop if position is short high will trigger the stop if position is long
Code:


Condition:=Cross(Mov(C,2,E),Mov(C,36,S));
CloseAtCrossover:=ValueWhen(1,Condition,C);
Value:=(C-CloseAtCrossover)/CloseAtCrossover;
Buy:=Value>0.03;
Buy;

with refrence to the above code given by you which is enough to test my strategy the only problem is it gives a buy signal before the next crossover or immediately after the exit of the previous trade. For the above given code for buy order my sell will be
Code:

cross(mov(c,36,s),c) 

sell short order will be
Code:


Condition:=Cross(Mov(C,36,s),Mov(C,2,e));
CloseAtCrossover:=ValueWhen(1,Condition,C);
Value:=(CloseAtCrossover-c) / C;


sell:=Value<0.03;

sell;

buy to cover order

cross(c,mov(c,36,s) 

plz correct me if i am wrong... warm regards supreme
supremeinfo  
#9 Posted : Monday, August 13, 2012 1:32:29 AM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

Hi Johnathan, Plz assist me to write the correct sell code for the above post ....still not getting results as desired Thanks & regards Supreme
jjstein  
#10 Posted : Monday, August 13, 2012 8:23:57 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)
Am on vacation this week, but will have a look when I get back.
jjstein  
#11 Posted : Tuesday, August 21, 2012 10:14:51 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)
supremeinfo wrote:
sir, if long stop loss will be the highest high price of the 15th bars behind


Well, here is something to try, though the HHV of the past 15 bars doesn't seem to work well -- it means that if today's close is lower than yesterday's high, you will be stopped out -- frequently.

Code:

{_Pyramid2}

{ VARIABLES }
FastMA:=Mov(C,2,E);
SlowMA:=Mov(C,36,S);
Entry:=Cross(FastMA,SlowMA); { Buy signal - start }
Exit:=Cross(SlowMA,FastMA); { Sell signal }

{ CONDITIONS }
PriceAtCrossover:=ValueWhen(1,Entry,C); { Close at time of signal }
Value:=(C-PriceAtCrossover)/PriceAtCrossover { percent change }
*(BarsSince(Entry)<BarsSince(Exit)); { after entry }

{ SIGNALS & STOPS }
Entry:=Value>0.03; { Buy signal - finish }

{ REMOVE EXTRA SIGNALS, +1=Buy -1=Sell }
Signal:=Cum(IsDefined(Entry+Exit))=1; { Enough data ? }
Signal:=ValueWhen(1,Entry-Exit<>0 OR Signal,Entry); { Current }
Entry:=Signal*(Alert(Signal=0,2) OR Entry*Cum(Entry)=1); { Current or 1st }
Exit:=(Signal=0)*(Alert(Signal,2) OR Exit*Cum(Exit)=1); { Current or 1st }
Entry:=(Exit=0)*entry; { favor EXIT }
Signal:=Entry-Exit; { Signal }
Season:=ValueWhen(1,Signal<>0,Entry)*2-1; { Signal active }

{ PLOT as SIGNAL: Style=VERTICAL LINE }
Signal;





{_Pyramid2_Stop}
TrailingStop:=HHV(HIGH,15); { Trailing stop }
Trade:=FmlVar("_Pyramid2","Season")=+1; { signal active, =1 }
Entry:=FmlVar("_Pyramid2","Signal")=+1; { signal active, =1 }

{ LATCH - Store value of stop }
Stop:=If(PREV=0, { no stop }
If(Trade and BarsSince(Entry)>=15,TrailingStop,0), { set stop }
If(CLOSE<=PREV,0, { signal or stop hit }
Max(PREV,TrailingStop) { raise stop }
));

{ PLOT as STOP: Style=DOT, Weight=3, advanced 1 bar }
ValueWhen(1,Ref(Stop,-1)>0,Ref(Stop,-1));





{_Test}
{_Pyramid2_with_stop}

{ VARIABLES }
Entry:=FmlVar("_Pyramid2","Signal")=+1; { Buy signal - start }
Exit:=FmlVar("_Pyramid2","Signal")=-1 or
Close<FmlVar("_Pyramid2_Stop","Stop"); { Sell signal }

{ REMOVE EXTRA SIGNALS, +1=Buy -1=Sell }
Signal:=Cum(IsDefined(Entry+Exit))=1; { Enough data ? }
Signal:=ValueWhen(1,Entry-Exit<>0 OR Signal,Entry); { Current }
Entry:=Signal*(Alert(Signal=0,2) OR Entry*Cum(Entry)=1); { Current or 1st }
Exit:=(Signal=0)*(Alert(Signal,2) OR Exit*Cum(Exit)=1); { Current or 1st }
Entry:=(Exit=0)*entry; { favor EXIT }
Signal:=Entry-Exit; { Signal }
Season:=ValueWhen(1,Signal<>0,Entry)*2-1; { Signal active }

{ PLOT as SIGNAL: Style=VERTICAL LINE }
Signal;


Here is a screen shot:
UserPostedImage
supremeinfo  
#12 Posted : Sunday, August 26, 2012 10:32:01 PM(UTC)
supremeinfo

Rank: Member

Groups: Registered, Registered Users
Joined: 8/8/2012(UTC)
Posts: 15

Hi johnathan ! Thx for your valuable time & effort to help me formulate the correct code....With a small modification in the code you gave in your first post i could successfully backtest my strategy... moreever came to know the the exact syntax for using "valuewhen" .....however still bit confused about resetting the entry condition because the system gives a new entry signal before the crossover of the next or immediately after exit of the previous signal but can manage it since i now have a better understanding of using correct tags & syntax after reading MS formula primer second time & your help Warm regards supreme
jjstein  
#13 Posted : Monday, August 27, 2012 10:05:32 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)
supremeinfo wrote:
however still bit confused about resetting the entry condition because the system gives a new entry signal before the crossover of the next


That's what this bit of re-usable code does -- filter out duplicate signals:
Code:

{ REMOVE EXTRA SIGNALS, +1=Buy -1=Sell }
Signal:=Cum(IsDefined(Entry+Exit))=1; { Enough data ? }
Signal:=ValueWhen(1,Entry-Exit<>0 OR Signal,Entry); { Current }
Entry:=Signal*(Alert(Signal=0,2) OR Entry*Cum(Entry)=1); { Current or 1st }
Exit:=(Signal=0)*(Alert(Signal,2) OR Exit*Cum(Exit)=1); { Current or 1st }
Entry:=(Exit=0)*entry; { favor EXIT }
Signal:=Entry-Exit; { Signal }
Season:=ValueWhen(1,Signal<>0,Entry)*2-1; { Signal active }

{ PLOT as SIGNAL: Style=VERTICAL LINE }
Signal;


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.