Advertisement
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
- // This sets the reward relative to the risk taken on a trade
- RR = input.float(2.0, title="Risk:Reward", step=0.1)
- // Input for risk percentage of equity per trade
- // This allows the user to set how much of their equity they want to risk on a single 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
- // The function takes two arguments:
- // stop - the stop loss price, and
- // riskPercentOfEquity - the percentage of equity to risk per trade
- getBetSize(float stop, float riskPercentOfEquity) =>
- onePercentCapital = strategy.equity * riskPercentOfEquity // Calculate the amount of equity to risk
- betSize = onePercentCapital / math.abs(close - stop) // Divide by the distance to the stop to get position size
- // Define the start of the week (Monday 00:00 UTC)
- // This sets a condition that is true only when the week starts
- var bool is_first_week_candle = false
- week_start = (dayofweek == dayofweek.monday and hour == 0)
- // Variables to store the high and low of the first 1-hour candle of the week
- // These will be used to determine the breakout levels for the week
- var float week_open_candle_high = na
- var float week_open_candle_low = na
- var bool long_trade_opened = false // To track if a long trade has been opened
- var bool short_trade_opened = false // To track if a short trade has been opened
- // Counters to limit trades to one long and one short per week
- var int long_trade_count = 0 // Tracks number of long trades this week
- var int short_trade_count = 0 // Tracks number of short trades this week
- // 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
- 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
- // Trade signals based on breakout of the first week's candle
- // This checks if the price breaks above or below the high or low of the first candle
- if (is_first_week_candle)
- // Long trade condition: Price breaks above the high of the first candle
- // and no long trade has been opened this week
- 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
- // and no short trade has been opened this week
- 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)
- // This ensures that no positions are carried over into the next week
- 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
Advertisement