Pinescript strategy that ONLY RUNS on DAILY data

29 views Asked by At

I was trying to implement a simple crossover entry + % take profit + % stop loss strategy that ONLY RUNS on DAILY data.

There are trades, etc registered on the backtest report. To confirm that this is only on daily data, I tried changing data view using the options on chart: 1D, 5D, 1M... All.

I was expecting that trade sheet wont change as data frequency is fixed, but it changed (viewed in Strategy Tester ยป List of Trades). When I switch from '1Y' to '5Y' on the chart, the trades (including the recent ones) that I see are different. Currently for me, this is unexpected behaviour.

Because it changed, I suspect I have coded something other than what I want. Any insights/hints on where I'm messing up?

Thanks. reproducible strategy in few lines and accompanying code with 'Gold' as an example ticker. Code also plots crossovers and other stuff

//@version=5

// TrialStrategy1 pseudocode
// if SMA(5 Day) > SMA(50 Day) -> Long Entry
// TP = Entry * (1 + 2%)
// SL = Entry * (1 - 2%)

strategy("TrialStrategy1", overlay=true, margin_long=100, margin_short=100)

//Variables
takeProfitF = 0.02
stopLossF = 0.02
fastDayPeriod1 = 5
slowDayPeriod1 = 50

// Figure out take profit price
longExitTakeProfitPrice  = strategy.position_avg_price * (1 + takeProfitF)
longExitStopLossPrice = strategy.position_avg_price * (1 - stopLossF)

// This is only to aid plotting
var strategyBuyBool = false

AuPricesClose = request.security("GOLD", "1D",close)

fastDayPeriod1SMA = ta.sma(AuPricesClose,fastDayPeriod1)
slowDayPeriod1SMA = ta.sma(AuPricesClose,slowDayPeriod1)

longCondition = ta.crossover(fastDayPeriod1SMA, slowDayPeriod1SMA)

if (longCondition)
    strategy.entry("Long Entry Id", strategy.long)
    strategyBuyBool = true
    // Placing the below exit in same scope so that it starts on the same candle
    strategy.exit("Long Exit Id", from_entry = "Long Entry Id", limit=longExitTakeProfitPrice, stop = longExitStopLossPrice)

plot(fastDayPeriod1SMA, color = color.green)
plot(slowDayPeriod1SMA, color = color.red)

plotshape(longCondition, color = color.lime, style=shape.triangleup, location=location.belowbar)
plotshape(strategyBuyBool, color = color.green, style=shape.arrowup, location=location.belowbar)
0

There are 0 answers