0110 Bot
0110 Bot
0110 Bot
0 at
https://mozilla.org/MPL/2.0/
// © wielkieef
//@version=4
//SOURCE
===================================================================================
===================================================================================
===================================================================================
====================================================
src = input(open,
title=" Source")
// Inputs
===================================================================================
===================================================================================
===================================================================================
===============================================
//ADX
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------
ADX_options = input("MASANAKAMURA",
title=" Adx Option", options = ["CLASSIC",
"MASANAKAMURA"], group = "ADX")
ADX_len = input(21,
title=" Adx Lenght", type = input.integer,
minval = 1, group = "ADX")
th = input(20,
title=" Adx Treshold", type = input.float,
minval = 0, step = 0.5, group = "ADX")
//
MACD-------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------
fast_length = input(15,
title=" Fast Length", type=input.integer,
group="MACD")
slow_length = input(16,
title=" Slow Length", type=input.integer,
group="MACD")
signal_length = input(26,
title=" Signal Smoothing", type=input.integer,
group="MACD")
// Range Filter
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------
per_ = input(15,
title=" Period", minval=1,
group = "Range Filter")
mult = input(2.6,
title=" mult.", minval=0.1, step =
0.1, group = "Range
Filter")
//
SAR--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------------
SHOW_SAR = input(true,
title="Show Parabolic SAR",
group="SAR")
Sst = input (0.5,
title=" Sar Start", step=0.01, minval =
0.01, group="SAR")
Sinc = input (0.2,
title=" Sar Int", step=0.01, minval =
0.01, group="SAR")
Smax = input (0.4,
title=" Sar Max", step=0.01, minval =
0.01, group="SAR")
// Volume
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------
volume_f = input(3.2,
title=" Volume mult.", minval = 0, step =
0.1, group="Volume")
sma_length = input(20,
title=" Volume lenght", minval = 1,
group="Volume")
volume_f1 = input(1.9,
title=" Volume mult. 1", minval = 0, step
= 0.1, group="Volume")
sma_length1 = input(22,
title=" Volume lenght 1", minval = 1,
group="Volume")
//BOLINGER BANDS
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------
SHOW_BB = input(true,
title="Show Bollinger Bands",
group="Bolinger Bands")
tf2 = input("",
title=" Timeframe 2", type =
input.resolution,
group="Bolinger Bands")
src2 = input(high,
title=" Source 2", type = input.source,
group="Bolinger Bands")
//TP PLOTSHAPE
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------
tp_long0 = input(0.9,
title=" % TP Long", type = input.float,
minval = 0, step = 0.1, group="Target Point")
tp_short0 = input(0.9,
title=" % TP Short", type = input.float,
minval = 0, step = 0.1, group="Target Point")
// SL PLOTSHAPE
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------
sl0 = input(4.2,
title=" % Stop loss", type = input.float,
minval = 0, step = 0.1, group="Stop Loss")
//INDICATORS
===================================================================================
===================================================================================
===================================================================================
==============================================
//
ADX--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------------------
calcADX(_len) =>
up =
change(high)
down =
-change(low)
plusDM =
na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM =
na(down) ? na : (down > up and down > 0 ? down : 0)
truerange =
rma(tr, _len)
_plus =
fixnan(100 * rma(plusDM, _len) / truerange)
_minus =
fixnan(100 * rma(minusDM, _len) / truerange)
sum =
_plus + _minus
_adx =
100 * rma(abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)
[_plus,_minus,_adx]
calcADX_Masanakamura(_len) =>
SmoothedTrueRange =
0.0
SmoothedDirectionalMovementPlus =
0.0
SmoothedDirectionalMovementMinus =
0.0
TrueRange =
max(max(high - low, abs(high - nz(close[1]))), abs(low - nz(close[1])))
DirectionalMovementPlus =
high - nz(high[1]) > nz(low[1]) - low ? max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus =
nz(low[1]) - low > high - nz(high[1]) ? max(nz(low[1]) - low, 0) : 0
SmoothedTrueRange :=
nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1]) /_len) + TrueRange
SmoothedDirectionalMovementPlus :=
nz(SmoothedDirectionalMovementPlus[1]) -
(nz(SmoothedDirectionalMovementPlus[1]) / _len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus :=
nz(SmoothedDirectionalMovementMinus[1]) -
(nz(SmoothedDirectionalMovementMinus[1]) / _len) + DirectionalMovementMinus
DIP =
SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIM =
SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX =
abs(DIP-DIM) / (DIP+DIM)*100
adx =
sma(DX, _len)
[DIP,DIM,adx]
[DIPlusC,DIMinusC,ADXC] =
calcADX(ADX_len)
[DIPlusM,DIMinusM,ADXM] =
calcADX_Masanakamura(ADX_len)
DIPlus =
ADX_options == "CLASSIC" ? DIPlusC : DIPlusM
DIMinus =
ADX_options == "CLASSIC" ? DIMinusC : DIMinusM
ADX =
ADX_options == "CLASSIC" ? ADXC : ADXM
L_adx =
DIPlus > DIMinus and ADX > th
S_adx =
DIPlus < DIMinus and ADX > th
//
MACD-------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------------
fast_ma =
ema(src, fast_length)
slow_ma =
ema(src, slow_length)
macd =
fast_ma - slow_ma
signal_ =
sma(macd, signal_length)
L_macd =
macd > signal_
S_macd =
macd < signal_
// Range Filter
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------
//
SAR--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
SAR =
sar(Sst, Sinc, Smax)
L_sar =
(SAR < close)
S_sar =
(SAR > close)
// Volume
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------
Volume_condt =
volume > sma(volume,sma_length)*volume_f
Volume_condt1 =
volume > sma(volume,sma_length1)*volume_f1
// BOLINGER BADNS
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------------------
per2 =
input(20)
dev2 =
input(3.0)
ma2 =
security(syminfo.tickerid, tf2, sma(src2, per2))
hb2 =
ma2 + security(syminfo.tickerid, tf2, stdev(src2, per2)) * dev2
lb2 =
ma2 - security(syminfo.tickerid, tf2, stdev(src2, per2)) * dev2
//STRATEGY
===================================================================================
===================================================================================
===================================================================================
=================================================
L_1 =
L_BB_CROSS and Volume_condt and L_RF
S_1 =
S_BB_CROSS and Volume_condt and S_RF
L_2 =
L_adx and L_RF and L_macd and L_sar and Volume_condt1
S_2 =
S_adx and S_RF and S_macd and S_sar and Volume_condt1
longCond :=
L_basic_condt
shortCond :=
S_basic_condt
CondIni_long := longCond[1] ? 1 :
shortCond[1] ? -1 : nz(CondIni_long[1]
)
CondIni_short := longCond[1] ? 1 :
shortCond[1] ? -1 : nz(CondIni_short[1]
)
longCondition = (longCond[1] and
nz(CondIni_long[1]) == -1 )
shortCondition = (shortCond[1] and
nz(CondIni_short[1]) == 1 )
//POSITION
PRICE------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------
last_open_longCondition := longCondition or
Final_long_BB[1] ? close[1] : nz(last_open_longCondition[1]
)
last_open_shortCondition := shortCondition or
Final_short_BB[1] ? close[1] : nz(last_open_shortCondition[1]
)
last_longCondition := longCondition or
Final_long_BB[1] ? time : nz(last_longCondition[1]
)
last_shortCondition := shortCondition or
Final_short_BB[1] ? time : nz(last_shortCondition[1]
)
in_longCondition = last_longCondition >
last_shortCondition
in_shortCondition = last_shortCondition >
last_longCondition
last_Final_longCondition := longCondition ?
time :
nz(last_Final_longCondition[1] )
last_Final_shortCondition := shortCondition ?
time :
nz(last_Final_shortCondition[1] )
nLongs := nz(nLongs[1]
)
nShorts := nz(nShorts[1]
)
if longCondition or Final_long_BB
nLongs := nLongs + 1
nShorts := 0
sum_long := nz(last_open_longCondition) +
nz(sum_long[1])
sum_short := 0.0
if shortCondition or Final_short_BB
nLongs := 0
nShorts := nShorts + 1
sum_short := nz(last_open_shortCondition)+
nz(sum_short[1])
sum_long := 0.0
Position_Price := nz(Position_Price[1])
Position_Price := longCondition or
Final_long_BB ? sum_long/nLongs : shortCondition or
Final_short_BB ? sum_short/nShorts : na
//
TP---------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------------------
//TP
SIGNALS----------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------------------
//SL
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------
Risk = sl0
Percent_Capital = 99
if Final_Long_tp or
Final_Long_sl
CondIni_long := -1
sum_long := 0.0
nLongs := na
if Final_Short_tp or
Final_Short_sl
CondIni_short := 1
sum_short := 0.0
nShorts := na
// Leverage
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------
Act_Lev = input(true,
title="Activate leverage?"
)
Max_Lev = input(2,
title="Max lev.",
type = input.integer, minval = 1, maxval = 5 )
sma_length_lev = input(20,
title="Volume lenght lev.",
minval = 1 )
Lev_vol = Act_Lev ?
min(Max_Lev,max(1, round(volume/sma(volume,sma_length_lev)))) : 1
rsiLen = 14
last_leverage_L := Long ? Lev_vol
: nz(last_leverage_L[1] )
last_leverage_S := Short ? Lev_vol
: nz(last_leverage_S[1] )
vol_x1 = Lev_vol[1] == 1
vol_x2 = Lev_vol[1] == 2
// Colors
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------
//
PLOTS==============================================================================
===================================================================================
===================================================================================
==========================================================
//Price plots
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------
//
PLOTSHAPES-------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------
plotshape(longCondition, title="Long",
style=shape.triangleup, location=location.belowbar,
color=color.blue, size=size.tiny ,
transp = 0 )
plotshape(shortCondition, title="Short",
style=shape.triangledown, location=location.abovebar,
color=color.red, size=size.tiny ,
transp = 0 )
//BACKTESTING inputs
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------
ACT_BT = input(true,
title="Backtest", type = input.bool,
group= "BACKTEST")
testStartYear = input(1997,
title="start year", minval = 1997, maxval =
3000, group= "BACKTEST")
testStartMonth = input(06,
title="start month", minval = 1, maxval =
12, group= "BACKTEST")
testStartDay = input(01,
title="start day", minval = 1, maxval =
31, group= "BACKTEST")
testPeriodStart =
timestamp(testStartYear,testStartMonth,testStartDay,0,0)
testStopYear = input(3333,
title="stop year", minval=1980, maxval =
2222, group= "BACKTEST")
testStopMonth = input(12,
title="stop month", minval=1, maxval=12,
group= "BACKTEST")
testStopDay = input(31,
title="stop day", minval=1, maxval=31,
group= "BACKTEST")
testPeriodStop = timestamp(testStopYear, testStopMonth,
testStopDay, 0, 0)
testPeriod = time >= testPeriodStart and time <=
testPeriodStop ? true : false
// Backtest
===================================================================================
===================================================================================
===================================================================================
=========================================================================
if
L_basic_condt
strategy.entry
("L", strategy.long , when = ACT_BT and testPeriod , qty = 2*Lev_vol)
if
S_basic_condt
strategy.entry
("S", strategy.short, when = ACT_BT and testPeriod , qty = 2*Lev_vol)