Skip to content

Commit 440db21

Browse files
update vwapbounce_signal
1 parent 2dbd7b9 commit 440db21

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

stratback/utils/TALibrary.py

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,9 @@ def vwapbounce_signal(
653653
long_only=True,
654654
short_only=False,
655655
shift=True,
656-
restrict_entry_zone=True,
657-
filter_by_secondary_timeframe=True,
656+
restrict_entry_zone=False,
657+
filter_by_secondary_timeframe=False,
658+
consider_wicks=False,
658659
):
659660
data = data.copy()
660661

@@ -667,37 +668,59 @@ def vwapbounce_signal(
667668
rsi_up = rsi.gt(rsi.shift())
668669
use_rsi_cond = {True: (rsi_up, ~rsi_up), False: (True, True)}
669670

671+
def vwap_crossabove(data, vwap_line, tf, consider_wicks=False):
672+
if consider_wicks:
673+
crossabove = (data.Close.gt(vwap_line) & data.Open.lt(vwap_line)) | (
674+
data.Open.gt(vwap_line)
675+
& data.Close.gt(vwap_line)
676+
& data.Low.lt(vwap_line)
677+
)
678+
else:
679+
crossabove = data.Close.gt(vwap_line) & data.Open.lt(vwap_line)
680+
crossabove = crossabove.groupby(crossabove.index.to_period(tf)).cumsum()
681+
return crossabove
682+
683+
def vwap_crossbelow(data, vwap_line, tf, consider_wicks=False):
684+
if consider_wicks:
685+
crossbelow = (data.Close.lt(vwap_line) & data.Open.gt(vwap_line)) | (
686+
data.Open.lt(vwap_line)
687+
& data.Close.lt(vwap_line)
688+
& data.High.gt(vwap_line)
689+
)
690+
else:
691+
crossbelow = data.Close.lt(vwap_line) & data.Open.gt(vwap_line)
692+
crossbelow = crossbelow.groupby(crossbelow.index.to_period(tf)).cumsum()
693+
return crossbelow
694+
670695
avwap_htf1 = calc_vwap(data, HTF1)
671-
vwap_crossabove_htf1 = data.close.gt(avwap_htf1) & data.open.lt(avwap_htf1)
672-
vwap_crossabove_htf1 = vwap_crossabove_htf1.groupby(
673-
vwap_crossabove_htf1.index.to_period(HTF1)
674-
).cumsum()
675-
vwap_crossbelow_htf1 = data.close.lt(avwap_htf1) & data.open.gt(avwap_htf1)
676-
vwap_crossbelow_htf1 = vwap_crossbelow_htf1.groupby(
677-
vwap_crossbelow_htf1.index.to_period(HTF1)
678-
).cumsum()
696+
vwap_crossabove_htf1 = vwap_crossabove(
697+
data, avwap_htf1, HTF1, consider_wicks=consider_wicks
698+
)
699+
700+
vwap_crossbelow_htf1 = vwap_crossbelow(
701+
data, avwap_htf1, HTF1, consider_wicks=consider_wicks
702+
)
679703

680704
avwap_htf2 = calc_vwap(data, HTF2)
681-
vwap_crossabove_htf2 = data.close.gt(avwap_htf2) & data.open.lt(avwap_htf2)
682-
vwap_crossabove_htf2 = vwap_crossabove_htf2.groupby(
683-
vwap_crossabove_htf2.index.to_period(HTF2)
684-
).cumsum()
685-
vwap_crossbelow_htf2 = data.close.lt(avwap_htf2) & data.open.gt(avwap_htf2)
686-
vwap_crossbelow_htf2 = vwap_crossbelow_htf2.groupby(
687-
vwap_crossbelow_htf2.index.to_period(HTF2)
688-
).cumsum()
689705

690706
if price_move_tp is not None:
691-
price_position = price_position_by_pivots(data, pivot_data_shift=pivot_shift)
692-
pexp = price_position.groupby(price_position.index.to_period("D")).expanding()
707+
price_position = price_position_by_pivots(
708+
data, pivot_data_shift=pivot_shift
709+
)
710+
pexp = price_position.groupby(
711+
price_position.index.to_period(HTF1)
712+
).expanding()
693713
price_move = pexp.apply(lambda x: x[-1]) - pexp.apply(lambda x: x[0])
694714
price_move = price_move.droplevel(0)
695715

696716
entry_hr_left = int(eval(entry_zone)[0].split(":")[0])
697717
entry_min_left = int(eval(entry_zone)[0].split(":")[1])
698718
entry_hr_right = int(eval(entry_zone)[1].split(":")[0])
699719
entry_min_right = int(eval(entry_zone)[1].split(":")[1])
700-
longCondition = (vwap_crossabove_htf1.eq(ntouch)) & use_rsi_cond[use_rsi][0]
720+
721+
longCondition = (vwap_crossabove_htf1.eq(ntouch)) & use_rsi_cond[
722+
use_rsi
723+
][0]
701724
if filter_by_secondary_timeframe:
702725
longCondition = longCondition & avwap_htf1.gt(avwap_htf2)
703726

@@ -712,7 +735,9 @@ def vwapbounce_signal(
712735
index=data.index,
713736
)
714737

715-
shortCondition = (vwap_crossbelow_htf1.eq(ntouch)) & use_rsi_cond[use_rsi][1]
738+
shortCondition = (vwap_crossbelow_htf1.eq(ntouch)) & use_rsi_cond[
739+
use_rsi
740+
][1]
716741
if filter_by_secondary_timeframe:
717742
shortCondition = shortCondition & avwap_htf1.lt(avwap_htf2)
718743
if restrict_entry_zone:
@@ -746,12 +771,14 @@ def vwapbounce_signal(
746771
short = in_session & shortCondition & (not long_only)
747772

748773
longX = (
749-
price_move.eq(price_move_tp) & price_move.shift().ne(price_move_tp)
774+
price_move.eq(price_move_tp)
775+
& price_move.shift().ne(price_move_tp)
750776
if price_move_tp is not None
751777
else pd.Series([False] * len(data), index=data.index)
752778
)
753779
shortX = (
754-
price_move.eq(-price_move_tp) & price_move.shift().ne(-price_move_tp)
780+
price_move.eq(-price_move_tp)
781+
& price_move.shift().ne(-price_move_tp)
755782
if price_move_tp is not None
756783
else pd.Series([False] * len(data), index=data.index)
757784
)

0 commit comments

Comments
 (0)