-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_functions.py
executable file
·52 lines (38 loc) · 1.34 KB
/
feature_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pandas as pd
import numpy as np
from scipy import stats
import scipy.optimize
from scipy.optimize import OptimizeWarning
import warnings
import math
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from matplotlib.finance import _candlestick
from matplotlib.dates import date2num
from datetime import datetime
class holder:
# Heiken Ashi Candles
def heikenashi(prices.periods):
"""
:param prices: dataframe of OHLC & volume data
:param periods: periods for which to create the candles
:return: heiken ashi OHLC candles
"""
results = holder()
# store candles in a dataframe in a dict
dict = {}
HAclose = prices['open', 'high', 'low', 'close'].sum(axis=1)/4
HAopen = HAclose.copy()
HAopen.iloc[0] = HAclose.iloc[0]
HAhigh = HAclose.copy()
HAlow = HAclose.copy()
for i in range(1,len(prices)):
HAopen.iloc[i] = (HAopen.iloc[i-1]+HAclose.iloc[i-1])/2
HAhigh.iloc[i] = np.array([prices.high.iloc[i],HAopen.iloc[i],HAclose.iloc[i]]).max()
HAlow.iloc[i] = np.array([prices.low.iloc[i], HAopen.iloc[i], HAclose.iloc[i]]).min()
df = pd.concat((HAopen,HAhigh,HAlow,HAclose),axis=1)
df.columns = ['open', 'high', 'low', 'close']
#df.index = df.index.droplevel(0)
dict[periods[0]] = df
results.candles = dict
return results