You need to enable JavaScript in order to visit our site!
 
Logo PTMC
NAVIGATION
CANCEL
 
or via PTMC account
Restore password
or via PTMC account
Logo PTMC
NAVIGATION
or via PTMC account
Restore password
or via PTMC account
or via PTMC account
Restore password
or via PTMC account
PTMC team 14.09 2013

How to make your own Trading System

Linear Regressions

Introduction

Linear regression is a well-known, well-studied instrument used for statistics and econometrics. That said, it’s less known in trading as some big names like moving averages or oscillators.

Linear regression is built as a line segment that is spaced at least distance from the source data. In terms of technical analysis, the linear regression can be called a trend line. However, unlike trend lines which analysts draw by hand, linear regressions are built automatically using mathematical formulas. When building a linear regression in Protrader all you really need to specify is the amount of points that will be used for building the line. This number of points is called the period.

Let’s try to work through this together. First, upload the linear regression indicator to the Protrader 3 terminal by opening a chart, clicking on indicators, import, and adding the regression. Now go back into the indicator menu and choose it from custom indicators and add it to the chart with a period of 25.

Pic 1. Linear regression (period = 25) on a 30m EUR/AUD chart showing a downward trend.

Linear regression (period = 25) on a 30m EUR/AUD chart showing a downward trend.

Pic 2. Linear regression (period = 80) on a 1m EUR/CAD chart showing an upward trend.

Linear regression (period = 80) on a 1m EUR/CAD chart showing an upward trend

As you can see from the drawing, the linear regression accurately indicates the current market situation. If the line is close to being horizontal then there is no trend. The period should be close to the current duration of the market phase (trend, trading range). If the period is too large, then the line will lag behind. Likewise, if the period is too small there will be more false alarms. This is similar to how most other technical analysis indicators work. Therefore before you go off and manually trading with this be sure to adjust the period to fit current market conditions. For automated trading it’s a bit easier to adjust this period through backtesting and optimization on historical data.

Starting your very own trading system

Let’s try making the foundation of a trading system using the basics of linear regression to help predict market momentum. Open and hold a long position when the line points upward and open another short position when the line points downward. For clarity add another indicator to Protrader 3, the Linear Regression Slope, which shows the slope in a separate window. It’s worth noting that the angle is not in degrees but in relative units. This just means that we buy when greater than zero and sell when less than zero.

Creating a program

We’ll be using AlgoStudio for creating indicators and trading systems. AlgoStudio is an integrated development environment for C# and is part of the Protrader 3 Desktop platform. AlgoStudio is going to give us the possibility to write, test on historical data, and optimize indicators and trading systems. We can even test on historical tick data. So our goal in creating a trading system is to reproduce a situation that is as close as possible to how the market works. In this example we are using C# for our indicators and trading system. Load AlgoStudio and open the file RegressionTrader.cs.

Here is the condition we’ll use for entering the market:

if(b>0.0)//buy if the angle of b is more than 0.0
{    
OpenOrder(OPERATION_BUY);
}
if(b<0.0)//sell if the angle of b is less than 0.0
{    
OpenOrder(OPERATION_SELL);
}

The codes for the indicators and system can be downloaded below. If you take a good look at the code you can see that the indicator code is completely integrated into the trading system. You don’t necessarily have do to this since you can just use the indicator definition with the function iCustom().

Testing

So let’s try a test. Below we’ll take the example of testing the system on EUR/USD with a 5m period. AlgoStudio lets you test the strategy with visual results on a graph. I use this feature in the screenshot below.

Pic 3. Graphic Balance

 Graphic Balance

Pic 4. Trades shown on the chart

Trades shown on the chart

Hmm, looks like we have a negative result. Judging by the chart, all but two trades were negative. Even though they aren’t really big losses they still count as a large part of the overall loss. If you look at the chart where the trades were made the reason for our loss becomes clear. It looks like the entrance and exit points are based more on price fluctuations than trends in market segments. You see that the price isn’t really going anywhere but the system is still generating trades. To fix this we could add a filter that would turn the system off if there wasn’t a clear trend. This would involve establishing some type of minimum threshold for system response. For example, a condition to enter the market not when the regression angle is greater than 0.0, but instead after the angle crosses some optimized point based on historical values. But that kind of filter would cut off lots of our possibilities to profit. Also we could run into a situation where the angle of regression just barely hits the filter level but the price action doesn’t continue in the correct direction. This would give us some pretty big losses.

Pic 5. The angle of linear regression

The angle of linear regression

In my opinion, the best option would be to use a filter that takes the nature of price changes into account. For example, price deviation from the regression line generally decreases when there is an increasing trend and increases when some range has been established on the market. Let’s try to capture this concept in our trading system by adding a rule to enter the market not only in the direction of the regression angle, but also there is a cross of the standard deviation channel.

Here is what I mean:

if(b>0.0&&BidClose(0)<bottomLine)
//condition for going long
{    
OpenOrder(OPERATION_BUY);
}
if(b<0.0&&BidClose(0)>topLine)
//condition for shorting
{    
OpenOrder(OPERATION_SELL);
}

The topline и bottomLine variables are borders of the standard deviation channel.

Optimization

Optimization is going to help us find the best parameters for the linear regression period. A distinctive feature of AlgoStudio is the possibility to change the settings of the genetic algorithm and go through optimization with specific objectives in mind. For our purposes we’ll choose maximum profit.

Pic 6. Optimization

Optimization

Pic 7. The best optimization result.

The best optimization result.

Hey alright – a positive result! The profit factor was 30.95. That’s a pretty great number, but it is too early to get excited since there aren’t really enough transactions in the test. In order to get a better idea about the quality of our system we need to test it a lot more on longer histories and then of course on a demo account.

What else are we missing in this basic system? Well, it would be good to add some stop losses to keep our overall loss low and some money management parameters that would get the most profit for this type of system and our account size.

LinearRegression.cs

LinearRegressionSlope.cs

RegressionTrader.cs


Have not tried PTMC yet? There is no better way to boost knowledge than to use it! Start trading with PTMC now!

Discussion
Join PTMC community to post your comments
No comments yet. Be the first.
PTMC is a professional trading platform that combines all the best charting and analytical tools to trade on different financial markets. It is developed by PFSOFT UK LTD, a global provider of trading technology for banks & brokers
© 2024. PTMC is based on Protrader technology
PFSOFT UK LTD
71-75, Shelton Street,
Covent Garden,
London, WC2H9SQ,
United Kingdom
Support & Development
PFSOFT Ukraine
Office #701
Mechnikova Street, 10B
49000, Dnipro, Ukraine
Contact us
Phone: +380 56 740 30 01
E-mail: info@protrader.org
Skype: PTMC_support
Find more ways to contact
Join us in social networks