# Untitled

unknown
plain_text
3 years ago
9.8 kB
5
Indexable
Never
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// la versione precedente senza funzioni e senza moltiplicatore e' la 93

//@version=4
study("The predictive power of price patterns", overlay=true)

_is_downtrend(_sma3) =>
bool downtrend = true
bool one_violation = false
for i=0 to 5
if _sma3[i] >= _sma3[i+1]
if one_violation
downtrend := false
break
one_violation := true
downtrend

_is_uptrend(_sma3) =>
bool uptrend = true
bool one_violation = false
for i=0 to 5
if _sma3[i] <= _sma3[i+1]
if one_violation
uptrend := false
break
one_violation := true
uptrend

_TWS(_is_downtrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_downtrend[i2]
a = (_open < _close) and (_open[i1] < _close[i1]) and (_open[i2] < _close[i2])
b = (_close > _close[i1]) and (_close[i1] > _close[i2])
c = (_close[i2] > _open[i1]) and (_open[i1] > _open[i2]) and (_close[i1] > _open) and (_open > _open[i1])
a and b and c
else
false

_TBC(_is_uptrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_uptrend[i2]
a = (_open > _close) and (_open[i1] > _close[i1]) and (_open[i2] > _close[i2])
b = (_close < _close[i1]) and (_close[i1] < _close[i2])
c = (_open[i2] > _open[i1]) and (_open[i1] > _close[i2]) and (_open[i1] > _open) and (_open > _close[i1])
a and b and c
else
false

_TIU(_is_downtrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_downtrend[i2]
a = _open[i2] > _close[i2]
b = (_open[i2] >= _open[i1]) and (_open[i1] > _close[i2])
c = (_open[i2] > _close[i1]) and (_close[i1] >= _close[i2])
d = if (_open[i2] == _open[i1])
(_close[i1] != _close[i2])
else if (_close[i1] == _close[i2])
(_open[i2] != _open[i1])
else
true
e = (_close > _open) and (_close > _open[i2])
a and b and c and d and e
else
false

_TID(_is_uptrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_uptrend[i2]
a = _close[i2] > _open[i2]
b = (_close[i2] > _open[i1]) and (_open[i1] >= _open[i2])
c = (_close[i2] >= _close[i1]) and (_close[i1] > _open[i2])
d = if (_open[i2] == _open[i1])
(_close[i1] != _close[i2])
else if (_close[i1] == _close[i2])
(_open[i2] != _open[i1])
else
true
e = (_close < _open) and (_close < _open[i2])
a and b and c and d and e
else
false

_TOU(_is_downtrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_downtrend[i2]
a = (_open[i2] > _close[i2])
b = (_close[i1] >= _open[i2]) and (_open[i2] > _close[i2]) and (_close[i2] >= _open[i1])
c = abs(_close[i1]-_open[i1]) > abs(_close[i2]-_open[i2])
d = (_close > _open) and (_close > _close[i1])
a and b and c and d
else
false

_TOD(_is_uptrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_uptrend[i2]
a = _close[i2] > _open[i2]
b = (_open[i1] >= _close[i2]) and (_close[i2] > _open[i2]) and (_open[i2] >= _close[i1])
c = abs(_close[i1]-_open[i1]) > abs(_close[i2]-_open[i2])
d = (_close < _open) and (_close < _close[i1])
a and b and c and d
else
false

_MS(_is_downtrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_downtrend[i2]
a = _open[i2] > _close[i2]
b = abs(_open[i1]-_close[i1]) > 0
c = (_close[i2] > _close[i1]) and (_close[i2] > _open[i1])
d = (_close > _open) and (2*_close > _open[i2]+_close[i2])
a and b and c and d
else
false

_ES(_is_uptrend, _open, _close, _mul) =>
i1 =_mul
i2 = 2*_mul
if _is_uptrend[i2]
a = _open[i2] < _close[i2]
b = abs(_open[i1]-_close[i1]) > 0
c = (_close[i1] > _close[i2]) and (_open[i1] > _close[i2])
d = (_close < _open) and (2*_close < _open[i2]+_close[i2])
a and b and c and d
else
false

risk_perc = input(title="Risk %", type=input.float, defval=1, minval=0.25, maxval=100, step=0.25, group="Account")
compound = input(title="Compound", type=input.bool, defval=false, group="Account")

show_trends = input(title="Show trends", type=input.bool, defval=false, group="Strategy")
check_chop = input(title="Check chop", type=input.bool, defval=true, group="Strategy")
show_all = input(title="Show all", type=input.bool, defval=true, group="Strategy")
multiplier = input(title="Multiplier", type=input.integer, defval=3, minval=1, maxval=60, group="Strategy")
long = input(title="Long (not Short)", type=input.bool, defval=true, group="Strategy")
stop_channel_size = input(title="Stop size", type=input.integer, defval=20, minval=1, maxval=1000)

filter_date_range = input(title="Enable range filter", type=input.bool, defval=false, group="Date Range")
date_from = input(title="From", type=input.time, defval=timestamp("20 Feb 2020 00:00 -0400"), group="Date Range")
date_to = input(title="To", type=input.time, defval=timestamp("20 Feb 2020 00:00 -0400"), group="Date Range")
window() => iff(filter_date_range, (time >= date_from) and (time <= date_to), true)

sma3 = sma(close,3*multiplier)
is_downtrend = _is_downtrend(sma3)
is_uptrend = _is_uptrend(sma3)
last=show_all? na:2*stop_channel_size

plot(is_downtrend and show_trends? sma3:na, style=plot.style_linebr, color=color.red, linewidth=1, show_last=last)
plot(is_uptrend and show_trends? sma3:na, style=plot.style_linebr, color=color.green, linewidth=1, show_last=last)

// long_channel_low = lowest(low,stop_channel_size)
// short_channel_high = highest(high,stop_channel_size)

// color_long_channel_low = color.red
// color_short_channel_high = color.red

// plot(long?long_channel_low:na, color=color_long_channel_low, show_last=last)
// plot(long?na:short_channel_high, color=color_short_channel_high, show_last=last)

o = open[multiplier-1]
c = close
l = low
h = high
for i=0 to multiplier-1
l := min(l,low[i])
h := max(h,high[i])

bool chop_ok = true
if check_chop
source = close
avg = sma3
pi = atan(1) * 4
periods = 30
highestHigh = highest(periods)
lowestLow = lowest(periods)
range = 25 / (highestHigh - lowestLow) * lowestLow
ema34 = ema(source, 34)
x1_ema34 = 0
x2_ema34 = 1
y1_ema34 = 0
y2_ema34 = (ema34[1] - ema34) / avg * range
c_ema34 = sqrt((x2_ema34 - x1_ema34)*(x2_ema34 - x1_ema34) + (y2_ema34 - y1_ema34)*(y2_ema34 - y1_ema34))
emaAngle_1 = round(180 * acos((x2_ema34 - x1_ema34)/c_ema34) / pi)
emaAngle = iff(y2_ema34 > 0, - emaAngle_1, emaAngle_1)
chop_ok := emaAngle >= 5

is_three_white_soldiers = chop_ok and _TWS(is_downtrend, o, c, multiplier)
is_three_inside_up = chop_ok and _TIU(is_downtrend, o, c, multiplier)
is_three_outside_up = chop_ok and _TOU(is_downtrend, o, c, multiplier)
is_morning_star = chop_ok and _MS(is_downtrend, o, c, multiplier)
bull = is_three_white_soldiers or is_three_inside_up or is_three_outside_up or is_morning_star
plotchar(is_three_white_soldiers, "three_white_soldiers", 'W', location.belowbar, #26A69A, size=size.tiny, show_last=last)
plotchar(is_three_inside_up, "three_inside_up", 'I', location.belowbar, #26A69A, size=size.tiny, show_last=last)
plotchar(is_three_outside_up, "three_outside_up", 'O', location.belowbar, #26A69A, size=size.tiny, show_last=last)
plotchar(is_morning_star, "morning_star", 'M', location.belowbar, #26A69A, size=size.tiny, show_last=last)

is_three_black_crows = chop_ok and _TBC(is_uptrend, o, c, multiplier)
is_three_inside_down = chop_ok and _TID(is_uptrend, o, c, multiplier)
is_three_outside_down = chop_ok and _TOD(is_uptrend, o, c, multiplier)
is_evening_star = chop_ok and _ES(is_uptrend, o, c, multiplier)
bear = is_three_black_crows or is_three_inside_down or is_three_outside_down or is_evening_star
plotchar(is_three_black_crows, "three_black_crows", 'B', location.abovebar, #EF5350, size=size.tiny, show_last=last)
plotchar(is_three_inside_down, "three_inside_down", 'I', location.abovebar, #EF5350, size=size.tiny, show_last=last)
plotchar(is_three_outside_down, "three_outside_down", 'O', location.abovebar, #EF5350, size=size.tiny, show_last=last)
plotchar(is_evening_star, "evening_star", 'E', location.abovebar, #EF5350, size=size.tiny, show_last=last)

// account_value = compound? (strategy.initial_capital + strategy.netprofit) : min(strategy.initial_capital,strategy.equity)
// risk_usx = account_value*risk_perc/100

// var float long_stop_price = na
// long_stop = crossunder(low, long_stop_price[1])
// plotshape(long_stop, style=shape.xcross, location=location.belowbar, color=color.red, editable=false)

// if long
//     if strategy.position_size == 0
//         if bull
//             one_contract_usx = close*syminfo.pointvalue
//             delta_usx = (close-long_channel_low)*syminfo.pointvalue
//             contracts = risk_usx / delta_usx
//             value = contracts * one_contract_usx
//             if value > account_value
//                 contracts := account_value / one_contract_usx
//             if contracts > 0
//                 long_stop_price := long_channel_low
//                 strategy.entry("long", strategy.long, contracts, when = window())
//     else
//         if long_stop or (bear and (close > strategy.position_avg_price))
//             strategy.close("long", when = strategy.position_size > 0, qty_percent = 100)