Advertisement
Guest User

Join dots with lines and intersect

a guest
Feb 21st, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. #LinesToDraw is the pandas series where we have our dots stored.
  2. #Input_High and Input_Low are pandas series where we have our highs and lows of the candlesticks stored.
  3.  
  4.  
  5. #Function to find the value of all the points of a line
  6. def FindYGivenIndex(i,index,Y,subindex,subY):
  7.     return Y+(i-index)*(subY-Y)/(subindex-index)
  8.  
  9. LineDots = list() #I will store a list of lines (with each dot in that line) here.
  10.  
  11. #Now we calculate the possible existing lines, and store each into a series:
  12.    
  13. for index in LinesToDraw.index:
  14.     Y = LinesToDraw.ix[index]
  15.    
  16.     for subindex in LinesToDraw.index:
  17.         subY = LinesToDraw.ix[subindex]
  18.        
  19.         if subindex > index and abs(subindex - index)<30 and abs(subindex - index)>3:
  20.            
  21.             LineDots.append( pd.DataFrame(range(index,subindex),index = range(index,subindex)).apply(FindYGivenIndex,args = (index,Y,subindex,subY,)) )
  22.  
  23. CurrentPlot.LineDots = LineDots
  24. CurrentPlot.LinesToDraw = list()
  25.      
  26. #Now for the resulting lines, we filter the ones that are over or under Input_High or Input_Low
  27.  
  28. for line in CurrentPlot.LineDots:
  29.     if len(line.dropna()):
  30.         try:
  31.             if (Input_High.ix[line.index].astype('float') <= line.iloc[:,0].astype('float')).all() or (Input_Low.ix[line.index].astype('float') >= line.iloc[:,0].astype('float')).all():
  32.                 CurrentPlot.LinesToDraw.append(line)
  33.         except:
  34.             pass
  35. #Now we can draw them, but as said, we iterated too many times, making the process _very_ slow.  
  36.  
  37. for line in CurrentPlot.LinesToDraw:
  38.     plot([CurrentPlot.X_Axis.ix[line.index[0]],CurrentPlot.X_Axis.ix[line.index[-1]]], [line.iloc[0],line.iloc[-1]], 'k-')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement