Building A Trading System

userque

TSP Legend
Reaction score
36
A place to toss around ideas regarding building trading systems (mainly in an excel spreadsheet) that might include machine learning capabilities.

JTH posed the question in our discussions about how to determine the trend.

One way would be for the code to recognize higher highs/lows=an uptrend; lower highs/lows=downtrend; mixed=no trend. (I believe JTH also mentioned a transitioning trend, or something like that :) ) Isn't this part of Dow Theory??

Well, before coding anything, we'll have to first clarify how we, ourselves, (or JTH, really :) ) can personally look at a chart an determine what trend we are in. Then I'll try to reduce it to code/formulas.

I'm assisting JTH with this, so it's his final determination; but any ideas are welcome I'm sure. We are doing this publicly as it may help another or another may help us.

Any other general questions are welcome too.
 
This is a tough nut to crack, should we start with the timeframe of data to be used, I'm guessing this will be done on the Daily scale?
 
This is a tough nut to crack, should we start with the timeframe of data to be used, I'm guessing this will be done on the Daily scale?

I'm thinking Daily as well. I have some good ideas.

I'll start by building one that calculates the trend: up, down, none. It'll go long in an up trend, short in a down trend, and cash in no trend.

It'll keep track of a simulated portfolio balance. That way, we'll know how well it performs.

There are some parameters associated with determining a higher high, and lower low, etc. How far do you look back in time, etc. It'll be clearer once I build it. We will be able to see how changing this parameter affects the results.

There's another variable associated with how the "portfolio balance" is calculated. More on that later. We should probably do this one step at a time, or it can get real convoluted real fast.

I build models where I can change the parameters manually; then, I add the automation afterwards. I let computer code run through scores, hundreds, or thousands of possibilities to find the optimal settings for the system.

We can start simply with the trend, then get as complicated as we want; one step at a time.

After we get this trend idea where you want it, we can proceed with your next idea/indicator/etc.

Let me know when I go off track. :)
 
I'm thinking Daily as well. I have some good ideas.

I'll start by building one that calculates the trend: up, down, none. It'll go long in an up trend, short in a down trend, and cash in no trend.

It'll keep track of a simulated portfolio balance. That way, we'll know how well it performs.

There are some parameters associated with determining a higher high, and lower low, etc. How far do you look back in time, etc. It'll be clearer once I build it. We will be able to see how changing this parameter affects the results.

There's another variable associated with how the "portfolio balance" is calculated. More on that later. We should probably do this one step at a time, or it can get real convoluted real fast.

I build models where I can change the parameters manually; then, I add the automation afterwards. I let computer code run through scores, hundreds, or thousands of possibilities to find the optimal settings for the system.

We can start simply with the trend, then get as complicated as we want; one step at a time.

After we get this trend idea where you want it, we can proceed with your next idea/indicator/etc.

Let me know when I go off track. :)

Yea I like that a lot.

Instead of cash in no trend, I'm wondering if we can get it to hold the position if the previous trend was up, until a circuit breaker is blown? My thinking is that market pauses are more likely to lead to a continuation of the previous trend. But this can also be where key reversal take place, so some sort of circuit breaker may be needed. I'm not sure how complicated this makes it (from the scientific perspective).

How far do you look back in time? I'm hoping we can work this within the forum's 2-IFT limitations. For myself, I tend to look at things in the 3-6 month timeframe, but trends tend to change within a 3 month timeframe, so maybe something 2-3 months out, something that won't generate more than 2 signals a month.

Perhaps 1 signal a month, with the occasional 2 signals generated under extreme conditions?
 
Yea I like that a lot.

Instead of cash in no trend, I'm wondering if we can get it to hold the position if the previous trend was up, until a circuit breaker is blown? My thinking is that market pauses are more likely to lead to a continuation of the previous trend. But this can also be where key reversal take place, so some sort of circuit breaker may be needed. I'm not sure how complicated this makes it (from the scientific perspective).

How far do you look back in time? I'm hoping we can work this within the forum's 2-IFT limitations. For myself, I tend to look at things in the 3-6 month timeframe, but trends tend to change within a 3 month timeframe, so maybe something 2-3 months out, something that won't generate more than 2 signals a month.

Perhaps 1 signal a month, with the occasional 2 signals generated under extreme conditions?

JTH, here is the beauty of what we're doing. Nothing has to be set in stone. There will be a parameter (now that you've given me the idea) that will control whether the system goes to cash, or continues the prior trend.

The flaw is in assuming one or the other. We will simply test them both and see what happens. :)

I probably didn't explain the 'how far back in time' thing.

If a trend is defined as higher highs and lows, or lower highs and lows; then we have to know how far back to look when determining whether the most recent high, is a higher high, etc. 5 days, 10 days, etc. It'll be clearer later.
 
Last edited:
JTH, here is the beauty of what we're doing. Nothing has to be set in stone. There will be a parameter (now that you've given me the idea) that will control whether the system goes to cash, or continues the prior trend.

The flaw is in assuming one or the other. We will simply test them both and see what happens. :)

I probably didn't explain the 'how far back in time' thing.

If a trend is defined as higher highs and lows, or lower highs and lows; then we have to know how far back to look when determining whether the most recent high, is a higher high, etc. 5 days, 10 days, etc. It'll be clearer later.

That's a very tough question to answer, I'm lucky I've had the opportunity to backtest in the past (using Ninja) otherwise I'd just be throwing out a truly wild guess (which I still am).

I like number 9, it matches up well with the 252 days of the year (9 x 28 = 252)

I also like number 13, it matches up well with the 52 weeks of the year. (13 x 4 = 52)
 
