Automated Price Trend Lines in Python | Algorithmic Trading Indicator

neurotrader · Intermediate ·📐 ML Fundamentals ·3y ago

About this lesson

Presenting a unique algorithm implemented in python for finding trend lines on price data. The python code shown builds a powerful trading indicator the slope of the current trend lines. I've used this indicator in the past as an input to predictive models. The algorithm could also be used as a component for recognizing chart patterns such as wedges, triangles, pennants and so on. The automated support and resistance trend lines are found using a gradient descent approach. It creates a price channel on any length of data. It has only one adjustable parameter, the number of candles in which the trend lines are fit. Full Code: https://github.com/neurotrader888/TrendLineAutomation The content covered on this channel is NOT to be considered as any financial or investment advice. Past results are not necessarily indicative of future results. This content is purely for education/entertainment.

Full Transcript

in this video I'll show you how to automatically identify trend lines on price data with python it uses a unique algorithm I created to optimally fit trend lines that I have not seen anywhere else I'll first explain how the algorithm works then show the python code so you can try for yourself the visualization you're currently seeing is the upper and lower trend lines fit to the most recent 24 candles of data the algorithm has only a single parameter the number of candles to fit a trend line on I'll be publishing some videos showing trading strategies that use this algorithm in the future so subscribe to not miss those there are two versions of this algorithm will show they're nearly identical one fits the trend lines with respect to the high and low prices of the candles shown in white the other if it's the trend lines using just the closing price shown in blue let's start with the closing price version here is a section of closing price data with 72 samples we'll start with the lower trend line the first step is to find the line of best fit this is your standard least squared error line the next step is to find the data point that is the furthest below the line in this case it's here marked with a red X I'll call this the pivot point now we can adjust the best fit lines intercept so that the line goes through the Pivot Point from here we use a gradient descent to find the optimal slope of all lines going through the pivot point to find the line that has the minimum squared distance from the data while also being below every point in the data the same process is used to find the upper shred line we find the pivot point that is the highest above the line of best fit then find the line going through the pivot point that is the closest to the data while also being above each point let's look at the python code that implements this algorithm the full code is available on GitHub and is linked in the description we'll start with fitting trend lines on the closing price this is implemented in the function fit trendline single it takes a single array as input the trend lines are fit to the data in this array first we get the slope and intercept of the line of best fit with numpy's polyfit function we use them to get the value of the line at each point in the array we find the pivot Points for both trend lines using numpy's ARG Max and argument main functions which return the index of the maximum and minimum values in an array respectively the main work of the algorithm is done in the optimize slope function but first let's look at the check trendline function this is used by the optimizing function this function assesses a potential trend line we pass a Boolean specifying if it is the upper or lower trend line I use the support and resistance terminology for this it also takes the Pivot Point index the slope being tested and the data we first find The Intercept of the line passing through the Pivot Point with the given slope we subtract the data from the line then test to ensure that it is less than or greater than all the data points depending on what type of trend line it is I used a small value instead of 0 for this check this is because floating Point error occasionally causes issues if the trend line is invalid the function returns a negative value as a flag otherwise we return the sum of squared differences this is what we will be minimizing now to the optimized slope function which minimizes the sum of squared differences with radiant descent we passed the type of trend line the pivot point and the initial slope value for the optimization which is the slope value of the line of best fit which we already found first we set the slope unit which is an ad hoc amount based on the data to change the slope by this is multiplied by the current step variable the current step variable will be decreased as the optimization converges we assess the initial slope with our check trendline function and begin the optimization Loop it runs until the step size has reached the minimum value to find the derivative I use numerical differentiation we increased the slope by a very small amount and see if the error went up or down this gives us the direction to change the slope if increasing the slope increase the error we make a new slope by decreasing the current best slope by the current step multiplied by the slope unit and vice versa we test our new slope value if the new slope value created either an invalid trend line or had a larger distance from the data we reduce our step size and move to the next iteration if the new slope was an improvement we record it and reassess our derivative on the next iteration and that's it we return the slope and intercept of the optimized trend line to fit trend lines on high and low data we need to take in the high low and close we find the line of best fit using the close and find our upper and lower pivots using the high and low prices time then when we call optimize slope we pass in the low price and high price for each trend line and I'll show a quick example of how you can use this on price data we load some data from a csb this is daily Bitcoin data I take the logarithm of the prices to have normalized scaling we set a look back I chose 30 days we Loop through each candle in the data set and pass our 30 most recent candles into our fit trend lines function we record the upper and lower trend line slope at each candle again use the support and resistance terminology this creates a pretty cool indicator now we can see how the trendline slopes change throughout time the algorithm presented offers a unique approach for automating trend lines its main benefit is it has only one parameter and can work on any size and section of input data its main drawback is that it is computationally expensive if you download the code for yourself you will notice it takes a decent amount of time to compute every candles trend lines even on daily data I originally implemented this algorithm in C plus and re-implemented it in Python for this video C plus plus is probably more appropriate for this algorithm if you were to use the python implementation on a lower time frame such as hourly or 15 minute data it might take too long and you probably will get bored anyways I'll be showing off applications and trading strategies using this algorithm in the future so subscribe to see that thanks for watching bye

Original Description

Presenting a unique algorithm implemented in python for finding trend lines on price data. The python code shown builds a powerful trading indicator the slope of the current trend lines. I've used this indicator in the past as an input to predictive models. The algorithm could also be used as a component for recognizing chart patterns such as wedges, triangles, pennants and so on. The automated support and resistance trend lines are found using a gradient descent approach. It creates a price channel on any length of data. It has only one adjustable parameter, the number of candles in which the trend lines are fit. Full Code: https://github.com/neurotrader888/TrendLineAutomation The content covered on this channel is NOT to be considered as any financial or investment advice. Past results are not necessarily indicative of future results. This content is purely for education/entertainment.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

Up next
Dropout in Deep Learning
AnuTech-CH
Watch →