Skip to content

Commit

Permalink
replace rsi cross alert with zigzag break of structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wahyubiman committed Jul 2, 2022
1 parent d08a9f8 commit 8dcdeed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Binary file modified app/__pycache__/config.cpython-38.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
RSI = 14
EMA_FAST = 34
EMA_SLOW = 168
ARGREL = 5
Binary file modified app/strategy/__pycache__/indicator_alert.cpython-38.pyc
Binary file not shown.
42 changes: 25 additions & 17 deletions app/strategy/indicator_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pandas_ta as ta
from app.config import *
from app.connector import ExchangeConnector
from scipy.signal import argrelmax, argrelmin


class IndicatorAlert:
Expand All @@ -21,21 +22,29 @@ async def _calculate_indicator(self, ohlcv):
LOG.info(
f"Calculate indicator for {ohlcv.name}")
try:
ohlcv['rsi'] = ta.rsi(ohlcv.close, RSI).round(
decimals=2) # calculate RSI
""" ohlcv['rsi'] = ta.rsi(ohlcv.close, RSI).round(
decimals=2) # calculate RSI """
ohlcv['ema_fast'] = ta.ema(ohlcv.close, EMA_FAST).round(decimals=int(
(len(str(ohlcv.close[-1:].values[0]).split('.')[1])))) # calculate EMA & round result same as price
ohlcv['ema_slow'] = ta.ema(ohlcv.close, EMA_SLOW).round(decimals=int(
""" ohlcv['ema_slow'] = ta.ema(ohlcv.close, EMA_SLOW).round(decimals=int(
(len(str(ohlcv.close[-1:].values[0]).split('.')[1])))) # calculate EMA & round result same as price
ohlcv['ema_trend'] = ohlcv.apply(
lambda x: 'up' if x['ema_fast'] > x['ema_slow'] else 'down', axis=1) # up if ema fast above ema slow
lambda x: 'up' if x['ema_fast'] > x['ema_slow'] else 'down', axis=1) # up if ema fast above ema slow """
ohlcv['timestamp'] = pd.to_datetime(ohlcv['timestamp'], unit='ms').dt.tz_localize(
'UTC').dt.tz_convert('Asia/Jakarta')
ohlcv['rsi_up'] = ta.cross(
""" ohlcv['rsi_up'] = ta.cross(
ohlcv.rsi, self._seri(42.5, ohlcv), asint=False)
ohlcv['rsi_down'] = ta.cross(self._seri(
57.5, ohlcv), ohlcv.rsi, asint=False)
return ohlcv[-2:-1]
57.5, ohlcv), ohlcv.rsi, asint=False) """
ohlcv['ph'] = ohlcv.iloc[argrelmax(
ohlcv.high.values, order=5)].high
ohlcv['pl'] = ohlcv.iloc[argrelmin(
ohlcv.low.values, order=5)].low
ohlcv['ph'] = ohlcv['ph'].fillna(method='ffill')
ohlcv['pl'] = ohlcv['pl'].fillna(method='ffill')
ohlcv['break_h'] = ta.cross(ohlcv.close, ohlcv.ph)
ohlcv['break_l'] = ta.cross(ohlcv.pl, ohlcv.close)
return ohlcv
except Exception as e:
LOG.exception(e)
pass
Expand All @@ -56,23 +65,22 @@ async def _signal(self, ohlcvs):
try:
result = []
for ohlcv in ohlcvs:
if len(ohlcv) >= EMA_SLOW:
last = ohlcv[-2:-1]
# check LONG or SHORT signal
if (last.rsi_up.values[0] and last.ema_trend.values[0] == 'up'):
result.append(
f'{ohlcv.name} - ${ohlcv.close[-1:].values[0]} - {ohlcv.rsi[-1:].values[0]} - LONG')
elif (last.rsi_down.values[0] and last.ema_trend.values[0] == 'down'):
result.append(
f'{ohlcv.name} - ${ohlcv.close[-1:].values[0]} - {ohlcv.rsi[-1:].values[0]} - SHORT')
last = ohlcv[-3:-2]
# check LONG or SHORT signal
if (last.break_h.values[0] == 1) & (last.ema_fast.values[0] < last.close.values[0]):
result.append(
f'{ohlcv.name} - MSB High Break on ${last.close} @{last.timestamp}')
elif (last.break_l.values[0] == 1) & (last.ema_fast.values[0] > last.close.values[0]):
result.append(
f'{ohlcv.name} - MSB Low Break on ${last.close} @{last.timestamp}')
return result
except Exception as e:
LOG.exception(e)

async def _main(self, ohlcvs):
""" """

new_ohlcvs = [await self._calculate_indicator(ohlcv) for ohlcv in ohlcvs if len(ohlcv) > EMA_SLOW]
new_ohlcvs = [await self._calculate_indicator(ohlcv) for ohlcv in ohlcvs if len(ohlcv) > EMA_FAST]
signal = await self._signal(ohlcvs)
return signal

Expand Down

0 comments on commit 8dcdeed

Please sign in to comment.