Sure! Below is a PineScript strategy for TradingView that implements the Initial Balance breakout strategy as you've described:
- Initial Balance Period: Defines the range between 9:30 AM and 10:30 AM.
- Buy Signal: Triggers when the price closes above the Initial Balance high.
- Stop Loss: Sets at the Initial Balance low.
- Take Profit: Sets a 1:1 risk-to-reward ratio based on the stop loss distance.
Here’s the code:
//@version=5
strategy("Initial Balance Breakout Strategy", overlay=true, margin_long=100)
// Variables to store Initial Balance high and low
var float ibHigh = na
var float ibLow = na
var bool hasTradedToday = false
// Get time in New York timezone
timeInSession = time
// Reset variables at the start of a new day
newDay = ta.change(time("D", "America/New_York"))
// If new day, reset ibHigh and ibLow, and reset hasTradedToday
if newDay
ibHigh := na
ibLow := na
hasTradedToday := false
// Define Initial Balance period (9:30 AM to 10:30 AM New York time)
sessionStart = timestamp("America/New_York", year, month, dayofmonth, 9, 30)
sessionEnd = timestamp("America/New_York", year, month, dayofmonth, 10, 30)
inIBPeriod = timeInSession >= sessionStart and timeInSession <= sessionEnd
// Update ibHigh and ibLow during Initial Balance period
if inIBPeriod
ibHigh := ibHigh == na ? high : math.max(ibHigh, high)
ibLow := ibLow == na ? low : math.min(ibLow, low)
// Plot ibHigh and ibLow
plot(ibHigh, color=color.green, title="Initial Balance High", linewidth=2)
plot(ibLow, color=color.red, title="Initial Balance Low", linewidth=2)
// After Initial Balance period, look for a close above ibHigh
enterTrade = not inIBPeriod and ibHigh != na and close > ibHigh and close[1] <= ibHigh and not hasTradedToday
if enterTrade
entryPrice = close
stopLossPrice = ibLow
takeProfitPrice = entryPrice + (entryPrice - stopLossPrice) // 1:1 Risk-Reward
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", "Long", stop=stopLossPrice, limit=takeProfitPrice)
hasTradedToday := true // Prevent multiple trades in one day
Explanation:
-
Time Definitions:
- Uses
timestamp to define the start and end of the Initial Balance period in the "America/New_York" timezone.
inIBPeriod checks if the current bar's timestamp falls within this period.
-
Initial Balance Calculation:
- During the Initial Balance period, the script updates
ibHigh and ibLow with the highest high and the lowest low.
var declares the variables only once, retaining their values across bars until explicitly changed.
-
Plotting:
- Plots the Initial Balance high and low as lines on the chart for visual reference.
-
Trade Execution:
- After the Initial Balance period, if the price closes above the
ibHigh and the previous close was at or below ibHigh, it triggers a buy signal.
- Sets
stopLossPrice at ibLow.
- Calculates
takeProfitPrice to maintain a 1:1 risk-to-reward ratio.
- Uses
strategy.entry to enter a long position and strategy.exit to set the stop loss and take profit.
- Ensures only one trade per day by using the
hasTradedToday flag.
Notes:
- Timezone Handling: The script uses the "America/New_York" timezone to align with market hours.
- Single Trade per Day: The
hasTradedToday variable prevents multiple trades in a single day.
- Risk Management: Adjust
margin_long if needed to match your account's leverage or margin settings.
How to Use:
-
Apply the Script:
- Copy and paste the script into TradingView's Pine Editor.
- Click on "Add to Chart".
-
Adjust Settings (Optional):
- Modify the session times if you need a different Initial Balance period.
- Change risk management parameters as needed.
-
Backtesting:
- Use TradingView's strategy tester to evaluate the performance over your chosen timeframe and assets.
Disclaimer:
- Always test the strategy thoroughly before using it for live trading.
- Past performance does not guarantee future results.
- Be aware of slippage and trading fees that may affect real-world performance.
Let me know if you have any questions or need further customization!