Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
104 views15 pages

C# Code

The document outlines a trading strategy script that includes features for managing stop-loss and take-profit areas, utilizing various technical indicators such as EMA, SMA, and RSI. It defines conditions for bullish and bearish signals, as well as background color coding based on RSI levels. Additionally, the script incorporates risk management settings for trailing stops and take-profit levels, allowing for customizable trading parameters.

Uploaded by

andrewhany1599
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views15 pages

C# Code

The document outlines a trading strategy script that includes features for managing stop-loss and take-profit areas, utilizing various technical indicators such as EMA, SMA, and RSI. It defines conditions for bullish and bearish signals, as well as background color coding based on RSI levels. Additionally, the script incorporates risk management settings for trailing stops and take-profit levels, allowing for customizable trading parameters.

Uploaded by

andrewhany1599
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

riskmanage = input.

bool(false, "On/Off -Strength ",inline = '1', group="Enable


Stop-loss/take-profit areas")
tpstrength = input.float(1,"",tooltip = "" ,inline = '1',group="Enable Stop-
loss/take-profit areas",display = display.none)
TP1 = input.bool(true, "TP 1 ",inline = '2', group="Enable Stop-loss/take-
profit areas")
TP2 = input.bool(true, "TP 2 ",inline = '2', group="Enable Stop-loss/take-
profit areas")
TP3 = input.bool(true, "TP 3 ",inline = '2', group="Enable Stop-loss/take-
profit areas")

// MainMA
emaLength = ma_value//input.int(title="EMA Length", defval=200, minval=2)
emaSource = close

ema = ta.ema(emaSource, emaLength)


ema2 = ta.ema(emaSource, emaLength)
plot(moving_avarg? ema : na, color=close[1] > ema and close > ema ? color.green :
color.red, linewidth=4,editable = false)

// Signals
supertrend(_close, factor, atrLen) =>
atr = ta.atr(atrLen)
upperBand = _close + factor * atr
lowerBand = _close - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ?
lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ?
upperBand : prevUpperBand
int direction = na
float superTrend = na
prevSuperTrend = superTrend[1]
if na(atr[1])
direction := 2
else if prevSuperTrend == prevUpperBand
direction := close > upperBand ? -1 : 1
else
direction := close < lowerBand ? 1 : -1
superTrend := direction == -1 ? lowerBand : upperBand
[superTrend, direction]

sma1 = ta.sma(close, 8)
sma2 = ta.sma(close, 9)
sma3 = ta.sma(close, 13)
[supertrend, direction] = supertrend(open, sigsensiviti, factor)

Cbull = ta.crossover(close, supertrend) and close >= sma3


Cbear = ta.crossunder(close, supertrend) and close <= sma3

bull = ta.crossover(close, supertrend) and close >= sma3 and not(close[1] > ema and
close > ema)
bear = ta.crossunder(close, supertrend) and close <= sma3 and not(not(close[1] >
ema and close > ema))
Sbull = ta.crossover(close, supertrend) and close >= sma3 and (close[1] > ema and
close > ema)
Sbear = ta.crossunder(close, supertrend) and close <= sma3 and not(close[1] > ema
and close > ema)

countbull = ta.barssince(Cbull)
countbear = ta.barssince(Cbear)
Trigger = nz(countbull, bar_index) < nz(countbear, bar_index) ? 1 : 0

// bgcolor
// Define the RSI
rsi_Source = close
rsi_Period = 14
rsi_ = ta.rsi(rsi_Source, rsi_Period)

// Define thresholds for high and highest bullish/bearish levels


highBull = 75
highestBull = 85
highBear = 25
highestBear = 15

// Background color based on RSI level


bgColor = rsi_ > highestBull ? color.rgb(0, 255, 204, 86) : rsi_ > highBull ?
#00ffcc10 :rsi_ < highestBear ? #e43a7333 : rsi_ < highBear ? color.rgb(228, 58,
115, 93) : na // High Bear - Light Red

bgcolor(over_rsi?bgColor:na, title="RSI Background")

//Ema Cloud
ema150 = ta.ema(close, 150)
ema250 = ta.ema(close, 250)
fill(plot(trend_cloud? ema150: na, "", na, editable=false), plot(trend_cloud?
ema250: na, "", na, editable=false), ema150 > ema250 ? color.new(color.green, 70) :
ema150 < ema250 ? color.new(color.red, 70) : na)

// Bar color
src = close
len = 14//input.int(14, minval=1, title='RSI Length')
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)

tierLow = rsi <= 45


tierMid = rsi > 45 and rsi <= 55
tierHigh = rsi > 55

color colorVar = na
colorVar := tierLow ? #db3943 : // Red for low RSI
tierMid ? #9900CA : // Purple for mid RSI
tierHigh ? #30b56a : // Green for high RSI
na

if (tierMid)
colorVar := rsi <= 50 ? #9900ca : #b200ff // Gradient between the mid values

if (tierHigh)
colorVar := rsi <= 65 ? #30b56a : #30b56a // Gradient between high values

green = #00DD00
red = #DD0000
barcolor(candlestick_color?candletype=="CleanScalper"?colorVar:candletype=="Trend
Ribbon"?uptrend?green:red:close[1] > ema and close > ema ? green : red:na)

// Define finer tiers for a more nuanced gradient


tierLow1 = rsi <= 30
tierLow2 = rsi > 30 and rsi <= 45

tierHigh2 = rsi > 65 and rsi <= 75


tierHigh3 = rsi > 55

color upTier = na
color downTier = na
downTier := tierLow1 ? #e43a72 : tierLow2 ? #e6002352 : color(na)
upTier := tierHigh2 ? #00ffcc : tierHigh3 ? #00ffcc65 :color(na)

// Cloud
k_length = 0.425
uphighColor = #ca009e4d
uplowColor = #ca009e27

downhighColor = #b300ff2f
downlowColor = #9700ca10

[middleKC1, upperKC1, lowerKC1] = ta.kc(close, 10, 10.5*k_length)


[middleKC2, upperKC2, lowerKC2] = ta.kc(close, 10, 9.5*k_length)
[middleKC3, upperKC3, lowerKC3] = ta.kc(close, 10, 8*k_length)
[middleKC4, upperKC4, lowerKC4] = ta.kc(close, 10, 3*k_length)

middleLineValue = ta.ema((upperKC1 + lowerKC1) / 2, 50)


middleLineColor = middleLineValue > middleLineValue[1] ? color.green : color.red
middleLine = plot(middleLineValue, "Middle Line",
color=middleLineColor,linewidth=2, editable=false)

k1 = plot(ta.ema(upperKC1, 50), "", color = downTier,


style=plot.style_circles,linewidth = 2, editable=false)
k2 = plot(ta.ema(upperKC2, 50), "", na, editable=false)
k3 = plot(ta.ema(upperKC3, 50), "", na, editable=false)
k4 = plot(ta.ema(upperKC4, 50), "", na, editable=false)
k5 = plot(ta.ema(lowerKC4, 50), "", na, editable=false)
k6 = plot(ta.ema(lowerKC3, 50), "", na, editable=false)
k7 = plot(ta.ema(lowerKC2, 50), "", na, editable=false)
k8 = plot(ta.ema(lowerKC1, 50), "", color = upTier,
style=plot.style_circles,linewidth = 2, editable=false)
fill(k1, k2, uphighColor , editable=false)
fill(k2, k3, uplowColor , editable=false)
//fill(k3, k4, color.new(red2, 90) , editable=false)
//fill(k5, k6, color.new(green2, 90) , editable=false)
fill(k6, k7, downlowColor , editable=false)
fill(k7, k8, downhighColor, editable=false)

//Tralling Step

amplitude = 3//input(title='Amplitude', defval=2)


channelDeviation = 2//input(title='Channel Deviation', defval=2)
showArrows = false//input(title='Show Arrows', defval=true)
showChannels = false//input(title='Show Channels', defval=true)
src5 = close
len_a = 14

up_a = ta.rma(math.max(ta.change(src5), 0), len_a)

var int trend = 0


var int nextTrend = 0
var float maxLowPrice = nz(low[1], low)
var float minHighPrice = nz(high[1], high)

var float up1 = 0.0


var float down1 = 0.0
float atrHigh = 0.0
float atrLow = 0.0
float arrowUp = na
float arrowDown = na

atr2 = ta.atr(100) / 2
dev = channelDeviation * atr2

highPrice1 = high[math.abs(ta.highestbars(amplitude))]
lowPrice1 = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)

if nextTrend == 1
maxLowPrice := math.max(lowPrice1, maxLowPrice)

if highma < maxLowPrice and close < nz(low[1], low)


trend := 1
nextTrend := 0
minHighPrice := highPrice1
minHighPrice
else
minHighPrice := math.min(highPrice1, minHighPrice)

if lowma > minHighPrice and close > nz(high[1], high)


trend := 0
nextTrend := 1
maxLowPrice := lowPrice1
maxLowPrice

if trend == 0
if not na(trend[1]) and trend[1] != 0
up1 := na(down1[1]) ? down1 : down1[1]
arrowUp := up_a - atr2
arrowUp
else
up1 := na(up1[1]) ? maxLowPrice : math.max(maxLowPrice, up1[1])
up1
atrHigh := up1 + dev
atrLow := up1 - dev
atrLow
else
if not na(trend[1]) and trend[1] != 1
down1 := na(up1[1]) ? up1 : up1[1]
arrowDown := down1 + atr2
arrowDown
else
down1 := na(down1[1]) ? minHighPrice : math.min(minHighPrice, down1[1])
down1
atrHigh := down1 + dev
atrLow := down1 - dev
atrLow

ht = trend == 0 ? up1 : down1

var color buyColor = #00e73a


var color sellColor = color.red

htColor = trend == 0 ? buyColor : sellColor


htPlot = plot(chaos_trend?ht:na, title='ATR', linewidth=2, color=htColor,editable =
false)

// Risk Manager
tpLabels(tp,bull,bear) =>
rsi1 = ta.rsi(close, 14)
countBull = ta.barssince(bull)
countBear = ta.barssince(bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
tp1Bull = ta.crossover (rsi1, 70), tp2Bull = ta.crossover (rsi1, 85), tp3Bull =
ta.crossover (rsi1, 100)
tp1Bear = ta.crossunder(rsi1, 30), tp2Bear = ta.crossunder(rsi1, 15), tp3Bear =
ta.crossunder(rsi1, 5)
tp1Bull := tp1Bull and (nz(ta.barssince(tp1Bull)[1], 9999) > countBull),
tp2Bull := tp2Bull and (ta.barssince(tp1Bull)[1] <= countBull), tp2Bull := tp2Bull
and (nz(ta.barssince(tp2Bull)[1], 9999) > countBull), tp3Bull := tp3Bull and
(ta.barssince(tp2Bull)[1] <= countBull), tp3Bull := tp3Bull and
(nz(ta.barssince(tp3Bull)[1], 9999) > countBull)
tp1Bear := tp1Bear and (nz(ta.barssince(tp1Bear)[1], 9999) > countBear),
tp2Bear := tp2Bear and (ta.barssince(tp1Bear)[1] <= countBear), tp2Bear := tp2Bear
and (nz(ta.barssince(tp2Bear)[1], 9999) > countBear), tp3Bear := tp3Bear and
(ta.barssince(tp2Bear)[1] <= countBear), tp3Bear := tp3Bear and
(nz(ta.barssince(tp3Bear)[1], 9999) > countBear)
trigger ? (tp == 1 ? tp1Bull : tp == 2 ? tp2Bull : tp3Bull) : (tp == 1 ?
tp1Bear : tp == 2 ? tp2Bear : tp3Bear)

plotshape(TP1 and tpLabels(1,Cbull,Cbear) and Trigger, "TP", shape.xcross,


location.abovebar, color.green , 0, "TP1", color.green , false,size = size.tiny)
plotshape(TP2 and tpLabels(2,Cbull,Cbear) and Trigger, "TP", shape.xcross,
location.abovebar, color.green , 0, "TP2", color.green , false,size =
size.tiny)
plotshape(TP3 and tpLabels(3,Cbull,Cbear) and Trigger, "TP", shape.xcross,
location.abovebar, color.green , 0, "TP3", color.green , false,size =
size.tiny)
plotshape(TP1 and tpLabels(1,Cbull,Cbear) and not Trigger, "TP", shape.xcross,
location.belowbar, color.red, 0, "TP1", color.red, false,size = size.tiny)
plotshape(TP2 and tpLabels(2,Cbull,Cbear) and not Trigger, "TP", shape.xcross,
location.belowbar, color.red, 0, "TP2", color.red, false,size = size.tiny)
plotshape(TP3 and tpLabels(3,Cbull,Cbear) and not Trigger, "TP", shape.xcross,
location.belowbar, color.red, 0, "TP3", color.red, false,size = size.tiny)

_bull = Cbull
_bear = Cbear

risksteps = tpstrength

percTrailingSL = 1//input.float(1, "", 0, step=0.1, inline="2", group="RISK


MANAGEMENT SETTINGS")
usePercSL = false//input(false, "% Trailing sl", inline="2", group="RISK
MANAGEMENT SETTINGS")

enableTpSlAreas = riskmanage

useTP1 = true //input(true, "", inline="4", group="RISK MANAGEMENT


SETTINGS")
multTP1 = risksteps //input.float(1, "TP 1", 0, inline="4", group="RISK
MANAGEMENT SETTINGS")
useTP2 = true//input(true, "", inline="5", group="RISK MANAGEMENT
SETTINGS")
multTP2 = risksteps * 2 //input.float(2, "TP 2", 0, inline="5",
group="RISK MANAGEMENT SETTINGS")
useTP3 = true //input(true, "", inline="6", group="RISK MANAGEMENT
SETTINGS")
multTP3 = risksteps * 3 //input.float(3, "TP 3", 0, inline="6",
group="RISK MANAGEMENT SETTINGS")
tpLabels = true //input(true, "Take profit labels", group="RISK MANAGEMENT
SETTINGS")

// Plots
none = close > 0
countBull = ta.barssince(_bull)
countBear = ta.barssince(_bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
atrBand = usePercSL ? (trigger ? low : high) * (percTrailingSL / 100) : ta.atr(14)
* 2.2
atrStop = trigger ? low - atrBand : high + atrBand
lastTrade(src) => ta.valuewhen(_bull or _bear, src, 0)
entry_y = lastTrade(close)
stop_y = lastTrade(atrStop)
tp1_y = (entry_y-lastTrade(atrStop))*multTP1 + entry_y
tp2_y = (entry_y-lastTrade(atrStop))*multTP2 + entry_y
tp3_y = (entry_y-lastTrade(atrStop))*multTP3 + entry_y
labelTpSl(cond, y, txt, color) =>
label labelTpSl = enableTpSlAreas and cond ? label.new(bar_index + 1, y, txt,
xloc.bar_index, yloc.price, color, label.style_label_left, color.white,
size.normal) : na
label.delete(labelTpSl[1])
labelTpSl(none, entry_y, "Entry : " + str.tostring(math.round_to_mintick(entry_y)),
color.orange)
labelTpSl(none, stop_y , "Stop loss : " +
str.tostring(math.round_to_mintick(atrStop)), color.red)
labelTpSl(useTP1 and multTP1 != 0, tp1_y, "TP 1 : " +
str.tostring(math.round_to_mintick(tp1_y)), color.green)
labelTpSl(useTP2 and multTP2 != 0, tp2_y, "TP 2 : " +
str.tostring(math.round_to_mintick(tp2_y)), color.green)
labelTpSl(useTP3 and multTP3 != 0, tp3_y, "TP 3 : " +
str.tostring(math.round_to_mintick(tp3_y)), color.green)
lineTpSl(cond, y, color, style) =>
line lineTpSl = enableTpSlAreas and cond ? line.new(bar_index - (trigger ?
countBull : countBear), y, bar_index + 1, y, xloc.bar_index, extend.none, color,
style) : na
line.delete(lineTpSl[1])
lineTpSl(none, entry_y, color.orange, line.style_dashed)
lineTpSl(none, stop_y , color.red , line.style_solid )
lineTpSl(useTP1 and multTP1 != 0, tp1_y, color.green, line.style_dotted)
lineTpSl(useTP2 and multTP2 != 0, tp2_y, color.green, line.style_dotted)
lineTpSl(useTP3 and multTP3 != 0, tp3_y, color.green, line.style_dotted)

// Reversal Signals
enableReversal = true
ReversalInputs = 25//input.int(14, minval=1, title="Reversals Sensitivity",
group="Reversal Settings")
overbought = 75//input(75, 'Reversal Down Level', group='Reversal Settings')
oversold = 25//input(25, 'Reversal Up Level', group='Reversal Settings')

upwardd = ta.rma(math.max(ta.change(close), 0), ReversalInputs)


dnwardd = ta.rma(-math.min(ta.change(close), 0), ReversalInputs)
source = dnwardd == 0 ? 100 : upwardd == 0 ? 0 : 100 - (100 / (1 + upwardd /
dnwardd))

revdn = ta.crossunder(source, overbought) and enableReversal


revup = ta.crossover(source, oversold) and enableReversal

plotshape(revup, 'Reversal Up Signal', shape.labelup, location.belowbar, #b300ff4f,


text='', size=size.small, textcolor=color.white)
plotshape(revdn, 'Reversal Down Signal', shape.labeldown, location.abovebar,
#ca009e79, text='', size=size.small, textcolor=color.white)

//Channel Breakouts
bars = 3//input(3)
checkbox = true//input.bool(title= "color chenging by trend", defval=true)

ph = ta.pivothigh(high, bars, bars)


pl = ta.pivotlow(low, bars, bars)

//INIT VARIABLES
var int ph_uptrend_flag = 0
var float ph_valid1 = 0
var float ph_valid2 = 0
var float ph_valid3 = 0
var float ph_valid4 = 0
var float ph_valid5 = 0
var float pl_valid1 = 0
var float pl_valid2 = 0
var float pl_valid3 = 0
var float pl_valid4 = 0
var float pl_valid5 = 0

var float ph_valid_old = 2


var float pl_valid_old = 0 // non meaningful number here for initialization
only

ph_non_na = nz(ph, 0)
pl_non_na = nz(pl, 0) // stores 0's instead of na's for non-pivot-pointed bars

if ph_non_na != 0
ph_valid5 := ph_valid4
ph_valid4 := ph_valid3
ph_valid3 := ph_valid2
ph_valid2 := ph_valid1
ph_valid1 := ph_non_na
ph_valid_old := ph_valid1
ph_valid_old
else
ph_valid1 := ph_valid1
ph_valid_old := ph_valid_old
ph_valid_old

if pl_non_na != 0
pl_valid5 := pl_valid4
pl_valid4 := pl_valid3
pl_valid3 := pl_valid2
pl_valid2 := pl_valid1
pl_valid1 := pl_non_na
pl_valid_old := pl_valid1
pl_valid_old
else
pl_valid1 := pl_valid1
pl_valid_old := pl_valid_old
pl_valid_old

max = math.max(ph_valid2, ph_valid1)


min = math.min(pl_valid2, pl_valid1)

avg = math.avg(ph_valid2, ph_valid1,pl_valid2, pl_valid1)

plot(c_break ?max:na, title = "High level", color=green)


plot(c_break ?min:na, title = "Low level", color=red)
//plot(avg, title = "Mid level", color=color.yellow )

// OrderBlock
box_ob = order_blocks//input.bool(false, "Toggle Order Block",
group="ORDER BLOCK")
box_hide_gray = true//input.bool(false, "Hide gray boxes", group="ORDER BLOCK")
bos_type = "High and Low"//input.string("High and Low", "MSB trigger",
["High and Low", "Close and Open"], group="ORDER BLOCK")
box_sv = true//input.bool(true, "Plot demand boxes", group="ORDER
BLOCK")
box_test_delay = 3//input.int(3, "Delay to count test of demand box", 1,
group="ORDER BLOCK")
box_fill_delay = 3//input.int(3, "Delay to count fill of demand box", 1,
group="ORDER BLOCK")
box_test_sv = true//input.bool(true, "Dim tested demand boxes", group="ORDER
BLOCK")
box_stop_sv = true//input.bool(true, "Stop plotting filled demand boxes",
group="ORDER BLOCK")

You might also like