简体   繁体   中英

how to write a buy or sell trigger after a number of bars meet condition which in my case is 2 renko bars above a certain EMA

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 ie 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 ie 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")

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM