Click here to Login




The Ultimate Guide to Create Trading Systems in QuantShare

Updated on 2014-06-14





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.


What is a trading system?

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.


Let us Create our First Trading System

- Select "Analysis -> Simulator" to open the simulator/backtester
- Click on "New" at the top to create a new trading system

This 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 dates

These are the basic settings. Other settings include: Ranking systems, money management scripts...


Your First Trading System Formula

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:
buy = 1;

If you add the following line:
sell = 1;

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:
buy = rsi(14) > 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:
rsi(14) > 70

In order to enter long when this condition is met, we need to associate it with the "buy" variable.
buy = rsi(14) > 70;


Other examples:

- Enter long when RSI crosses above 70:
buy = cross(rsi(14), 70);

- Enter long when stock reaches 10-bar high:
buy = (high == hhv(high, 10));

- Enter long when stock increase and exit when it decrease:
buy = close > ref(close, 1);
sell = close < ref(close, 1);



Using Multiple Rules:

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:

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

You can also write this formula like this:

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



- Enter long when open is greater than previous bar's high OR when close is greater than previous bar's high

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

You can also write this formula like this:

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


Plot it on a chart:

Exercise: Say we want to buy a security when it increases 3 days in a row.

Which of the following formulas is correct?

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);


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:

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);


Click on "Update Graph".

You can clearly see now that both "buy" variables are exactly the same.


Orders:

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):
SetSimTiming(_Sell, _Close, -1);

To exit at the open of next bar (default order):
SetSimTiming(_Sell, _Open, 0);


Trading System Type

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.

Example:
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.

short = perf(close, 5) > 10;
cover = sma(5) < sma(10);



Define Your Stops

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:
Stop Loss: Exit any position if it falls below a certain amount or a certain percentage.
Trailing Stop: 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).
Profit Stop: Exit any position if it reaches a certain amount or a certain percentage
N-Bar Stop: Exit any position after a specific number of bars


Symbols

This 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.

Note: 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".


Maximum Number of Positions

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)


Trading System Period/Time frame

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).

Important:
If you want to backtest a daily trading system, make sure you have downloaded EOD data for the symbols referenced by your trading system.
If you want to backtest an intra-day trading system, make sure you have downloaded intra-day data 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).


Ranking Your Securities

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:

SetSimLongRank(perf(close, 5));

This is for long orders. You can also specify a ranking formula for short orders by typing:

SetSimShortRank(perf(close, 5));


Backtesting a Trading System

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:

Trading System Implementation:
Example of a trading system implemented in QuantShare Software
Trading System: Buy Stocks based on their Sharpe Ratio Rank
Trading System: Buy stocks with the highest Sharpe ratio
Trading System: Moving average crossover and few others buy and sell rules
How to turn any ordinary trading strategy into a very profitable one
Buy the best/top rated stocks or how to create powerful rank based trading systems
Backtesting trading strategies using intra-day data

General Information:
How to build and backtest a robust stock trading system
Troubleshooting a Trading System
9 mistakes you should avoid when backtesting an end-of-day stock trading system
Reducing risk and increasing return by combining several trading systems
True portfolio simulation applied to trading systems
3 ways to rank stocks in a trading system - Simulator and Potfolio
6 ideas to implement in your portfolio to reduce your trading risk
6 techniques to improve the performance of your stock trading strategy
Trading System Analysis: Backtesting report and custom measures

ETF Systems:
Select the best ETFs combination to maximize your return and reduce your investment risk
Backtesting a Strategy Based on Bond and Stock Index ETFs
Create Profitable Trading Strategies with Exchange Traded Funds (ETFs)

Money Management:
Basic trading system implemented using the money management tool
Create a trading strategy using the money management tool - Part 1
Create a trading strategy using the money management tool - Part 2
Money Management: Optimize a trading system
Several money management strategies in a trading system
Money Management: Scale-in Trading Strategy
5 position sizing techniques you can use in your trading system
How to combine long, short and custom portfolio strategies within a trading system

Advanced Techniques:
Day Trading: A trading system that combines intraday and EOD data
How to Select the Best Market Indicator for your Trading System
4 indicators to create adaptive trading systems
Intermarket Analysis - Correlation and Trading Strategies
How to create buy and sell trading rules based on News Data
4 original breadth indicators you should consider in your market timing strategy
How to Backtest an Intraday Stock Trading System with EOD Ranking
Backtesting chart patterns using the auto support and resistance indicator
10 masks to create thousands of rules to use into your trading system
How to choose which technical indicators to use in your trading system
How to create a trading system, screen and composite using earnings surprise data
How to Switch On/Off Trading Rules in Your Stock Trading System
How to Backtest Each Stock or Asset Individually
How to Backtest Your Trading System for Each Industry












one comment (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1232 days ago

Profile Graphs
Posted 1337 days ago

QuantShare
Previous Posts

How Does QuantShare Work?
Posted 3709 days ago

Troubleshooting a Trading System
Posted 3750 days ago


More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.