Stealing other indicators signals

Stealing other indicators signals

AlgoMindLab

đź§  PROMPT 1 (Base Strategy Prompt, for signal extraction and testing):

You are a professional Pine Script v6 developer.

You’ve been building indicators and strategies for the TradingView platform for a long time.

You know the exact difference between an indicator and a strategy, and you never use code that is unsupported in Pine Script v6.


Please help me build a strategy for backtesting external indicators.

This strategy should receive buy/sell signals from another indicator using the input.source function and execute entries and exits based on those signals.


Let’s start with two signals only — one for Buy and one for Sell — and trigger trades accordingly.


However, such a strategy would be too basic. So let’s add the ability to set:


Take Profit and Stop Loss levels (in percent)


A Break Even option (also in percent)


Make sure that if TP, SL, or BE are set to 0, they will simply not be used.


Also:


Add the option to exit the position on an opposite signal — but make it toggleable via checkbox


Add options to enable/disable Long and Short trades separately — so we can test indicators properly


Please write the full code with these features:








CODE THAT I HAVE AFTER 1 PROMPT:


//@version=6


strategy("External Signals Strategy Tester", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)




// === Signal Inputs ===


longSignal = input.source(title="Buy Signal", defval=close)


shortSignal = input.source(title="Sell Signal", defval=close)




// === Trade Settings ===


enableLong  = input.bool(true, "Enable Long")


enableShort  = input.bool(true, "Enable Short")


closeOnReverse = input.bool(true, "Close on Opposite Signal")


reversePosition = input.bool(false, "Reverse Position on Opposite Signal")




// === Risk Management (in %) ===


takeProfitPerc = input.float(2.0, "Take Profit (%)", step=0.1)


stopLossPerc = input.float(1.0, "Stop Loss (%)", step=0.1)


breakevenPerc = input.float(1.0, "BreakEven (%)", step=0.1)




// === Entry Conditions ===


longCondition = ta.crossover(longSignal, 0)


shortCondition = ta.crossover(shortSignal, 0)




// === Position Reversal or Exit Logic ===


var bool reverseDone = false // Prevents double execution within the same bar


reverseDone := false




// === Reverse from Short to Long ===


if reversePosition and strategy.position_size < 0 and longCondition and enableLong


 strategy.close("Short", comment="Reverse to Long")


 strategy.entry("Long", strategy.long)


 reverseDone := true




// === Reverse from Long to Short ===


if reversePosition and strategy.position_size > 0 and shortCondition and enableShort


 strategy.close("Long", comment="Reverse to Short")


 strategy.entry("Short", strategy.short)


 reverseDone := true




// === Close on opposite signal (if reversal is disabled) ===


if not reversePosition and closeOnReverse


 if strategy.position_size > 0 and shortCondition


  strategy.close("Long", comment="Close on opposite signal (Short)")


 if strategy.position_size < 0 and longCondition


  strategy.close("Short", comment="Close on opposite signal (Long)")




// === Standard Entries (only if no position and no reversal) ===


if strategy.position_size == 0 and not reverseDone


 if enableLong and longCondition


  strategy.entry("Long", strategy.long)


 if enableShort and shortCondition


  strategy.entry("Short", strategy.short)




// === Position Management: TP / SL / BE ===


if strategy.position_size != 0


 entryPrice = strategy.position_avg_price


 profitPerc = (close - entryPrice) / entryPrice * 100 * (strategy.position_size < 0 ? -1 : 1)




 // Take Profit


 if takeProfitPerc > 0 and profitPerc >= takeProfitPerc


  strategy.close_all(comment="Take Profit")




 // Stop Loss


 if stopLossPerc > 0 and profitPerc <= -stopLossPerc


  strategy.close_all(comment="Stop Loss")




 // BreakEven


 if breakevenPerc > 0 and profitPerc >= breakevenPerc


  if strategy.position_size > 0


   strategy.exit("BE Long", from_entry="Long", stop=entryPrice)


  else if strategy.position_size < 0


   strategy.exit("BE Short", from_entry="Short", stop=entryPrice)




// === Signal Visualization ===


plotshape(longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)


plotshape(shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)






đź§  PROMPT 2 (Strategy extension with signal logic from 3 MAs):

I found an indicator that uses a signal system based on MA1 and MA2 crossovers:


A Buy signal is triggered when MA1 crosses MA2 from below


A Sell signal is triggered when MA1 crosses MA2 from above


Additionally, there’s a filter system using a Long-Term MA that acts as a trend confirmation:


For example, a signal is only valid if the Long-Term MA is above or below the crossover point, depending on the direction


Please modify the previous strategy to receive three input.source inputs (for MA1, MA2, and the Long-Term MA), and implement the logic accordingly based on this indicator's behavior.

Report Page