1
votes

I am new to pinescript on TradingView and have everything working on a renko based strategy, however I would like to know how I create a buy signal on the second bar that meets my condition. My code snippet is below and it will print the "buy" signal on every bar above the EMA where the open is above it.

My issue is I want only one "buy" printed and not quite sure how to count the conditions being true so that the "buy" is painted only once. I'm struggling to work out how to do this i.e. saving values via an array counter or something similar.

My other question is can I control the changing of the printing of price styles as renko in pinescript versus candles i.e. when I choose my strategy for use ideally I don't want to click on price style etc.

// Plot Buy and Sell Signals
renko_buy = renko_low > emaFast
renko_sell = renko_high < emaFast

// only want to plot this shape if meet this condition twice i.e. after 
// second bar only that meets this condition of being above the EMA
plotshape(renko_buy, color=lime, style=shape.arrowup, text="Buy")

// only want tom plot this shape if meet this conditention twice i.e. 
// after second bar only that meets this condition of being under the EMA
plotshape(renko_sell, color=red, style=shape.arrowdown, text="Sell")
2

2 Answers

1
votes

Here's an example

//@version=3
study("Buy on second trigger")

myCondition = close > open

conditionMetTimes = 0
conditionMetTimes := nz(conditionMetTimes[1])

if myCondition
    conditionMetTimes := conditionMetTimes + 1

BUY = 0
if myCondition and conditionMetTimes >= 2
    conditionMetTimes := 0
    BUY := 1

plot(BUY)
0
votes

The barssince function counts a number of bars since a condition was true. barssince(condition) → series[integer]

https://www.tradingview.com/pine-script-reference/v4/#fun_barssince