Skip to content

Commit

Permalink
Merge branch 'master' into yakir/feat/delete-orders
Browse files Browse the repository at this point in the history
  • Loading branch information
yakir4123 authored Jul 1, 2024
2 parents 8672ba7 + 37ffdfc commit 0567661
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 24 deletions.
18 changes: 0 additions & 18 deletions addNewDriverGuide.md

This file was deleted.

1 change: 1 addition & 0 deletions jesse/indicators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,4 @@
from .waddah_attr_explosion import waddah_attar_explosion
from .stiffness import stiffness
from .ttm_squeeze import ttm_squeeze
from .support_resistance_with_break import support_resistance_with_breaks
117 changes: 117 additions & 0 deletions jesse/indicators/support_resistance_with_break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from collections import namedtuple

import numpy as np
from .ema import ema

SupportResistanceWithBreaks = namedtuple('SupportResistanceWithBreaks', ['support', 'resistance', 'red_break', 'green_break', 'bear_wick', 'bull_wick'])


def support_resistance_with_breaks(candles: np.ndarray, left_bars: int = 15, right_bars: int = 15, vol_threshold: int = 20) -> SupportResistanceWithBreaks:
"""
support_resistance_with_breaks
@author LuxAlgo
credits: https://www.tradingview.com/script/JDFoWQbL-Support-and-Resistance-Levels-with-Breaks-LuxAlgo
:param candles: np.ndarray
:param left_bars: int - default: 15
:param right_bars: int - default: 15
:param vol_threshold: int - default: 20
:return: SupportResistanceWithBreaks(support, resistance, red_break, green_break, bear_wick, bull_wick)
"""
resistance = _resistance(candles[:, 3], left_bars, right_bars)
support = _support(candles[:, 4], left_bars, right_bars)

short = ema(candles[:, 5], 5)
long = ema(candles[:, 5], 10)
osc = 100 * (short - long) / long

last_candles = candles[0]

red_break = True if last_candles[2] < support and not abs(
last_candles[1] - last_candles[2]) < abs(last_candles[1] - last_candles[3]) and osc > vol_threshold else False
green_break = True if last_candles[2] > resistance and abs(
last_candles[1] - last_candles[4]) > abs(last_candles[1] - last_candles[2]) and osc > vol_threshold else False

bull_wick = True if last_candles[2] > resistance and abs(last_candles[1] - last_candles[4]) > abs(last_candles[1] - last_candles[2]) else False
bear_wick = True if last_candles[2] < support and abs(last_candles[1] - last_candles[2]) < abs(last_candles[1] - last_candles[3]) else False

return SupportResistanceWithBreaks(support, resistance, red_break, green_break, bear_wick, bull_wick)


def _resistance(source, left_bars, right_bars):
pivot_highs = [None] * len(source) # Initialize result list with None

for i in range(left_bars, len(source) - right_bars):
is_pivot_high = True

# Check left bars for higher high
for j in range(1, left_bars + 1):
if source[i] <= source[i - j]:
is_pivot_high = False
break

# Check right bars for higher high
if is_pivot_high:
for j in range(1, right_bars + 1):
if source[i] <= source[i + j]:
is_pivot_high = False
break

if is_pivot_high:
is_pivot_high = source[i]

pivot_highs[i] = is_pivot_high

next_valid = None
first_value = None
for i in range(len(pivot_highs)):
if pivot_highs[i] is False:
pivot_highs[i] = next_valid
elif pivot_highs[i] is not None: # Update next_valid if it's not False or None
next_valid = pivot_highs[i]
first_value = i if first_value is None else first_value

pivot_highs[:first_value - 1] = [pivot_highs[first_value]] * len(pivot_highs[:first_value - 1])
pivot_highs[-right_bars:] = [pivot_highs[-right_bars - 1]] * len(pivot_highs[-right_bars:])

return pivot_highs[-1]


def _support(source, left_bars, right_bars):
pivot_lows = [None] * len(source) # Initialize result list with None

for i in range(left_bars, len(source) - right_bars):
is_pivot_low = True

# Check left bars for lower low
for j in range(1, left_bars + 1):
if source[i] >= source[i - j]:
is_pivot_low = False
break

# Check right bars for lower low
if is_pivot_low:
for j in range(1, right_bars + 1):
if source[i] >= source[i + j]:
is_pivot_low = False
break

if is_pivot_low:
is_pivot_low = source[i]

pivot_lows[i] = is_pivot_low

next_valid = None
first_value = None
for i in range(len(pivot_lows)):
if pivot_lows[i] is False:
pivot_lows[i] = next_valid
elif pivot_lows[i] is not None: # Update next_valid if it's not False or None
next_valid = pivot_lows[i]
first_value = i if first_value is None else first_value

pivot_lows[:first_value - 1] = [pivot_lows[first_value]] * len(pivot_lows[:first_value - 1])
pivot_lows[-right_bars:] = [pivot_lows[-right_bars - 1]] * len(pivot_lows[-right_bars:])

return pivot_lows[-1]
1 change: 0 additions & 1 deletion jesse/modes/optimize_mode/Optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import click
import numpy as np
import pydash
from numpy import ndarray
from pandas import json_normalize
from timeloop import Timeloop

Expand Down
4 changes: 2 additions & 2 deletions jesse/modes/optimize_mode/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_fitness(
candles=training_candles,
warmup_candles=training_warmup_candles,
hyperparameters=hp,
fast_mode=True
fast_mode=False
)['metrics']
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand Down Expand Up @@ -121,7 +121,7 @@ def get_fitness(
candles=testing_candles,
warmup_candles=testing_warmup_candles,
hyperparameters=hp,
fast_mode=True
fast_mode=False
)['metrics']

# log for debugging/monitoring
Expand Down
2 changes: 1 addition & 1 deletion jesse/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.48.5'
__version__ = '0.48.6'
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ quantstats==0.0.47
requests==2.32.0
scipy
statsmodels==0.14.0
TA-Lib==0.4.28
TA-Lib==0.4.31
tabulate==0.8.9
timeloop==1.0.2
websocket-client==1.8.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages

# also change in version.py
VERSION = '0.48.5'
VERSION = '0.48.6'
DESCRIPTION = "A trading framework for cryptocurrencies"
with open("requirements.txt", "r", encoding="utf-8") as f:
REQUIRED_PACKAGES = f.read().splitlines()
Expand Down
12 changes: 12 additions & 0 deletions tests/test_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2358,3 +2358,15 @@ def test_ttm_squeeze():
result = ta.ttm_squeeze(candles)

assert result == True


def test_support_resistance_with_breaks():
candles = np.array(test_candles_19)
result = ta.support_resistance_with_breaks(candles)

assert result.support == 116.26
assert result.resistance == 288.41
assert result.red_break == False
assert result.green_break == False
assert result.bear_wick == False
assert result.bull_wick == False

0 comments on commit 0567661

Please sign in to comment.