@version : Strategy
@version : Strategy
//@version=5
//indicator("Pegasus", "Pegasus",overlay = false, max_labels_count
= 500)
strategy("PEGASUS-STRATEGY", overlay = false,
process_orders_on_close = true)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length = input(10, 'ATR Length')
type vector
array<float> out
//-----------------------------------------------------------------------------}
//Supertrend
//-----------------------------------------------------------------------------{
var holder = array.new<supertrend>(0)
var factors = array.new<float>(0)
atr = ta.atr(length)
//Compute Supertrend for multiple factors
k=0
for factor in factors
get_spt = holder.get(k)
//-----------------------------------------------------------------------------}
//K-means clustering
//-----------------------------------------------------------------------------{
factor_array = array.new<float>(0)
data = array.new<float>(0)
//Intialize clusters
var array<vector> factors_clusters = na
var array<vector> perfclusters = na
idx = dist.indexof(dist.min())
perfclusters.get(idx).out.push(value)
factors_clusters.get(idx).out.push(factor_array.get(i))
i += 1
//Update centroids
new_centroids = array.new<float>(0)
for cluster_ in perfclusters
new_centroids.push(cluster_.out.avg())
centroids := new_centroids
//-----------------------------------------------------------------------------}
//Signals and trailing stop
//-----------------------------------------------------------------------------{
//Get associated supertrend
var float target_factor = na
var float perf_idx = na
var float perf_ama = na
if not na(perfclusters)
//Get average factors within target cluster
target_factor := nz(factors_clusters.get(from).out.avg(),
target_factor)
//-----------------------------------------------------------------------------}
//Dashboard
//-----------------------------------------------------------------------------{
var table_position = dashLoc == 'Bottom Left' ? position.bottom_left
: dashLoc == 'Top Right' ? position.top_right : position.bottom_right
var table_size = textSize == 'Tiny' ? size.tiny : textSize == 'Small' ?
size.small : size.normal
var tb = table.new(table_position, 4, 4, bgcolor = #1e222d,
border_color = #373a46, border_width = 1, frame_color = #373a46,
frame_width = 1)
if showDash
if barstate.isfirst
tb.cell(0, 0, 'Cluster', text_color = color.white, text_size =
table_size)
tb.cell(0, 1, 'Best', text_color = color.white, text_size =
table_size)
tb.cell(0, 2, 'Average', text_color = color.white, text_size =
table_size)
tb.cell(0, 3, 'Worst', text_color = color.white, text_size =
table_size)
if barstate.islast
topN = perfclusters.get(2).out.size()
midN = perfclusters.get(1).out.size()
btmN = perfclusters.get(0).out.size()
//Size
tb.cell(1, 1, str.tostring(topN), text_color = color.white,
text_size = table_size)
tb.cell(1, 2, str.tostring(midN), text_color = color.white,
text_size = table_size)
tb.cell(1, 3, str.tostring(btmN), text_color = color.white,
text_size = table_size)
//Content
tb.cell(3, 1, str.tostring(factors_clusters.get(2).out),
text_color = color.white, text_size = table_size, text_halign =
text.align_left)
tb.cell(3, 2, str.tostring(factors_clusters.get(1).out),
text_color = color.white, text_size = table_size, text_halign =
text.align_left)
tb.cell(3, 3, str.tostring(factors_clusters.get(0).out),
text_color = color.white, text_size = table_size, text_halign =
text.align_left)
disp /= switch i
0 => btmN
1 => midN
2 => topN
i += 1
tb.cell(2, 4 - i, str.tostring(disp, '#.####'), text_color =
color.white, text_size = table_size)
//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
css = os ? bullCss : bearCss
plot(ts, 'Trailing Stop', os != os[1] ? na : css, force_overlay = true)
plot(perf_ama, 'Trailing Stop AMA',ta.cross(close, perf_ama) ? na:
close > perf_ama ? amaBullCss : amaBearCss, force_overlay = true)
//Candle coloring
barcolor(showGradient ? color.from_gradient(perf_idx, 0, 1,
color.new(css, 80), css) : na)
//Signals
n = bar_index
if showSignals
if os > os[1]
label.new(n, ts, str.tostring(int(perf_idx * 10)), color =
bullCss, style = label.style_label_up, textcolor = color.white, size =
size.tiny, force_overlay = true)
if os < os[1]
label.new(n, ts, str.tostring(int(perf_idx * 10)), color =
bearCss, style = label.style_label_down, textcolor = color.white, size
= size.tiny,force_overlay = true)
////indicator
//indicator("AI supertrend_osc Clustering Oscillator [LuxAlgo]",
"LuxAlgo - AI supertrend_osc Clustering Oscillator")
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length_osc = input(10, 'atr_osc length_osc')
minMult_osc = input.int(1, 'Factor Range Oscillator', minval = 0,
inline = 'factor osc')
maxMult_osc = input.int(5, '', minval = 0, inline = 'factor osc')
step_osc = input.float(.5, 'step_osc', minval = 0, step = 0.1)
smooth = input.float(1, minval = 1)
//Trigger error
if minMult_osc > maxMult_osc
runtime.error('Minimum factor is greater than maximum factor
in the range')
//Optimization
maxIter_osc = input.int(1000, 'Maximum Iteration step_oscs', minval
= 0, group = 'Optimization')
maxdata_osc_osc = input.int(10000, 'Historical Bars Calculation',
minval = 0, group = 'Optimization')
//Style
bullCss_osc = input(color.new(#5b9cf6, 50), 'Bullish', inline = 'bull',
group = 'Style')
strongbullCss_osc = input(color.new(#5b9cf6, 28), 'Strong', inline =
'bull', group = 'Style')
neutCss = input(#9598a1, 'Neutral', inline = 'neut', group = 'Style')
bearCss_osc = input(color.new(#f77c80, 50), 'Bearish', inline =
'bear', group = 'Style')
strongbearCss_osc = input(color.new(#f77c80, 28), 'Strong', inline =
'bear', group = 'Style')
//-----------------------------------------------------------------------------}
//UDT's
//-----------------------------------------------------------------------------{
type supertrend_osc
float upper = hl2
float lower = hl2
float output
int trend
type vector_osc
array<float> out
//-----------------------------------------------------------------------------}
//supertrend_osc
//-----------------------------------------------------------------------------{
var holder_osc = array.new<supertrend_osc>(0)
var factors_osc = array.new<float>(0)
atr_osc = ta.atr(length_osc)
//-----------------------------------------------------------------------------}
//k_osc-means clustering
//-----------------------------------------------------------------------------{
data_osc = array.new<float>(0)
//Intialize clusters
var array<vector_osc> clusters = na
idx = dist.indexof(dist.min())
if idx != -1
clusters.get(idx).out.push(value)
//Update centroids_osc
new_centroids_osc = array.new<float>(0)
for cluster_ in clusters
new_centroids_osc.push(cluster_.out.avg())
centroids_osc := new_centroids_osc
//-----------------------------------------------------------------------------}
//Get centroids_osc
//-----------------------------------------------------------------------------{
//Get associated supertrend_osc
var float bull = 0
var float neut = 0
var float bear = 0
var den_osc = 0
if not na(clusters)
bull += 2/(smooth+1) * nz(centroids_osc.get(2) - bull)
neut += 2/(smooth+1) * nz(centroids_osc.get(1) - neut)
bear += 2/(smooth+1) * nz(centroids_osc.get(0) - bear)
den_osc += 1
//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
plot_bull = plot(math.max(bull, 0), color = na, editable = false)
plot_bull_ext = plot(math.max(bear, 0), 'Strong Bullish Oscillator',
bear > 0 ? strongbullCss_osc : na, style = plot.style_circles)
//
=========================================
============================
// if showSignals
// if os > os[1]
// label.new(n, ts, str.tostring(int(perf_idx * 10))
// , color = bullCss
// , style = label.style_label_up
// , textcolor = color.white
// , size = size.tiny)
// if os < os[1]
// label.new(n, ts, str.tostring(int(perf_idx * 10))
// , color = bearCss
// , style = label.style_label_down
// , textcolor = color.white
// , size = size.tiny)
plot_bearish_series = math.min(bear, 0)
plot_bullish_series = math.max(bull, 0)
ids = int(perf_idx * 10)
// Calculate SL and TP
buy_sl = low - (low * 0.01) // SL for buy is the low of the candle
minus 1% buffer
buy_tp = buy_sl + 5 * (high - buy_sl) // 1:5 Risk Reward
sell_sl = high + (high * 0.01) // SL for sell is the high of the candle
plus 1% buffer
sell_tp = sell_sl - 5 * (sell_sl - low) // 1:5 Risk Reward
if sell_signal
strategy.entry("SHORT", strategy.short)
if buy_signal
strategy.entry("LONG", strategy.long)
if strategy.position_size>0
strategy.exit("EXIT_LONG","LONG",stop= low, limit = high )
if strategy.position_size<0
strategy.exit("EXIT_SHORT","SHORT",stop= high, limit = low )
// Alert Conditions
alertcondition(buy_signal, title="BUY SIGNAL")
alertcondition(sell_signal, title="SELL SIGNAL")