Page 1 of 1

TS code

Posted: Wed Jun 20, 2007 8:19 pm
by Ken_S
I apply the following strategy to AAPL, 4day, 10tick/bar, regular session chart. I only want one entry/exit per day. If price gaps up/down, I want to sellshort/buy 100 shares if price moves .30 towards the previous days close and close the position after a $.04 profit. I also want to exit the position after X minutes.
My Entriestoday part of the code doesn't appear to be working since I get many more than one entry/exit per day. Also, it always exits at the very next bar after entry which probably means my maxopenordertime used to exit isn't working.
Any idea what the problem is?

Inputs: Pullback$(.30), targetprofit(.02), maxopenordertime(400);

Variables: enterprice(0), exprice(0), value1(0);

If Date <> Date[1] and open <> close[1] then begin
{short entry prices}
If Open > close[1] then begin
enterprice = Open - pullback$;
exprice = enterprice - targetprofit;
Value1 = -1;
end;
{long entry prices}
If Open < close[1] then begin
enterprice = Open + pullback$;
exprice = enterprice + targetprofit;
Value1 = 1;
end;
end;
{exits}

If Marketposition = 0 and value1 = 1 and Entriestoday(currentdate) = 0 then Buy 100 shares
next bar at enterprice stop;
If Marketposition = 0 and value1 = -1 and Entriestoday(currentdate) = 0 then sellshort 100 shares
next bar at enterprice stop;


If marketposition = 1 then begin
If currenttime - entrytime >= maxopenordertime then sell 100 shares next bar at market;
sell 100 shares next bar at exprice limit;

end;

If marketposition = -1 then begin
If currenttime - entrytime >= maxopenordertime then buytocover 100 shares next bar at market;
buytocover 100 shares next bar at exprice limit;

End;
setexitonclose;

Posted: Thu Jul 05, 2007 3:45 am
by traderjeffb
Ken-
I am NOT an expert programmer....I am learning easy language but I'll give you my 2? worth and you can see if this helps and play with it from here...until a person who KNOWS this stuff replies....
I left some of your code in so you could see what I found that wasn't working...
my code does not work on 10 tick charts....I have not figured out why yet...still working on that...maybe you can fix that one....and my code is kinda ugly....so again for what it's worth....try this on 1min. or anything larger then you can concentrate on making it work on tick charts....(I also changed the targetprofit to 50?



Inputs: Pullback$(.30), targetprofit(.50), maxopenordertime(400);

Variables: enterprice(0), exprice(0), GoLong(false), GoShort(false), value2(0);


Value2 = Entriestoday(currentdate);

If Date <> Date[1] and open <> close[1] then begin
{short entry prices}
If Open > close[1] then begin
enterprice = Open - pullback$;
exprice = enterprice - targetprofit;
GoShort = true;
end;

{long entry prices}
If Open < close[1] then begin
enterprice = Open + pullback$;
exprice = enterprice + targetprofit;
GoLong = True;
end;
end;

{exits}

If Marketposition = 0 and GoLong = true and value2 = 0 then
Buy 100 shares next bar at enterprice stop;
GoLong = false;

If Marketposition = 0 and GoShort = true and value2 = 0 then
sellshort 100 shares next bar at enterprice stop;
GoShort = false;

If marketposition = 1 then begin
If currenttime - entrytime >= maxopenordertime then {sell 100 shares next bar at market; }
sell 100 shares next bar at exprice limit;

end;

If marketposition = -1 then begin
If currenttime - entrytime >= maxopenordertime then
buytocover 100 shares next bar at market;
{buytocover 100 shares next bar at exprice limit; }

End;

setexitonclose;

Posted: Fri Jul 06, 2007 2:49 am
by Ken_S
Here is my updated code with a few more features that is working on a 10 tick chart. My next step is to play around with using limit orders instead of market.

Inputs: Pullback$(.07), targetprofit(.12), stoplosspercent(100), maxopenordertime(390);

Variables: enterprice(0), exprice(0), value1(0), value2(0);

If Date <> Date[1] and open <> close[1] and entriestoday(currentdate) = 0 then begin
enterprice = 0;
exprice = 0;

{short entry prices}
If Open > close[1] then begin
enterprice = Open - pullback$;
exprice = enterprice - targetprofit;
Value1 = -1;

end;
{long entry prices}
If Open < close[1] then begin
enterprice = Open + pullback$;
exprice = enterprice + targetprofit;
Value1 = 1;

end;
end;

{exits}

If Marketposition = 0 and value1 = 1 and Entriestoday(currentdate) = 0 and close >= enterprice
then Buy 100 shares next bar at market;

If Marketposition = 0 and value1 = -1 and Entriestoday(currentdate) = 0 and close <= enterprice
then sellshort 100 shares next bar at market;

Value2 = calctime(entrytime, +maxopenordertime);

{End of day exit}
If time >= 1550 then begin;

Sell ("EOD Sell") next bar All Contracts at Market;
BuyToCover ("EOD BtC") next bar All Contracts at Market;

end;

If marketposition = 1 then begin

If (absvalue(close-entryprice)/entryprice)*100 >= stoplosspercent then
Sell ("StLoss Sell") next bar all contracts at market;

value1 = 0;
If time >= value2 then sell ("Timed Sell") all shares next bar at market;
sell all shares next bar at exprice limit;
enterprice = 0;

end;

If marketposition = -1 then begin

If (absvalue(close-entryprice)/entryprice)*100 >= stoplosspercent then
BuytoCover ("StLoss Buy") next bar all contracts at market;

value1 = 0;
If time >= value2 then buytocover ("Timed Btc") all shares next bar at market;
buytocover all shares next bar at exprice limit;
enterprice = 0;

End;
Setexitonclose;