Advertisement
Guest User

Untitled

a guest
Oct 5th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 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. // This sets the reward relative to the risk taken on a trade
  6. RR = input.float(2.0, title="Risk:Reward", step=0.1)
  7.  
  8. // Input for risk percentage of equity per trade
  9. // This allows the user to set how much of their equity they want to risk on a single trade
  10. riskPercentOfEquity = input.float(1, title="Risk per Trade (%)", step=1) / 100
  11.  
  12. // Function to calculate the position size based on risk and stop loss distance
  13. // The function takes two arguments:
  14. // stop - the stop loss price, and
  15. // riskPercentOfEquity - the percentage of equity to risk per trade
  16. getBetSize(float stop, float riskPercentOfEquity) =>
  17. onePercentCapital = strategy.equity * riskPercentOfEquity // Calculate the amount of equity to risk
  18. betSize = onePercentCapital / math.abs(close - stop) // Divide by the distance to the stop to get position size
  19.  
  20. // Define the start of the week (Monday 00:00 UTC)
  21. // This sets a condition that is true only when the week starts
  22. var bool is_first_week_candle = false
  23. week_start = (dayofweek == dayofweek.monday and hour == 0)
  24.  
  25. // Variables to store the high and low of the first 1-hour candle of the week
  26. // These will be used to determine the breakout levels for the week
  27. var float week_open_candle_high = na
  28. var float week_open_candle_low = na
  29. var bool long_trade_opened = false // To track if a long trade has been opened
  30. var bool short_trade_opened = false // To track if a short trade has been opened
  31.  
  32. // Counters to limit trades to one long and one short per week
  33. var int long_trade_count = 0 // Tracks number of long trades this week
  34. var int short_trade_count = 0 // Tracks number of short trades this week
  35.  
  36. // If it's the first candle of the week, store its high and low values
  37. if (week_start)
  38. week_open_candle_high := high // Set the high of the first hour candle
  39. week_open_candle_low := low // Set the low of the first hour candle
  40. is_first_week_candle := true // Mark that the first candle has been processed
  41. long_trade_opened := false // Reset long trade flag
  42. short_trade_opened := false // Reset short trade flag
  43. long_trade_count := 0 // Reset long trade counter
  44. short_trade_count := 0 // Reset short trade counter
  45.  
  46. // Trade signals based on breakout of the first week's candle
  47. // This checks if the price breaks above or below the high or low of the first candle
  48.  
  49. if (is_first_week_candle)
  50. // Long trade condition: Price breaks above the high of the first candle
  51. // and no long trade has been opened this week
  52. if (close > week_open_candle_high and not long_trade_opened and long_trade_count < 1)
  53. stop_loss_long = week_open_candle_low // Set stop loss at the low of the first candle
  54. take_profit_long = close + RR * (close - week_open_candle_low) // Set take profit using the RR ratio
  55. bet_size_long = getBetSize(stop_loss_long, riskPercentOfEquity) // Calculate the position size based on risk
  56. long_trade_opened := true // Mark that a long trade has been opened
  57. long_trade_count += 1 // Increment the long trade counter for the week
  58. strategy.entry("Long", strategy.long, bet_size_long) // Enter the long trade
  59.  
  60. strategy.exit("Long exit", "Long", limit=take_profit_long, stop=stop_loss_long) // Set take profit and stop loss for the trade
  61.  
  62. // Short trade condition: Price breaks below the low of the first candle
  63. // and no short trade has been opened this week
  64. if (close < week_open_candle_low and not short_trade_opened and short_trade_count < 1)
  65. stop_loss_short = week_open_candle_high // Set stop loss at the high of the first candle
  66. take_profit_short = close - RR * (week_open_candle_high - close) // Set take profit using the RR ratio
  67. bet_size_short = getBetSize(stop_loss_short, riskPercentOfEquity) // Calculate the position size based on risk
  68. short_trade_opened := true // Mark that a short trade has been opened
  69. short_trade_count += 1 // Increment the short trade counter for the week
  70. strategy.entry("Short", strategy.short, bet_size_short) // Enter the short trade
  71.  
  72. strategy.exit("Short exit", "Short", limit=take_profit_short, stop=stop_loss_short) // Set take profit and stop loss for the trade
  73.  
  74. // Close all open trades at the end of the week (Friday 23:59 UTC)
  75. // This ensures that no positions are carried over into the next week
  76. if (dayofweek == dayofweek.friday and hour == 23 and minute == 59)
  77. strategy.close("Long")
  78. strategy.close("Short")
  79. is_first_week_candle := false // Reset the flag to allow a new week's trades
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement