In this article, I will show you how to mix end-of-day and intraday data by creating an intraday trading system that trades different stocks every day. Let us say you want to implement the following intraday trading system: - Enter long when a stock breaks the first 30-minute resistance Besides the above rule, you want to trade stocks that performed best the previous day. Let us say that for each trading day, you want to pick from S&P 500 stocks, the first 20 stocks that had the best performance. You will then trade these stocks using the above intraday rule. Each day, you are going to perform the same process. But before trading or investing in such strategy, how can you backtest it? Create the Intraday Trading System Let us begin by creating the intraday trading system. Click here to learn how to implement a trading system. We must first create the function that calculates the 30-minute resistance. For this, let us use an indicator available on the sharing server (Support and resistance lines based on N-Minutes high/low of a trading session). After downloading it; select "Tools -> Create Functions" to see how this indicator was implemented. This chart displays the high and low of the 30 first minutes of a trading session: In QuantShare programming language we should write: res30 = FirstMinutesHL(30); The second step would be to implement the breakout; this is done using the "cross" function: buy = cross(close, res30); How to Rank Stocks Ranking securities on fly is one of the most powerful features of QuantShare. The "comp" function is capable of ranking and creating composites on fly in different tools such as the screener, simulator, portfolio… More information about this function can be found in the documentation and in the following articles: How to create market indicators using the composite function - Part 1, Part 2, Part 3. New Ranking and Percentile Composite Functions Trading Indicators using the Rank and Percentile functions To rank stocks based on their daily performance, type: a = comp(perf(close, 1), "rank"); To buy top 20 stocks with the highest performance, type: buy = comp(perf(close, 1), "rank") <= 20; The best stock has a rank of 1, the next one has a rank of 2 and so on… Intraday Stock Trading System with EOD Ranking Now, it is time to combine EOD and intraday rules. Since our strategy is based on intraday data, we can reference our EOD rank rule we implemented previously using the "TimeframeSet" function. This function allows us to switch to a different time frame, performs calculation then returns back to the original time frame. Here is the process: - We start with intraday time frame - We use "Timeframeset" function to switch to daily time frame - We calculate one-bar return (The time series is automatically synchronized with the main time frame) - We move back to the intraday time frame - We rank stocks based on the one-bar return and get the 20 best performing ones - We create the intraday rule (cross) - We combine the ranking and the cross rules Here is how to implement the formula: // EOD TimeframeSet(-1); rule1 = perf(close, 1); rule1 = timeframedecompress(rule1); // This function is used to decompress the time series and synchronize it with intraday data TimeframeRestore(); dailyRule = comp(rule1, "rank") <= 20; // Intraday res30 = FirstMinutesHL(30); inRule = cross(close, res30); // Buy Rule buy = inRule and dailyRule; // Sell Rule sell = day() != ref(day(), 1); // Exit the same day SetSimTiming(_Sell, _Close, -1); // Sell at close of current bar (instead of open of next bar) Before backtesting this strategy, make sure you already downloaded end-of-day and intraday one-minute data for the analyzed symbols. A similar trading system is available for download in the sharing server. You can get it here: Intraday Trading System with Daily Ranking
|