Guest User

Lege - System

a guest
Oct 5th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. //@version=5
  2. strategy("Lege - System", overlay=true, initial_capital = 1000)
  3.  
  4. // Input for adjustable Risk-Reward ratio
  5. RR = input.float(2.0, title="Risk:Reward", step=0.1)
  6.  
  7. // Input for risk percentage of equity per trade
  8. riskPercentOfEquity = input.float(1, title="Risk per Trade (%)", step=1) / 100
  9.  
  10. // Function to calculate the position size based on risk and stop loss distance
  11. getBetSize(float stop, float riskPercentOfEquity) =>
  12. onePercentCapital = strategy.equity * riskPercentOfEquity
  13. betSize = onePercentCapital / math.abs(close - stop)
  14.  
  15. // Define the start of the week (Sunday 00:00 UTC)
  16. var bool is_first_week_candle = false
  17. var bool plot_label = false // To control label plotting
  18. week_start = (dayofweek == dayofweek.sunday and hour == 0) // Weekly open is on Sunday at 00:00 UTC
  19.  
  20. // Variables to store the high and low of the first 1-hour candle of the week
  21. var float week_open_candle_high = na
  22. var float week_open_candle_low = na
  23. var bool long_trade_opened = false
  24. var bool short_trade_opened = false
  25.  
  26. // Counters to limit trades to one long and one short per week
  27. var int long_trade_count = 0
  28. var int short_trade_count = 0
  29.  
  30. // If it's the first candle of the week, store its high and low values
  31. if (week_start)
  32. week_open_candle_high := high // Set the high of the first hour candle
  33. week_open_candle_low := low // Set the low of the first hour candle
  34. is_first_week_candle := true // Mark that the first candle has been processed
  35. plot_label := true // Enable label plotting
  36. long_trade_opened := false // Reset long trade flag
  37. short_trade_opened := false // Reset short trade flag
  38. long_trade_count := 0 // Reset long trade counter
  39. short_trade_count := 0 // Reset short trade counter
  40.  
  41. // Plot a blue label above the first candle of the week, but only once
  42. plotshape(series=plot_label, location=location.abovebar, color=color.blue, style=shape.labeldown, size=size.small)
  43. plot_label := false // Reset after plotting the label
  44.  
  45. // Trade signals based on breakout of the first week's candle
  46. if (is_first_week_candle)
  47. // Long trade condition: Price breaks above the high of the first candle
  48. if (close > week_open_candle_high and not long_trade_opened and long_trade_count < 1)
  49. stop_loss_long = week_open_candle_low // Set stop loss at the low of the first candle
  50. take_profit_long = close + RR * (close - week_open_candle_low) // Set take profit using the RR ratio
  51. bet_size_long = getBetSize(stop_loss_long, riskPercentOfEquity) // Calculate the position size based on risk
  52. long_trade_opened := true // Mark that a long trade has been opened
  53. long_trade_count += 1 // Increment the long trade counter for the week
  54. strategy.entry("Long", strategy.long, bet_size_long) // Enter the long trade
  55. strategy.exit("Long exit", "Long", limit=take_profit_long, stop=stop_loss_long) // Set take profit and stop loss for the trade
  56.  
  57. // Short trade condition: Price breaks below the low of the first candle
  58. if (close < week_open_candle_low and not short_trade_opened and short_trade_count < 1)
  59. stop_loss_short = week_open_candle_high // Set stop loss at the high of the first candle
  60. take_profit_short = close - RR * (week_open_candle_high - close) // Set take profit using the RR ratio
  61. bet_size_short = getBetSize(stop_loss_short, riskPercentOfEquity) // Calculate the position size based on risk
  62. short_trade_opened := true // Mark that a short trade has been opened
  63. short_trade_count += 1 // Increment the short trade counter for the week
  64. strategy.entry("Short", strategy.short, bet_size_short) // Enter the short trade
  65. strategy.exit("Short exit", "Short", limit=take_profit_short, stop=stop_loss_short) // Set take profit and stop loss for the trade
  66.  
  67. // Close all open trades at the end of the week (Friday 23:59 UTC)
  68. if (dayofweek == dayofweek.friday and hour == 23 and minute == 59)
  69. strategy.close("Long")
  70. strategy.close("Short")
  71. is_first_week_candle := false // Reset the flag to allow a new week's trades
Advertisement
Add Comment
Please, Sign In to add comment