Utilize RSI and SMA indicators provided by indicators module in TPS strategy
This commit is contained in:
parent
a879c0cf3e
commit
c777259d71
@ -1,35 +1,15 @@
|
|||||||
import numpy as np
|
from numpy import where
|
||||||
|
|
||||||
from pandas import DataFrame, Series
|
from pandas import DataFrame, Series
|
||||||
|
|
||||||
def calculate_moving_average(data: DataFrame, window: int = 200) -> Series:
|
from indicators import rsi, sma
|
||||||
"""
|
|
||||||
Calculate the 200-period moving average and return it as a Series without modifying the original DataFrame.
|
|
||||||
"""
|
|
||||||
return data['Close'].rolling(window = window).mean()
|
|
||||||
|
|
||||||
def calculate_rsi(data: DataFrame, period: int = 2) -> Series:
|
|
||||||
"""
|
|
||||||
Calculate the 2-period RSI and return it as a Series without modifying the original DataFrame.
|
|
||||||
"""
|
|
||||||
delta = data['Close'].diff()
|
|
||||||
gain = np.where(delta > 0, delta, 0)
|
|
||||||
loss = np.where(delta < 0, -delta, 0)
|
|
||||||
|
|
||||||
alpha = 1 / period
|
|
||||||
avg_gain = Series(gain).ewm(alpha = alpha, adjust = False).mean()
|
|
||||||
avg_loss = Series(loss).ewm(alpha = alpha, adjust = False).mean()
|
|
||||||
|
|
||||||
rs = avg_gain / avg_loss
|
|
||||||
return 100 - (100 / (1 + rs))
|
|
||||||
|
|
||||||
def tps(data: DataFrame) -> Series:
|
def tps(data: DataFrame) -> Series:
|
||||||
"""
|
"""
|
||||||
Calculate signals based on the Time, Price, Scale-in (TPS) strategy.
|
Calculate signals based on the Time, Price, Scale-in (TPS) strategy.
|
||||||
Returns a Series with 'Long' for signals and 'None' otherwise, without modifying the original DataFrame.
|
Returns a Series with 'Long' for signals and 'None' otherwise, without modifying the original DataFrame.
|
||||||
"""
|
"""
|
||||||
ma_200 = calculate_moving_average(data)
|
ma_200 = sma(data, period = 200)
|
||||||
rsi_2 = calculate_rsi(data)
|
rsi_2 = rsi(data, period = 2)
|
||||||
|
|
||||||
above_ma_200 = data['Close'] > ma_200
|
above_ma_200 = data['Close'] > ma_200
|
||||||
|
|
||||||
@ -37,4 +17,4 @@ def tps(data: DataFrame) -> Series:
|
|||||||
rsi_below_25_for_two_days = rsi_below_25 & rsi_below_25.shift(1, fill_value = False)
|
rsi_below_25_for_two_days = rsi_below_25 & rsi_below_25.shift(1, fill_value = False)
|
||||||
|
|
||||||
conditions = above_ma_200 & rsi_below_25_for_two_days
|
conditions = above_ma_200 & rsi_below_25_for_two_days
|
||||||
return Series(np.where(conditions, 'L', 'N'), index = data.index)
|
return Series(where(conditions, 'L', 'N'), index = data.index)
|
Loading…
Reference in New Issue
Block a user