Advertisement
PineCoders

MABB

Aug 12th, 2019
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.83 KB | None | 0 0
  1. //@version=3
  2. // —————————— To swap between strategy and indicator modes,
  3. // —————————— swap strategy() and study() lines below and comment out/comment last lines of script.
  4. // strategy("Moving Average and/or Bbands bot V1.1", shorttitle="MABB Strategy", overlay=true, pyramiding=1000)
  5. study("Moving Average and/or Bbands bot V1.1", shorttitle="MABB Indicator", overlay=true)
  6.  
  7. //Make the backtest numbers more legible depending on the market you're trading, altcoin, forex, or commodities.
  8. qty = 1
  9.  
  10. // If you're trading an altcoin, make this true and the backtest numbers are now equivalent to 1 satoshi
  11. isALT = input(false, "Altcoin")
  12.  
  13. if isALT
  14. qty:= 100000000
  15.  
  16. // If you're trading forex, make this true and the backtest numbers are now equivalent to $0.0001
  17. isForex = input(false, "Forex")
  18. if isForex
  19. qty:= 10000
  20.  
  21. //* Backtesting Period Selector | Component *//
  22. //* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *//
  23. //* https://www.tradingview.com/u/pbergden/ *//
  24. //* Modifications made *//
  25. markersOn = input(true, "Plot Markers")
  26. testStartYear = input(1, "Backtest Start Year")
  27. testStartMonth = input(8, "Backtest Start Month")
  28. testStartDay = input(25, "Backtest Start Day")
  29. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  30.  
  31. testStopYear = input(999999, "Backtest Stop Year")
  32. testStopMonth = input(9, "Backtest Stop Month")
  33. testStopDay = input(26, "Backtest Stop Day")
  34. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  35.  
  36. testPeriod() =>
  37. time >= testPeriodStart and time <= testPeriodStop ? true : false
  38. /////////////// END - Backtesting Period Selector | Component ///////////////
  39.  
  40. //* Heiken Ashi Candles *//
  41. isHA = false
  42.  
  43. data = isHA ? heikenashi(tickerid) : tickerid
  44.  
  45. o = security(data, period, open)
  46. h = security(data, period, high)
  47. l = security(data, period, low)
  48. c = security(data, period, close)
  49.  
  50. g = c > o
  51. r = c < o
  52.  
  53. col = c > o ? green : red
  54.  
  55. plotcandle(o, h, l, c, "Heiken Ashi", col, black)
  56.  
  57. //Initial open logic, needs to be set at the beginning as this is affected by most of the following settings
  58. long = na
  59. short = na
  60.  
  61. //* Moving Average Logic *\\
  62. // Enable this to only long or short if you are above or below the Moving Average
  63. useMA = input(true, "Use Moving Average Cross")
  64. ma1Input = input(50, "Moving Average 1")
  65. ma2Input = input(200, "Moving Average 2")
  66.  
  67. ma1 = sma(c, ma1Input)
  68. ma2 = sma(c, ma2Input)
  69.  
  70. maLong = c > ma1 and ma1 > ma2
  71. maShort = c < ma1 and ma1 < ma2
  72.  
  73. ma1Plot = na
  74. ma2Plot = na
  75.  
  76. if useMA
  77. ma1Plot := ma1
  78. ma2Plot := ma2
  79. long := maLong
  80. short := maShort
  81.  
  82. plot(ma1Plot, "ma1", blue)
  83. plot(ma2Plot, "ma2", orange)
  84.  
  85. //* Bollinger Bands Logic *\\
  86. // Enable this to only long or short if you are above or below the Bollinger Bands
  87.  
  88. useBbands = input(false, "Use Bollinger Bands")
  89.  
  90. bblength = input(20, minval=1)
  91. mult = input(2.0, minval=0.001, maxval=50)
  92. basis = sma(c, bblength)
  93. dev = mult * stdev(c, bblength)
  94. upper = basis + dev
  95. lower = basis - dev
  96.  
  97. basisPlot = na
  98. p1Plot = na
  99. p2Plot = na
  100.  
  101. if useBbands
  102. long := c < lower
  103. short := c > upper
  104. basisPlot := basis
  105. p1Plot := upper
  106. p2Plot := lower
  107.  
  108. if useBbands and useMA
  109. long := c < lower and maLong
  110. short := c > upper and maShort
  111.  
  112. plot(basisPlot, color=red)
  113. p1 = plot(p1Plot, color=blue)
  114. p2 = plot(p2Plot, color=blue)
  115. fill(p1, p2)
  116.  
  117. //////////////////////////
  118. //* Strategy Component *//
  119. //////////////////////////
  120.  
  121. // Count your long short conditions for more control with Pyramiding
  122. sectionLongs = 0
  123. sectionLongs := nz(sectionLongs[1])
  124. sectionShorts = 0
  125. sectionShorts := nz(sectionShorts[1])
  126.  
  127. if long
  128. sectionLongs := sectionLongs + 1
  129. sectionShorts := 0
  130.  
  131. if short
  132. sectionLongs := 0
  133. sectionShorts := sectionShorts + 1
  134.  
  135. // Pyramiding Inputs
  136.  
  137. pyrl = input(1, "Pyramiding less than") // If your count is less than this number
  138. pyre = input(0, "Pyramiding equal to") // If your count is equal to this number
  139. pyrg = input(1000000, "Pyramiding greater than") // If your count is greater than this number
  140.  
  141. // These check to see your signal and cross references it against the pyramiding settings above
  142. longCondition = long and sectionLongs <= pyrl or long and sectionLongs >= pyrg or long and sectionLongs == pyre
  143. shortCondition = short and sectionShorts <= pyrl or short and sectionShorts >= pyrg or short and sectionShorts == pyre
  144.  
  145. // Get the price of the last opened long or short
  146. last_open_longCondition = na
  147. last_open_shortCondition = na
  148. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  149. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  150.  
  151. // Count your actual opened positions for things like getting your average order price
  152. sectionLongConditions = 0
  153. sectionLongConditions := nz(sectionLongConditions[1])
  154. sectionShortConditions = 0
  155. sectionShortConditions := nz(sectionShortConditions[1])
  156.  
  157. if longCondition
  158. sectionLongConditions := sectionLongConditions + 1
  159. sectionShortConditions := 0
  160.  
  161. if shortCondition
  162. sectionLongConditions := 0
  163. sectionShortConditions := sectionShortConditions + 1
  164.  
  165. // Check if your last postion was a long or a short
  166. last_longCondition = na
  167. last_shortCondition = na
  168. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  169. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  170.  
  171. in_longCondition = last_longCondition > last_shortCondition
  172. in_shortCondition = last_shortCondition > last_longCondition
  173.  
  174. // Keep track of the highest high since you last opened a position
  175. last_high = na
  176. last_low = na
  177. last_high_short = na
  178. last_low_short = na
  179. last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  180. last_high_short := not in_shortCondition ? na : in_shortCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  181. last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  182. last_low_short := not in_longCondition ? na : in_longCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  183.  
  184. // Trailing Stop
  185. isTS = input(false, "Trailing Stop")
  186. tsi = input(0, "Activate Trailing Stop Price") / qty
  187. ts = input(0, "Trailing Stop") / qty
  188. long_ts = isTS and not na(last_high) and crossunder(low, last_high - ts) and longCondition == 0 and high >= (last_open_longCondition + tsi)
  189. short_ts = isTS and not na(last_low) and crossover(high, last_low + ts) and shortCondition == 0 and low <= (last_open_shortCondition - tsi)
  190. tsColor = isTS and in_longCondition and last_high >= (last_open_longCondition + tsi) ? blue : isTS and in_shortCondition and last_low <= (last_open_shortCondition - tsi) ? blue : white
  191. tsiColor = isTS and in_longCondition and last_high >= (last_open_longCondition + tsi) ? white : isTS and in_shortCondition and last_low <= (last_open_shortCondition - tsi) ? white : blue
  192. plot(isTS and in_longCondition ? last_open_longCondition + tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
  193. plot(isTS and in_shortCondition ? last_open_shortCondition - tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
  194. plot(isTS and in_longCondition and last_high >= (last_open_longCondition + tsi) ? last_high - ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
  195. plot(isTS and in_shortCondition and last_low <= (last_open_shortCondition - tsi) ? last_low + ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
  196.  
  197. // Take profit
  198. isTP = input(false, "Take Profit")
  199. tp = input(0, "Take Profit") / qty
  200. long_tp = isTP and crossover(high, last_open_longCondition + tp) and longCondition == 0
  201. short_tp = isTP and crossunder(low, last_open_shortCondition - tp) and shortCondition == 0
  202. tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
  203. plot(isTP and in_longCondition and last_high < last_open_longCondition + tp ? last_open_longCondition + tp : na, "Long TP", tpColor, style=3, linewidth=2)
  204. plot(isTP and in_shortCondition and last_low > last_open_shortCondition - tp ? last_open_shortCondition - tp : na, "Short TP", tpColor, style=3, linewidth=2)
  205.  
  206. // Stop Loss
  207. isSL = input(false, "Stop Loss")
  208. sl = input(0, "Stop Loss") / qty
  209. long_sl = isSL and crossunder(low, last_open_longCondition - sl) and longCondition == 0
  210. short_sl = isSL and crossover(high, last_open_shortCondition + sl) and shortCondition == 0
  211. slColor = isSL and in_longCondition and last_low_short > last_open_longCondition - sl ? red : isSL and in_shortCondition and last_high_short < last_open_shortCondition + sl ? red : white
  212. plot(isSL and in_longCondition ? last_open_longCondition - sl : na, "Long SL", slColor, style=3, linewidth=2)
  213. plot(isSL and in_shortCondition ? last_open_shortCondition + sl : na, "Short SL", slColor, style=3, linewidth=2)
  214.  
  215. // Margin Call. Depending on your leverage, this will mimick a margin call at -80%.
  216. isMargin = input(false, "Margin Call")
  217. leverage = input(1, "Leverage")
  218. long_call = last_open_longCondition - (0.8 + 0.2 * (1/leverage)) / leverage * last_open_longCondition
  219. short_call = last_open_shortCondition + (0.78 + 0.2 * (1/leverage)) / leverage * last_open_shortCondition
  220. long_call_signal = isMargin and crossunder(low, long_call)
  221. short_call_signal = isMargin and crossunder(high, short_call)
  222. marginColor = isMargin and in_longCondition and last_low_short > long_call ? black : isMargin and in_shortCondition and last_high_short < short_call ? black : white
  223. plot(isMargin and in_longCondition ? long_call : na, "Long Margin", marginColor, style=3, linewidth=2)
  224. plot(isMargin and in_shortCondition ? short_call : na, "Short Margin", marginColor, style=3, linewidth=2)
  225.  
  226. // Get the average price of your open positions and plot them
  227. totalLongs = 0.0
  228. totalLongs := nz(totalLongs[1])
  229. totalShorts = 0.0
  230. totalShorts := nz(totalShorts[1])
  231. averageLongs = 0.0
  232. averageLongs := nz(averageLongs[1])
  233. averageShorts = 0.0
  234. averageShorts := nz(averageShorts[1])
  235.  
  236. if longCondition
  237. totalLongs := totalLongs + last_open_longCondition
  238. totalShorts := 0.0
  239.  
  240. if shortCondition
  241. totalLongs := 0.0
  242. totalShorts := totalShorts + last_open_shortCondition
  243.  
  244. averageLongs := totalLongs / sectionLongConditions
  245. averageShorts := totalShorts / sectionShortConditions
  246.  
  247. longProfit = averageLongs > 0 and close >= averageLongs ? green : red
  248. shortProfit = averageShorts > 0 and close <= averageShorts ? green : red
  249.  
  250. plot1 = plot(averageLongs > 0 ? averageLongs : na, color=white)
  251. plot2 = plot(close, color=white)
  252. plot3 = plot(averageShorts > 0 ? averageShorts : na, color=white)
  253.  
  254. fill(plot1, plot2, color=longProfit, transp=50)
  255. fill(plot2, plot3, color=shortProfit, transp=50)
  256.  
  257. //Enable this to double your order size every time your pyramid on top of an existing position. (Martingale strategy)
  258. // useMartin = input(true, "Martingale")
  259.  
  260. // longMartin = 0
  261. // longMartin := nz(longMartin[1])
  262. // shortMartin = 0
  263. // shortMartin := nz(shortMartin[1])
  264.  
  265. // // Check to see if this is our first order, set the order qty to 1
  266. // if longCondition and sectionLongConditions == 1
  267. // longMartin := longMartin + 1
  268. // shortMartin := 0
  269. // if shortCondition and sectionShortConditions == 1
  270. // longMartin := 0
  271. // shortMartin := shortMartin + 1
  272.  
  273. // confirm that this order is being added to an existing order
  274. // if longCondition and sectionLongConditions > 1
  275. // longMartin := longMartin * 2
  276. // if shortCondition and sectionShortConditions > 1
  277. // shortMartin := shortMartin * 2
  278.  
  279. // Close Conditions amalgamation for cleaner plots and signals
  280. // Define the plot colors for each close condition
  281. longCloseCol = na
  282. shortCloseCol = na
  283. longCloseCol := long_tp ? purple : long_sl ? maroon : long_ts ? blue : long_call_signal ? black : longCloseCol[1]
  284. shortCloseCol := short_tp ? purple : short_sl ? maroon : short_ts ? blue : short_call_signal ? black : shortCloseCol[1]
  285.  
  286. // Create a single close for all the different closing conditions.
  287. long_close = long_tp or long_sl or long_ts or long_call_signal ? 1 : 0
  288. short_close = short_tp or short_sl or short_ts or short_call_signal ? 1 : 0
  289.  
  290. // Get the time of the last close
  291. last_long_close = na
  292. last_short_close = na
  293. last_long_close := long_close ? time : nz(last_long_close[1])
  294. last_short_close := short_close ? time : nz(last_short_close[1])
  295.  
  296. // Check for a close since your last open.
  297. if long_close and last_long_close[1] > last_longCondition
  298. long_close := 0
  299. if short_close and last_short_close[1] > last_shortCondition
  300. short_close := 0
  301.  
  302.  
  303. // —————————— Plot markers
  304. plotshape(markersOn and longCondition, style=shape.triangleup, color=lime, location=location.belowbar, size=size.small, text="Enter\nLong")
  305. plotshape(markersOn and shortCondition, style=shape.triangledown, color=red, location=location.abovebar, size=size.small, text="Enter\nShort")
  306. plotshape(markersOn and long_close, style=shape.triangledown, color=green, location=location.abovebar, size=size.small, text="Close\nLong")
  307. plotshape(markersOn and short_close, style=shape.triangleup, color=maroon, location=location.belowbar, size=size.small, text="Close\nShort")
  308.  
  309. // —————————— Alert conditions (need to create alerts using ALT-A in TV).
  310. alertcondition(longCondition, "MABB Enter Long", "MABB Enter Long")
  311. alertcondition(shortCondition, "MABB Enter Short", "MABB Enter Short")
  312. alertcondition(long_close, "MABB Close Long", "MABB Close Long")
  313. alertcondition(short_close, "MABB Close Short", "MABB Close Short")
  314.  
  315. // —————————— Comment out this block for study() mode.
  316. // if testPeriod()
  317. // strategy.entry("Long", strategy.long, qty=qty, when=longCondition)
  318. // strategy.entry("Short", strategy.short, qty=qty, when=shortCondition)
  319.  
  320. // strategy.close("Long", when=long_close)
  321. // strategy.close("Short", when=short_close)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement