Darshan Puranik
Darshan Puranik

Reputation: 1083

Why does my strategy doesnt show any data?

I am writing a simple strategy in pine script for backtesting in TradingView. Logic is simple. If today's close is less than 52 weeks low then purchase INR 10000 worth of stocks. My code looks as follows:

//@version=4
strategy("Darshan 52 week low", overlay=true)

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

// is close/open/high/low is less than 52 week low
if (close < weekly_lc)
    // if yes buy INR 10000 worth of stocks
    quantityToBuy = 10000/close
    strategy.entry("long", strategy.long, quantityToBuy)

This doesnt produce any data when ran against NSE:ITC stock. I am not sure why and there is no debugger available to see line by line behavior. I Tried to plot weekly_lc and it worked fine.

Update 1: I am putting my whole script here with exit condition.

    //@version=4
strategy("Darshan 52 week low", overlay=true)

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

highAfterPurchase=0

// is close/open/high/low is less than 52 week low
if (close <= weekly_lc)
    // if yes buy INR 10000 worth of stocks
    quantityToBuy = 10000/close
    strategy.entry("darshan-long", strategy.long, quantityToBuy)

    // Set the purchase price as high
    highAfterPurchase = close

if (close > highAfterPurchase)
    highAfterPurchase = close

// is close price 15% lesser than high then exit 
closeHighDelta = highAfterPurchase - highAfterPurchase * 0.15
if (close < closeHighDelta)
    strategy.exit("exit", "darshan-long")

Strategy Tester screen looks as follows:

enter image description here

Upvotes: 0

Views: 2267

Answers (1)

vitruvius
vitruvius

Reputation: 21294

Well, if you look closely, you have one trade indeed. Entry condition has been met on 2000-04-24 with a price of 12.80. Exit condition is still Open, which means that your exit condition has not been met yet and you are still long.

I will try to show you what's going on with your strategy by showing you how to debug.

First, let's convert your strategy into an indicator. I would always start with an indicator and convert it to a strategy later when I'm satisfied with it.

To show if we have a BUY signal or SELL signal, we will use the plotshape() function and some additional variables.

//@version=4
study("Darshan 52 week low", overlay=true)

var isLong = false      // Flag to see if we are currently long
var isShort = false     // Flag to see if we are currently short

var highAfterPurchase = 0.0

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

buySignal = not isLong and (close < weekly_lc)  // Buy only if not already long

highAfterPurchase := iff(buySignal, close, nz(highAfterPurchase[1]))    // If we are already long, update highAfterPurchase with the current close
                                                                        // else, keep the old value
// is close price 15% lesser than high then exit 
closeHighDelta = highAfterPurchase - (highAfterPurchase * 0.15)

sellSignal = not isShort and (close < closeHighDelta)   // Sell only if not alread short

if (buySignal)  // Reset signals
    isLong := true
    isShort := false

if (sellSignal) // Reset signals
    isLong := false
    isShort := true

plotshape(series=buySignal, text="BUY", style=shape.triangleup, color=color.green, location=location.belowbar, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, color=color.red, location=location.abovebar, size=size.small)

Now, to debug this, we will create another indicator with the only difference being, overlay=false and different plots. Here we want to plot signals that we are interested in. Signals that we want to see their values.

//@version=4
study("Darshan 52 week low Debug", overlay=false)

var isLong = false      // Flag to see if we are currently long
var isShort = false     // Flag to see if we are currently short

var highAfterPurchase = 0.0

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

buySignal = not isLong and (close < weekly_lc)  // Buy only if not already long

highAfterPurchase := iff(buySignal, close, nz(highAfterPurchase[1]))    // If we are already long, update highAfterPurchase with the current close
                                                                        // else, keep the old value
// is close price 15% lesser than high then exit 
closeHighDelta = highAfterPurchase - (highAfterPurchase * 0.15)

sellSignal = not isShort and (close < closeHighDelta)   // Sell only if not alread short

if (buySignal)  // Reset signals
    isLong := true
    isShort := false

if (sellSignal) // Reset signals
    isLong := false
    isShort := true

plot(series=close, title="close", color=color.green, linewidth=2)
plot(series=weekly_lc, title="weekly_lc", color=color.blue, linewidth=2)
plot(series=highAfterPurchase, title="highAfterPurchase", color=color.orange, linewidth=2)
plot(series=closeHighDelta, title="closeHighDelta", color=color.red, linewidth=2)

Now, your Buy Condition is, when the green line (close) goes below the blue line (weekly_lc) and your Sell Condition is when the green line (close) goes below the red line (closeHighDelta).

If you look at the plots (you can make some of them invisible from the settings if you cannot see clearly), your buy condition only happens once and your sell condition never becomes TRUE. So, you are always LONG.

Here is the modified strategy:

//@version=4
strategy("Darshan 52 week low", overlay=true)

// Time inputs that the strategy is going to apply on
FromMonth = input(defval = 01, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 01, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 2017)
ToMonth   = input(defval = 08, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 31, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 2019, title = "To Year", minval = 2017)

// Time frame calculations
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false // create function "within window of time"

var isLong = false      // Flag to see if we are currently long
var isShort = false     // Flag to see if we are currently short

var highAfterPurchase = 0.0

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

buySignal = not isLong and (close < weekly_lc)  // Buy only if not already long

highAfterPurchase := iff(buySignal, close, nz(highAfterPurchase[1]))    // If we are already long, update highAfterPurchase with the current close
                                                                        // else, keep the old value
// is close price 15% lesser than high then exit 
closeHighDelta = highAfterPurchase - (highAfterPurchase * 0.15)

sellSignal = not isShort and (close < closeHighDelta)   // Sell only if not alread short

if (buySignal)  // Reset signals
    isLong := true
    isShort := false

if (sellSignal) // Reset signals
    isLong := false
    isShort := true

strategy.entry(id="darshan-long", long=strategy.long, when=buySignal and window())
strategy.close(id="darshan-long", when=sellSignal and window())

As a side note, you should use the := operator when you want to re-assign values to variables.

enter image description here

enter image description here

Upvotes: 2

Related Questions