Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("Lege - System", overlay=true, initial_capital = 1000)
- // Input for adjustable Risk-Reward ratio
- RR = input.float(2.0, title="Risk:Reward", step=0.1)
- // Input for risk percentage of equity per trade
- riskPercentOfEquity = input.float(1, title="Risk per Trade (%)", step=1) / 100
- // Function to calculate the position size based on risk and stop loss distance
- getBetSize(float stop, float riskPercentOfEquity) =>
- onePercentCapital = strategy.equity * riskPercentOfEquity
- betSize = onePercentCapital / math.abs(close - stop)
- // Define the start of the week (Sunday 00:00 UTC)
- var bool is_first_week_candle = false
- var bool plot_label = false // To control label plotting
- week_start = (dayofweek == dayofweek.sunday and hour == 0) // Weekly open is on Sunday at 00:00 UTC
- // Variables to store the high and low of the first 1-hour candle of the week
- var float week_open_candle_high = na
- var float week_open_candle_low = na
- var bool long_trade_opened = false
- var bool short_trade_opened = false
- // Counters to limit trades to one long and one short per week
- var int long_trade_count = 0
- var int short_trade_count = 0
- // If it's the first candle of the week, store its high and low values
- if (week_start)
- week_open_candle_high := high // Set the high of the first hour candle
- week_open_candle_low := low // Set the low of the first hour candle
- is_first_week_candle := true // Mark that the first candle has been processed
- plot_label := true // Enable label plotting
- long_trade_opened := false // Reset long trade flag
- short_trade_opened := false // Reset short trade flag
- long_trade_count := 0 // Reset long trade counter
- short_trade_count := 0 // Reset short trade counter
- // Plot a blue label above the first candle of the week, but only once
- plotshape(series=plot_label, location=location.abovebar, color=color.blue, style=shape.labeldown, size=size.small)
- plot_label := false // Reset after plotting the label
- // Trade signals based on breakout of the first week's candle
- if (is_first_week_candle)
- // Long trade condition: Price breaks above the high of the first candle
- if (close > week_open_candle_high and not long_trade_opened and long_trade_count < 1)
- stop_loss_long = week_open_candle_low // Set stop loss at the low of the first candle
- take_profit_long = close + RR * (close - week_open_candle_low) // Set take profit using the RR ratio
- bet_size_long = getBetSize(stop_loss_long, riskPercentOfEquity) // Calculate the position size based on risk
- long_trade_opened := true // Mark that a long trade has been opened
- long_trade_count += 1 // Increment the long trade counter for the week
- strategy.entry("Long", strategy.long, bet_size_long) // Enter the long trade
- strategy.exit("Long exit", "Long", limit=take_profit_long, stop=stop_loss_long) // Set take profit and stop loss for the trade
- // Short trade condition: Price breaks below the low of the first candle
- if (close < week_open_candle_low and not short_trade_opened and short_trade_count < 1)
- stop_loss_short = week_open_candle_high // Set stop loss at the high of the first candle
- take_profit_short = close - RR * (week_open_candle_high - close) // Set take profit using the RR ratio
- bet_size_short = getBetSize(stop_loss_short, riskPercentOfEquity) // Calculate the position size based on risk
- short_trade_opened := true // Mark that a short trade has been opened
- short_trade_count += 1 // Increment the short trade counter for the week
- strategy.entry("Short", strategy.short, bet_size_short) // Enter the short trade
- strategy.exit("Short exit", "Short", limit=take_profit_short, stop=stop_loss_short) // Set take profit and stop loss for the trade
- // Close all open trades at the end of the week (Friday 23:59 UTC)
- if (dayofweek == dayofweek.friday and hour == 23 and minute == 59)
- strategy.close("Long")
- strategy.close("Short")
- is_first_week_candle := false // Reset the flag to allow a new week's trades
Advertisement
Add Comment
Please, Sign In to add comment