// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MasterX1987
//@version=6
//strategy("Freqtrade Strategy Translation", overlay=true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 1)
indicator('KamaFama', max_lines_count = 500, max_labels_count = 500, max_bars_back = 500)
color GREEN = #26a69a
color GREEN_LIGHT = color.new(GREEN, 50)
color GREEN_LIGHTER = color.new(GREEN, 75)
color RED = #ef5350
color RED_LIGHT = color.new(RED, 50)
color RED_LIGHTER = color.new(RED, 75)
tp_sl_entry_transparent = 30
color tp_color = color.new(color.green, tp_sl_entry_transparent)
color entry_color = color.rgb(120, 123, 134, tp_sl_entry_transparent)
color sl_color = color.new(color.red, tp_sl_entry_transparent)
line_style = line.style_dotted
type Trade
int id = 0
int timestamp = na
bool hold = false
bool hold2 = false
int start_bar_index = 0
int count = 0
string entry_tag
string exit_tag
float market_order_comission
float limit_order_comission
float entry_price
float sl_price
float tp_price
float min_rate
float min_profit
float open_rate
float break_even_price
bool entry_hit = false
bool sl_hit = false
bool tp_hit = false
float position_size_left = 100
float risk_percent
bool is_closed = false
bool wait = false
bool enter = false
bool exit = false
float close_price = 0
bool can_break_even = false
bool force_closed = false
float profit = 0
float current_profit
float risk_reward
line entry_line
line stoploss_line
line target1_line
line target2_line
line target3_line
line target4_line
var Trade trade = na
var int id = 0
var int first_trade_date = na
var int last_trade_bar_index = 0
var int trade_count = 0
var int profit_trades = 0
var int loss_trades = 0
var float total_profit = 0.0
var float profit = 0.0
var float loss = 0.0
var int mc = 0
var int rc = 0
var int sc = 0
var int mac = 0
var line tp_line = na
var label tp_label = na
var line entry_line = na
var label entry_label = na
var line close_line = na
var label close_label = na
var line sl_line = na
var label sl_label = na
var label lable_at_signal = na
var label conds_label = na
var label conds_label_2 = na
var string conds_text = na
var float PI = 2 * math.asin(1)
histb(x, t) =>
h = t == 0 ? x == 0 ? 0.0 : x == 1 ? 1.0 : x == 2 ? 2.0 : x == 3 ? 3.0 : x == 4 ? 4.0 : x == 5 ? 5.0 : x == 6 ? 6.0 : x == 7 ? 7.0 : x == 8 ? 8.0 : x == 9 ? 9.0 : x == 10 ? 10.0 : 11.0 : x == 0 ? 0.5 : x == 1 ? 1.5 : x == 2 ? 2.5 : x == 3 ? 3.5 : x == 4 ? 4.5 : x == 5 ? 5.5 : x == 6 ? 6.5 : x == 7 ? 7.5 : x == 8 ? 8.5 : x == 9 ? 9.5 : x == 10 ? 10.5 : 11.5
h
correlationTrend(Series_1, Period) => // Correlation Trend Function
period = ta.highest(int(Period), 2)
Ex = 0.0
Ey = 0.0
Ex2 = 0.0
Ey2 = 0.0
Exy = 0.0
for i = 0 to period - 1 by 1
X = nz(Series_1[i])
Y = -i
Ex := Ex + X
Ex2 := Ex2 + X * X
Exy := Exy + X * Y
Ey2 := Ey2 + Y * Y
Ey := Ey + Y
Ey
denominator = (period * Ex2 - Ex * Ex) * (period * Ey2 - Ey * Ey)
denominator == 0.0 or bar_index == 0 ? 0.0 : (period * Exy - Ex * Ey) / math.sqrt(denominator)
RoundUp(number, decimals) =>
factor = math.pow(10, decimals)
math.ceil(number * factor) / factor
// Truncated Hilbert transform
_hilbertTransform(src) =>
out = 0.0962 * src + 0.5769 * nz(src[2]) - 0.5769 * nz(src[4]) - 0.0962 * nz(src[6])
out
_computeComponent(src, mesaPeriodMult) =>
out = _hilbertTransform(src) * mesaPeriodMult
out
_smoothComponent(src) =>
out = 0.2 * src + 0.8 * nz(src[1])
out
_computeAlpha(src, fastLimit, slowLimit) =>
mesaPeriod = 0.0
mesaPeriodMult = 0.075 * nz(mesaPeriod[1]) + 0.54
smooth = (4 * src + 3 * nz(src[1]) + 2 * nz(src[2]) + nz(src[3])) / 10
detrender = _computeComponent(smooth, mesaPeriodMult)
// Compute InPhase and Quadrature components
I1 = nz(detrender[3])
Q1 = _computeComponent(detrender, mesaPeriodMult)
// Advance the phase of I1 and Q1 by 90 degrees
jI = _computeComponent(I1, mesaPeriodMult)
jQ = _computeComponent(Q1, mesaPeriodMult)
// Phasor addition for 3 bar averaging
I2 = I1 - jQ
Q2 = Q1 + jI
// Smooth the I and Q components before applying the discriminator
I2 := _smoothComponent(I2)
Q2 := _smoothComponent(Q2)
// Homodyne Discriminator
Re = I2 * nz(I2[1], I2) + Q2 * nz(Q2[1], Q2)
Im = I2 * nz(Q2[1], Q2) - Q2 * nz(I2[1], I2)
Re := _smoothComponent(Re)
Im := _smoothComponent(Im)
if Re != 0 and Im != 0
mesaPeriod := 2 * PI / math.atan(Im / Re)
mesaPeriod
mesaPeriod := math.min(mesaPeriod, 1.5 * nz(mesaPeriod[1], mesaPeriod))
mesaPeriod := math.max(mesaPeriod, 0.67 * nz(mesaPeriod[1], mesaPeriod))
mesaPeriod := math.min(math.max(mesaPeriod, 6), 50)
mesaPeriod := _smoothComponent(mesaPeriod)
phase = 0.0
if I1 != 0
phase := 180 / PI * math.atan(Q1 / I1)
phase
deltaPhase = nz(phase[1], phase) - phase
deltaPhase := math.max(deltaPhase, 1)
alpha = math.max(fastLimit / deltaPhase, slowLimit)
out = alpha
out
// Inputs
use_trailing_stop = input.bool(true, title = 'Use Trailing Stop', group = 'Bools', inline = '1')
use_stoploss = input.bool(true, title = 'Use Stoploss', group = 'Bools', inline = '1')
show_profit_panel = input.bool(true, title = 'Show Profit Panel', group = 'Bools', inline = '2')
show_stoploss = input.bool(true, title = 'Show Stoploss', group = 'Bools', inline = '2')
show_conds = input.bool(true, title = 'Show Conditions', group = 'Bools', inline = '3')
show_indi = input.bool(false, title = 'Show Indicators', group = 'Bools', inline = '3')
//Protection
stoploss = input.float(-0.25, title = 'Stoploss', minval = -1, maxval = 0, group = 'Protection')
trailing_stop = input.float(0.05, title = 'Traling Stop', minval = 0, maxval = 1, group = 'Protection')
stop_duration_candles = input.int(5, title = 'CooldownPeriod', minval = 0, group = 'Protection')
// Mama & Fama
fmama_fast = input.float(title="Fast Limit", step=0.01, defval=0.25, group = 'MAMA-FAMA')
fmama_slow = input.float(title="Slow Limit", step=0.01, defval=0.025, group = 'MAMA-FAMA')
fmama_src = input.source(title="Source", defval=hl2, group = 'MAMA-FAMA')
// Kama
kama_length = input.int(title="Length", defval=84, minval=1, group='KAMA')
kama_fast = input.int(title="Fast EMA Length", defval=2, group='KAMA')
kama_slow = input.int(title="Slow EMA Length", defval=30, group='KAMA')
kama_src = input.source(title="Source", defval=close, group='KAMA')
// Williams %R
willy_length = input.int(title="Length", defval=14, minval=1, group='Williams %R')
// Sell
sell_fastx = input.int(84, title = 'Sell FastK', minval = 50, maxval = 100, group = 'Sell')
// Indicators
fastk = ta.stoch(close, high, low, 5)
// Mama & Fama
alpha = _computeAlpha(fmama_src, fmama_fast, fmama_slow)
alpha2 = alpha / 2
mama = 0.0
mama := alpha * fmama_src + (1 - alpha) * nz(mama[1])
fama = 0.0
fama := alpha2 * mama + (1 - alpha2) * nz(fama[1])
mama_diff = 0.0
mama_diff := (mama - fama) / fmama_src
// Kama
mom = math.abs(ta.change(kama_src, kama_length))
volatility = math.sum(math.abs(ta.change(kama_src)), kama_length)
// Efficiency Ratio
er = volatility != 0 ? mom / volatility : 0
fastAlpha = 2 / (kama_fast + 1)
slowAlpha = 2 / (kama_slow + 1)
alphaK = math.pow(er * (fastAlpha - slowAlpha) + slowAlpha, 2)
kama = 0.0
kama := alphaK * kama_src + (1 - alphaK) * nz(kama[1], kama_src)
// RSI
rsi_84 = ta.rsi(close, 84)
rsi_112 = ta.rsi(close, 112)
// CTI
cti = correlationTrend(close, 20)
// Williams %R
willy_upper = ta.highest(willy_length)
willy_lower = ta.lowest(willy_length)
r_14 = 100 * (close - willy_upper) / (willy_upper - willy_lower)
c48 = ta.highest(close, 48)
c288 = ta.highest(close, 288)
// Buy conditions
buy_condition_1 = kama > fama and fama > mama * 0.981 and r_14 < -61.3 and mama_diff < -0.025 and cti < -0.715 and c48 >= close * 1.05 and c288 >= close * 1.125 and rsi_84 < 60 and rsi_112 < 60
buy_condition = buy_condition_1 // or buy_condition_new
if not na(trade)
if trade.start_bar_index == 0
trade.start_bar_index := bar_index
trade.open_rate := open
trade.min_rate := low
trade.min_profit := (100 / trade.open_rate * trade.min_rate - 100) / 100
first_trade_date := trade_count == 0 ? timestamp(year, month, dayofmonth, hour, minute) : first_trade_date
entry_line := line.new(x1 = trade.start_bar_index, y1 = trade.entry_price, x2 = bar_index, y2 = trade.entry_price, color = entry_color, style = line.style_solid, width = 2, force_overlay = true)
if use_stoploss and show_stoploss
sl_line := line.new(x1 = trade.start_bar_index, y1 = trade.sl_price, x2 = bar_index, y2 = trade.sl_price, color = sl_color, style = line_style, width = 2, force_overlay = true)
if use_trailing_stop
tp_line := line.new(x1 = trade.start_bar_index, y1 = trade.tp_price, x2 = bar_index, y2 = trade.tp_price, color = tp_color, style = line_style, width = 2, force_overlay = true)
lable_at_signal := label.new(bar_index, trade.tp_price * 1.09, '', style = label.style_label_center, textcolor = color.white, force_overlay = true)
if not use_trailing_stop
lable_at_signal := label.new(bar_index, trade.entry_price * 1.1, '', style = label.style_label_center, textcolor = color.white, force_overlay = true)
if low < trade.min_rate and bar_index != trade.start_bar_index
trade.min_rate := low
trade.min_profit := (100 / trade.open_rate * trade.min_rate - 100) / 100
if use_trailing_stop
if close >= trade.tp_price and not trade.tp_hit
trade.tp_hit := true
if use_stoploss
if close <= trade.sl_price and bar_index != trade.start_bar_index and not trade.sl_hit
trade.sl_hit := true
if not trade.is_closed
if use_trailing_stop
line.set_xy1(tp_line, trade.start_bar_index, trade.tp_price)
line.set_xy2(tp_line, bar_index + 1, trade.tp_price)
line.set_xy1(entry_line, trade.start_bar_index, trade.entry_price)
line.set_xy2(entry_line, bar_index + 1, trade.entry_price)
if use_stoploss and show_stoploss
line.set_xy1(sl_line, trade.start_bar_index, trade.sl_price)
line.set_xy2(sl_line, bar_index + 1, trade.sl_price)
lable_at_signal.set_x(int(math.avg(trade.start_bar_index, bar_index + 2)))
else
if use_trailing_stop
line.set_xy1(tp_line, trade.start_bar_index, trade.tp_price)
line.set_xy2(tp_line, bar_index, trade.tp_price)
line.set_xy1(entry_line, trade.start_bar_index, trade.entry_price)
line.set_xy2(entry_line, bar_index, trade.entry_price)
if use_stoploss and show_stoploss
//line.delete(sl_line)
line.set_xy1(sl_line, trade.start_bar_index, trade.sl_price)
line.set_xy2(sl_line, bar_index, trade.sl_price)
lable_at_signal.set_x(int(math.avg(trade.start_bar_index, bar_index)))
sign = trade.profit >= 0 ? '+' : na
if trade.exit_tag == ''
lable_at_signal.set_text(sign + str.tostring(trade.profit, '#.##') + '%, ' + str.tostring(total_profit, '#.##') + '%\n' + trade.entry_tag)
else
lable_at_signal.set_text(sign + str.tostring(trade.profit, '#.##') + '%, ' + str.tostring(total_profit, '#.##') + '%\n' + trade.entry_tag + '\n' + trade.exit_tag)
lable_at_signal.set_color(trade.profit >= 0 ? color.green : color.red)
if use_trailing_stop
if trade.tp_hit
linefill.new(entry_line, tp_line, color = color.new(color.green, 85))
if use_stoploss and show_stoploss
if trade.sl_hit
linefill.new(sl_line, entry_line, color = color.new(color.red, 85))
if trade.is_closed
close_line := line.new(x1 = trade.start_bar_index, y1 = trade.close_price, x2 = bar_index, y2 = trade.close_price, color = color.white, style = line_style, width = 2, force_overlay = true)
if trade.profit <= 0
lable_at_signal.set_xy(int(math.avg(trade.start_bar_index, bar_index)), trade.entry_price * 1.1)
linefill.new(close_line, entry_line, color = color.new(color.red, 85))
else
lable_at_signal.set_xy(int(math.avg(trade.start_bar_index, bar_index)), trade.close_price * 1.1)
linefill.new(close_line, entry_line, color = color.new(color.green, 85))
if use_stoploss and show_stoploss
line.delete(sl_line)
if use_trailing_stop
line.delete(tp_line)
trade := na
if na(trade)
if buy_condition and bar_index >= last_trade_bar_index + stop_duration_candles
id := id + 1
trade := Trade.new()
trade.id := id
trade.entry_tag := 'buy'
//if buy_condition_1
//trade.entry_tag := 'buy'
//trade.entry_tag
//strategy.entry("Buy 1", strategy.long)
//else
//trade.entry_tag := 'buy_new'
//trade.entry_tag
//strategy.entry("Buy New", strategy.long)
trade.entry_price := close
trade.entry_hit := true
trade.timestamp := timestamp(year, month, dayofmonth, hour, minute)
if use_stoploss
trade.sl_price := close + close * stoploss
if use_trailing_stop
trade.tp_price := close + close * trailing_stop
else
trade.count := trade.count + 1
trade.profit := 100 / trade.entry_price * close - 100
trade.current_profit := trade.profit / 100
if trade.current_profit > 0 and fastk >= sell_fastx
trade.exit := true
trade.exit_tag := 'fastk_profit_sell'
if use_stoploss
if low <= trade.sl_price
trade.exit := true
trade.exit_tag := 'stoploss'
trade.profit := 100 / trade.entry_price * trade.sl_price - 100
trade.current_profit := trade.profit / 100
if use_trailing_stop
if close >= trade.tp_price
trade.exit := true
trade.exit_tag := 'trailing_stop'
trade.profit := 100 / trade.entry_price * trade.tp_price - 100
trade.current_profit := trade.profit / 100
if trade.exit
trade_count := trade_count + 1
if trade.profit > 0
profit_trades := profit_trades + 1
else
loss_trades := loss_trades + 1
total_profit := total_profit + trade.profit
if trade.exit_tag == 'stoploss'
trade.close_price := trade.sl_price
else if trade.exit_tag == 'trailing_stop'
trade.close_price := trade.tp_price
else
trade.close_price := close
trade.is_closed := true
last_trade_bar_index := bar_index
// Plotting ...
plotshape(buy_condition, style = shape.triangleup, location = location.belowbar, color = color.green, force_overlay = true)
plot(mama, title='MAMA', linewidth=3, color=#58a74e, force_overlay=true)
plot(fama, title='FAMA', linewidth=3, color=#f6b26b, force_overlay=true)
plot(kama, title='KAMA', linewidth=3, color=#6d1e7f, force_overlay=true)
plot(show_indi ? fastk : na, title='fastk', linewidth=2, color=#95f66b)
plot(show_indi ? 84 : na, title='HLine', linewidth=1, color=#b1aaa5, style = plot.style_circles)
plot(show_conds ? histb(0, 1) : na, 'kama > fama', kama > fama ? GREEN_LIGHT : RED_LIGHT, histbase = histb(0, 0), style = plot.style_columns)
plot(show_conds ? histb(1, 1) : na, 'fama > mama * 0.981', fama > mama * 0.981 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(1, 0), style = plot.style_columns)
plot(show_conds ? histb(2, 1) : na, 'Williams_%R < -61.3', r_14 < -61.3 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(2, 0), style = plot.style_columns)
plot(show_conds ? histb(3, 1) : na, 'mama_diff < -0.025', mama_diff < -0.025 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(3, 0), style = plot.style_columns)
plot(show_conds ? histb(4, 1) : na, 'cti < -0.715', cti < -0.715 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(4, 0), style = plot.style_columns)
plot(show_conds ? histb(5, 1) : na, 'close[48].max > close * 1.05', c48 > close * 1.05 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(5, 0), style = plot.style_columns)
plot(show_conds ? histb(6, 1) : na, 'close[288].max > close * 1.125', c288 > close * 1.125 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(6, 0), style = plot.style_columns)
plot(show_conds ? histb(7, 1) : na, 'rsi_84 < 60', rsi_84 < 60 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(7, 0), style = plot.style_columns)
plot(show_conds ? histb(8, 1) : na, 'rsi_112 < 60', rsi_112 < 60 ? GREEN_LIGHT : RED_LIGHT, histbase = histb(8, 0), style = plot.style_columns)
var table conds_table = na
if show_conds
conds_table := table.new(position.middle_right, 1, 9, border_color = color.green, border_width = 0)
table.cell(conds_table, 0, 0, 'rsi_112 < 60', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 1, 'rsi_84 < 60', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 2, 'close[288].max > close * 1.125', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 3, 'close[48].max > close * 1.05', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 4, 'cti < -0.715', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 5, 'mama_diff < -0.025', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 6, 'Williams_%R < -61.3', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 7, 'fama > mama * 0.981', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
table.cell(conds_table, 0, 8, 'kama > fama', bgcolor = color.rgb(0, 0, 0, 75), height = 12, text_color = color.green, text_size = size.small)
var table profit_table = na
if show_profit_panel
profit_table := table.new(position.bottom_right, 3, 10, border_color = color.green, border_width = 0, force_overlay = true)
table.cell(profit_table, 0, 0, '═════════════════════════════' + '\n' + "Mastaaa's KamaFama" + '\n' + '═════════════════════════════', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 12, height = 10, text_size = size.normal)
table.cell(profit_table, 1, 0, '', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 6, height = 3, text_size = size.normal)
table.merge_cells(profit_table, 0, 0, 1, 0)
table.cell(profit_table, 0, 1, 'First trade:', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 8, height = 3, text_size = size.normal, text_halign = text.align_left)
table.cell(profit_table, 1, 1, str.format('{0,date,long}', first_trade_date), bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 12, height = 3, text_size = size.normal)
//table.cell(profit_table, 1, 1, str.tostring(first_trade_date), bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 12, height = 3, text_size = size.normal)
table.cell(profit_table, 0, 2, 'Total trades:', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal, text_halign = text.align_left)
table.cell(profit_table, 1, 2, str.tostring(trade_count), bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal)
table.cell(profit_table, 0, 3, 'Profit trades:', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal, text_halign = text.align_left)
table.cell(profit_table, 1, 3, str.tostring(profit_trades), bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal)
table.cell(profit_table, 0, 4, 'Loss trades:', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal, text_halign = text.align_left)
table.cell(profit_table, 1, 4, str.tostring(loss_trades), bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal)
table.cell(profit_table, 0, 5, 'Winrate:', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal, text_halign = text.align_left)
table.cell(profit_table, 1, 5, str.tostring(RoundUp(profit_trades / trade_count * 100, 2)) + '%', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal)
table.cell(profit_table, 0, 9, '═════════════════════════════' + '\n' + '💰 Profit: ' + str.tostring(total_profit, '#.##') + '% 💰' + '\n' + '═════════════════════════════', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 10, text_size = size.normal, text_halign = text.align_center, text_valign = text.align_top)
table.cell(profit_table, 1, 9, '', bgcolor = color.rgb(0, 0, 0, 75), text_color = color.green, width = 5, height = 3, text_size = size.normal)
table.merge_cells(profit_table, 0, 9, 1, 9)
Show all 425 lines