buy = 1;

sell = 1;

buy = rsi(14) > 70;

rsi(14) > 70

buy = rsi(14) > 70;

Other examples:

buy = cross(rsi(14), 70);

buy = (high == hhv(high, 10));

buy = close > ref(close, 1);

sell = close < ref(close, 1);

Using Multiple Rules:

buy = close > open AND volume > sma(volume, 5);

a = close > open;

b = volume > sma(volume, 5);

buy = a AND b;

buy = open > high[1] OR close > high[1];

buy = open > ref(high, 1) OR close > ref(high, 1);

Plot it on a chart:

buy = llv(close > close[1], 3) == 1;

buy = llv(close > close[1], 3);

buy = close > close[1] and close[1] > close[2] and close[2] > close[3];

buy = istrue(close > ref(close, 1), 3);

buy = close > close[1] and close[1] > close[2] and close[2] > close[3];

plot(buy, "Buy Test1", colorRed);

buy = istrue(close > ref(close, 1), 3);

plot(buy, "Buy Test2", colorGreen);

Orders:

SetSimTiming(_Sell, _Close, -1);

SetSimTiming(_Sell, _Open, 0);

Example:

short = perf(close, 5) > 10;

cover = sma(5) < sma(10);

Stop Loss

Trailing Stop

Profit Stop

N-Bar Stop

Note

Important:

SetSimLongRank(perf(close, 5));

SetSimShortRank(perf(close, 5));

This is the ultimate guide to help you get started with backtesting and creating trading systems in QuantShare.Keep in mind that now we are just scratching the surface of what is possible to do with QuantShare.At the end of this post, you will find a quick video presentation of some of the backtester features we will show you here as well as many links to other blog posts related to backtesting and creating trading systems.Let us start by two definitions:1/ A trading system is a group of rules and parameters that determine exact entry and exit points (signals) for a given security.2/ Backtesting refers to testing a trading strategy using historic data. The main goal is to determine how profitable a strategy is if we invested in it in the past.- Select "Analysis -> Simulator" to open the simulator/backtester- Click on "New" at the top to create a new trading systemThis will bring the trading system control where you can define all settings necessary to create your trading system:In order to create a trading system you must define the following settings:- Formula to define buy, sell, short and cover rules- System Type (Long, short or both)- Symbols (Stocks, ETFs, Futures...)- Period or time frame- Maximum number of positions- Simulation start/end datesThese are the basic settings. Other settings include: Ranking systems, money management scripts...To open the formula editor, select "Strategy" icon at the top then select "Create trading system using the formula editor" tab.The formula should be implemented in QS language.QS language is an easy to use programming language you can use to create trading systems, charts, composites...You can find a beginner guide here: https://www.quantshare.com/sa-499-quantshare-programming-language-tutorial The formula would be applied to every security included in your trading system. Its main role is to generate buy/sell signals.For a long only system, you should create two variables (buy and sell). In the formula editor, you can use the CONTROL+SPACE short-cut any time to display the list of available functions/indicators.A basic system to buy every security on every bar would be:If you add the following line:Then your system will enter long on every bar and exit on the next bar.Let us see now another example:- Enter long when security's RSI (relative strength indicator) becomes higher than 70:Notice that each time after you type your function (Example: RSI) and the first parenthesis, QuantShare automatically shows a tool tip with the definition of the function as well as the different parameters that it accepts.Explanation:The function name to create the relative strength indicator is "RSI". The simplest form of that function accepts one parameter which is the RSI period.Therefore, here is how the RSI becomes higher than 70 should be translated into QS language:In order to enter long when this condition is met, we need to associate it with the "buy" variable.- Enter long when RSI crosses above 70:- Enter long when stock reaches 10-bar high:- Enter long when stock increase and exit when it decrease:If you want to set two or more rules to enter long then you can use the "and" or "or" operator:Example:- Enter long when close is higher than open and when volume is higher than 5-bar volume average:You can also write this formula like this:- Enter long when open is greater than previous bar's high OR when close is greater than previous bar's highYou can also write this formula like this:Exercise: Say we want to buy a security when it increases 3 days in a row.Which of the following formulas is correct?The answer is: They are all correct.Not sure? Just plot the variable "buy" on a chart and you will see that they are the same.Plotting a variable on a chart is a very good idea to have a visual confirmation of the values of that variable for every bar.How to do that?- Open a chart (View -> New Chart)- Right click on the chart then "Create a new pane"- Right click on that new pane then select "Edit Formula"- Type something like:Click on "Update Graph".You can clearly see now that both "buy" variables are exactly the same.By default, an order to buy/sell at the open of next bar is generated when the buy or sell rule is true (=1).You can change this using the following function: "SetSimTiming".To exit at the close of the signal date (The signal date is the date at which the exit order was initiated - when "sell" was equal to 1):To exit at the open of next bar (default order):In "System type" ("Strategy" tab), you can define whether you want to have a long, short or combined long/short trading system.When "Long" is selected, your trading system will be able to buy or enter long securities.When "Short" is selected, your trading system will be able to short securities (short-selling)We already saw that in order to create long signals, we need to initialize (buy and sell) variables. In order to create short signals, we must initialize (short and cover) variables.Short sell a security if its 5-bar performance is higher than 10% and cover it (buy back the security) if its 5-bar moving average drops below its 10-bar moving average.A stop order is an order to sell a security if a certain condition is met.Under the formula editor, you can choose one of these orders:: Exit any position if it falls below a certain amount or a certain percentage.: Exit any position if it falls below a certain amount or a certain percentage starting from the highest (long trades)/lowest (short trades) price it reached since the position was initiated (This stop is used to protect profits).: Exit any position if it reaches a certain amount or a certain percentage: Exit any position after a specific number of barsThis is the symbols or securities that will be used by your trading system.If you want to create a trading system that trades U.S. stocks then you should select U.S. symbols. If you want your trading system to generate entries and exits for EUR/USD equity then specify EURUSD as symbol.You can do that by clicking on "Symbols & Dates" icon at the top then adding one or several conditions in the symbols control.As an example, you can select "Index" as a condition then add "NASDAQ 100" to reference NASDAQ 100 stocks only.You can also add a "Custom Symbols" condition then type one or several symbols there.: In order to select stocks by industry, sector, index..., you must enable QuantShare to manage your symbols. To do that, select "Symbol -> Auto-Manage Symbols", check the exchanges you want to track, check "Force the update of ..." option then click on "Save".You can update the maximum number of positions allowed in the trading system (at the same time) buy updating the "Number of positions" field (Under "Strategy").Let us say you want to trade EURUSD and thus have specified EURUSD as symbol in your trading system. In that case, the number of positions should be equal to 1.If you want to trade NASDAQ100 stocks and don't want to invest in more than 10 stocks at the same time then you should put 10 as number of positions.Each position will get around 10% of the total capital.If you set the number of positions to 5 then each position will get around 20% of the total capital.This means that if you have $100,000 as equity and your trading system generates 5 orders (GOOG, AAPL, C, GE, MSFT for example) then the simulator will invest around $20,000 in each stock. If GOOG price is $555 then the simulator will buy 36 shares (36 * $555 = $19,980)Under "Symbols & Dates", you can select the start/end period of your simulation/backtest as well as the time frame used by your trading system.If you want your trading system to generate entry and exit signals using end-of-day data, just select "Daily" as time frame.If you need an intra-day trading system then select an intra-day time frame such as tick, 1-second, 1-Minute, 1-Hour...You can also have a custom time frame by selecting "Custom" next to "Select a time frame". Once selected, type the number of seconds that is needed to build a single bar.To create a 1-minute, 30 seconds time frame, just type: 90 (60s + 30s).If you want to backtest a daily trading system, make sure you have downloadeddata for the symbols referenced by your trading system.If you want to backtest an intra-day trading system, make sure you have downloadeddata for the symbols referenced by your trading system.You can download data using the downloader tool (Download -> Download Manager).We do have several downloaders in the sharing server (both for EOD and intra-day data).Think about the following situation:On day N, your trading system generates 5 signals and your portfolio is allowed to take only 3 positions.In this case, which securities will be bought first?By default, symbols are ordered by alphabetical order and the first one is taken first.But you can update this default behavior by ranking securities based on a given formula.Let us say that you want the stock with the best 5-bar performance to be purchased first.In that case, you just need to type:This is for long orders. You can also specify a ranking formula for short orders by typing:Before we backtest our trading system, select "Symbols & Dates" icon then specific the start and end backtesting period at the bottom.After saving your system, select it in the simulator manager (Analysis -> Simulator) then click on "Simulate" or "Backtest". When done, you will get a detailed report on how your trading system would have performed in the past.By now, you should be able to create and backtest some basic trading systems. But if you want to create more advanced strategies and learn more about QuantShare powerful backtesting features I suggest you take a look at the following blog posts: