Skip to content

Commit 9d63c6a

Browse files
authored
V0.0.17.1 more indicators (#26)
* update pre-commit * srsi stoch tsi uo wo ao kama roc: indicators
1 parent e5a3592 commit 9d63c6a

File tree

3 files changed

+378
-13
lines changed

3 files changed

+378
-13
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
33
# Ruff version.
4-
rev: v0.5.2
4+
rev: v0.5.7
55
hooks:
66
# Run the linter.
77
- id: ruff

src/fmp_py/fmp_chart_data.py

Lines changed: 318 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
)
2424
from ta.momentum import (
2525
RSIIndicator,
26-
# StochRSIIndicator,
27-
# StochasticOscillator,
28-
# TSIIndicator,
29-
# UltimateOscillator,
30-
# WilliamsRIndicator,
31-
# AwesomeOscillatorIndicator,
32-
# KAMAIndicator,
33-
# ROCIndicator,
26+
StochRSIIndicator,
27+
StochasticOscillator,
28+
TSIIndicator,
29+
UltimateOscillator,
30+
WilliamsRIndicator,
31+
AwesomeOscillatorIndicator,
32+
KAMAIndicator,
33+
ROCIndicator,
3434
# PercentagePriceOscillator,
3535
# PercentageVolumeOscillator,
3636
)
@@ -1131,13 +1131,319 @@ def rsi(self, period: int = 14) -> None:
11311131
>>> fmp.rsi(14)
11321132
>>> print(fmp.return_chart())
11331133
"""
1134-
change = self.chart.copy()
1135-
change[f"rsi{period}"] = (
1136-
RSIIndicator(close=change["close"], window=period, fillna=True)
1134+
chart = self.chart.copy()
1135+
chart[f"rsi{period}"] = (
1136+
RSIIndicator(close=chart["close"], window=period, fillna=True)
11371137
.rsi()
11381138
.round(2)
11391139
).astype(float)
1140-
self.chart = change
1140+
self.chart = chart
1141+
1142+
#####################################
1143+
# Stochastic RSI
1144+
#####################################
1145+
def srsi(self, period: int = 14, smooth1: int = 3, smooth2: int = 3) -> None:
1146+
"""
1147+
Calculates the Stochastic RSI (Relative Strength Index) for the given period.
1148+
Parameters:
1149+
period (int): The number of periods to consider for the calculation. Default is 14.
1150+
smooth1 (int): The number of periods to use for smoothing the Stochastic RSI. Default is 3.
1151+
smooth2 (int): The number of periods to use for smoothing the Stochastic RSI's signal line. Default is 3.
1152+
Returns:
1153+
None
1154+
"""
1155+
1156+
chart = self.chart.copy()
1157+
chart[f"srsi{period}"] = (
1158+
StochRSIIndicator(
1159+
close=chart["close"],
1160+
window=period,
1161+
smooth1=smooth1,
1162+
smooth2=smooth2,
1163+
fillna=True,
1164+
)
1165+
.stochrsi()
1166+
.round(2)
1167+
).astype(float)
1168+
1169+
chart[f"srsi{period}_d"] = (
1170+
StochRSIIndicator(
1171+
close=chart["close"],
1172+
window=period,
1173+
smooth1=smooth1,
1174+
smooth2=smooth2,
1175+
fillna=True,
1176+
)
1177+
.stochrsi_d()
1178+
.round(2)
1179+
).astype(float)
1180+
1181+
chart[f"srsi{period}_k"] = (
1182+
StochRSIIndicator(
1183+
close=chart["close"],
1184+
window=period,
1185+
smooth1=smooth1,
1186+
smooth2=smooth2,
1187+
fillna=True,
1188+
)
1189+
.stochrsi_k()
1190+
.round(2)
1191+
).astype(float)
1192+
1193+
self.chart = chart
1194+
1195+
#####################################
1196+
# Stochastic Oscillator
1197+
#####################################
1198+
def stoch(self, period: int = 14, smooth: int = 3) -> None:
1199+
"""
1200+
Calculates the Stochastic Oscillator for the given period.
1201+
1202+
Parameters:
1203+
period (int): The number of periods to consider for the calculation. Default is 14.
1204+
smooth (int): The number of periods to use for smoothing the Stochastic Oscillator. Default is 3.
1205+
1206+
Returns:
1207+
None
1208+
1209+
Example:
1210+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1211+
>>> fmp.stoch(14, 3)
1212+
>>> print(fmp.return_chart())
1213+
"""
1214+
chart = self.chart.copy()
1215+
chart[f"stoch{period}"] = (
1216+
StochasticOscillator(
1217+
high=chart["high"],
1218+
low=chart["low"],
1219+
close=chart["close"],
1220+
window=period,
1221+
smooth_window=smooth,
1222+
fillna=True,
1223+
)
1224+
.stoch()
1225+
.round(2)
1226+
).astype(float)
1227+
1228+
chart[f"stoch{period}_sig"] = (
1229+
StochasticOscillator(
1230+
high=chart["high"],
1231+
low=chart["low"],
1232+
close=chart["close"],
1233+
window=period,
1234+
smooth_window=smooth,
1235+
fillna=True,
1236+
)
1237+
.stoch_signal()
1238+
.round(2)
1239+
).astype(float)
1240+
1241+
self.chart = chart
1242+
1243+
#####################################
1244+
# True Strength Index
1245+
#####################################
1246+
def tsi(self, period_slow: int = 25, period_fast: int = 13) -> None:
1247+
"""
1248+
Calculates the True Strength Index (TSI) for the given chart data.
1249+
Parameters:
1250+
period_slow (int): The number of periods to use for the slow TSI calculation. Default is 25.
1251+
period_fast (int): The number of periods to use for the fast TSI calculation. Default is 13.
1252+
Returns:
1253+
None
1254+
1255+
Example:
1256+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1257+
>>> fmp.tsi(25, 13)
1258+
>>> print(fmp.return_chart())
1259+
"""
1260+
chart = self.chart.copy()
1261+
chart["tsi"] = (
1262+
TSIIndicator(
1263+
close=chart["close"],
1264+
window_slow=period_slow,
1265+
window_fast=period_fast,
1266+
fillna=True,
1267+
)
1268+
.tsi()
1269+
.round(2)
1270+
).astype(float)
1271+
1272+
self.chart = chart
1273+
1274+
#####################################
1275+
# Ultimate Oscillator
1276+
#####################################
1277+
def uo(
1278+
self,
1279+
period1: int = 7,
1280+
period2: int = 14,
1281+
period3: int = 28,
1282+
weight1: float = 4.0,
1283+
weight2: float = 2.0,
1284+
weight3: float = 1.0,
1285+
) -> None:
1286+
"""
1287+
Calculate the Ultimate Oscillator (UO) for the given chart data.
1288+
Parameters:
1289+
period1 (int): The number of periods to use for the first UO calculation. Default is 7.
1290+
period2 (int): The number of periods to use for the second UO calculation. Default is 14.
1291+
period3 (int): The number of periods to use for the third UO calculation. Default is 28.
1292+
weight1 (float): The weight to apply to the first UO calculation. Default is 4.0.
1293+
weight2 (float): The weight to apply to the second UO calculation. Default is 2.0.
1294+
weight3 (float): The weight to apply to the third UO calculation. Default is 1.0.
1295+
Returns:
1296+
None
1297+
1298+
Example:
1299+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1300+
>>> fmp.uo(7, 14, 28, 4.0, 2.0, 1.0)
1301+
>>> print(fmp.return
1302+
"""
1303+
chart = self.chart.copy()
1304+
chart["uo"] = (
1305+
UltimateOscillator(
1306+
high=chart["high"],
1307+
low=chart["low"],
1308+
close=chart["close"],
1309+
window1=period1,
1310+
window2=period2,
1311+
window3=period3,
1312+
weight1=weight1,
1313+
weight2=weight2,
1314+
weight3=weight3,
1315+
fillna=True,
1316+
)
1317+
.ultimate_oscillator()
1318+
.round(2)
1319+
).astype(float)
1320+
1321+
self.chart = chart
1322+
1323+
#####################################
1324+
# Williams %R
1325+
#####################################
1326+
def wr(self, period: int = 14) -> None:
1327+
"""
1328+
Calculate the Williams %R for the given chart data.
1329+
Parameters:
1330+
period (int): The number of periods to consider for the calculation. Default is 14.
1331+
Returns:
1332+
None
1333+
1334+
Example:
1335+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1336+
>>> fmp.wr(14)
1337+
>>> print(fmp.return_chart())
1338+
"""
1339+
chart = self.chart.copy()
1340+
chart[f"wr{period}"] = (
1341+
WilliamsRIndicator(
1342+
high=chart["high"],
1343+
low=chart["low"],
1344+
close=chart["close"],
1345+
lbp=period,
1346+
fillna=True,
1347+
)
1348+
.williams_r()
1349+
.round(2)
1350+
).astype(float)
1351+
1352+
self.chart = chart
1353+
1354+
###############################
1355+
# Awesome Oscillator
1356+
###############################
1357+
def ao(self, period1: int = 5, period2: int = 34) -> None:
1358+
"""
1359+
Calculate the Awesome Oscillator (AO) for the given chart data.
1360+
Parameters:
1361+
period1 (int): The number of periods to use for the first AO calculation. Default is 5.
1362+
period2 (int): The number of periods to use for the second AO calculation. Default is 34.
1363+
Returns:
1364+
None
1365+
1366+
Example:
1367+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1368+
>>> fmp.ao(5, 34)
1369+
>>> print(fmp.return_chart())
1370+
"""
1371+
chart = self.chart.copy()
1372+
chart["ao"] = (
1373+
AwesomeOscillatorIndicator(
1374+
high=chart["high"],
1375+
low=chart["low"],
1376+
window1=period1,
1377+
window2=period2,
1378+
fillna=True,
1379+
)
1380+
.awesome_oscillator()
1381+
.round(2)
1382+
).astype(float)
1383+
1384+
self.chart = chart
1385+
1386+
####################################
1387+
# Kaufman's Adaptive Moving Average
1388+
####################################
1389+
def kama(self, period: int = 10, pow1: int = 2, pow2: int = 30) -> None:
1390+
"""
1391+
Calculate the Kaufman's Adaptive Moving Average (KAMA) for the given chart data.
1392+
Parameters:
1393+
period (int): The number of periods to consider for the calculation. Default is 10.
1394+
pow1 (int): The number of periods to consider for the first power factor. Default is 2.
1395+
pow2 (int): The number of periods to consider for the second power factor. Default is 30.
1396+
Returns:
1397+
None
1398+
1399+
Example:
1400+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1401+
>>> fmp.kama(10, 2, 30)
1402+
>>> print(fmp.return_chart
1403+
"""
1404+
chart = self.chart.copy()
1405+
chart[f"kama{period}"] = (
1406+
KAMAIndicator(
1407+
close=chart["close"],
1408+
window=period,
1409+
pow1=pow1,
1410+
pow2=pow2,
1411+
fillna=True,
1412+
)
1413+
.kama()
1414+
.round(2)
1415+
).astype(float)
1416+
1417+
self.chart = chart
1418+
1419+
#####################################
1420+
# Rate of Change (ROC)
1421+
#####################################
1422+
def roc(self, period: int = 12) -> None:
1423+
"""
1424+
Calculate the Rate of Change (ROC) for the given chart data.
1425+
Parameters:
1426+
period (int): The number of periods to consider for the calculation. Default is 12.
1427+
Returns:
1428+
None
1429+
1430+
Example:
1431+
>>> fmp = FmpCharts(symbol="AAPL", from_date="2021-01-01", to_date="2021-01-10")
1432+
>>> fmp.roc(12)
1433+
>>> print(fmp.return_chart())
1434+
"""
1435+
chart = self.chart.copy()
1436+
chart[f"roc{period}"] = (
1437+
ROCIndicator(
1438+
close=chart["close"],
1439+
window=period,
1440+
fillna=True,
1441+
)
1442+
.roc()
1443+
.round(2)
1444+
).astype(float)
1445+
1446+
self.chart = chart
11411447

11421448
def return_chart(self) -> pd.DataFrame:
11431449
return self.chart

0 commit comments

Comments
 (0)