how to calculate trendline for stock charts
I have read the topic: How do I calculate a trendline for a graph?
What I am looking for though is how to find the开发者_如何转开发 line that touches the outer extreme points of a graph. The intended use is calculation of support, resistance lines for stock charts. So it is not a merely simple regression but it should also limit the number of touch points and there should be a way to find the relevant interval.
You could think about using a method that calculates the concave hull of your data. There are probably existing python implementations you could find. This will give you the boundary that encloses your timeseries. If there are outliers in your dataset that you wish to exclude, you could apply some sort of filter or smoothing to your data before you calculate the concave hull. I'm not 100% sure what you mean by "limit the number of touch points" and "find the relevant interval", but hopefully this will get you started.
I would approach it just as in mathematics. First, create the graph list (optional) or only once all the points, once x and y. Then, a function with the appropriate conditions.
For example: it's not really a solution.
import random
class StockCharts():
x_line = []
width = 100
height = 100
def startgen(self):
for y in range(0,self.height):
zeile = []
for x in range(0,self.width):
zeile.append(random.randint(0,100))
self.x_line.append(zeile)
def analyse(self, x, y):
starty = max([0,y-1])
endy = min([y+1,self.height-1])
startx = max([0,x-1])
endx = min([x+1,self.width-1])
num = 0
for sy in range(starty, endy+1):
for sx in range(startx, endx):
pass # her you can write your if-clauses
def showgen(self):
for y in range(0, self.height):
print self.x_line[y]
print
stock = StockCharts()
stock.startgen()
stock.showgen()
精彩评论