Advertisement
Guest User

Framework

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