QS language is a very simple programming language used in QuantShare to plot lines, create indicators, composite, trading systems, watchlists, scan stocks... Previously, we created a blog post that introduces QS language. We strongly suggest you read that post before continuing here. QuantShare Programming Language Tutorial Besides the popular and less popular indicators, there are a lot of functions that can be used in QS language to plot and detect your own pattern, formula, indicators... Many of the most basic functions were introduced in the blog post referenced in the link above. Introducing the logical operators (And/Or) Let us begin by one example: a = close > 10.5 AND volume > 1000; In the above example, the "And" operator was used (you can also use && instead). The formula assigns a value of "1" to each bar of the "a" array/vector if the close is higher than 10.5 AND the volume is higher than 1000; b = close > 10.5 OR volume > 1000; The above formula assigns a value of "1" to each bar of the "a" array/vector if the close is higher than 10.5 OR the volume higher than 1000. You can use || instead of OR. If the conditions are not met then "a" would get a value of "0" on the corresponding bar. You can combine AND and OR operators. Make sure to use parentheses to make the code clean. Example: c = (close > 10.5 AND volume > 1000) OR (close > 10.5 AND volume < 1000); Here is how the values of the a, b and c variables will look like given a sample close and volume data. Referencing another time-series When analyzing a particular formula, whether it is for charting, backtesting or screening, that formula is always applied for each symbol individually. When the formula finds the variable "close", the engine will use the close price of the currently analyzed security. The "GetSeries" function allows you to reference an external security time-series. closeSPY = getseries("spy", close); This will get the close series of the "SPY" ETF and associate it with the "closeSPY" variable. Now, let us analyze the following formula: closeSPY = getseries("spy", close); a = close / closeSPY; filter = sma(a, 10); If you are running this formula using the screener tool and you are analyzing say two symbols (IWM and QQQ) then screener will run that formula on these two symbols. The first run will calculate the ratio of the IWM close series to the SPY close series and then return the 10-bar moving average of that ratio. The second run will calculate the ratio of the QQQ close series to the SPY close series and then return the 10-bar moving average of that ratio. Note how the SPY series is fixed here since we explicitly referenced the ticker symbol "SPY" in the "GetSeries" function. Indicators Crossover Often you need to detect when a time-series or an indicator crosses above or below another time-series, indicator or an horizontal line. This can be easily achieved using the "cross" formula. Below are three examples: // Returns 1 for each trading bar where the close price crosses above 20. The close price must be lower than 20 in the previous bar and higher than 20 in the current bar a = cross(close, 20); // Returns 1 for each trading bar where the close price crosses above its 20-bar simple moving average a = cross(close, sma(20)); // Returns 1 for each trading bar where the close price crosses below its 20-bar simple moving average (The opposite of the previous line) // This is equivalent to 20-bar simple moving average crossing above the close price a = cross(sma(20), close); Tips In the QuantShare formula editor, there is a very important shortcut that allows you to display the list of available functions depending on the cursor location (start of the line, after "=" operator, inside a function parameter...). That shortcut is CONTROL+SPACE. When typing a function and after you type the first parenthesis (Example: a = rsi( ), QuantShare will display a tooltip that shows the number of parameters that the function accepts and a description of each parameter. You can also click on that tooltip to display other variations (if exists). This is in case the function accepts different numbers of parameters. As an example, the "RSI" function has two variations. One that accepts two parameters: a = rsi(close, 14); // 14-bar RSI of the close time-series And another one that accept just one parameter: a = rsi(14); // Same as above. In this case, the close time-series will be used by default
|