hey guys, i have built a script that goes long or short based on changes in slope of the MACD histogram, i use the 5 minute chart as default. However there is not enough granularity in the 5 minute charts for the exit positions which are based on percentage win and loss, if anyone knows how to make it so it makes exit decisions based on the 1 second chart, or 1 minute chart and entry decisions based on the 5 minute in the same strategy i would really appreciate it. The code is below.
//@version=5
strategy(title="MACD Histogram Slope Change Strategy", initial_capital=150000, use_bar_magnifier = true, currency= currency.USD, default_qty_type= strategy.percent_of_equity, default_qty_value=90)
longLossPerc = input.float(1, title="Long Stop Loss (%)",
minval=0.0, step=0.01) * 0.01
shortLossPerc = input.float(0.1, title="Short Stop Loss (%)",
minval=0.0, step=0.01) * 0.01
// this is for the percent_of_equity
longProfitPerc = input.float(5, title="Long Take Profit (%)",
minval=0.0, step=0.1) * 0.01
shortProfitPerc = input.float(5, title="Short Take Profit (%)",
minval=0.0, step=0.1) * 0.01
// MACD settings
fastLength = input(12, "Fast Length")
slowLength = input(26, "Slow Length")
signalSmoothing = input(9, "Signal Smoothing")
// Using built-in function to get MACD values
[macdLine, signalLine, histogram] = ta.macd(close, fastLength, slowLength, signalSmoothing)
// Determining the direction change of the histogram
histogramPrev1 = nz(histogram[1], 0)
histogramPrev2 = nz(histogram[2], 0)
// Conditions for long entry and exit
longCondition = histogramPrev1 < histogramPrev2 and histogram > histogramPrev1
shortCondition = histogramPrev1 > histogramPrev2 and histogram < histogramPrev1
// Entering and exiting positions
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
longStopPrice = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)
longExitPrice = strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)
// Plotting
plot(macdLine, "MACD Line", color.blue)
plot(signalLine, "Signal Line", color.red)
plot(histogram, "Histogram", color=color.green, style=plot.style_histogram)
// Plotting buy and sell signals
if strategy.position_size > 0
strategy.exit("XL STP", stop=longStopPrice)
if strategy.position_size < 0
strategy.exit("XS STP", stop=shortStopPrice)
if strategy.position_size > 0
strategy.exit("XL TP", limit=longExitPrice)
if strategy.position_size < 0
strategy.exit("XS TP", limit=shortExitPrice)