My coding skills are not amazing, and I am stuck at what should probably be a simple task. I am designing an intraday strategy (15 minute bars) and am trying to code the exit on the first profitably open of the week. In other words, on the first bar on Monday at 8:00 if profit position is > 0 then exit at market.
Any help would be appreciated
Profitable week's open [SOLVED]
- TJ
- Posts: 7742
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2222 times
- rrams
- Posts: 128
- Joined: 10 Feb 2011
- Location: USA
- Has thanked: 7 times
- Been thanked: 70 times
- Contact:
Re: Profitable week's open
Not only do the holidays make the solution more difficult; you also need to code print statements to show that the exits are happening the way you want them to. In other words, proving you have it correct can be just as troublesome as writing the original code.
Here is my attempt that just sells when the market re-opens that monday on a holiday or else the next day at 8am if closed all day.
Try to change it yourself if not the correct behaviour. Sorry I use a compact style. Study the print output over several years of 15min data.
Here is my attempt that just sells when the market re-opens that monday on a holiday or else the next day at 8am if closed all day.
Try to change it yourself if not the correct behaviour. Sorry I use a compact style. Study the print output over several years of 15min data.
Code: Select all
// sell on first trading day of the week if profitable
// (not the same as sell on first profitable day of new week)
input: StopAmnt$(200);
vars: PD(-1), WD(0), // previous day, day of week,
CM(0), MD(0); // current month, day of the month
WD=DayOfWeek(D); // DayOfWeek: Saturday=6, 0=Sunday, 1=Monday
MD=DayOfMonth(D); // current day of month 1-31
CM=Month(D); // current month of year 1-12
// DayOfWeek: Saturday=6, 0=Sunday, 1=Monday
if D<>D[1] then PD=DayOfWeek(D[1]); // date of one bar ago
// some of the holidays that are POSSIBLE on a monday
if D<>D[1] and // below will only print if market open at all
((CM=7 and (MD=4 or MD=5) and WD=1) // Independence Day
or (CM=1 and WD=1 and (MD=1 or MD=2)) // New Year
or (CM=1 and WD=1 and MD>14 and MD<22) // Martin Luther King 3rd Mon in Jan ( 12-5pm halt for Equity )
or (CM=2 and WD=1 and MD>14 and MD<22) // President's Day 3rd Mon in Feb ( FX, Bond, Energy, Metals )
or (CM=5 and WD=1 and MD>24) // Memorial Day Last Mon in May ( Lumber, Livestock closed )
or (CM=9 and WD=1 and MD<8)) then // Labor Day 1st Mon in Sep ( no change open or close. )
print("Holiday today: ", FormatDate("ddd, M/d/y", DateTime));
// change next line to add your conditions
if MarketPosition=0 then buy next bar market;
// if previous day>current day or previous day=sunday
// or today=monday and time is 8:00am then sell if profitable
if OpenPositionProfit>0 and T>=800 and OpenEntryDate(0)<D and
(PD=0 or (PD>WD and WD<>0) or WD=1) then
begin
sell next bar market;
print(Symbol, ": Bought on ", DateToString(DateToJulian(OpenEntryDate(0))),
" Sold on: ", FormatDate("ddd, M/d/y", DateTime));
end;
SetStopContract;
SetStopLoss(StopAmnt$);
Re: Profitable week's open [SOLVED]
Thanks very much for the reply and the code. This is perfect. Thanks again.