Advertisement
Guest User

Initial with default RSI ind.

a guest
Apr 21st, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.08 KB | None | 0 0
  1. //@version=3
  2. // THIS SCRIPT IS MEANT TO ACCOMPANY COMMAND EXECUTION BOTS
  3. // THE INCLUDED STRATEGY IS NOT MEANT FOR LIVE TRADING, BUT CAN BE USED AT YOUR OWN RISK
  4. // THIS STRATEGY IS AN EXAMLE TO START EXPERIMENTATING WITH YOUR OWN IDEAS
  5. ////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. // comment out the next line to use this script as an alert script
  8. strategy(title="Dragon Bot - Default Script", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
  9. // remove the // in the next line to use this script as an alert script
  10. // study(title="Dragon Bot - Default Script", overlay=true)
  11.  
  12. // Dragon-Bot default script version 2.0
  13. // This can also be used with bot that reacts to tradingview alerts.
  14. // Use the script as "strategy" for backtesting
  15. // Comment out line 8 and de-comment line 10 to be able to set tradingview alerts.
  16. // You should also comment out (place // before it) the lines 360, 364, 368 and 372 (strategy.entry and strategy.close) to be able to set the alerts.
  17. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // In this first part of the script we setup variables and make sure the script keeps all information it used in the past. //
  19. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. longs = 0
  21. longs := nz(longs[1])
  22.  
  23. shorts = 0
  24. shorts := nz(shorts[1])
  25.  
  26. buyprice = 0.0
  27. buyprice := buyprice[1]
  28.  
  29. sellprice = 0.0
  30. sellprice := sellprice[1]
  31.  
  32. scaler = 0.0
  33. scaler := scaler[1]
  34.  
  35. sellprofit = input(1.0, minval=0.0, step=0.1, title="main strat profit")
  36. sellproffinal = sellprofit/100
  37.  
  38. enable_shorts = input(1, minval=0, maxval=1, title="Shorts on/off")
  39.  
  40. enable_flipping = input(0, minval=0, maxval=1, title="Flipping on/off -> Go directly from long -> short or short -> long without closing ")
  41.  
  42. enable_stoploss = input(0, minval=0, maxval=1, title="Stoploss on/off")
  43. sellstoploss = input(30.0, minval=0.0, step=1.0, title="Stoploss %")
  44. sellstoplossfinal = sellstoploss/100
  45.  
  46. enable_trailing = input(1, minval=0, maxval=1, title="Trailing on/off")
  47. enable_trailing_ATR = input(1, minval=0, maxval=1, title="Trailing use ATR on/off")
  48. ATR_Multi = input(1.0, minval=0.0, step=0.1, title="Multiplier for ATR")
  49. selltrailing = input(10.0, minval=0.0, step=1.0, title="Trailing %")
  50. selltrailingfinal = selltrailing/100
  51.  
  52. Backtestdate = input(0, minval=0, maxval=1, title="backtest date on/off")
  53.  
  54. // Component Code by pbergden - Start backtest dates
  55. // The following code snippet is taken from an example by pbergen
  56. // All rights to this snippet remain with pbergden
  57. testStartYear = input(2018, "Backtest Start Year")
  58. testStartMonth = input(1, "Backtest Start Month")
  59. testStartDay = input(1, "Backtest Start Day")
  60. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  61.  
  62. testStopYear = input(2019, "Backtest Stop Year")
  63. testStopMonth = input(1, "Backtest Stop Month")
  64. testStopDay = input(1, "Backtest Stop Day")
  65. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  66.  
  67. // A switch to control background coloring of the test period
  68. testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
  69. testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
  70. bgcolor(testPeriodBackgroundColor, transp=97)
  71.  
  72. testPeriod() =>
  73. time >= testPeriodStart and time <= testPeriodStop ? true : false
  74.  
  75. /////////////////////////////////////////////////////////////////////////////////////////////////////
  76. // In this second part of the script we setup indicators that we can use for our actual algorithm. //
  77. /////////////////////////////////////////////////////////////////////////////////////////////////////
  78.  
  79.  
  80. //ATR
  81. lengthtr = input(20, minval=1, title="ATR Length")
  82. ATRsell = input(0, minval=0, title="1 for added ATR when selling")
  83. ATR=rma(tr(true), lengthtr)
  84. Trail_ATR=rma(tr(true), 10) * ATR_Multi
  85. atr = 0.0
  86. if ATRsell == 1
  87. atr := ATR
  88.  
  89. //OC2
  90. lengthoc2 = input(20, minval=1, title="OC2 Length")
  91. OC2sell = input(0, minval=0, title="1 for added OC2 when selling")
  92. OC2mult = input(1, minval=1, title="OC2 multiplayer")
  93. OC= abs(open[1]-close)
  94. OC2=rma(OC, lengthoc2)
  95. oc2 = 0.0
  96. if OC2sell == 1
  97. oc2 := OC2*OC2mult
  98.  
  99. //ADX
  100. lenadx = input(10, minval=1, title="DI Length")
  101. lensig = input(10, title="ADX Smoothing", minval=1, maxval=50)
  102.  
  103. up = change(high)
  104. down = -change(low)
  105. plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
  106. minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
  107. trur = rma(tr, lenadx)
  108. plus = fixnan(100 * rma(plusDM, lenadx) / trur)
  109. minus = fixnan(100 * rma(minusDM, lenadx) / trur)
  110. sum = plus + minus
  111. sigadx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
  112.  
  113. //StochRSI
  114. smoothKRSI = input(3, minval=1)
  115. smoothDRSI = input(3, minval=1)
  116. lengthRSI = input(14, minval=1)
  117. lengthStochRSI = input(14, minval=1)
  118. srcRSI = input(close, title="RSI Source")
  119. buyRSI = input(30, minval=1, title="RSI Buy Value")
  120. sellRSI = input(70, minval=1, title="RSI Sell Value")
  121. rsi1 = rsi(srcRSI, lengthRSI)
  122. krsi = sma(stoch(rsi1, rsi1, rsi1, lengthStochRSI), smoothKRSI)
  123. drsi = sma(krsi, smoothDRSI)
  124.  
  125. // Bollinger bands
  126. lengthbb = input(20, minval=1)
  127. srcbb = input(close, title="Sourcebb")
  128. multbb = input(2.0, minval=0.001, maxval=50)
  129. bb_buy_value = input(0.5, step=0.1, title="BB Buy Value")
  130. bb_sell_value = input(0.5, step=0.1, title="BB Sell Value")
  131. basisbb = sma(srcbb, lengthbb)
  132. devbb = multbb * stdev(srcbb, lengthbb)
  133. upperbb = basisbb + devbb
  134. lowerbb = basisbb - devbb
  135. bbr = (srcbb - lowerbb)/(upperbb - lowerbb)
  136. bbbuy = basisbb - (devbb*bb_buy_value)
  137. bbsell = basisbb + (devbb*bb_sell_value)
  138.  
  139. //RSI
  140. RSILength = input(9, minval=1)
  141. ROCLength = input(9, minval=1)
  142.  
  143. RSIMin = input(30, minval=1, step = 1)
  144.  
  145. nRes = rsi(roc(close,ROCLength),RSILength)
  146.  
  147. longRSI = nRes < RSIMin
  148.  
  149. //ema very short
  150. shorter = ema(close, 2)
  151. shorterlong = ema(close, 5)
  152.  
  153. //ema short
  154. short = ema(close, 10)
  155. long = ema(close, 30)
  156.  
  157. //ema long
  158. shortday = ema(close, 110)
  159. longday = ema(close, 360)
  160.  
  161. //ema even longer
  162. shortlongerday = ema(close, 240)
  163. longlongerday = ema(close, 720)
  164.  
  165. //declaring extra timeframe value
  166. profit = security(tickerid, period, close)
  167.  
  168.  
  169. ////////////////////////////////////////////////////////////////////////
  170. // In the 3rd part of the script we define all the entries and exits //
  171. ///////// This third part is basically the acual algorithm ////////////
  172. ///////////////////////////////////////////////////////////////////////
  173.  
  174. //Declaring function with the long entries
  175. OPENLONG_funct() =>
  176. // You can add more buy entries to the script
  177. longentry1 = false
  178. longentry2 = false
  179. longentry3 = false
  180. longentry4 = false
  181. longentry5 = false
  182. makelong_funct = false
  183. if longRSI//close<bbbuy and krsi<buyRSI and longRSI// You could for instance add "and shortday > longday"
  184. longentry1 := close>close[1]
  185. // longentry2 := ...
  186. // if another thing we want to buy on happens
  187. // longentry3 := ...
  188. //All the buy entries go above, this last variable is what the function puts out
  189. // if you add more entries, add them in the following list too
  190. makelong_funct := longentry1 or longentry2 or longentry3 or longentry4 or longentry5
  191.  
  192. //Declaring function wit the short entries
  193. OPENSHORT_funct() =>
  194. // You can add more buy entries to the script
  195. shortentry1 = false
  196. shortentry2 = false
  197. shortentry3 = false
  198. shortentry4 = false
  199. shortentry5 = false
  200. makeshort_funct = false
  201. if close>bbsell and krsi>sellRSI // You could for instance add "and shortday < longday"
  202. shortentry1 := close<close[1]
  203. // shortentry2 := ...
  204. // if another thing we want to buy on happens
  205. // shortentry3 := ...
  206. //All the buy entries go above, this last variable is what the function puts out
  207. // if you add more entries, add them in the following list too
  208. makeshort_funct := shortentry1 or shortentry2 or shortentry3 or shortentry4 or shortentry5
  209.  
  210. //Declaring function with the long exits
  211. CLOSELONG_funct() =>
  212. // You can add more buy entries to the script
  213. longexit1 = false
  214. longexit2 = false
  215. longexit3 = false
  216. longexit4 = false
  217. longexit5 = false
  218. closelong_funct = false
  219. if close>bbsell //and krsi>sellRSI
  220. longexit1 := close<close[1]
  221. // longexit2 := ...
  222. // if another thing we want to close on on happens you can add them here...
  223. // longexit3 := ...
  224. //All the buy entries go above, this last variable is what the function puts out
  225. // if you add more exits, add them in the following list too
  226. closelong_funct := longexit1 or longexit2 or longexit3 or longexit4 or longexit5
  227.  
  228. //Declaring function wit the short exits
  229. CLOSESHORT_funct() =>
  230. // You can add more buy entries to the script
  231. shortexit1 = false
  232. shortexit2 = false
  233. shortexit3 = false
  234. shortexit4 = false
  235. shortexit5 = false
  236. closeshort_funct = false
  237. if close<bbsell //and krsi<sellRSI
  238. shortexit1 := close>close[1]
  239. // shortexit2 := ...
  240. // if another thing we want to close on on happens you can add them here...
  241. // shortexit3 := ...
  242. //All the buy entries go above, this last variable is what the function puts out
  243. // if you add more exits, add them in the following list too
  244. closeshort_funct := shortexit1 or shortexit2 or shortexit3 or shortexit4 or shortexit5
  245.  
  246. /////////////////////////////////////////////////////////////////////////////////////
  247. ////////////// End of "entries" and "exits" definition code /////////////////////////
  248. /////////////////////////////////////////////////////////////////////////////////////
  249. /// In the fourth part we do the actual work, as defined in the part before this ////
  250. ////////////////////// This part does not need to be changed ////////////////////////
  251. /////////////////////////////////////////////////////////////////////////////////////
  252.  
  253. //OPEN LONG LOGIC
  254. makelong = false
  255. //buy with backtesting on specific dates
  256. if Backtestdate > 0 and testPeriod()
  257. if (longs < 1 and shorts < 1) or (short > 0 and enable_flipping > 0 and enable_shorts > 0 and longs < 1)
  258. makelong := OPENLONG_funct()
  259.  
  260. //buy without backtesting on specific dates
  261. if Backtestdate < 1
  262. if (longs < 1 and shorts < 1) or (short > 0 and enable_flipping > 0 and enable_shorts > 0 and longs < 1)
  263. makelong := OPENLONG_funct()
  264.  
  265. if makelong
  266. buyprice := close
  267. scaler := close
  268. longs := 1
  269. shorts := 0
  270.  
  271. //OPEN SHORT LOGIC
  272. makeshort = false
  273.  
  274. //buy with backtesting on specific dates
  275. if Backtestdate > 0 and testPeriod()
  276. if (shorts < 1 and longs < 1 and enable_shorts > 0) or (longs > 0 and enable_flipping > 0 and enable_shorts > 0)
  277. makeshort := OPENSHORT_funct()
  278.  
  279. //buy without backtesting on specific dates
  280. if Backtestdate < 1
  281. if (shorts < 1 and longs < 1 and enable_shorts > 0) or (longs > 0 and enable_flipping > 0 and enable_shorts > 0)
  282. makeshort := OPENSHORT_funct()
  283.  
  284.  
  285. if makeshort
  286. buyprice := close
  287. scaler := close
  288. shorts := 1
  289. longs := 0
  290.  
  291. //Calculating values for traling stop
  292. if longs > 0 and enable_flipping < 1
  293. if close > scaler+Trail_ATR and enable_trailing_ATR > 0
  294. scaler := close
  295. if close > scaler * (1.0 + selltrailingfinal) and enable_trailing_ATR < 1
  296. scaler := close
  297. if shorts > 0 and enable_flipping < 1
  298. if close < scaler-Trail_ATR and enable_trailing_ATR > 0
  299. scaler := close
  300. if close < scaler * (1.0 - selltrailingfinal) and enable_trailing_ATR < 1
  301. scaler := close
  302.  
  303. long_exit = false
  304. long_security1 = false
  305. long_security2 = false
  306. long_security3 = false
  307.  
  308. //CLOSE LONG LOGIC
  309. if longs > 0 and enable_flipping < 1
  310. if ( (buyprice + (buyprice*sellproffinal) + atr + oc2) < close) and ( (buyprice + (buyprice*sellproffinal) ) < profit)
  311. long_exit := CLOSELONG_funct()
  312. //security
  313. if enable_stoploss > 0
  314. long_security1 := close < ( buyprice * (1.0 - sellstoplossfinal) )
  315. if enable_trailing > 0 and enable_trailing_ATR < 1
  316. long_security2 := close < ( scaler * (1.0 - selltrailingfinal) )
  317. if enable_trailing > 0 and enable_trailing_ATR > 0
  318. long_security2 := close < ( scaler - Trail_ATR)
  319.  
  320. //CLOSE LONG LOGIC
  321. if longs > 0 and enable_flipping > 0
  322. //security
  323. if enable_stoploss > 0
  324. long_security1 := close < ( buyprice * (1.0 - sellstoplossfinal) )
  325. if enable_trailing > 0 and enable_trailing_ATR < 1
  326. long_security2 := close < ( scaler * (1.0 - selltrailingfinal) )
  327. if enable_trailing > 0 and enable_trailing_ATR > 0
  328. long_security2 := close < ( scaler - Trail_ATR)
  329.  
  330. closelong = long_exit or long_security1 or long_security2 or long_security3
  331.  
  332. short_exit = false
  333. short_security1 = false
  334. short_security2 = false
  335. short_security3 = false
  336.  
  337. if closelong
  338. longs := 0
  339.  
  340. //CLOSE SHORT LOGIC
  341. if shorts > 0 and enable_flipping < 1
  342. if ( (buyprice - (buyprice*(sellproffinal) - atr - oc2) > close) and ( (buyprice - (buyprice*sellproffinal) ) > profit) )
  343. short_exit := CLOSESHORT_funct()
  344. //security
  345. if enable_stoploss > 0
  346. short_security1 := close > ( buyprice * (1.0 + sellstoplossfinal) )
  347. if enable_trailing > 0 and enable_trailing_ATR < 1
  348. short_security2 := close > ( scaler * (1.0 + selltrailingfinal) )
  349. if enable_trailing > 0 and enable_trailing_ATR > 0
  350. short_security2 := close > ( scaler + Trail_ATR)
  351. if shorts > 0 and enable_flipping > 0
  352. //security
  353. if enable_stoploss > 0
  354. short_security1 := close > ( buyprice * (1.0 + sellstoplossfinal) )
  355. if enable_trailing > 0 and enable_trailing_ATR < 1
  356. short_security2 := close > ( scaler * (1.0 + selltrailingfinal) )
  357. if enable_trailing > 0 and enable_trailing_ATR > 0
  358. short_security2 := close > ( scaler + Trail_ATR)
  359.  
  360. closeshort = short_exit or short_security1 or short_security2 or short_security3
  361.  
  362. if closeshort
  363. shorts := 0
  364.  
  365. ///////////////////////////////////////////////////////////////////////////////////////
  366. ///////////// The last section takes care of the alerts //////////////////////////////
  367. //////////////////////////////////////////////////////////////////////////////////////
  368. plotshape(makelong, style=shape.arrowup)
  369. alertcondition(makelong, title="openlong", message="openlong")
  370. strategy.entry("BuyLONG", strategy.long, oca_name="DBCross", oca_type=strategy.oca.cancel, when= makelong, comment="Open Long")
  371.  
  372. plotshape(makeshort, style=shape.arrowdown)
  373. alertcondition(makeshort, title="openshort", message="openshort")
  374. strategy.entry("BuySHORT", strategy.short, oca_name="DBCross", oca_type=strategy.oca.cancel, when= makeshort, comment="Open Short")
  375.  
  376. plotshape(closelong, style=shape.arrowdown)
  377. alertcondition(closelong, title="closelong", message="closelong")
  378. strategy.close("BuyLONG", when=closelong)
  379.  
  380. plotshape(closeshort, style=shape.arrowup)
  381. alertcondition(closeshort, title="closeshort", message="closeshort")
  382. strategy.close("BuySHORT", when=closeshort)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement