More fun with Brownian Motion

Friday, Mar 15 2019 by

There was an article here a couple of weeks ago about simulating share prices using a Random Walk generator in Excel. This set me thinking about simulating feedback from the traders - working on the assumption that stock price movements are random, but steered by simple psychological quirks. I did try years ago to write a prediction algorithm using Regression - guess what, to no avail. And TL:DR, there's no payoff to this article either.

The reason I'm writing it up is this - I noticed that among all the Technical Analysis tools available, one that's missing is the ability to draw Support and Resistance lines automatically. I may have missed such a tool deliberately in my eagerness to work out how to generate these myself.. anyhow.

I'm using the language R, for which folk everywhere are writing packages of code for all kinds of applications - there are a few on the topic of share-trading: quantmod, TTR. TTR stands for Technical Trading Rules, and its purpose is, I suppose, to write compound rules that parse a history of prices & volumes for some over-complicated bespoke signal. But there's no Support/Resistance Level function in it. Most of the functions available are variations of things like Bollinger bands, things which use moving averages and standard deviations. This seems to be a gap - it's a commonplace to talk about "breaking through the resistance", "reluctance to fall below the support",  and so on. Seeing these levels is subjective, but of course that doesn't mean we can't winkle them out mathematically.

Here goes. First, generate a random-walk sequence, and mark local peaks and bottoms. (not sure if these pics are too wide..)


Then take those extremes and run a clustering algorithm on them - I used one called kmeans. Maybe others would be better, idk. I won't describe all the fiddles involved here to identify what to keep, it was pretty traumatic. Here's the result..


So now I can extend the sequence, taking these levels into account, and updating the resistance/support levels from the recent past, each time. All I did was to make the wandering line baulk at crossing a resistance line upwards, or a support line downwards. The line-generation program only looks at the sequence's recent history, so old levels drop out after a…

Unlock this article instantly by logging into your account

Don’t have an account? Register for free and we’ll get out your way


As per our Terms of Use, Stockopedia is a financial news & data site, discussion forum and content aggregator. Our site should be used for educational & informational purposes only. We do not provide investment advice, recommendations or views as to whether an investment or strategy is suited to the investment needs of a specific individual. You should make your own decisions and seek independent professional advice before doing so. The author may own shares in any companies discussed, all opinions are his/her own & are general/impersonal. Remember: Shares can go down as well as up. Past performance is not a guide to future performance & investors may not get back the amount invested.

Do you like this Post?
4 thumbs up
0 thumbs down
Share this post with friends

4 Posts on this Thread show/hide all

schober 16th Mar 1 of 4

Why not use point and figure chart software to identify support and resistance?
(I've found stockcharts historic prices are not always correct and the charts need checking!)
There are a few open source packages in Python and R
I've used
Unfortunately it doesnt seem able to use uk or canadian prices from yahoo!

It might be useful to put your data through Pypf and see if it gives the same s&r values

| Link | Share | 1 reply
snickers 16th Mar 2 of 4

In reply to post #458588

Thanks I've not heard of point & figure charts. There turns out to be an R package rnpf which makes charts like this - it says that this is the contemporary version of the XOXO type of chart in your picture..


very helpful!


so I claim the win.

You used to be able to download prices from Yahoo via quantmod, but this was stopped a year or so ago. Overuse or underuse?

| Link | Share
schober 24th Mar 3 of 4

I did consider Rpnf but found that installing R on Puppy Linux was not straightforwards; so opted for Python from ActiveState & Pypf . I cant say I'm impressed with the newer pnf, presumably it will plot the old way? Pnf I find is good way to filter out the noise inherent in the time series and so to present the data more clearly
I would suggest you play about with the box size to see what works best - I found it quite difficult to read the graph. Iuse a box size derived from the average true range.

| Link | Share
aflash 26th Mar 4 of 4

"breaking through the resistance", "reluctance to fall below the support", and so on. Seeing these levels is subjective, but of course that doesn't mean we can't winkle them out mathematically.

You will make yourself a better investor/speculator/trader by writing code for Support and Resistance levels. However software designed to recognize them exists. I subscribe to three services, one reliant on code and two reliant on humans.
They are very helpful to my investment style and should you be interested more details can be provided.

| Link | Share

Please subscribe to submit a comment

Stock Picking Tutorial Centre

Let’s get you setup so you get the most out of our service
Done, Let's add some stocks
Brilliant - You've created a folio! Now let's add some stocks to it.

  • Apple (AAPL)

  • Shell (RDSA)

  • Twitter (TWTR)

  • Volkswagon AG (VOK)

  • McDonalds (MCD)

  • Vodafone (VOD)

  • Barratt Homes (BDEV)

  • Microsoft (MSFT)

  • Tesco (TSCO)
Save and show me my analysis