Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.61 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(0.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(1, minval=0, maxval=1, title="Stoploss on/off")
  43. sellstoploss = input(5.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. RSILength = input(20, minval=1)
  100. ROCLength = input(20, minval=1)
  101. BuyZone = input(30, minval=1)
  102. SellZone = input(70, minval=1)
  103. xPrice = close
  104. //hline(SellZone, color=red, linestyle=line, title = "Upper")
  105. //hline(BuyZone, color=green, linestyle=line, title = "Lower")
  106. nRes = rsi(roc(xPrice,ROCLength),RSILength)
  107. pos = 0
  108. //pos := nz(pos[1])
  109. pos := nRes < BuyZone ? -1 : nRes > SellZone ? 1 : nz(pos[1])
  110.  
  111. //nz(pos[1], 0)))
  112. barcolor(pos == -1 ? red: pos == 1 ? green : blue )
  113. position = blue
  114. position := (pos == -1 ? red: pos == 1 ? green : blue )
  115. //plot(nRes, color=blue, title="RSI/ROC")
  116.  
  117. //ADX
  118. lenadx = input(10, minval=1, title="DI Length")
  119. lensig = input(10, title="ADX Smoothing", minval=1, maxval=50)
  120.  
  121. up = change(high)
  122. down = -change(low)
  123. plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
  124. minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
  125. trur = rma(tr, lenadx)
  126. plus = fixnan(100 * rma(plusDM, lenadx) / trur)
  127. minus = fixnan(100 * rma(minusDM, lenadx) / trur)
  128. sum = plus + minus
  129. sigadx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
  130.  
  131. //StochRSI
  132. smoothKRSI = input(3, minval=1)
  133. smoothDRSI = input(3, minval=1)
  134. lengthRSI = input(14, minval=1)
  135. lengthStochRSI = input(14, minval=1)
  136. srcRSI = input(close, title="RSI Source")
  137. buyRSI = input(30, minval=1, title="RSI Buy Value")
  138. sellRSI = input(70, minval=1, title="RSI Sell Value")
  139. rsi1 = rsi(srcRSI, lengthRSI)
  140. krsi = sma(stoch(rsi1, rsi1, rsi1, lengthStochRSI), smoothKRSI)
  141. drsi = sma(krsi, smoothDRSI)
  142.  
  143. // Bollinger bands
  144. lengthbb = input(20, minval=1)
  145. srcbb = input(close, title="Sourcebb")
  146. multbb = input(2.0, minval=0.001, maxval=50)
  147. bb_buy_value = input(0.5, step=0.1, title="BB Buy Value")
  148. bb_sell_value = input(0.5, step=0.1, title="BB Sell Value")
  149. basisbb = sma(srcbb, lengthbb)
  150. devbb = multbb * stdev(srcbb, lengthbb)
  151. upperbb = basisbb + devbb
  152. lowerbb = basisbb - devbb
  153. bbr = (srcbb - lowerbb)/(upperbb - lowerbb)
  154. bbbuy = basisbb - (devbb*bb_buy_value)
  155. bbsell = basisbb + (devbb*bb_sell_value)
  156.  
  157. //ema very short
  158. shorter = ema(close, 2)
  159. shorterlong = ema(close, 5)
  160.  
  161. //ema short
  162. short = ema(close, 10)
  163. long = ema(close, 30)
  164.  
  165. //ema long
  166. shortday = ema(close, 110)
  167. longday = ema(close, 360)
  168.  
  169. //ema even longer
  170. shortlongerday = ema(close, 240)
  171. longlongerday = ema(close, 720)
  172.  
  173. //declaring extra timeframe value
  174. profit = security(tickerid, period, close)
  175.  
  176.  
  177. ////////////////////////////////////////////////////////////////////////
  178. // In the 3rd part of the script we define all the entries and exits //
  179. ///////// This third part is basically the acual algorithm ////////////
  180. ///////////////////////////////////////////////////////////////////////
  181.  
  182. //Declaring function with the long entries
  183. OPENLONG_funct() =>
  184. // You can add more buy entries to the script
  185. longentry1 = false
  186. longentry2 = false
  187. longentry3 = false
  188. longentry4 = false
  189. longentry5 = false
  190. makelong_funct = false
  191. //if close<bbbuy and krsi<buyRSI // You could for instance add "and shortday > longday"
  192. // longentry1 := close>close[1]
  193. // longentry2 := ...
  194. // if another thing we want to buy on happens
  195. longentry3 := position == green and position[1] != green
  196. //All the buy entries go above, this last variable is what the function puts out
  197. // if you add more entries, add them in the following list too
  198. makelong_funct := longentry1 or longentry2 or longentry3 or longentry4 or longentry5
  199.  
  200. //Declaring function wit the short entries
  201. OPENSHORT_funct() =>
  202. // You can add more buy entries to the script
  203. shortentry1 = false
  204. shortentry2 = false
  205. shortentry3 = false
  206. shortentry4 = false
  207. shortentry5 = false
  208. makeshort_funct = false
  209. //if close>bbsell and krsi>sellRSI // You could for instance add "and shortday < longday"
  210. // shortentry1 := close<close[1]
  211. // shortentry2 := ...
  212. // if another thing we want to buy on happens
  213. shortentry3 := position == red and position[1] != red
  214. //All the buy entries go above, this last variable is what the function puts out
  215. // if you add more entries, add them in the following list too
  216. makeshort_funct := shortentry1 or shortentry2 or shortentry3 or shortentry4 or shortentry5
  217.  
  218. //Declaring function with the long exits
  219. CLOSELONG_funct() =>
  220. // You can add more buy entries to the script
  221. longexit1 = false
  222. longexit2 = false
  223. longexit3 = false
  224. longexit4 = false
  225. longexit5 = false
  226. closelong_funct = false
  227. //if close>bbsell and krsi>sellRSI
  228. // longexit1 := close<close[1]
  229. // longexit2 := ...
  230. // if another thing we want to close on on happens you can add them here...
  231. longexit3 := position == red and position[1] != red
  232. //All the buy entries go above, this last variable is what the function puts out
  233. // if you add more exits, add them in the following list too
  234. closelong_funct := longexit1 or longexit2 or longexit3 or longexit4 or longexit5
  235.  
  236. //Declaring function wit the short exits
  237. CLOSESHORT_funct() =>
  238. // You can add more buy entries to the script
  239. shortexit1 = false
  240. shortexit2 = false
  241. shortexit3 = false
  242. shortexit4 = false
  243. shortexit5 = false
  244. closeshort_funct = false
  245. //if close<bbsell and krsi<sellRSI
  246. // shortexit1 := close>close[1]
  247. // shortexit2 := ...
  248. // if another thing we want to close on on happens you can add them here...
  249. shortexit3 := position == green and position[1] != green
  250. //All the buy entries go above, this last variable is what the function puts out
  251. // if you add more exits, add them in the following list too
  252. closeshort_funct := shortexit1 or shortexit2 or shortexit3 or shortexit4 or shortexit5
  253.  
  254. /////////////////////////////////////////////////////////////////////////////////////
  255. ////////////// End of "entries" and "exits" definition code /////////////////////////
  256. /////////////////////////////////////////////////////////////////////////////////////
  257. /// In the fourth part we do the actual work, as defined in the part before this ////
  258. ////////////////////// This part does not need to be changed ////////////////////////
  259. /////////////////////////////////////////////////////////////////////////////////////
  260.  
  261. //OPEN LONG LOGIC
  262. makelong = false
  263. //buy with backtesting on specific dates
  264. if Backtestdate > 0 and testPeriod()
  265. if (longs < 1 and shorts < 1) or (short > 0 and enable_flipping > 0 and enable_shorts > 0 and longs < 1)
  266. makelong := OPENLONG_funct()
  267.  
  268. //buy without backtesting on specific dates
  269. if Backtestdate < 1
  270. if (longs < 1 and shorts < 1) or (short > 0 and enable_flipping > 0 and enable_shorts > 0 and longs < 1)
  271. makelong := OPENLONG_funct()
  272.  
  273. if makelong
  274. buyprice := close
  275. scaler := close
  276. longs := 1
  277. shorts := 0
  278.  
  279. //OPEN SHORT LOGIC
  280. makeshort = false
  281.  
  282. //buy with backtesting on specific dates
  283. if Backtestdate > 0 and testPeriod()
  284. if (shorts < 1 and longs < 1 and enable_shorts > 0) or (longs > 0 and enable_flipping > 0 and enable_shorts > 0)
  285. makeshort := OPENSHORT_funct()
  286.  
  287. //buy without backtesting on specific dates
  288. if Backtestdate < 1
  289. if (shorts < 1 and longs < 1 and enable_shorts > 0) or (longs > 0 and enable_flipping > 0 and enable_shorts > 0)
  290. makeshort := OPENSHORT_funct()
  291.  
  292.  
  293. if makeshort
  294. buyprice := close
  295. scaler := close
  296. shorts := 1
  297. longs := 0
  298.  
  299. //Calculating values for traling stop
  300. if longs > 0 and enable_flipping < 1
  301. if close > scaler+Trail_ATR and enable_trailing_ATR > 0
  302. scaler := close
  303. if close > scaler * (1.0 + selltrailingfinal) and enable_trailing_ATR < 1
  304. scaler := close
  305. if shorts > 0 and enable_flipping < 1
  306. if close < scaler-Trail_ATR and enable_trailing_ATR > 0
  307. scaler := close
  308. if close < scaler * (1.0 - selltrailingfinal) and enable_trailing_ATR < 1
  309. scaler := close
  310.  
  311. long_exit = false
  312. long_security1 = false
  313. long_security2 = false
  314. long_security3 = false
  315.  
  316. //CLOSE LONG LOGIC
  317. if longs > 0 and enable_flipping < 1
  318. if ( (buyprice + (buyprice*sellproffinal) + atr + oc2) < close) and ( (buyprice + (buyprice*sellproffinal) ) < profit)
  319. long_exit := CLOSELONG_funct()
  320. //security
  321. if enable_stoploss > 0
  322. long_security1 := close < ( buyprice * (1.0 - sellstoplossfinal) )
  323. if enable_trailing > 0 and enable_trailing_ATR < 1
  324. long_security2 := close < ( scaler * (1.0 - selltrailingfinal) )
  325. if enable_trailing > 0 and enable_trailing_ATR > 0
  326. long_security2 := close < ( scaler - Trail_ATR)
  327.  
  328. //CLOSE LONG LOGIC
  329. if longs > 0 and enable_flipping > 0
  330. //security
  331. if enable_stoploss > 0
  332. long_security1 := close < ( buyprice * (1.0 - sellstoplossfinal) )
  333. if enable_trailing > 0 and enable_trailing_ATR < 1
  334. long_security2 := close < ( scaler * (1.0 - selltrailingfinal) )
  335. if enable_trailing > 0 and enable_trailing_ATR > 0
  336. long_security2 := close < ( scaler - Trail_ATR)
  337.  
  338. closelong = long_exit or long_security1 or long_security2 or long_security3
  339.  
  340. short_exit = false
  341. short_security1 = false
  342. short_security2 = false
  343. short_security3 = false
  344.  
  345. if closelong
  346. longs := 0
  347.  
  348. //CLOSE SHORT LOGIC
  349. if shorts > 0 and enable_flipping < 1
  350. if ( (buyprice - (buyprice*(sellproffinal) - atr - oc2) > close) and ( (buyprice - (buyprice*sellproffinal) ) > profit) )
  351. short_exit := CLOSESHORT_funct()
  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. if shorts > 0 and enable_flipping > 0
  360. //security
  361. if enable_stoploss > 0
  362. short_security1 := close > ( buyprice * (1.0 + sellstoplossfinal) )
  363. if enable_trailing > 0 and enable_trailing_ATR < 1
  364. short_security2 := close > ( scaler * (1.0 + selltrailingfinal) )
  365. if enable_trailing > 0 and enable_trailing_ATR > 0
  366. short_security2 := close > ( scaler + Trail_ATR)
  367.  
  368. closeshort = short_exit or short_security1 or short_security2 or short_security3
  369.  
  370. if closeshort
  371. shorts := 0
  372.  
  373. ///////////////////////////////////////////////////////////////////////////////////////
  374. ///////////// The last section takes care of the alerts //////////////////////////////
  375. //////////////////////////////////////////////////////////////////////////////////////
  376. plotshape(makelong, style=shape.arrowup)
  377. alertcondition(makelong, title="openlong", message="openlong")
  378. strategy.entry("BuyLONG", strategy.long, oca_name="DBCross", oca_type=strategy.oca.cancel, when= makelong, comment="Open Long")
  379.  
  380. plotshape(makeshort, style=shape.arrowdown)
  381. alertcondition(makeshort, title="openshort", message="openshort")
  382. strategy.entry("BuySHORT", strategy.short, oca_name="DBCross", oca_type=strategy.oca.cancel, when= makeshort, comment="Open Short")
  383.  
  384. plotshape(closelong, style=shape.arrowdown)
  385. alertcondition(closelong, title="closelong", message="closelong")
  386. strategy.close("BuyLONG", when=closelong)
  387.  
  388. plotshape(closeshort, style=shape.arrowup)
  389. alertcondition(closeshort, title="closeshort", message="closeshort")
  390. strategy.close("BuySHORT", when=closeshort)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement