Advertisement
Guest User

TTS by Von

a guest
Mar 25th, 2021
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. //@version=4
  2. //VonKrieger
  3. // To change this from a Strategy to a Indicator:-
  4. // 1) uncomment the next line and comment out the strategy line.
  5. // 2) at the end of the file comment out the last 2 lines
  6.  
  7. // study(title="VonKrieger_Turtle_Indicator", overlay=true)
  8. strategy(title="VonKrieger_Turtle_Strategy", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1, pyramiding=5)
  9.  
  10. stopInput = input(2.0, "Stop N", step=.5)
  11. pyramidInput = input(1, "Pyramid N", step=.5)
  12. l1LongInput = input(20, "L1 Long", minval=5)
  13. l2LongInput = input(55, "L2 Long", minval=5)
  14. l1LongExitInput = input (10, "L1 Long Exit", minval=5)
  15. l2LongExitInput = input (20, "L2 Long Exit", minval=5)
  16.  
  17. FromYear = input(2000, "From Year", minval=1900), FromMonth = input(1, "From Month", minval=1, maxval=12), FromDay = input(1, "From Day", minval=1, maxval=31)
  18. ToYear = input(9999, "To Year", minval=1900), ToMonth = input(1, "To Month", minval=1, maxval=12), ToDay = input(1, "To Day", minval=1, maxval=31)
  19. FromDate = timestamp(FromYear, FromMonth, FromDay, 00, 00), ToDate = timestamp(ToYear, ToMonth, ToDay, 23, 59)
  20. TradeDateIsAllowed() => time >= FromDate and time <= ToDate
  21. l1Long = highest(l1LongInput)
  22. l1LongExit = lowest(l1LongExitInput)
  23. l2Long = highest(l2LongInput)
  24. l2LongExit = lowest(l2LongExitInput)
  25.  
  26. bool win = false // tracks if last trade was winning trade of losing trade.
  27. float totalPrice = 0.0 // tracks total price, used for calculating avgPrice
  28. float buyPrice = 0.0 // tracks the buy price of the last long position.
  29. float avgPrice = 0.0 // tracks the avg price of all currently held long positions.
  30. float nextBuyPrice = 0.0 // tracks the next buy price
  31. float stopPrice = na // tracks the stop price
  32. int totalBuys = 0 // tracks the total # of pyramid buys
  33. bool inBuy = false // tracks if we are in a long position or not.
  34. float l1LongPlot = highest(l1LongInput) // tracks the L1 price to display
  35. float l2LongPlot = highest(l2LongInput) // tracks the L2 price to display
  36. float n = atr(14) // tracks the n used to calculate stops and pyramid buys
  37. string mode = 'L1' // tracks whether we are in a L1 position or L2 position.
  38. bool fake = na // tracks if this is a fake trade, see comments below.
  39. string longLevel = na // tracks where long positions, stops, pyramid buys occur.
  40.  
  41. // by default use the last value from the previous bar.
  42. buyPrice := buyPrice[1]
  43. totalBuys := totalBuys[1]
  44. nextBuyPrice := nextBuyPrice[1]
  45. stopPrice := stopPrice[1]
  46. avgPrice := avgPrice[1]
  47. totalPrice := totalPrice[1]
  48. win := win[1]
  49.  
  50. // State to track if we are in a long positon or not.
  51. inBuy := not inBuy[1] and (close > l1Long[1] or close > l2Long[1]) ? true : inBuy[1]
  52. inBuy := inBuy[1] and close < stopPrice ? false : inBuy
  53. inBuy := inBuy[1] and mode[1] == 'L1' and close < l1LongExit[1] ? false : inBuy
  54. inBuy := inBuy[1] and mode[1] == 'L2' and close < l2LongExit[1] ? false : inBuy
  55.  
  56.  
  57. // State to track if we are ia a fake trade. If the last trade was a winning, we need to skip the next trade.
  58. // We still track it though as a fake trade (not counted against us). as the outcome determines if we can
  59. // can take the next trade.
  60. fake := not inBuy[1] and close > l1Long[1] and win[1] ? true : fake[1]
  61. fake := not inBuy[1] and close > l1Long[1] and not win[1] ? false : fake
  62. fake := close > l2Long[1] ? false : fake
  63.  
  64. // Series representing the l1 and l2 levels. If we break out above the l1 or l2 level, we want the
  65. // line to stay at the breakout level, not follow it up.
  66. l1LongPlot := iff(not inBuy or (inBuy and mode == 'L1' and fake),l1Long[1],l1LongPlot[1])
  67. l2LongPlot := iff(not inBuy or (inBuy and mode == 'L1' and fake),l2Long[1],l2LongPlot[1])
  68.  
  69. // Variable in the series is only set when it happens. Possible values is L1, L2, SR
  70. // (stopped out with a loss), SG (exited with a gain), and 'P' for pyramid buy.
  71. longLevel := not inBuy[1] and close > l1Long[1] ? 'L1' : na
  72. longLevel := not inBuy[1] and close > l2Long[1] ? 'L2' : longLevel
  73. longLevel := not inBuy[1] and close > l1Long[1] and close > l2Long[1] and fake ? 'L2' : longLevel
  74. // longLevel := longLevel == na and close > l2Long[1] and mode[1] != 'L2' ? 'L2' : longLevel
  75. longLevel := longLevel == na and close > l2Long[1] and mode[1] == 'L1' ? na : longLevel // don't switch to L2 if we are already in a L1
  76. longLevel := longLevel == na and close > l2Long[1] and mode[1] == 'L1' and fake[1] ? 'L2' : longLevel
  77.  
  78. // Either 'L1' or 'L2' depending on what breakout level we are in.
  79. mode := longLevel == na ? mode[1] : longLevel
  80.  
  81. // Variables to track calculating avgPrice and nextBuyPrice for pyramiding.
  82. if longLevel == 'L1' or longLevel == 'L2'
  83. buyPrice = close
  84. totalBuys := 1
  85. totalPrice := close
  86. avgPrice := buyPrice
  87. stopPrice := close - (stopInput*n)
  88. nextBuyPrice := high + (pyramidInput*n)
  89.  
  90. // Marks if we hit our next buy price, if so mark it with a 'P'
  91. longLevel := inBuy[1] and close > nextBuyPrice and TradeDateIsAllowed() and totalBuys < 5 ? 'P' : longLevel
  92.  
  93. if longLevel == 'P'
  94. buyPrice = close
  95. totalBuys := totalBuys[1] + 1
  96. totalPrice := totalPrice[1] + buyPrice
  97. avgPrice := totalPrice / totalBuys
  98. stopPrice := close - (stopInput*n)
  99. nextBuyPrice := high + (pyramidInput*n)
  100.  
  101. // Tracks stops and exits, marking them with SG or SR
  102. longLevel := longLevel == na and inBuy[1] and close < stopPrice and close >= avgPrice ? 'SG' : longLevel
  103. longLevel := longLevel == na and inBuy[1] and close < stopPrice and close < avgPrice ? 'SR' : longLevel
  104. longLevel := longLevel == na and mode[1] == 'L1' and inBuy[1] and (close < l1LongExit[1]) and close >= avgPrice ? 'SG' : longLevel
  105. longLevel := longLevel == na and mode[1] == 'L2' and inBuy[1] and (close < l2LongExit[1]) and close >= avgPrice ? 'SG' : longLevel
  106. longLevel := longLevel == na and mode[1] == 'L1' and inBuy[1] and (close < l1LongExit[1]) and close < avgPrice ? 'SR' : longLevel
  107. longLevel := longLevel == na and mode[1] == 'L2' and inBuy[1] and (close < l2LongExit[1]) and close < avgPrice ? 'SR' : longLevel
  108.  
  109. // Tracks if the trade was a win or loss.
  110. win := longLevel == 'SG' ? true : win
  111. win := longLevel == 'SR' ? false : win
  112.  
  113. // Variables used to tell strategy when to enter/exit trade.
  114. enterLong = (longLevel == 'L1' or longLevel == 'L2' or longLevel == 'P') and not fake and TradeDateIsAllowed()
  115. exitLong = (longLevel == 'SG' or longLevel == 'SR') and not fake and TradeDateIsAllowed()
  116.  
  117. p1 = plot(l1LongPlot, title="l1 long", linewidth=3, style=plot.style_stepline, color=color.green)
  118. p2 = plot(l1LongExit[1], title="l1 exit", linewidth=3, style=plot.style_stepline, color=color.red)
  119. p3 = plot(l2LongPlot, title="l2 long", linewidth=2, style=plot.style_stepline, color=color.green)
  120. p4 = plot(l2LongExit[1], title="l2 exit", linewidth=2, style=plot.style_stepline, color=color.red)
  121. color1 = color.new(color.black, 0)
  122. color2 = color.new(color.black, 100)
  123. col= (inBuy) ? color1 : color2
  124. p5 = plot(stopPrice, title="stop", linewidth=2, style=plot.style_circles, join=true, color=col)
  125. p6 = plot(nextBuyPrice, title="next buy", linewidth=2, style=plot.style_circles, join=true, color=col)
  126.  
  127. fill(p1, p3, color=color.green)
  128. fill(p2, p4, color=color.red)
  129.  
  130. plotshape(longLevel == 'L1' and not fake ? true : false, color=color.green, transp=40, style=shape.triangleup, text="L1") // up arrow for entering L1 trade
  131. plotshape(fake and longLevel == 'L1' ? true : false, color=color.gray, transp=40, style=shape.triangleup, text="L1") // up arrow for entering L1 trade
  132. plotshape((mode == 'L2' or (mode == 'L1' and not fake)) and longLevel == 'P' ? true : false, color=color.green, transp=40, style=shape.triangleup, text='P') // up arrow for entering L1 trade
  133. plotshape(mode == 'L1' and fake and longLevel == 'P' ? true : false, color=color.gray, transp=40, style=shape.triangleup, text='P') // up arrow for entering L1 trade
  134. plotshape(longLevel == 'L2' ? true : false, color=color.green, transp=40, style=shape.triangleup, text="L2") // up arrow for entering L2 trade
  135.  
  136. plotarrow(longLevel == 'L1' ? 1 : 0, colordown=color.black, colorup=color.green, transp=40) // up arrow for entering L1 trade
  137. plotarrow(longLevel == 'L2' ? 1 : 0, colordown=color.black, colorup=color.green, transp=40) // up arrow for entering L2 trade
  138. plotarrow(longLevel == 'SR' ? -1 : 0, colordown=color.red, colorup=color.purple, transp=40) // down arrow for losing trade
  139. plotarrow(longLevel == 'SG' ? -1 : 0, colordown=color.green, colorup=color.purple, transp=40) // down arrow for winning trade
  140.  
  141. // plotarrow(win ? -1 : 0, colordown=color.yellow, colorup=color.purple, transp=40) // down arrow for winning trade
  142. // plotshape(inBuy[1], color=color.blue, transp=40, text='X') // down arrow for winning trade
  143. // label.new(bar_index, high, style=label.style_none, text=longLevel)
  144.  
  145. alertcondition(low < stopPrice, title="crosses under stop price", message="price crossed under stop price")
  146. alertcondition(high > l1Long, title="crosses over L1 price", message="price crossed over L1 price")
  147. alertcondition(high > l2Long, title="crosses over L2 price", message="price crossed over L2 price")
  148. alertcondition(low < l1LongExit, title="crosses under L1 exit price", message="price crossed under L1 exit price")
  149. alertcondition(low < l2LongExit, title="crosses under L2 exit price", message="price crossed under L2 exit price")
  150.  
  151. strategy.entry("long", strategy.long, comment='long', when=enterLong)
  152. strategy.close("long", when=exitLong)
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement