Bar Delay in Trading System

#1
Im putting together a trading system to generate buy and sell signals (only operating long) and I would appreciate if someone could help me with Metastock language.
How can I tell Metastock to buy or sell only if the following bar is not opposite to the first one?
Ex:
bar 1: buy signal
bar 2: buy signal
bar 3: execute buy

bar 1: buy signal
bar 2: sell signal
bar 3: ignore signal

Thanks a lot for your help!!!
Traderbrazil
 
#2
Hello SNIGDHA!!
Thanks for your reply although Im not really familiar with MS language yet.
Below is the formula I'm using for buy signal:
C>Fml("Ehlers Distant Coefficient Filter")
If its not too much trouble for you please write down how I could use the if function attached to it with the delay (3rd bar buy if 2nd bar is not sell)

thanks a lot!
Traderbrazil
 

oxusmorouz

Well-Known Member
#3
Hello SNIGDHA!!
Thanks for your reply although Im not really familiar with MS language yet.
Below is the formula I'm using for buy signal:
C>Fml("Ehlers Distant Coefficient Filter")
If its not too much trouble for you please write down how I could use the if function attached to it with the delay (3rd bar buy if 2nd bar is not sell)

thanks a lot!
Traderbrazil
Tools ---> Expert adviser ---> new ---> Name it ---> Symbol ---> New

Name : Long Enter
Condition:
a:= Fml("Ehlers Distant Coefficient Filter");
b:= Sum( C > a,2) = 2;
d:= Your Exit condition {Say, C < a};
f:= If(BarsSince(b) < BarsSince(d),1,0);
f > Ref(F,-1);


Name: Long Exit
Condition:
a:= Fml("Ehlers Distant Coefficient Filter");
b:= Sum( C > a,2) = 2;
d:= Your Exit condition {Say, C < a};
f:= If(BarsSince(b) > BarsSince(d),1,0);
f > Ref(F,-1);

Name : Short Enter
Condition:
a:= Fml("Ehlers Distant Coefficient Filter");
b:= Sum( C < a,2) = 2;
d:= Your Exit condition {Say, C > a};
f:= If(BarsSince(b) < BarsSince(d),1,0);
f > Ref(F,-1);

Name: Short Exit
Condition:
Condition:
a:= Fml("Ehlers Distant Coefficient Filter");
b:= Sum( C < a,2) = 2;
d:= Your Exit condition {Say, C > a};
f:= If(BarsSince(b) > BarsSince(d),1,0);
f > Ref(F,-1);

Regards.
 
Last edited:
#5
Hello oxusmorouz,

I find your answer very brilliant to the querry of traderbrazil.

I am new to metastock and shall be very grateful to know the answers of my innocent looking questions;

============================

po:=ref(o,-1);
pl:=ref(l,-1);
ph:=ref(h.-1);
pc:=ref(c,-1);

{fml1}
If(po>=pc,po+(po-pl),po-(ph-po));

{fml2}
if(po>pc,ph,
if(po<pc,pl,po));
================================

suppose signals are:

fml1>fml2 {buy}

fml1<fml2 {sell}

===============================


Que-1: In the above formula, I took all the datas of previous day. Suppose I take today's data and then change the formula as;-
ref(fml1,-1) & ref(fml2,-1)
Is there any difference between two?

Que-2: Is this formula alright to get tomorrow's signal? If not, please suggest the proper way to do the things.

Que-3:please suggest the proper way to get signals through "EXPERT ADVISOR"; to get the list of stocks of prospect buy for the next day through "EXPLORER" and to test this on "ENHANCED SYSTEM TESTER".

BASUDEO
 
#6
Hello OXUS,

I find your expert advice in most of the threads. I shall be very grateful if you please reply my query. I want to change my previous post as follows. Please plot this and give your expert opinion accordingly.

po:=Ref(O,-1);
pl:=Ref(L,-1);
ph:=Ref(H,-1);
pc:=Ref(C,-1);

fml1:=
If(po>=pc,po-(ph-pc),po+(pc-pl));

fml2:=
If(po>pc,pl,
If(po<pc,ph,po));

Mov(fml1,3,S);
Mov(fml2,8,S);

================================

suppose signals are:

fml1<fml2 {buy}

fml1>fml2 {sell}

===============================


Que-1: In the above formula, I took all the datas of previous day. Suppose I take today's data and then change the formula as;-
ref(fml1,-1) & ref(fml2,-1)
Is there any difference between two?

Que-2: Is this formula alright to get tomorrow's signal? If not, please suggest the proper way to do the things.

Que-3:please suggest the proper way to get signals through "EXPERT ADVISOR"; to get the list of stocks of prospect buy for the next day through "EXPLORER" and to test this on "ENHANCED SYSTEM TESTER".

BASUDEO
 

oxusmorouz

Well-Known Member
#7
Hello OXUS,

I find your expert advice in most of the threads. I shall be very grateful if you please reply my query. I want to change my previous post as follows. Please plot this and give your expert opinion accordingly.

po:=Ref(O,-1);
pl:=Ref(L,-1);
ph:=Ref(H,-1);
pc:=Ref(C,-1);

fml1:=
If(po>=pc,po-(ph-pc),po+(pc-pl));

