0% found this document useful (1 vote)
3K views7 pages

1 Mins Strategy

This document contains the source code for a Pine Script trading strategy. It defines inputs for sensitivity levels, moving averages, and other parameters. It then establishes signals and exit conditions for long and short trades based on crossovers of a moving average and customized indicators. Profits and losses are calculated using adjustable stop loss and take profit levels based on ATR or percentages.

Uploaded by

shivus7337069
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 (1 vote)
3K views7 pages

1 Mins Strategy

This document contains the source code for a Pine Script trading strategy. It defines inputs for sensitivity levels, moving averages, and other parameters. It then establishes signals and exit conditions for long and short trades based on crossovers of a moving average and customized indicators. Profits and losses are calculated using adjustable stop loss and take profit levels based on ATR or percentages.

Uploaded by

shivus7337069
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/ 7

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © Samuel Ralte

//@version=5
strategy("1 mins strategy ", overlay=true)

// Inputs
group0 = "Sell Sensitivity"
a = input(2, title="Sensitivity", group = group0)
c = input(1, title='ATR Period',group = group0)
h = true

xATR = ta.atr(c)
nLoss = a * xATR

src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,


close, lookahead=barmerge.lookahead_off) : close

xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ?
math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] >
nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2

pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ?
-1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1
: iff_3

xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue

emaa = ta.ema(src, 1)

s1 = src < xATRTrailingStop and ta.crossover(xATRTrailingStop, emaa)

///////////////////////////////////

group1 = "Buy Sensitivity"


a1 = input(2, title= "Sensitivity", group = group1)
c1 = input(300, title='ATR Period 1',group = group1)
h1 = true
xATR1 = ta.atr(c1)
nLoss1 = a1 * xATR1

src1 = h1 ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,


close, lookahead=barmerge.lookahead_off) : close

xATRTrailingStop1 = 0.0
iff_11 = src1 > nz(xATRTrailingStop1[1], 0) ? src1 - nLoss1 : src1 + nLoss1
iff_21 = src1 < nz(xATRTrailingStop1[1], 0) and src1[1] < nz(xATRTrailingStop1[1],
0) ? math.min(nz(xATRTrailingStop1[1]), src1 + nLoss1) : iff_11
xATRTrailingStop1 := src1 > nz(xATRTrailingStop1[1], 0) and src1[1] >
nz(xATRTrailingStop1[1], 0) ? math.max(nz(xATRTrailingStop1[1]), src1 - nLoss1) :
iff_21

pos1 = 0
iff_31 = src1[1] > nz(xATRTrailingStop1[1], 0) and src1 < nz(xATRTrailingStop1[1],
0) ? -1 : nz(pos1[1], 0)
pos1 := src1[1] < nz(xATRTrailingStop1[1], 0) and src1 > nz(xATRTrailingStop1[1],
0) ? 1 : iff_31

xcolor1 = pos1 == -1 ? color.red : pos1 == 1 ? color.green : color.blue

ema1aa = ta.ema(src1, 1)

b1 = src1 > xATRTrailingStop1 and ta.crossover(ema1aa, xATRTrailingStop1)

// //////////////////////////////
EEEEEE = 80
BBBB = 27
BBBBB = 50

AAAA(BBB, BBBB, BBBBB) =>


fastMA = ta.ema(BBB, BBBB)
slowMA = ta.ema(BBB, BBBBB)
AAAA = fastMA - slowMA
AAAA

AAAAA(EEEEEE, BBBB, BBBBB) =>


AAA = 0.5 // input(0.5)
var CCCCC = 0.0
var DDD = 0.0
var DDDDDD = 0.0
var EEEEE = 0.0
BBBBBB = AAAA(close, BBBB, BBBBB)
CCC = ta.lowest(BBBBBB, EEEEEE)
CCCC = ta.highest(BBBBBB, EEEEEE) - CCC
CCCCC := CCCC > 0 ? (BBBBBB - CCC) / CCCC * 100 : nz(CCCCC[1])
DDD := na(DDD[1]) ? CCCCC : DDD[1] + AAA * (CCCCC - DDD[1])
DDDD = ta.lowest(DDD, EEEEEE)
DDDDD = ta.highest(DDD, EEEEEE) - DDDD
DDDDDD := DDDDD > 0 ? (DDD - DDDD) / DDDDD * 100 : nz(DDDDDD[1])
EEEEE := na(EEEEE[1]) ? DDDDDD : EEEEE[1] + AAA * (DDDDDD - EEEEE[1])
EEEEE

