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

Notification

Icon
Error

Options
Go to last post Go to first unread
Spaceant  
#1 Posted : Wednesday, September 20, 2006 3:53:58 AM(UTC)
Spaceant

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 8/16/2005(UTC)
Posts: 182

Hi,

I want to develop indicator / expert advisor and system tester on gap trade by Jake Bernstein. In fact, I want to develop for multigap as well... I will do it one by one, but need some help. Here are the requirements:-

l The market opens either above the previous daily high or below the previous daily low by a given number of ticks or points (say 50 pts)

l A gap buy signal is generated once the market has opened on a gap lower and then comes back up to penetrate the previous daily low by a given number of ticks

l A gap sell signal is generated once the market has opened on a gap higher and then comes back down to penetrate the previous daily high by a given number of ticks

l [censored] buy or sell trades are closed out at the end of the day or at a predetermined stop loss

here is my code based on Jose's previous work: -


{ Message }
message:=Input("(Plot on intraday charts)",0,0,0);

{ Day's true start }
dCount:=Fml("Calendar Day counter");
dStart:=dCount>ValueWhen(2,1,dCount);

{ Previous x Days' High }
Hd:=HighestSince(pds,dStart,H);
Hd:=ValueWhen(1,dStart,ValueWhen(2,1,Hd));
Hd:=ValueWhen(1,Hd>0,Hd);

{ Previous x Days' Low }
Ld:=LowestSince(pds,dStart,L);
Ld:=ValueWhen(1,dStart,ValueWhen(2,1,Ld));
Ld:=ValueWhen(1,Ld>0,Ld);

{ Previous Day's Close }
Cd:=ValueWhen(1,dStart,ValueWhen(2,1,C));
Cd:=ValueWhen(1,Cd>0,Cd);

If(L > Ld ,1, If(H < Hd ,-1,0))

there is something wrong.....

can I get any help?

sa

wabbit  
#2 Posted : Wednesday, September 20, 2006 6:32:33 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)
sa,

You haven't defined the "pds" variable.

Add:

pds:=Input("Periods",1,100,10);

to your code, somewhere near the top!



wabbit [:D]


Spaceant  
#3 Posted : Wednesday, September 20, 2006 10:03:45 AM(UTC)
Spaceant

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 8/16/2005(UTC)
Posts: 182

Wabbit,

{ User input }
pds:=Input("Use Highest/Lowest of past x days",1,260,1);

{ Message }
message:=Input("(Plot on intraday charts)",0,0,0);

{ Day's true start }
dCount:=Fml("Calendar Day counter");
dStart:=dCount>ValueWhen(2,1,dCount);

{ Previous x Days' High }
Hd:=HighestSince(pds,dStart,H);
Hd:=ValueWhen(1,dStart,ValueWhen(2,1,Hd));
Hd:=ValueWhen(1,Hd>0,Hd);

{ Previous x Days' Low }
Ld:=LowestSince(pds,dStart,L);
Ld:=ValueWhen(1,dStart,ValueWhen(2,1,Ld));
Ld:=ValueWhen(1,Ld>0,Ld);

{ Previous Day's Close }
Cd:=ValueWhen(1,dStart,ValueWhen(2,1,C));
Cd:=ValueWhen(1,Cd>0,Cd);

If(L > Ld ,1, If(H < Hd ,-1,0))

I haven't copied the first line.... there are some problem on the last statement as to reset the signal, to avoid redundent / repeated signals... and I know that some improvments are required.....

sa

wabbit  
#4 Posted : Wednesday, September 20, 2006 7:23:47 PM(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)
sa,

It goes to show the importance of sending the FULL code in use to the Forum for us to view. It's a bit hard to diagnose what is going wrong if we don't have full visibility of the code.

spaceant wrote:
I want to develop indicator / expert advisor and system tester on gap trade by Jake Bernstein. In fact, I want to develop for multigap as well... I will do it one by one, but need some help. Here are the requirements:-

The market opens either above the previous daily high or below the previous daily low by a given number of ticks or points (say 50 pts)


First, you have to find what today's opening price is, then see if today is a Gap Day:

todaysOpen:=ValueWhen(1,dCount<>Ref(dCount,-1),OPEN);
gUp:=todaysOpen>Hd; {add in your latitude here...}
gDn:=todaysOpen<Ld; {add in your latitude here...}

spaceant wrote:
A gap buy signal is generated once the market has opened on a gap lower and then comes back up to penetrate the previous daily low by a given number of ticks


x:=gDn and (L>Ld); {add some latitude here...}
gBuy:=x and Alert(x=0,2);

spaceant wrote:
A gap sell signal is generated once the market has opened on a gap higher and then comes back down to penetrate the previous daily high by a given number of ticks


x:=gUp and (H<Hd); {add some latitude here...}
gSell:=x and Alert(x=0,2);
spaceant wrote:
Gap buy or sell trades are closed out at the end of the day or at a predetermined stop loss.

Use a latch....

NOTE: Previous days close code is not refernced and therefore removed....

Code:

{This code contains extracts/ideas from Jose Silva}
{and requires Jose's Calendar Day Counter}
{from http://www.metastocktools.com}

{ User input }
pds:=Input("Use Highest/Lowest of past x days",1,260,1);

{ Message }
message:=Input("(Plot on intraday charts)",0,0,0);

{ Day's true start }
dCount:=Fml("Calendar Day counter");
dStart:=dCount>ValueWhen(2,1,dCount);

{ Previous x Days' High }
Hd:=HighestSince(pds,dStart,H);
Hd:=ValueWhen(1,dStart,ValueWhen(2,1,Hd));
Hd:=ValueWhen(1,Hd>0,Hd);

{ Previous x Days' Low }
Ld:=LowestSince(pds,dStart,L);
Ld:=ValueWhen(1,dStart,ValueWhen(2,1,Ld));
Ld:=ValueWhen(1,Ld>0,Ld);

{Trades}
todaysOpen:=ValueWhen(1,dCount<>Ref(dCount,-1),OPEN);
gUp:=todaysOpen>Hd; {add in your latitude here...}
gDn:=todaysOpen<Ld; {add in your latitude here...}

x:=gDn and (L>Ld); {add some latitude here...}
gBuy:=x and Alert(x=0,2);

x:=gUp and (H<Hd); {add some latitude here...}
gSell:=x and Alert(x=0,2);

gBuy-gSell;



I have not tested this code (its all wrirtten directly into this Forum) so see if it works like you think it should. If it doesn't work like you think it should, report back with a FULL description of how it doesn't work (an annotated graphic speaks a thousand words). Reports like, "It doesn't work." and "There is something wrong....." are not very helpful to pin-pointing the source of the trouble.



wabbit [:D]


Spaceant  
#5 Posted : Thursday, September 21, 2006 4:24:44 AM(UTC)
Spaceant

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 8/16/2005(UTC)
Posts: 182

Wabbit,

Thanks for your help!

Well, I have not used the latch before. Do you mean I can use a latch function to develop a System Tester or an Expert Advisor... or both?

In the download area, I can find "Forum20.dll" (under ForumDll_200). Is it the most updated one?

However, the manual's name is ForumDll Manual, in which, the latch function is...

ExtFml(“forum.DateRange", Enter Long, Exit Long, Enter Short, Exit Short)

Should it be ExtFml(“forum20.DateRange", Enter Long, Exit Long, Enter Short, Exit Short) ??

Thanks!

sa

wabbit  
#6 Posted : Thursday, September 21, 2006 4:32:21 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)
sa,

If you download the Forum .dll (I think the latest version installs itself Forum20 ?) then you need to define your entry and exit criteria, long and short (if any)

something like:

Code:

{all above here is the same}

{Trades}
todaysOpen:=ValueWhen(1,dCount<>Ref(dCount,-1),OPEN);
gUp:=todaysOpen>Hd; {add in your latitude here...}
gDn:=todaysOpen<Ld; {add in your latitude here...}

LE:=gDn and (L>Ld); {add some latitude here...}
LX:=gUp and (H<Hd); {add some latitude here...}
SE:=0;
SX:=0;

Tr:=ExtFml("Forum20.Latch" LE, LX, SE, SX);

{plot}
Tr;




wabbit [:D]


Spaceant  
#7 Posted : Wednesday, October 4, 2006 5:35:26 AM(UTC)
Spaceant

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 8/16/2005(UTC)
Posts: 182

Wabbit,

I have amended the code a bit as follows:-

---------> -----------

{This code contains extracts/ideas from Jose Silva}

{and requires Jose's Calendar Day Counter}

{from http://www.metastocktools.com}

{ User input }

pds:=Input("Use Highest/Lowest of past x days",1,260,1);

{ Message }

message:=Input("(Plot on intraday charts)",0,0,0);

{ Day's true start }

dCount:=Fml("Calendar Day counter");

dStart:=dCount>ValueWhen(2,1,dCount);

{ Previous x Days' High }

Hd:=HighestSince(pds,dStart,H);

Hd:=ValueWhen(1,dStart,ValueWhen(2,1,Hd));

Hd:=ValueWhen(1,Hd>0,Hd);

{ Previous x Days' Low }

Ld:=LowestSince(pds,dStart,L);

Ld:=ValueWhen(1,dStart,ValueWhen(2,1,Ld));

Ld:=ValueWhen(1,Ld>0,Ld);

{Trades}

todaysOpen:=ValueWhen(1,dCount<>Ref(dCount,-1),OPEN);

gUp:=todaysOpen>Hd; {add in your latitude here...}

gDn:=todaysOpen<Ld; {add in your latitude here...}

LE:=gDn AND (L>Ld); {add some latitude here...}

LX:=Cross(Fml( "*ATRTrailingStop(Buy)"),C );

SE:= gUp AND (H<Hd); {add some latitude here...}

SX:=Cross(C,Fml( "*ATRTrailingStop(Sell)"));

Tr:=ExtFml("Forum.Latch",LE, LX, SE, SX);

{plot}

Tr;

------->------------

*ATRTrailingStop(Buy)

HHV(H - 2.5*ATR(5),10)

----------------à-----------

*ATRTrailingStop(Sell)

LLV(L + 2.5*ATR(5),10)

1) Referring to the 5-min chart on 28 Sep, I have plot two indicators (Tr & SX) and with an expert same as Tr. I don't know why the Short Entry is closed in a second SX signal.... there is an earlier SX signal as marked with a circle.

(Edited afterwards: there is something wrong that is probably relates *ATR Trailing Stop(Buy / *ATR Trailing Stop (Sell).... still not sure!)

2) Also, if I want this indicator / expert advisor to be a day trading indicator, i.e. open trade must be closed on market closing... how can I code this?

3) Can I set a filter to give a maximum of one signal per day? i.e. Only one sell entry or buy entry even when the price go up and down, as shown in the 5-min chart with 2 sell entries (can I eliminate the second one?)

4) It is quite strange that the same data plotted in 1-min chart, there is no signal given, why is that?

(Edited afterwards: Forget this as it is due to not enough bars are loaded)

thanks!

sa

UserPostedImage

UserPostedImage

wabbit  
#8 Posted : Wednesday, October 4, 2006 7:14:57 PM(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)
Spaceant wrote:
1) Referring to the 5-min chart on 28 Sep, I have plot two indicators (Tr & SX) and with an expert same as Tr. I don't know why the Short Entry is closed in a second SX signal.... there is an earlier SX signal as marked with a circle.

(Edited afterwards: there is something wrong that is probably relates *ATR Trailing Stop(Buy / *ATR Trailing Stop (Sell).... still not sure!)

If you want to make sure the latch is doing everything properly, plot all four entry and exit indicators along with the Tr. Depending on which version of the latch you have, if you are already short and a SX signal is generate on the same bar as a SE signal, the latch will stay in the short trade. The only way to see these effects is to plot LE, LX, SE and SX and Tr.


Spaceant wrote:
2) Also, if I want this indicator / expert advisor to be a day trading indicator, i.e. open trade must be closed on market closing... how can I code this?
Always a tricky proposition to find the last bar of the day before the day has eneded, but you can do it retrospectively. Of course, in real life you cannot trade retrospectively!

Spaceant wrote:
3) Can I set a filter to give a maximum of one signal per day? i.e. Only one sell entry or buy entry even when the price go up and down, as shown in the 5-min chart with 2 sell entries (can I eliminate the second one?)
You can program a simple counter that gets reset on each new day. If the counter is zero and entry signal is generated then enter, otherwise if the counter is greater than zero, do not enter the trade.

Spaceant wrote:
4) It is quite strange that the same data plotted in 1-min chart, there is no signal given, why is that?

(Edited afterwards: Forget this as it is due to not enough bars are loaded)

[:D]





wabbit [:D]




Spaceant  
#9 Posted : Monday, October 9, 2006 4:34:53 AM(UTC)
Spaceant

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 8/16/2005(UTC)
Posts: 182

Wabbit,

Always a tricky proposition to find the last bar of the day before the day has eneded, but you can do it retrospectively. Of course, in real life you cannot trade retrospectively!
Is it wise to set a time trigger to close the position say 5 min before closing, 16:10?

You can program a simple counter that gets reset on each new day. If the counter is zero and entry signal is generated then enter, otherwise if the counter is greater than zero, do not enter the trade.

I have tried to code it...but it might too "simple" that I do not have a clue. What function should I use?

Sa
wabbit  
#10 Posted : Tuesday, October 10, 2006 2:25:03 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)
sa,

I am unable to test this code at the moment, so please have a close look and see if it achieves what you want. To limit the trades to just one long trade and one short trade per day:

Code:

{This code contains extracts/ideas from Jose Silva}
{and requires Jose's Calendar Day Counter}
{from http://www.metastocktools.com}

{ User input }
pds:=Input("Use Highest/Lowest of past x days",1,260,1);

{ Message }
message:=Input("(Plot on intraday charts)",0,0,0);

{ Day's true start }
dCount:=Fml("Calendar Day counter");
dStart:=dCount>ValueWhen(2,1,dCount);

{ Previous x Days' High }
Hd:=HighestSince(pds,dStart,H);
Hd:=ValueWhen(1,dStart,ValueWhen(2,1,Hd));
Hd:=ValueWhen(1,Hd>0,Hd);

{ Previous x Days' Low }
Ld:=LowestSince(pds,dStart,L);
Ld:=ValueWhen(1,dStart,ValueWhen(2,1,Ld));
Ld:=ValueWhen(1,Ld>0,Ld);

{Trades}
todaysOpen:=ValueWhen(1,dCount<>Ref(dCount,-1),OPEN);
gUp:=todaysOpen>Hd; {add in your latitude here...}
gDn:=todaysOpen<Ld; {add in your latitude here...}

{limit entries to one long AND one short trade per day}

{long entry}
count:=gDn AND (L>Ld);
reset:=dStart;
LE:=cum(count)-valuewhen(1,reset,count)=1;

{short entry}
count:=gUp AND (H<Hd);
reset:=dStart;
SE:=cum(count)-valuewhen(1,reset,count)=1;

{exits}
LX:=Cross(Fml( "*ATRTrailingStop(Buy)"),C );
SX:=Cross(C,Fml( "*ATRTrailingStop(Sell)"));

{trade latch}
Tr:=ExtFml("Forum.Latch",LE, LX, SE, SX);

{plot}
Tr;


If you only want one trade per day, long OR short, then try:

Code:

{This code contains extracts/ideas from Jose Silva}
{and requires Jose's Calendar Day Counter}
{from http://www.metastocktools.com}

{ User input }
pds:=Input("Use Highest/Lowest of past x days",1,260,1);

{ Message }
message:=Input("(Plot on intraday charts)",0,0,0);

{ Day's true start }
dCount:=Fml("Calendar Day counter");
dStart:=dCount>ValueWhen(2,1,dCount);

{ Previous x Days' High }
Hd:=HighestSince(pds,dStart,H);
Hd:=ValueWhen(1,dStart,ValueWhen(2,1,Hd));
Hd:=ValueWhen(1,Hd>0,Hd);

{ Previous x Days' Low }
Ld:=LowestSince(pds,dStart,L);
Ld:=ValueWhen(1,dStart,ValueWhen(2,1,Ld));
Ld:=ValueWhen(1,Ld>0,Ld);

{Trades}
todaysOpen:=ValueWhen(1,dCount<>Ref(dCount,-1),OPEN);
gUp:=todaysOpen>Hd; {add in your latitude here...}
gDn:=todaysOpen<Ld; {add in your latitude here...}

{limit entries to one trade per day, long OR short}

{entry}
count:=(gDn AND (L>Ld)) OR (gUp AND (H<Hd));
reset:=dStart;
E:=cum(count)-valuewhen(1,reset,count)=1;

{exits}
LX:=Cross(Fml( "*ATRTrailingStop(Buy)"),C );
SX:=Cross(C,Fml( "*ATRTrailingStop(Sell)"));

{trade latch}
Tr:=ExtFml("Forum.Latch",E, LX, E, SX);

{plot}
Tr;


Hopefully, if neither solves your problem, you can see what I was trying to achieve and code your own solution.


--

As for coding the condition to trigger an alert near the end of the day..... To do this properly requires foresight, which is not possible in MS. To demonstrate:

You want to code a condition hat will trigger if the bar displayed is timestamped 16:10. What if it was a slow trading day and the last bar was at, say, 15:30? How are you going to cope with this situation. Of course with 20/20 hindsight, we would be able to say that 15:30 was the last trade of the day, but at 15:45 on that day, how would you know that 15:30 was the last trade of the day?

Paradoxically, because you have a trade open during the trading day, which you will exit before trading closes, there will be a bar at the end of the day that will contain your trade - - but this bar doesn't exist until you make the exit transaction! Can you see the dilemma for trying to code the end of the day?


wabbit [:D]
Spaceant  
#11 Posted : Wednesday, October 11, 2006 4:48:01 AM(UTC)
Spaceant

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 8/16/2005(UTC)
Posts: 182

Hi Wabbit,

I have tried the above codes and have the following comments:-

A) The first code seems working. One strange time did happen when I try it using the Sept data (don't seem relate to the data itself). Please see the image files attached below, one with sell signal with data loaded from 25 Sep and one without sell signal with data loaded from 15 Sep. Two charts are the same just with different periods load. Don't know why?

UserPostedImage
UserPostedImage

Well, I would like to use the Tester to test this indicator with different timeframe. If I just used it to test, I assume that the result will be on a timeframe of 1-min.
How can it be done with other timeframe?

Do we need to use TWV dll or Power Pivot Plus?


B) the second code seems not working, with E, reserved word, changed to another word , say Et.


sa

wabbit  
#12 Posted : Wednesday, October 11, 2006 5:38:35 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)
Your system code is sensitive to the number of periods loaded....

UserPostedImage
is missing the SE signal at the beginning of the 28th...

The SE signal is obviously sensitive to the number of bars loaded.


All of the code is based on time periods.... if you have a 30 period MA in a 5-min bar intraday chart then you are looking at about 150 minutes, if the chart is a 1-min intraday chart you will only be looking at about 30 minutes, if the chart is displaying ticks you could be looking at a time period as small as 30/1000 of one minute!


wabbit [:D]


Users browsing this topic
Guest (Hidden)
Similar Topics
Scott Andrews Presents Master the Gap Trades (MetaStock Webinar Series)
by KellyC 4/23/2010 1:57:24 PM(UTC)
Gap Trade and Multiple-day Gap Trade (Formula Assistance)
by garykong 5/28/2006 4:18:20 PM(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.