Reputation: 1
Hi Pine Script community,
I’m hitting a wall with a strategy in Pine Script v6
and could use your expertise. I’ve built a strategy that uses KST crossover signals from a custom pair chart (not SP:SPX
or TVC:GOLD
) to trade two symbols: go long SP:SPX
on a KST up-cross, go long TVC:GOLD
on a down-cross, with $1M fully invested each trade. The KST signals come from the custom pair’s close
, but trades execute using SP:SPX
and TVC:GOLD
prices fetched via request.security()
.
Problem: My custom equity plot (using spxClose
and goldClose
) tracks SP:SPX
and TVC:GOLD
correctly—trends up post-April 2018 as expected. But the Strategy Tester’s “List of Trades” tab shows the custom pair’s close
prices instead of SP:SPX
and TVC:GOLD
closing prices. This also messes up “Profit” and “Cumulative Profit” columns, making them reflect the pair’s price moves, not SPX/GOLD.
What I’ve Tried:
Fetched Prices: Used spxClose = request.security("SP:SPX", timeframe.period, close)
and goldClose = request.security("TVC:GOLD", timeframe.period, close)
for trade sizing (qty=strategy.equity / spxClose
).
Custom P&L: Calculated profit
manually with spxQty * (spxClose - spxEntryPrice)
and plotted equity = 1000000 + profit
—works great, but Tester ignores it.
Price Override: Tried setting a tradePrice
variable to switch between spxClose
and goldClose
—no dice, Tester sticks to chart’s close
.
Indicator Mode: Switched to indicator()
for pure simulation—equity plotted fine but lost Tester functionality.
Timing Fix: Delayed trades one bar (monthly chart) so P&L aligns with execution month (e.g., February for January signal)—fixed equity timing but not Tester prices.
**Code Snippet (Simplified): **pinescript
//@version=6
strategy("KST SPX/GOLD Switch", overlay=false, initial_capital=1000000)
// KST from custom pair
float kst = ta.sma(ta.roc(close, 8), 8) + (2 * ta.sma(ta.roc(close, 12), 8)) + (3 * ta.sma(ta.roc(close, 30), 8)) + (4 * ta.sma(ta.roc(close, 50), 21))
float signal = ta.sma(kst, 8)
bool buySpxSignal = ta.crossover(kst, signal)[1] // Delayed one bar
bool buyGoldSignal = ta.crossunder(kst, signal)[1]
// Fetch prices
float spxClose = request.security("SP:SPX", timeframe.period, close)
float goldClose = request.security("TVC:GOLD", timeframe.period, close)
// Track positions
var float spxQty = 0.0, goldQty = 0.0, profit = 0.0, spxEntryPrice = 0.0, goldEntryPrice = 0.0
if buySpxSignal and not na(spxClose)
if goldQty > 0
profit := profit + (goldQty * (goldClose - goldEntryPrice))
goldQty := 0.0
strategy.close("GOLD Long")
spxQty := strategy.equity / spxClose
strategy.entry("SPX Long", strategy.long, qty=spxQty)
spxEntryPrice := spxClose
if buyGoldSignal and not na(goldClose)
if spxQty > 0
profit := profit + (spxQty * (spxClose - spxEntryPrice))
spxQty := 0.0
strategy.close("SPX Long")
goldQty := strategy.equity / goldClose
strategy.entry("GOLD Long", strategy.long, qty=goldQty)
goldEntryPrice := goldClose
float equity = 1000000 + profit
plot(equity, color=color.purple, title="Equity ($)", linewidth=2)
Goal: Get “List of Trades” to show `SP:SPX` and `TVC:GOLD` closing prices (e.g., ~$5,000 for SPX, ~$2,000 for GOLD in 2025) instead of my pair’s `close`. “Profit” and “Cumulative Profit” should follow suit.
Question: Is there a way to force the Strategy Tester to use `spxClose` and `goldClose` for its trade list prices? Am I missing a trick with `strategy.entry()` or another function? Or is this a hard TradingView limitation when trading external symbols on a custom chart?
Any help or wild ideas appreciated—I’m on a monthly chart, v6, and the equity plot’s solid, but the Tester’s display is driving me nuts! Thanks!
Upvotes: 0
Views: 31