Hi,
I´ve a couple of question about trading times in code.
- My code snippet for when I´m not allowed to take a trade is attached. It works fine. Now I want to add one more time span. It´s not possible to use “less than” and “greater than” because they interfer with each other. I want to use a command like “between start time and stop time”. Any ideas how to code that?
- When I do backtesting and there are time spans involved it takes more time than necessary to backtest. For example time span 090000-120000 – it test also numeric value 097000 and 098000 which are no real times. Is there a way to work around that to save time?
Greatful for som help here!
/Bengt
A1.
Why not use NoEntryStart2 and NoEntryStop2 as the second set of time constraints?
A2.
No way round it as I have found unless you can limit your time constraints to < 1 hour and use 1 min in range 1 to 60 etc
What I do is use a coarse step first … 001500 or even 003000 or even 010000 … then narrow in as necessary.
seatrout – Please use the ‘Insert PRT Code’ button when posting code rather than attaching an image of it.
Just use OR:
starttime1 = 100000
endtime1 = 120000
starttime2 = 160000
endtime3 = 190000
if (time >= starttime1 and time <= endtime1) or (time >= starttime2 and time <= endtime2) then
When back testing you can speed it up by ignoring the main code or quitting in tests that don’t involve use-able times. I would use two variables. For example when testing every hour – one variable is starttime (000000 to 230000) and the other is addedhours (010000 to 240000). You can code something along similar lines when dealing with minutes or seconds. Here is the hourly example.
endtime = starttime + addedhours
if endtime > 240000 then
quit
endif
if endtime = 240000 then
endtime = 000000
endif
Sorry – I don´t understand how to use it. So I don´t know how to code it for minutes either… Vonasi, can you please clarify!
Here is an example of the theory applied to a simple strategy to show how it works. To test every 15 minutes set up four optimised variables.
- StartTime from 0 to 23 step 1
- AddedHours from 1 to 24 step 1
- AddedEndMinutes from 0 to 45 step 15
- AddedStartMinutes from 0 to 45 step 15
This will then test every possible start time at 15 minute intervals and every possible end time at 15 minute intervals and ignore and quit any test that has an end time that is not a possible end time to speed up the back test. This obviously gives 9216 possible optimisations which does not leave much capacity for optimising other variables!
I would always advise against optimising time unless there are very sound reasons to such as market hours because optimising time will often turn out to be pure data mining.
once starttime = starttime * 10000
once addedhours = addedhours * 10000
once addedendminutes = addedendminutes * 100
once addedstartminutes = addedstartminutes * 100
once starttime = starttime + addedstartminutes
once endtime = starttime + addedhours + addedendminutes
if endtime > 240000 then
quit
endif
if endtime = 240000 then
endtime = 0
endif
if time >= starttime and time < endtime then
if not onmarket and rsi[14] < 30 then
buy 1 contract at market
endif
endif
if onmarket and rsi[14] > 70 then
sell at market
endif
graph starttime
graph endtime
Thank you very much!
I gonna adjust it to my needs.
Added as Log 178 to here …
Snippet Link Library