Last edited:
That's a very tough question to answer, I'm lucky I've had the opportunity to backtest in the past (using Ninja) otherwise I' just be throwing out a truly wild guess (which I still am).

I like number 9, it matches up well with the 252 days of the year (9 x 28 = 252)

I also like number 13, it matches up well with the 52 weeks of the year. (13 x 4 = 52)

:) No problem. That will also be a parameter setting. We'll change it as easily as typing a number in an excel cell. Then we'll see how well each setting performs. We will see how well the common values of 5, 9, 13, 26, etc. do vs. non-traditional values.

When we automate this 'changing of all the different parameter settings' process; we will have built a Machine Learning algorithm. But that's a little further down the road. :)
 
9 and 13 are fairly short term trend averages... On dailies

Have you seen the longer trend weeklies at 20/50 or even monthlies? Should a system react differently in a bull vs bear market/trend?


Sent from my (Daughter forcing me to use an) iPhone using Tapatalk...
 
I think there is a mis-understanding of the parameter I'm referring to. :)

I'll draw a diagram later today.
 
9 and 13 are fairly short term trend averages... On dailies

Have you seen the longer trend weeklies at 20/50 or even monthlies? Should a system react differently in a bull vs bear market/trend?


Sent from my (Daughter forcing me to use an) iPhone using Tapatalk...

I don't think they are talking about 9 & 13 day moving averages. I understood it as using the last 9 or 13 market peaks & troughs to determine a trend. That's something a person could do easily with a chart but the trick here is to get a program to do it numerically. That's not as easy as it sounds. For example, what do you call a peak or a trough? Because of the daily jitters in the market you can't just go by whether the closing price is above/below the previous day's price. You get up & down days in up, down & flat markets. That's the kind of stuff moving averages are designed to dampen out but I don't think that is where this system is going.
 
I don't think they are talking about 9 & 13 day moving averages. I understood it as using the last 9 or 13 market peaks & troughs to determine a trend. That's something a person could do easily with a chart but the trick here is to get a program to do it numerically. That's not as easy as it sounds. For example, what do you call a peak or a trough? Because of the daily jitters in the market you can't just go by whether the closing price is above/below the previous day's price. You get up & down days in up, down & flat markets. That's the kind of stuff moving averages are designed to dampen out but I don't think that is where this system is going.

Oh, ok, will go back to spectating... "This is not the idiot you are looking for"
 
1.jpg

Guess I only need the one on the right.

The parameter I referred to determines how far back the system looks for highs and lows while trying to determine whether a higher high, or lower high has occurred.

If a shorter time frame is used, E is a higher high.

If a longer time frame is used, E is NOT a higher high.

I've already got this coded into the sheet and will post it later today. :)
 
2.jpg


  • Parameters etc. will be in the first two columns.
  • The data is SP500.
  • MAX is the maximum close within the time frame defined by the parameter.
  • HIHELP is a column used to help calculate the HI column.
  • HI? indicates whether the close for that row/day is a local high.
  • PREVHI indicates the previous local high value.
  • HIERHI indicates whether the close for that row/day is a higher high, lower high, or neither. 1 is a higher high. -1/(1) is a lower high. 0 is neither.

Next, I'll repeat this for the LOW.

Then, I'll add the simulated trading account code.
 
Oh, ok, will go back to spectating... "This is not the idiot you are looking for"

Sorry, you don't get to hold that title.

As I understand it, this is a portion of the overall system, where we first determine if the market is trending, oscillating, or transitioning. This helps us to understand what tools to use, based on the conditions the markets are operating under.

As an example, you might not want to use a moving average indicator during oscillating (whipsawing) conditions because it could force you in & out of the markets prematurely.
 
View attachment 37704


  • Parameters etc. will be in the first two columns.
  • The data is SP500.
  • MAX is the maximum close within the time frame defined by the parameter.
  • HIHELP is a column used to help calculate the HI column.
  • HI? indicates whether the close for that row/day is a local high.
  • PREVHI indicates the previous local high value.
  • HIERHI indicates whether the close for that row/day is a higher high, lower high, or neither. 1 is a higher high. -1/(1) is a lower high. 0 is neither.

Next, I'll repeat this for the LOW.

Then, I'll add the simulated trading account code.

Interesting, I did something similar a few years back, with the T21220-23 indicators on the 4/13/26/52 high/low charts from Freestockcharts.com. I used them to identify oversold conditions, but never applied them in a way to identify all market conditions. This should be fun...
 
3.jpg

Next, I have to logically tie it all together into one TREND SIGNAL, before moving on to the simulated account.
 
Interesting, I did something similar a few years back, with the T21220-23 indicators on the 4/13/26/52 high/low charts from Freestockcharts.com. I used them to identify oversold conditions, but never applied them in a way to identify all market conditions. This should be fun...

Fun...Absolutely! Like you, I started something like this a while back, but abandoned it for a precursor to my current system. I've always wondered how a ML system, giving every sort of indicator (rather than looking for patterns as I currently do), would perform; but I've never been motivated enough to build it.

Doing it publicly, while also helping others, gives me the added motivation I need.
 
1.jpg

Finished our custom TREND indicator. Added a new parameter. The new parameter tells the system to treat NO TREND as either a move to CASH, or a continuation of the previous signal.

No one mentioned this, but we eventually should add an additional TREND indicator that's based on the High and/or Low of the day, instead of the Close. We can see how they compare to each other with regard to profit.

EDIT: 1 is up trend, -1/(1) is down trend, 0 is cash (0 only appears if the parameter is set for it to appear)

On to the simulated account.

(Btw, it doesn't take long to build this; I'm just nibbling at it here and there. :) )
 
Back
Top