mAAAAA = AAAAA(EEEEEE, BBBB, BBBBB)


mColor = mAAAAA > mAAAAA[1] ? color.new(color.green, 20) : color.new(color.red, 20)

if mAAAAA[3] <= mAAAAA[2] and mAAAAA[2] > mAAAAA[1] and mAAAAA > 75
alert('Red', alert.freq_once_per_bar)
if mAAAAA[3] >= mAAAAA[2] and mAAAAA[2] < mAAAAA[1] and mAAAAA < 25
alert('Green', alert.freq_once_per_bar)

// EMA Filter
// i_emaFilterEnabled = input.bool(defval = true , title = "Enable EMA Filter")
i_emaSource = input.source(close,"EMA Source" ,group = "Moving Average")
ema_type = input.string("EMA", title="Choose MA type", options=["EMA", "SMA",
"WMA","Zero_Lag_Tema","Zero_Lag_EMA"])
ema_length = input(30, "Length")
enable_plot = input(false,title = "Plot MA")
// colorbars = input.bool(true, "Color bars?", group="UI Options")
ema_color = color.lime
ema = 0.0
zlagtema(float src, simple int len) =>
float ema1 = ta.ema(src, len)
float ema2 = ta.ema(ema1, len)
float ema3 = ta.ema(ema2, len)
float out = 3 * (ema1 - ema2) + ema3
float ema1a = ta.ema(out, len)
float ema2a = ta.ema(ema1a, len)
float ema3a = ta.ema(ema2a, len)
float outf = 3 * (ema1a - ema2a) + ema3a
outf
lag = math.floor((ema_length - 1) / 2)

if ema_type == "SMA"
ema := ta.sma(i_emaSource, ema_length)
else if ema_type == "WMA"
ema := ta.wma(i_emaSource, ema_length)
else if ema_type == "EMA"
ema := ta.ema(i_emaSource, ema_length)
else if ema_type == "Zero_Lag_Tema"
ema := zlagtema(i_emaSource, ema_length)
else if ema_type == "Zero_Lag_EMA"
ema := ta.ema(close + close - close[lag], ema_length)

// barcolor(colorbars ? close > ema ? color.new(color.lime, 0) : close < ema ?


color.new(color.red, 0) : na : na)

plot(enable_plot ? ema : na, title="EMA", color=ema_color, linewidth=2)


bool abovema = close > ema
bool belowma = close < ema

long = b1 and mAAAAA > mAAAAA[1] and mAAAAA < 25 and abovema
short = s1 and mAAAAA < mAAAAA[1] and mAAAAA > 75 and belowma
plotshape(long,title = "Buy",style = shape.labelup,location =
location.belowbar,text = "Long",textcolor = color.white,color = color.teal,size =
size.small)
plotshape(short,title = "Sell",style = shape.labeldown,location =
location.abovebar,text = "Short",textcolor = color.white,color = color.maroon,size
= size.small)

// Start / End Time Periods


