-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCI.py
38 lines (26 loc) · 986 Bytes
/
CCI.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
# import required libraries
import os
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
warnings.filterwarnings('ignore')
# Function to calculate CCI
def calculate_cci(data, period):
# calculate the typical price
data['Typical Price'] = (data['High'] + data['Low'] + data['Close']) / 3
# calculate the simple moving average (SMA) of the Typical Price
sma = data['Typical Price'].rolling(window=period).mean()
# Calculate the mean deviation manually
mean_deviation = data['Typical Price'].rolling(window=period).apply(
lambda x: (np.abs(x - x.mean()).mean()), raw=True
)
# calculate the CCI
cci = (data['Typical Price'] - sma) / (0.015 * mean_deviation)
return cci
# load data set
hourly_data = pd.read_csv("data/gold_hourly_data.csv")
# calculate CCI
hourly_data['CCI3'] = calculate_cci(hourly_data, 3)
hourly_data['CCI9'] = calculate_cci(hourly_data, 9)
print(hourly_data.tail(5))