fml2:=
If(po>pc,pl,
If(po<pc,ph,po));

Mov(fml1,3,S);
Mov(fml2,8,S);

================================

suppose signals are:

fml1<fml2 {buy}

fml1>fml2 {sell}

===============================
Que-1: In the above formula, I took all the datas of previous day. Suppose I take today's data and then change the formula as;-
ref(fml1,-1) & ref(fml2,-1)
Is there any difference between two?
It seems appropriate to do so.

Que-2: Is this formula alright to get tomorrow's signal? If not, please suggest the proper way to do the things.
Yes. Since the delay is by one bar.
Que-3:please suggest the proper way to get signals through "EXPERT ADVISOR"; to get the list of stocks of prospect buy for the next day through "EXPLORER" and to test this on "ENHANCED SYSTEM TESTER".

BASUDEO
If you have an idea in mind and want it coded, I'd be glad to assist you. However, a read of metastock formula premier for coding and use of tradesim for backtesting is recommended.

Sincerely yours.
 
Last edited:
#8
Hello OXUS,

Thank you so much for your such a prompt reply. I have slightly updated the formula. The revised formula is:

{NEUTRAL}

fml1:=If(O>(C*102/100),L,
If(O<(C*98/100),H,(O+C)/2));

Mov(Ref(fml1,-1),3,S);


{Volatile}

a:=C+L-O;
b:=C+H-O;
d:=2*O-H;
f:=2*O-L;
fml1:=If(O>=C,(a*d/f)*O/C,(b*f/d)*O/C);

Mov(Ref(fml1,-1),3,S);

{buy signal}cross(neutral,volatile)

{sell signal}cross(volatile,neutral)

{above formula only shows the trend and I dont know how to optimize it to pick most profitable troughs and peaks}
=============================

dear oxus, I have data upto 13/07/2007. I could not visualize as the bar of 13/07/2007 refers to the data of previous bar so what signal it gives. I mean the signal for 13/07/2007 or 14/07/2007. Where is the signal for actusal 13/07/2007 as the chart is not extending to next bar. If the bar of 13/07/2007 gives the signal for next bar by taking the data of the previous bar, why it is necesaary to take the data of the previous bar? This may seem very innocent but it is boggling me.

You please give a simple example of writing "expert advisor". When I tried to plot the signals I am getting the signal for the same bar and not the next bar.

Thanks a lot,

Basudeo
 

oxusmorouz

Well-Known Member
#9
Hello OXUS,

Thank you so much for your such a prompt reply. I have slightly updated the formula. The revised formula is:

{NEUTRAL}

fml1:=If(O>(C*102/100),L,
If(O<(C*98/100),H,(O+C)/2));

Mov(Ref(fml1,-1),3,S);


{Volatile}

a:=C+L-O;
b:=C+H-O;
d:=2*O-H;
f:=2*O-L;
fml1:=If(O>=C,(a*d/f)*O/C,(b*f/d)*O/C);

Mov(Ref(fml1,-1),3,S);

{buy signal}cross(neutral,volatile)

{sell signal}cross(volatile,neutral)

{above formula only shows the trend and I dont know how to optimize it to pick most profitable troughs and peaks}
=============================

dear oxus, I have data upto 13/07/2007. I could not visualize as the bar of 13/07/2007 refers to the data of previous bar so what signal it gives. I mean the signal for 13/07/2007 or 14/07/2007. Where is the signal for actusal 13/07/2007 as the chart is not extending to next bar. If the bar of 13/07/2007 gives the signal for next bar by taking the data of the previous bar, why it is necesaary to take the data of the previous bar? This may seem very innocent but it is boggling me.

You please give a simple example of writing "expert advisor". When I tried to plot the signals I am getting the signal for the same bar and not the next bar.

Thanks a lot,

Basudeo
The bar delay in metastock is used only for testing systems, not for scanning stocks. If you are to write the formula in expert advisor, try using the original code, without the bar delay. Since I use Tradesim for backtest, and since I haven't used metastock's backtester since last november, my knowledge with backtester is a bit shaky.

Regarding the expert advisor, here's an example of a moving average crossover (just an example...)

Tools ---> Expert adviser ---> new ---> Give it a name ---> Symbols ---> new

1) Name = Long Enter.

Formula:
a:= Mov(C,5,S); {Fast MA}
b:= Mov(C,8,S); {Slow MA}
Cross(a,b); {Condition}

2) Short Enter:

Formula:
a:= Mov(C,5,S); {Fast MA}
b:= Mov(C,8,S); {Slow MA}
Cross(b,a); {Condition}

x----------------------------------------x

Go to highlights ---> New

1) Name = Long Phase

Colour = Green

Formula:
a:= Mov(C,5,S); {Fast MA}
b:= Mov(C,8,S); {Slow MA}
BarsSince(a) < BarsSince(b); {Condition}

2) Name = Short Phase

Colour = Red

Formula:
a:= Mov(C,5,S); {Fast MA}
b:= Mov(C,8,S); {Slow MA}
BarsSince(a) > BarsSince(b); {Condition}

x--------------------------------------------------x

Writing commentary is a tricky part and pretty difficult as compared with coding. However, it isn't as important.

Sincerely yours.
 

Similar threads