Maurizio-Ciullo

Indicatore Candlestick Morning-Evening-Star

Jan 26th, 2022 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Maurizio-Ciullo
  3.  
  4. // Candelstick Pattern Morning-Evening-Star
  5.  
  6. //@version=4
  7.  
  8. strategy(title="Bot Good-Evening-Morning-Star ETH/PERP FTX 4H LONG E SHORT", shorttitle = "E/M Star", overlay=true,
  9.      pyramiding=0, initial_capital=150,
  10.      commission_type=strategy.commission.percent,
  11.      commission_value=0.1, slippage=3,
  12.      default_qty_type=strategy.percent_of_equity,
  13.      default_qty_value=27.5)
  14.  
  15. //  Inputs
  16.  
  17. input_take_prof_long_mult = input(title="take_prof_long_mult", type=input.float, defval=2.5, minval=0, maxval=100000, step=0.1, group="Profit")
  18. input_stop_loss_long_mult = input(title="stop_loss_long_mult", type=input.float, defval=2.5, minval=0, maxval=100000, step=0.1, group="Stop")
  19.  
  20. input_take_prof_short_mult = input(title="take_prof_short_mult", type=input.float, defval=2.5, minval=0, maxval=100000, step=0.1, group="Profit")
  21. input_stop_loss_short_mult = input(title="stop_loss_short_mult", type=input.float, defval=2.5, minval=0, maxval=100000, step=0.1, group="Stop")
  22.  
  23. // Variables
  24.  
  25. stop_loss_long_ticks = abs(strategy.position_avg_price - high[2]) / syminfo.mintick
  26. plot(stop_loss_long_ticks, title="stop_loss_long_ticks", linewidth=3, color=color.orange)
  27. stop_loss_long_mult = stop_loss_long_ticks * input_stop_loss_long_mult / 100
  28. plot(stop_loss_long_mult, title="stop_loss_long_mult", color=color.purple, transp=3, linewidth=4)
  29. stop_loss_long_e_mult_ticks = stop_loss_long_ticks + stop_loss_long_mult
  30. plot(stop_loss_long_e_mult_ticks, title="stop_loss_long_e_mult_ticks", color=color.red, transp=3, linewidth=4)
  31. stop_loss_long = strategy.position_avg_price + stop_loss_long_e_mult_ticks / 10
  32. plot(stop_loss_long, title="stop_loss_long", color=color.yellow, transp=3, linewidth=4)
  33.  
  34. //* imput_stop_loss_long
  35.  
  36. // Detect morning/shooting star start /////////////////////////////////////////////////////////////////////////////////////////////
  37.  
  38. //candle1 = left candle, candle2 = middle candle, candle3 = right candle
  39. candle1_Open = open[2]
  40. candle1_Close = close [2]
  41. candle2_Open = open[1]
  42. candle2_Close = close[1]
  43. candle2_Low = low[1]
  44. candle2_High = high[1]
  45. candle3_Open = open
  46. candle3_Close = close
  47.  
  48. //bullish calc first
  49. candle1_BodySize = candle1_Open - candle1_Close
  50. candle2_BodySize = candle2_Close - candle2_Open
  51. candle2_WickLength = candle2_High - candle2_Close
  52. candle3_BodySize = candle3_Close - candle3_Open
  53.  
  54. //logic to figure out bullishish Star pattern formed
  55. bullishStar = (candle1_Close < candle1_Open) and ((candle2_Open <= candle2_Close) and (candle2_BodySize <= (.333333*(candle1_BodySize))) and (candle2_WickLength > candle2_BodySize)) and ((candle3_Open >= candle2_Close) and (candle3_Close > candle3_Open) and (candle2_BodySize <= (.333333*(candle3_BodySize))) and (candle3_Close > candle1_Open))
  56.  
  57. //plot the indicator on chart
  58. plotshape(bullishStar, style = shape.triangleup, location = location.belowbar, color=color.green, size = size.small)
  59.  
  60. //now calc bearish
  61. candle1_BodySize := candle1_Close - candle1_Open
  62. candle2_BodySize := candle2_Open - candle2_Close
  63. candle2_WickLength := candle2_Close - candle2_Low
  64. candle3_BodySize := candle3_Open - candle3_Close
  65.  
  66. //logic to figure out bearish Star pattern forming
  67. bearishStar = (candle1_Close > candle1_Open) and ((candle2_Open >= candle2_Close) and (candle2_BodySize <= (.333333*(candle1_BodySize))) and (candle2_WickLength > candle2_BodySize)) and ((candle3_Open >= candle2_Close) and (candle3_Close < candle3_Open) and (candle2_BodySize <= (.333333*(candle3_BodySize))) and (candle3_Close < candle1_Open))
  68.  
  69. //plot the indicator on chart
  70. plotshape(bearishStar, style = shape.triangledown, location = location.abovebar, color=color.red, size = size.small)
  71.  
  72. //below allows you to setup alerts
  73. alertcondition(bullishStar, title = "Bullish Star", message = "[CurrencyPair] [TimeFrame], Bullish Star formed")
  74. alertcondition(bearishStar, title = "Bearish Star", message = "[CurrencyPair] [TimeFrame], Bearish Star formed")
  75.  
  76. ////================================BACKTEST================================//Uncomment this section if backtesting (must also switched this from a study to a strategy above)
  77.  
  78. // // === INPUT BACKTEST RANGE ===
  79. // FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
  80. // FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  81. // FromYear = input(defval = 2017, title = "From Year", minval = 2017)
  82. // ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
  83. // ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  84. // ToYear = input(defval = 9999, title = "To Year", minval = 2017)
  85.  
  86. // // === FUNCTION EXAMPLE ===
  87. // start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
  88. // finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
  89. // window() => time >= start and time <= finish ? true : false // create function "within window of time"
  90.  
  91. // // === EXECUTION === //Keep this commented out unless backtesting
  92. // strategy.entry("L", strategy.long, 25000, when = bullishStar == 1 and window()) // buy long when "within window of time" AND crossover
  93. // strategy.exit("exit", "L", profit = 40, loss = 20)
  94.  
  95. // strategy.entry("S", strategy.short, 25000, when = bearishStar == 1 and window()) // buy long when "within window of time" AND crossover
  96. // // strategy.exit("exit", "S", profit = 40, loss = 20)
  97.  
  98. // Detect morning/shooting star end /////////////////////////////////////////////////////////////////////////////////////////////
  99.  
  100. // STEP 1 DATARANGE:
  101. // Make input options that configure backtest date range
  102.  
  103. startDate = input(title="Start Date", type=input.integer,
  104.      defval=18, minval=1, maxval=31, group="Periodo")
  105. startMonth = input(title="Start Month", type=input.integer,
  106.      defval=08, minval=1, maxval=12, group="Periodo")
  107. startYear = input(title="Start Year", type=input.integer,
  108.      defval=2017, minval=1800, maxval=2100, group="Periodo")
  109.  
  110. endDate = input(title="End Date", type=input.integer,
  111.      defval=01, minval=1, maxval=31, group="Periodo")
  112. endMonth = input(title="End Month", type=input.integer,
  113.      defval=01, minval=1, maxval=12, group="Periodo")
  114. endYear = input(title="End Year", type=input.integer,
  115.      defval=2121, minval=1800, maxval=2100, group="Periodo")
  116.  
  117. // STEP 2 DATARANGE:
  118. // Look if the close time of the current bar
  119. // falls inside the date range
  120.  
  121. inDateRange = (time >= timestamp(syminfo.timezone, startYear,
  122.          startMonth, startDate, 0, 0)) and
  123.      (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  124.      
  125. //Condizione Entry Long: Chiusura candela bullishStar
  126. condEntryLong = bullishStar and inDateRange
  127. //Condizione Exit Long: Low candela - percentuale moltiplicatore stop
  128. condExitLong = close < low[4] //- input_stop_loss_long_mult
  129.  
  130. // Condizione Entry Short: Chiusura candela bearishStar
  131. condEntryShort = bearishStar and inDateRange
  132. // Condizione Exit Short: High candela + percentuale moltiplicatore stop
  133. condExitShort = close > high[4] //+ input_stop_loss_short_mult
  134.  
  135. // Esecuzione Ordini
  136.  
  137. if (condEntryLong)
  138.     strategy.entry("long", true)
  139.     strategy.exit("stop loss", from_entry = "long", stop = low[1])
  140.    
  141. //if (condExitLong)    
  142. //    strategy.close(id="long")
  143.    
  144. if (condEntryShort)
  145.     strategy.entry("short", false)
  146.     strategy.exit("stop loss", from_entry = "short", stop = high[1])
  147.    
  148. //if (condExitShort)    
  149. //    strategy.close(id="short")
  150.  
  151. //from_entry_to_stop = (strategy.position_avg_price - high[2])
  152. //plot(from_entry_to_stop, title="from_entry_to_stop")
  153.  
  154. //plot(high[2] - strategy.position_avg_price * 0.01 * 10, title="stop_loss_limit_short", color=color.purple, transp=3, linewidth=4)
  155. //plot(abs(strategy.position_avg_price - high[2]) / syminfo.mintick / 100 * 100, title="stop_loss_long")//* imput_stop_loss_long
  156.  
  157.  
Add Comment
Please, Sign In to add comment