Facebook
From Lousy Armadillo, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 71
  1. from sklearn.linear_model import LinearRegression
  2.  
  3. def SQZMIval(data, depth = 20):
  4.     new = []
  5.     high = data.High
  6.     low = data.Low
  7.     close = data.Close
  8.  
  9.     for x in range(1, len(data)):
  10.         if abs(-x-depth) < len(data):
  11.             a = max(data.iloc[-x-depth:-x]['High'])
  12.             b = min(data.iloc[-x-depth:-x]['Low'])
  13.             average1 = (a + b)/2
  14.  
  15.             c = sum(data.iloc[-x-depth:-x]['Close'])/depth
  16.  
  17.             average2 = (average1 + c)/2
  18.  
  19.             final = data.iloc[-x]['Close'] - average2
  20.  
  21.             new.insert(0, final)
  22.         else:
  23.             new.insert(0, 0)
  24.     new.insert(0,0)
  25.  
  26.     data['Average1'] = pd.Series(new)
  27.  
  28.     z = []
  29.     for y in range(1,(depth+1)):
  30.         z.append(y)
  31.  
  32.     for x in range(1, len(data)):
  33.         if abs(-x-depth) < len(data):
  34.             final = data.iloc[-x-depth:-x]['Average1']
  35.  
  36.             X = np.array(z).reshape(-1, 1)
  37.             Y = final.values
  38.             linear_regressor = LinearRegression()
  39.             linear_regressor.fit(X, Y)
  40.             Y_pred = linear_regressor.predict(X)
  41.  
  42.             new.insert(0, round(Y_pred[-1],2))
  43.         else:
  44.             new.insert(0, 0)
  45.     new.insert(0,0)
  46.  
  47.  
  48.     return pd.Series(new)