I'm having an issue with my script. Upon testing it, if I run the short order script, it gets me a profit factor of 2 or so, then the long order version of the script gives me a profit factor of 1.5 or something. For some reason when I combine the scripts into one, it gives me a profit factor of 0.9.
Note: This is tested on BTCUSDT (Kucoin), with Pyramiding of 10, and everything else is default values
Long script:
//@version=5
strategy("MACD-EMA Strategy", overlay=true)
// Create variable to store TP level
var float longTp = na
var float stopLossLong = na
// MACD settings
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Smoothing")
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)
// 200 Period EMA
ema200 = ta.ema(close, 200)
// Entry conditions
enterLong = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > ema200
// Strategy logic
if (enterLong)
strategy.entry("Long", strategy.long)
alert("Long Order: " + str.tostring(bar_index))
longTp := close + ((close - ema200) * 1.5) // Calculate TP for the long trade
// Take Profit level based on open trades
takeProfit = strategy.position_size > 0 ? longTp : na
// Check if take profit or stop loss level is hit
if strategy.position_size > 0
strategy.exit("Exit TP", "Long", limit=longTp, comment="Take Profit Long")
// Plot TP and Stop Loss lines
plot(takeProfit, color=color.green, title="Take Profit", linewidth=1, style=plot.style_line)
Short script:
//@version=5
strategy("Inverted MACD-EMA Strategy", overlay=true)
// Create variables to store TP and Stop Loss levels
var float shortTp = na
var float stopLossShort = na
// MACD settings
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Smoothing")
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)
// 200 Period EMA
ema200 = ta.ema(close, 200)
// Entry conditions (inverted for short strategy)
enterShort = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < ema200
// Strategy logic
if (enterShort)
strategy.entry("Short", strategy.short)
alert("Short Order: " + str.tostring(bar_index))
shortTp := close - ((ema200 - close) * 1.5) // Calculate TP for the short trade
// Take Profit level based on open trades
takeProfit = strategy.position_size < 0 ? shortTp : na
// Check if take profit or stop loss level is hit
if strategy.position_size < 0
strategy.exit("Exit TP", "Short", limit=shortTp, comment="Take Profit Short")
// Plot TP and Stop Loss lines
plot(takeProfit, color=color.red, title="Take Profit", linewidth=1, style=plot.style_line)
Combined script:
//@version=5
strategy("Combined MACD-EMA Strategy", overlay=true)
// Create variables to store TP and Stop Loss levels for long and short trades
var float longTp = na
var float stopLossLong = na
var float shortTp = na
var float stopLossShort = na
// MACD settings
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Smoothing")
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)
// 200 Period EMA for long trades
ema200Long = ta.ema(close, 200)
// Entry conditions for long trades
enterLong = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > ema200Long
// Strategy logic for long trades
if (enterLong)
strategy.entry("Long", strategy.long)
alert("Long Order: " + str.tostring(bar_index))
longTp := close + ((close - ema200Long) * 1.5) // Calculate TP for the long trade
// Take Profit level for long trades based on open positions
takeProfitLong = strategy.position_size > 0 ? longTp : na
// Check if take profit or stop loss level is hit for long trades
if strategy.position_size > 0
strategy.exit("Exit TP Long", "Long", limit=longTp, comment="Take Profit Long")
// Plot TP and Stop Loss lines for long trades
plot(takeProfitLong, color=color.green, title="Take Profit Long", linewidth=1, style=plot.style_line)
// 200 Period EMA for short trades
ema200Short = ta.ema(close, 200)
// Entry conditions for short trades
enterShort = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < ema200Short
// Strategy logic for short trades
if (enterShort)
strategy.entry("Short", strategy.short)
alert("Short Order: " + str.tostring(bar_index))
shortTp := close - ((ema200Short - close) * 1.5) // Calculate TP for the short trade
// Take Profit level for short trades based on open positions
takeProfitShort = strategy.position_size < 0 ? shortTp : na
// Check if take profit or stop loss level is hit for short trades
if strategy.position_size < 0
strategy.exit("Exit TP Short", "Short", limit=shortTp, comment="Take Profit Short")
// Plot TP and Stop Loss lines for short trades
plot(takeProfitShort, color=color.red, title="Take Profit Short", linewidth=1, style=plot.style_line)
Tried renaming the functions all be separate in the two scripts. No luck.
What's happening here is, when you run both directions, sometimes it interrupts the previous trade before it hits its TP. Hedging is not allowed on Tradingview so any opposite direction entry signal will close the previous trade.
Attached is an example that I tested on BTCBUSD:BINANCE
on 12h timeframe with the default settings.
If I run short only, this is the first trade that takes place. It enters the trade on Dec 31, 2019 and exits the trade with profit on Mar 12, 2020.
Now, when I run it in both directions, a long trade is triggered on Mar 03, 2020. This closes the open short trade with a loss. That's why the statistics are different when you run both directions.