// This source code is subject to the terms of the Mozilla Public License 2.
0 at
[Link]
// © Crypto_Saiyan
//@version=5
strategy('SAIYANATOR Strategy v1.37', overlay=true, max_boxes_count=20,
max_labels_count=500, default_qty_type = strategy.percent_of_equity,
default_qty_value = 100, initial_capital = 100)
/////////////////////////////////////////////////////////////////////////
//--------------------------ORDER-BLOCKS-------------------------------//
/////////////////////////////////////////////////////////////////////////
sens = [Link](20, minval=1, title='Sensitivity', group='Order Blocks',
tooltip='Lower the sensitivity to show more order blocks. A higher sensitivity will
show less order blocks.')
sens /= 100
// OB
OBMitigationType = [Link]("Close", title="Mitigation Type", options=["Close",
"Wick"], group="Order Blocks", tooltip="Choose how Order Blocks are mitigated")
OBBullMitigation = OBMitigationType=="Close" ? close[1] : low
OBBearMitigation = OBMitigationType=="Close" ? close[1] : high
//OB Colors
col_bullish = [Link](#5db49e, title="Bullish OB Border", inline="a",
group="Order Blocks")
col_bullish_ob = [Link]([Link](#64C4AC, 85), title="Background",
inline="a", group="Order Blocks")
col_bearish = [Link](#ff9100, title="Bearish OB Border", inline="b",
group="Order Blocks")
col_bearish_ob = [Link]([Link](#ff7b00, 85), title="Background",
inline="b", group="Order Blocks")
// Delacring Variables
bool ob_created = false
bool ob_created_bull = false
var int cross_index = na
// Declaring Box Arrays
var box drawlongBox = na
var longBoxes = array.new_box()
var box drawShortBox = na
var shortBoxes = array.new_box()
// Custom Rate of Change (ROC) calculation. This is to calculate high momentum
moves in the market.
pc = (open - open[4]) / open[4] * 100
// Sensitivty is negative as this is a Bearish OB
if [Link](pc, -sens)
ob_created := true
cross_index := bar_index
cross_index
// If the ROC crossover our Sensitivty input - Then create an Order Block
if [Link](pc, sens)
ob_created_bull := true
cross_index := bar_index
cross_index
// -------------------------------
// Bearish OB Creation
// -------------------------------
// Check if we should create a OB, Also check if we haven't created an OB in the
last 5 candles.
if ob_created and cross_index - cross_index[1] > 5
float last_green = 0
float highest = 0
// Loop through the most recent candles and find the first GREEN (Bullish)
candle. We will place our OB here.
for i = 4 to 15 by 1
if close[i] > open[i]
last_green := i
break
// Draw our OB on that candle - then push the box into our box arrays.
drawShortBox := [Link](left=bar_index[last_green], top=high[last_green],
bottom=low[last_green], right=bar_index[last_green], bgcolor=col_bearish_ob,
border_color=col_bearish, extend=[Link])
[Link](shortBoxes, drawShortBox)
// -------------------------------
// Bullish OB Creation
// -------------------------------
// Check if we should create a OB, Also check if we haven't created an OB in the
last 5 candles.
if ob_created_bull and cross_index - cross_index[1] > 5
float last_red = 0
float highest = 0
// Loop through the most recent candles and find the first RED (Bearish)
candle. We will place our OB here.
for i = 4 to 15 by 1
if close[i] < open[i]
last_red := i
break
// Draw our OB on that candle - then push the box into our box arrays.
drawlongBox := [Link](left=bar_index[last_red], top=high[last_red],
bottom=low[last_red], right=bar_index[last_red], bgcolor=col_bullish_ob,
border_color=col_bullish, extend=[Link])
[Link](longBoxes, drawlongBox)
// ----------------- Bearish Order Block -------------------
// Clean up OB boxes and place alerts
if [Link](shortBoxes) > 0
for i = [Link](shortBoxes) - 1 to 0 by 1
sbox = [Link](shortBoxes, i)
top = box.get_top(sbox)
bot = box.get_bottom(sbox)
// If the two last closes are above the high of the bearish OB - Remove the
OB
if OBBearMitigation > top
[Link](shortBoxes, i)
[Link](sbox)
// ----------------- Bullish Clean Up -------------------
// Clean up OB boxes and place alerts
if [Link](longBoxes) > 0
for i = [Link](longBoxes) - 1 to 0 by 1
sbox = [Link](longBoxes, i)
bot = box.get_bottom(sbox)
top = box.get_top(sbox)
// If the two last closes are below the low of the bullish OB - Remove the
OB
if OBBullMitigation < bot
[Link](longBoxes, i)
[Link](sbox)
///////////////////////////////////////////////////////////////////////////////////
/////////
//---------------------------------BUY/SELL-
SIGNALS---------------------------------------//
///////////////////////////////////////////////////////////////////////////////////
/////////
//General inputs
showtext = input(true, title='Show Buy/Sell?', group="BUY/SELL Signals") // Show
buy & sell signals?
cc = input(true, title='Green/redshift on/off?') // Trend color change?
nfilter = input(true, title='Noise filter on/off?')
useCurrentRes = input(true, title='Use Current Timeframe for filter?')
resCustom = [Link](title='Use Different Timeframe for filter? Uncheck Box
Above!', defval='60')
len = input(110, title='TEMA period')
FfastLength = [Link](15, minval=1, title='Filter fast length')
FslowLength = [Link](30, minval=1, title='Filter slow length')
FsignalLength = [Link](50, minval=1, title='Filter signal length')
percentStop = [Link](0.8, "STOP LOSS %", 0, group="TP/SL")
percentTake = [Link](0.45, "TAKE PROFIT %", 0)
// Entry & Exit
enableE = input(true, "EXIT & ENTRY ON/OFF", group="EXIT & ENTRY")
Stop = [Link]([Link](#83868f, 5), "STOP LOSS", group="EXIT &
ENTRY")
Entry = [Link]([Link](#8d020e, 5), "ENTRY", group="EXIT &
ENTRY")
Tp1 = [Link]([Link](#038007, 5), "TAKE PROFIT", group="EXIT &
ENTRY")
//
//Get real close price
t_id = [Link]([Link], [Link])
realC = [Link](t_id, [Link], close)
//
f_chartTfInMinutes() =>
float _resInMinutes = [Link] * (
[Link] ? 1 :
[Link] ? 1. :
[Link] ? 60. * 24 :
[Link] ? 60. * 24 * 7 :
[Link] ? 60. * 24 * 30.4375 : na)
// Functions
securityNoRep(sym, res, src) => [Link](sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on)
//Set persistent variables (buy price, sell price, last transaction type)
buyprice = 0.0
buyprice := nz(buyprice[1])
sellprice = 0.0
sellprice := nz(sellprice[1])
last_tran = false
last_tran := nz(last_tran[1])
//
//Truncate function used for rounding variables
truncate(number, decimals) =>
factor = [Link](10, decimals)
int(number * factor) / factor
//
//Triple EMA definition
ema1 = [Link](realC, len)
ema2 = [Link](ema1, len)
ema3 = [Link](ema2, len)
//
//Triple EMA trend calculation
avg = 3 * (ema1 - ema2) + ema3
out = avg
out1 = [Link](t_id, [Link], out)
ma_up = out1 >= out1[1]
ma_down = out1 < out1[1]
col = cc ? ma_up ? [Link] : ma_down ? [Link] : [Link] : [Link]
t_UP = ma_up
t_DOWN = ma_down
t_NON = t_UP == t_DOWN
//
//Filter formula
Fsource = close
macd_colorChange = true
Fres = useCurrentRes ? [Link] : resCustom
FfastMA = [Link](Fsource, FfastLength)
FslowMA = [Link](Fsource, FslowLength)
Fmacd = FfastMA - FslowMA
Fsignal = [Link](Fmacd, FsignalLength)
outMacD = [Link]([Link], Fres, Fmacd)
outSignal = [Link]([Link], Fres, Fsignal)
Fbuy = outMacD >= outSignal or not nfilter
Fsell = outMacD < outSignal or not nfilter
//
//Entry & exit conditions
longCond = t_UP and not last_tran and not t_NON and Fbuy and [Link]
shortCond = t_DOWN and last_tran and not t_NON and Fsell and [Link]
//
if longCond
buyprice := realC //Set buyprice
last_tran := true //Set long condition
last_tran
if shortCond
sellprice := realC //Set sellprice
last_tran := false //Set short condition
last_tran
//
PercVal = last_tran ? 100 * (realC - buyprice) / buyprice : 100 * (realC -
sellprice) / sellprice //Percent change since last buy/sell
PercCol = PercVal >= 0 ? [Link] : [Link] //Color of percent text
//
goodlong = longCond and buyprice <= sellprice // Buying with a profit?
goodshort = shortCond and sellprice > buyprice // Selling with a profit?
txtlight_b = [Link] // Buy signal set to always green.
txtlight_s = goodshort ? [Link] : [Link] // Sell signal change color
depending on profitable or not.
buytxt = [Link](truncate(buyprice, 8))
selltxt = [Link](truncate(sellprice, 8))
// Get components
source = close
lastCond = 0, lastCond := longCond ? 1 : shortCond ? -1 : lastCond[1]
bull = longCond and lastCond[1] == -1
bear = shortCond and lastCond[1] == 1
countBull = [Link](bull)
countBear = [Link](bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
rsi = [Link](close, 21)
rsiOb = rsi > 70 and rsi > [Link](rsi, 10)
rsiOs = rsi < 30 and rsi < [Link](rsi, 10)
dHigh = securityNoRep([Link], "D", high [1])
dLow = securityNoRep([Link], "D", low [1])
dClose = securityNoRep([Link], "D", close[1])
ema = [Link](close, 144)
emaBull = close > ema
equal_tf(res) => [Link](res) == f_chartTfInMinutes() and not
[Link]
higher_tf(res) => [Link](res) > f_chartTfInMinutes() or [Link]
too_small_tf(res) => ([Link] and res=="1") or ([Link] and
[Link](res) < 10)
securityNoRep1(sym, res, src) =>
bool bull_ = na
bull_ := equal_tf(res) ? src : bull_
bull_ := higher_tf(res) ? [Link](sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on) : bull_
bull_array = request.security_lower_tf([Link], higher_tf(res) ?
[Link](f_chartTfInMinutes()) + ([Link] ? "S" : "") :
too_small_tf(res) ? ([Link] ? "3" : "10") : res, src)
if [Link](bull_array) > 1 and not equal_tf(res) and not higher_tf(res)
bull_ := [Link](bull_array)
[Link](bull_array)
bull_
TF1Bull = securityNoRep1([Link], "1" , emaBull)
TF3Bull = securityNoRep1([Link], "3" , emaBull)
TF5Bull = securityNoRep1([Link], "5" , emaBull)
TF15Bull = securityNoRep1([Link], "15" , emaBull)
TF30Bull = securityNoRep1([Link], "30" , emaBull)
TF60Bull = securityNoRep1([Link], "60" , emaBull)
TF120Bull = securityNoRep1([Link], "120" , emaBull)
TF240Bull = securityNoRep1([Link], "240" , emaBull)
TF480Bull = securityNoRep1([Link], "480" , emaBull)
TFDBull = securityNoRep1([Link], "1440", emaBull)
//
srcStop = close
atrBand = srcStop * (percentStop / 100)
atrBand2 = srcStop * (percentTake / 100)
atrStop = trigger ? srcStop - atrBand : srcStop + atrBand
atrTake = trigger ? srcStop - atrBand2 : srcStop + atrBand2
lastTrade(src) => [Link](bull or bear, src, 0)
entry_y = lastTrade(srcStop)
stop_y = lastTrade(atrStop)
tp1_y = (entry_y - lastTrade(atrTake)) + entry_y
labelTpSl(y, txt, color) =>
label labelTpSl = percentStop != 0 ? [Link](bar_index + 1, y, txt,
xloc.bar_index, [Link], color, label.style_label_left, [Link],
[Link]) : na
[Link](labelTpSl[1])
labelTpSl(entry_y, "Entry: " + [Link](math.round_to_mintick(entry_y)), color
= Entry)
labelTpSl(stop_y , "Stop Loss: " + [Link](math.round_to_mintick(stop_y)),
color = Stop)
labelTpSl(tp1_y, "Take Profit: " + [Link](math.round_to_mintick(tp1_y)),
color = Tp1)
lineTpSl(y, color) =>
line lineTpSl = percentStop != 0 ? [Link](bar_index - (trigger ? countBull :
countBear) + 4, y, bar_index + 1, y, xloc.bar_index, [Link], color,
line.style_solid) : na
[Link](lineTpSl[1])
lineTpSl(entry_y, color = Entry)
lineTpSl(stop_y, color = Stop)
lineTpSl(tp1_y, color = Tp1)
////////////////////////////////////////////////////////////////////////////////
//--------------------------FILTER-OVER/UNDER-EMA-----------------------------//
////////////////////////////////////////////////////////////////////////////////
// inputs
emafilter = [Link](true, title="Filter Over/Under EMA", group="EMA Filter")
emalength = [Link](100, title="EMA Length")
//Inputs
i_priceSrc = [Link] ( close, 'Price Source', group='Properties')
i_basisType = [Link] ( 'SMA', 'Basis Type', options=['SMA', 'EMA',
'VWMA'], group='Properties')
///////////////////////////////////////////////////////////////////////////////
// function declarations
f_maType ( _price, _len, _type ) =>
_type == 'SMA' ? [Link] ( _price, _len ) : _type == 'EMA' ? [Link] ( _price,
_len ) : [Link] ( _price, _len )
// use Heikin Ashi candle data to catch momentum
openHA = [Link]([Link]([Link]), [Link],
open)
closeHA = [Link]([Link]([Link]), [Link],
close)
highHA = [Link]([Link]([Link]), [Link],
high)
lowHA = [Link]([Link]([Link]), [Link],
low)
shadowless_rise = (openHA <= lowHA) ? 1 : 0
shadowless_fall = (openHA >= highHA) ? 1 : 0
emaline = [Link](close, emalength)
if emafilter and close < emaline
bull := 0
if emafilter and close > emaline
bear := 0
////////////////////////////////////////////////////////////////////////////////
//--------------------------NEURAL-NETRORK------------------------------------//
////////////////////////////////////////////////////////////////////////////////
price = plot(close, title='Close Line', color=[Link]([Link], 0),
display=[Link])
////////////////////////////////////////////////////////////////////////////////
//TREND INDICATORS▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
len111 = [Link](defval=200, minval=0, maxval=2000, title='Trend EMA Length',
group="Neural Network")
src111 = close
out111 = [Link](src111, len111)
ma111 = plot(out111, title='EMA 200', linewidth=5, color=[Link]([Link], 0),
offset=0)
mabuy = out111 > out111[1]
masell = out111 < out111[1]
//5 EMAs////////////////////////////////////////////////////////////////////////
len1 = 9
src1 = close
out11 = [Link](src1, len1)
ema11color = out11 > out11[1] ? #00bcd4 : #e91e63
ema11 = plot(out11, title='EMA 9', linewidth=3, color=[Link](ema11color, 50),
offset=0)
fill(price, ema11, title='EMA 9 Fill', color=[Link](ema11color, 90),
editable=true)
len2 = 21
src2 = close
out12 = [Link](src2, len2)
ema12color = out12 > out12[1] ? #00bcd4 : #e91e63
ema12 = plot(out12, title='EMA 21', linewidth=3, color=[Link](ema12color, 50),
offset=0)
fill(price, ema12, title='EMA 21 Fill', color=[Link](ema12color, 90),
editable=true)
len3 = 55
src3 = close
out13 = [Link](src3, len3)
ema13color = out13 > out13[1] ? #00bcd4 : #e91e63
ema13 = plot(out13, title='EMA 55', linewidth=3, color=[Link](ema13color, 50),
offset=0, display=[Link])
fill(price, ema13, title='EMA 55 Fill', color=[Link](ema13color, 90),
editable=true)
len4 = 100
src4 = close
out14 = [Link](src4, len4)
ema14color = out14 > out14[1] ? #00bcd4 : #e91e63
ema14 = plot(out14, title='EMA 100', linewidth=3, color=[Link](ema14color, 50),
offset=0, display=[Link])
fill(price, ema14, title='EMA 100 Fill', color=[Link](ema14color, 90),
editable=true)
len5 = 200
src5 = close
out15 = [Link](src5, len5)
ema15color = out15 > out15[1] ? #00bcd4 : #e91e63
ema15 = plot(out15, title='EMA 200', linewidth=3, color=[Link](ema15color, 50),
offset=0, display=[Link])
fill(price, ema15, title='EMA 200 Fill', color=[Link](ema15color, 90),
editable=true)
//HMA///////////////////////////////////////////////////////////////////////////
len6 = 100
src6 = close
hma = [Link](2 * [Link](src6, len6 / 2) - [Link](src6, len6),
[Link]([Link](len6)))
hmacolor = close > hma ? #00bcd4 : #e91e63
plot(hma, title='HMA Line', color=[Link](hmacolor, 25), linewidth=6)
///////////////////////////////////////////////////////////////////////
//-------------------------DASHBOARD---------------------------------//
//////////////////////////////////////////////////////////////////////
showDashboard = input(true, "Show Dashboard", group="TREND DASHBOARD")
locationDashboard = [Link]("Bottom Right", "Table Location", ["Top Right",
"Middle Right", "Bottom Right"], group="TREND DASHBOARD")
tableTextColor = input([Link], "Table Text Color", group="TREND DASHBOARD")
tableBgColor = input([Link], "Table Background Color", group="TREND
DASHBOARD")
sizeDashboard = [Link]("Normal", "Table Size", ["Large", "Normal",
"Small"], group="TREND DASHBOARD")
// Functions
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = [Link]([Link](x - x[1]), t)
smoothrng = [Link](avrng, wper) * m
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
: x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
percWidth(len, perc) => ([Link](len) - [Link](len)) * perc / 100
swingPoints(prd) =>
pivHi = [Link](prd, prd)
pivLo = [Link] (prd, prd)
last_pivHi = [Link](pivHi, pivHi, 1)
last_pivLo = [Link](pivLo, pivLo, 1)
hh = pivHi and pivHi > last_pivHi ? pivHi : na
lh = pivHi and pivHi < last_pivHi ? pivHi : na
hl = pivLo and pivLo > last_pivLo ? pivLo : na
ll = pivLo and pivLo < last_pivLo ? pivLo : na
[hh, lh, hl, ll]
f_kc(src, len, sensitivity) =>
basis = [Link](src, len)
span = [Link](len)
[basis + span * sensitivity, basis - span * sensitivity]
var dashboard_loc = locationDashboard == "Top Right" ? position.top_right :
locationDashboard == "Middle Right" ? position.middle_right : locationDashboard ==
"Bottom Right" ? position.bottom_right : locationDashboard == "Top Center" ?
position.top_center : locationDashboard == "Middle Center" ? position.middle_center
: locationDashboard == "Bottom Center" ? position.bottom_center : locationDashboard
== "Top Left" ? position.top_left : locationDashboard == "Middle Left" ?
position.middle_left : position.bottom_left
var dashboard_size = sizeDashboard == "Large" ? [Link] : sizeDashboard ==
"Normal" ? [Link] : sizeDashboard == "Small" ? [Link] : [Link]
var dashboard = showDashboard ? [Link](dashboard_loc, 2, 15, tableBgColor,
#000000, 2, tableBgColor, 1) : na
dashboard_cell(column, row, txt, signal=false) => [Link](dashboard, column,
row, txt, 0, 0, signal ? #ffffff : tableTextColor, text_size=dashboard_size)
dashboard_cell_bg(column, row, col) => table.cell_set_bgcolor(dashboard, column,
row, col)
if [Link] and showDashboard
dashboard_cell(0, 0 , "Market Info")
dashboard_cell(0, 1 , "Current Position")
dashboard_cell(0, 2 , "Current Trend")
dashboard_cell(0, 3 , "Timeframe")
dashboard_cell(0, 4 , "1 minute:")
dashboard_cell(0, 5 , "3 minutes:")
dashboard_cell(0, 6 , "5 minutes:")
dashboard_cell(0, 7 , "15 minutes:")
dashboard_cell(0, 8 , "30 minutes:")
dashboard_cell(0, 9, "1 Hour:")
dashboard_cell(0, 10, "2 Hours:")
dashboard_cell(0, 11, "4 Hours:")
dashboard_cell(0, 12, "Daily:")
dashboard_cell(1, 0 , "")
dashboard_cell(1, 1 , emaBull ? "Go Long" : "Go Short", true),
dashboard_cell_bg(1, 1, emaBull ? #009705 : #97000d)
dashboard_cell(1, 2 , emaBull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 2, emaBull ? #009705 : #97000d)
dashboard_cell(1, 3 , "Trends")
dashboard_cell(1, 4 , TF1Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 4 , TF1Bull ? #009705 : #97000d)
dashboard_cell(1, 5 , TF3Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 5 , TF3Bull ? #009705 : #97000d)
dashboard_cell(1, 6 , TF5Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 6 , TF5Bull ? #009705 : #97000d)
dashboard_cell(1, 7 , TF15Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 7 , TF15Bull ? #009705 : #97000d)
dashboard_cell(1, 8 , TF30Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 8 , TF30Bull ? #009705 : #97000d)
dashboard_cell(1, 9, TF60Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 9, TF60Bull ? #009705 : #97000d)
dashboard_cell(1, 10, TF120Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 10, TF120Bull ? #009705 : #97000d)
dashboard_cell(1, 11, TF240Bull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 11, TF240Bull ? #009705 : #97000d)
dashboard_cell(1, 12, TFDBull ? "Bullish" : "Bearish", true),
dashboard_cell_bg(1, 12, TFDBull ? #009705 : #97000d)
/////////////////////////////////////////////////////////////////////////////
// Alerts
// to automate put this in trendinview message:
{{[Link].alert_message}}
i_alert_txt_entry_long = input.text_area(defval = "", title = "Long Entry Message",
group = "Alerts")
i_alert_txt_entry_short = input.text_area(defval = "", title = "Short Entry
Message", group = "Alerts")
// Entries and Exits with TP/SL
if bull
//[Link]("Short" , alert_message = i_alert_txt_exit_short)
[Link]("Long" , [Link] , alert_message = i_alert_txt_entry_long)
[Link]("Close Long", from_entry = "Long", limit = tp1_y , stop = stop_y,
comment_loss = "Stop Loss", comment_profit = "Take Profit", qty_percent = 100)//,
alert_message = i_alert_txt_TP_long)
if bear
//[Link]("Long" , alert_message = i_alert_txt_exit_long)
[Link]("Short" , [Link], alert_message =
i_alert_txt_entry_short)
[Link]("Close Short", from_entry = "Short", limit = tp1_y , stop =
stop_y, comment_loss = "Stop Loss", comment_profit = "Take Profit", qty_percent =
100)//, alert_message = i_alert_txt_TP_short)
//WATERMARK
Watermark = [Link](position.bottom_left, 1, 4, border_width=3)
[Link](Watermark, 0, 0, text='SAIYANATOR v1.37',
text_color=[Link]([Link], 85), text_size=[Link])