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]
|