i_startPeriodEnabled = input.bool(true, 'Start', group='Date Range', inline='Start
Period')
i_startPeriodTime = input.time(timestamp('12 Jun 2023'), '', group='Date Range',
inline='Start Period')
i_endPeriodEnabled = input.bool(true, 'End', group='Date Range', inline='End
Period')
i_endPeriodTime = input.time(timestamp('31 Dec 2030'), '', group='Date Range',
inline='End Period')

isStartPeriodEnabledAndInRange = i_startPeriodEnabled ? i_startPeriodTime <= time :


true
isEndPeriodEnabledAndInRange = i_endPeriodEnabled ? i_endPeriodTime >= time : true
isStartEndPeriodsAndTimeInRange = isStartPeriodEnabledAndInRange and
isEndPeriodEnabledAndInRange

// Leverage
leverage = input.float(2.0, "Leverage", minval=1.0, maxval=20.0,group="Leverage")
// Input options
riskPercentage = input.float(100, "Risk Percentage", minval=1, maxval=100)

if isStartEndPeriodsAndTimeInRange
if long
strategy.entry('LONG', strategy.long, qty=(strategy.equity * riskPercentage
/ 100 / close) * leverage)
if short
strategy.entry('SHORT', strategy.short, qty=(strategy.equity *
riskPercentage / 100 / close) * leverage)
// Input options
atrValue = ta.atr(14)
stopLossType = input.string("ATR", "Stop Loss Type", options=["ATR",
"Percent"],group = "STOP LOSS/TAKE PROFIT TYPE")
takeProfitType = input.string("R/R","Take Profit Type",options = ["R/R","Percent"])
// TP/SL PERCENT

// TP/SL ATR
atrMultiplier = input(2.5, "Atr Stoploss", group="Stoploss/Take Profit")
stopPer = input(1.0, title="Stop Loss %") / 100

// TP/SL
takeProfitMultiple = input(1.0, title='Risk/Rward',group = "TAKE PROFIT")
takePer1 = input(2.5, title='Take Profit 1 %') / 100
useTwoTakeProfit = input.bool(true, "Use Take Profits 2")
takePer2 = input(3.0, title='Take Profit 2 %') / 100
takeProfitpercent1 = input.float(30, "Take Profit 1 Percent",tooltip = "Will only
effect if take profit 2 is enable")

// Calculate stop loss levels


var float stopLossLong = na
var float stopLossShort = na

if stopLossType == "ATR"
stopLossLong := strategy.position_avg_price - (atrValue * atrMultiplier)
stopLossShort := strategy.position_avg_price + (atrValue * atrMultiplier)

else
stopLossLong := strategy.position_avg_price * (1 - stopPer)
stopLossShort := strategy.position_avg_price * (1 + stopPer)

// Determine stop loss and take profit levels


stopLoss = atrMultiplier * atrValue
takeProfit = takeProfitMultiple * stopLoss

// Determine where you've entered and in what direction


var float shortTake1 = na
var float longTake1 = na
var float shortTake2 = na
var float longTake2 = na
var float shortTake12 = na
var float longTake12 = na
var float shortTake4 = na
var float longTake4 = na

if takeProfitType == "R/R"
shortTake1 := strategy.position_avg_price - takeProfit
longTake1 := strategy.position_avg_price + takeProfit
else
shortTake12 := strategy.position_avg_price * (1 - takePer1)
longTake12 := strategy.position_avg_price * (1 + takePer1)
shortTake4 := strategy.position_avg_price * (1 - takePer2)
longTake4 := strategy.position_avg_price * (1 + takePer2)

// Input quantity for take profit 1


takeProfitpercent = useTwoTakeProfit ? strategy.position_size *
takeProfitpercent1 / 100 : strategy.position_size

// Exit conditions
if strategy.position_size > 0
if takeProfitType == "R/R"
strategy.exit("tp/sl_1", stop=stopLossLong, limit=longTake1)
if useTwoTakeProfit
strategy.exit("tp/sl_2", stop=stopLossLong, limit=longTake4)

else
strategy.exit("tp/sl_1", stop=stopLossLong, limit=longTake12,
qty=takeProfitpercent)
if useTwoTakeProfit
strategy.exit("tp/sl_2", stop=stopLossLong, limit=longTake4)
if strategy.position_size < 0
if takeProfitType == "R/R"
strategy.exit("tp/sl_1", stop=stopLossShort, limit=shortTake1)
if useTwoTakeProfit
strategy.exit("tp/sl_2", stop=stopLossShort, limit=shortTake4)
else
strategy.exit("tp/sl_1", stop=stopLossShort, limit=shortTake12,
qty=takeProfitpercent)
if useTwoTakeProfit
strategy.exit("tp/sl_2", stop=stopLossShort, limit=shortTake4)

// Dashboard Table Text Size


i_tableTextSize = input.string(title="Dashboard Size", defval="Auto",
options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Dashboards")
table_text_size(s) =>
switch s
"Auto" => size.auto
"Huge" => size.huge
"Large" => size.large
"Normal" => size.normal
"Small" => size.small
=> size.tiny
tableTextSize = table_text_size(i_tableTextSize)

// Dashboard

i_showDashboard = input.bool(title="Show Dashboard", defval=true,


group="Dashboards", inline="Show Dashboards")

f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) =>


_cellText = _title + "\n" + _value
table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor,
text_color=_txtcolor, text_size=tableTextSize)
// Draw dashboard table
if i_showDashboard
var bgcolor = color.new(color.gray,100)

// Keep track of Wins/Losses streaks


newWin = (strategy.wintrades > strategy.wintrades[1]) and
(strategy.losstrades == strategy.losstrades[1]) and (strategy.eventrades ==
strategy.eventrades[1])
newLoss = (strategy.wintrades == strategy.wintrades[1]) and
(strategy.losstrades > strategy.losstrades[1]) and (strategy.eventrades ==
strategy.eventrades[1])

varip int winRow = 0


varip int lossRow = 0
varip int maxWinRow = 0
varip int maxLossRow = 0

if newWin
lossRow := 0
winRow := winRow + 1
if winRow > maxWinRow
maxWinRow := winRow

if newLoss
winRow := 0
lossRow := lossRow + 1
if lossRow > maxLossRow
maxLossRow := lossRow

// Prepare stats table


var table dashTable = table.new(position.top_right, 15, 3, border_width=2)

if barstate.islastconfirmedhistory
// Update table
lastTime = strategy.position_size == 0 ?
strategy.closedtrades.exit_time(strategy.closedtrades-1) : time
dollarReturn = strategy.netprofit
f_fillCell(dashTable, 0, 2, "Start Trade:", str.format("{0,date,long}",
strategy.closedtrades.entry_time(0)) , bgcolor, color.new(color.white,0))
// f_fillCell(dashTable, 1, 2, "End:", str.format("{0,date,long}",
lastTime) , bgcolor, color.white)
_profit = (strategy.netprofit / strategy.initial_capital) * 100
f_fillCell(dashTable, 2, 2, "Net Profit:", str.tostring(_profit, '##.##') +
"%", color.new(color.black,100), _profit > 0 ? color.green : color.red)
_numOfDaysInStrategy = (lastTime - strategy.closedtrades.entry_time(0)) /
(1000 * 3600 * 24)
f_fillCell(dashTable, 3, 2, "Percent Per Day", str.tostring(_profit /
_numOfDaysInStrategy, '#########################.#####')+"%",
color.new(color.black,100), _profit > 0 ? color.green : color.red)
_winRate = ( strategy.wintrades / strategy.closedtrades ) * 100
f_fillCell(dashTable, 4, 2, "Percent Profitable:", str.tostring(_winRate,
'##.##') + "%", color.new(color.black,100), _winRate < 50 ? color.red : _winRate <
75 ? #999900 : color.green)
f_fillCell(dashTable, 5, 2, "Profit Factor:",
str.tostring(strategy.grossprofit / strategy.grossloss, '##.###'),
color.new(color.black,100),strategy.grossprofit > strategy.grossloss ?
color.green : color.red)
f_fillCell(dashTable, 6, 2, "Total Trades:",
str.tostring(strategy.closedtrades), bgcolor, color.white)
f_fillCell(dashTable, 7, 2, "Winning Trades:",
str.tostring(strategy.wintrades),color.new(color.black,100), strategy.wintrades >
strategy.losstrades ? color.green : color.red)
f_fillCell(dashTable, 8, 2, "Losing Trades:",
str.tostring(strategy.losstrades),color.new(color.black,100),strategy.wintrades <
strategy.losstrades ? color.green : color.red)
f_fillCell(dashTable, 9, 2, "Max Wins In A Row:", str.tostring(maxWinRow,
'######') , bgcolor,maxWinRow > maxLossRow ? color.rgb(13, 196, 242) : color.red)
f_fillCell(dashTable, 10, 2, "Max Losses In A Row:",
str.tostring(maxLossRow, '######') , bgcolor, color.rgb(142, 147, 166))

You might also like