Browse Source

EMA still not working

thomas
thomas auguste 7 months ago
parent
commit
af0998ae6c
  1. 10
      auto_trading/indicators/EMA.py
  2. 2
      auto_trading/indicators/price_v.py
  3. 2
      main.py

10
auto_trading/indicators/EMA.py

@ -13,12 +13,14 @@ class EMA(Indicator):
back_time = 0
previousEMA = 0
multiplier = 0.0
first = True
def __init__(self, back_time : int):
"""back_time represents the number of last prices you consider for the computation"""
super().__init__()
self.back_time = back_time
self.multiplier = 2 / (self.back_time + 1)
self.first = True
def __call__(self, data: pd.DataFrame) -> pd.Series:
"""Return a dataframe of valuation of each stock from the input data.
@ -36,11 +38,17 @@ class EMA(Indicator):
price = data.loc[data.index[0][0]].close
ret = (price < self.previousEMA).astype(float) * 2 - 1
print(price)
print(self.previousEMA)
return ret
def computeEMA(self, data: pd.DataFrame) -> pd.Series :
price = data.loc[data.index[0][0]].close
newEMA = self.previousEMA + self.multiplier * (price - self.previousEMA)
if(self.first) :
self.first = False
newEMA = price * self.multiplier
else :
newEMA = self.previousEMA + self.multiplier * (price - self.previousEMA)
return newEMA

2
auto_trading/indicators/price_v.py

@ -33,5 +33,5 @@ class Follow_Trend(Indicator):
ret = (diff > 0).astype(float) * 2 - 1
else:
ret = (diff < 0).astype(float) * 2 - 1
print(type(ret))
#print(type(ret))
return ret

2
main.py

@ -21,7 +21,7 @@ if __name__ == "__main__":
bt = Backtest("./data/NYSE_smallest.csv")
trend = Follow_Trend(1)
ema = EMA(10)
ema = EMA(4)
ptf = InMemoryPortfolio(
base_balance=100, change_rate_getter=lambda: bt.current_change
)

Loading…
Cancel
Save