Limiting Backtesting Strategy Results to Specific Times of the Day


Category:
0
0

I want to only trade during the hours of 10am to 1pm.  I have attempted to modify the code in a simple moving average strategy to try and get the report results to limit backtesting results to the hours of 10-1pm.  Is that possible?  I’ve tried a few different commands like “input opentime = 1000; input durationhours = 3;” and “MarketopenTime = 1000; MarketcloseTime = 1300 “but I honestly don’t know the proper syntax.  I’m fresh on my journey to learning Thinkscript.  🙂

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on May 7, 2020 12:03 pm
185 views
0
Private answer

You can use the code provided in the following post in the Stock Scanners topic:

https://www.hahn-tech.com/ans/scan-within-a-specific-hour-time-range/

The solution in that post provides a true/false variable that can be used to filter all manner of items on the chart. The last line of the code produces a variable named "scanPeriod". Depending on how you configure the last line of code (read the instructions provided in that post), you can set the start and end times so that the "scanPeriod" variable is true only within the start and end times selected in the user inputs.

You didn't provide your fully functioning code for the chart strategy, so I can not give you an exact example of how to implement this in your own code. Its up to you to figure out how to implement this in your own code.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 7, 2020 3:22 pm
0
Hi Pete, Thanks for the reply. I checked out the link and think I understood it correctly however being new to Thinkscript, my grasp might not be correct as it didn't even produce visible candles. Doh! Code is below: declare lower; input startTime = 1000; input endTime = 1300; def startOfScan = SecondsFromTime(startTime) > 0; def endOfScan = SecondsTillTime(endTime) > 0; plot scanPeriod = startOfScan and endOfScan; input price = close; input length = 15; input averageType = AverageType.SIMPLE; input mode = {default "Trend Following", "Reversal"}; plot Avg = MovingAverage(averageType, price, length); Avg.SetDefaultColor(GetColor(0)); def crossAbove = price crosses above Avg; def crossBelow = price crosses below Avg; def buy; def sell; switch (mode) { case "Trend Following": buy = crossAbove; sell = crossBelow; case "Reversal": buy = crossBelow; sell = crossAbove; } AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "MovAvgStratLE"); AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "MovAvgStratSE");
( at May 8, 2020 9:51 am)
0
Well it is not enough to simply add that code to your existing strategy. You need to wire things up. You have not used the "scanPeriod" variable in any of your conditions. So it is completely inactive. How do you "wire things up"? For example in your AddOrder statement to buy, you have a condition named "buy". If you want those buy signals to only occur within the span of time you have chosen the scanPeriod variable needs to included. "buy and scanPeriod". This sure would have been much easier if you had simply posted your code in your original question.
( at May 8, 2020 11:08 am)
0
10-4! Let me roll up my sleeves more and get this configured correctly!
( at May 8, 2020 12:31 pm)