How to reduce repainting in this code
Posted: Wed Mar 16, 2022 2:02 pm
Hi brilliant people,
Take a look at the below code. It is repainting pretty badly. Can you see ways of improving the code to reduce this?
In advance, thank you for your help.
---
//@version=5
// Name: P&F Mex Scalp strat
// Revision: 0.1
// Date: 11-Jun-2018
strategy('Copy P&F scalp strat V5', shorttitle='Copy MEXlongOnly_strat V5', overlay=true)
timeframe = input('3')
box = input('Traditional')
boxsize = input.float(5)
reversal = input(1)
pnf = ticker.pointfigure(syminfo.tickerid, 'close', box, boxsize, reversal)
//non-repainting open
nonRepaintingSecurityOpen(sym, tf, src) =>
request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
pnf_open = nonRepaintingSecurityOpen(pnf, timeframe, open)
//non-repainting close
nonRepaintingSecurityClose(sym, tf, src) =>
request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
pnf_close = nonRepaintingSecurityClose(pnf, timeframe, close)
//pnf_open = request.security(pnf, timeframe, open)
//pnf_close = request.security(pnf, timeframe, close)
p1 = plot(pnf_open, title='pnf_open', color=color.new(color.green, 0))
p2 = plot(pnf_close, title='pnf_close', color=color.new(color.maroon, 0))
base = pnf_close > pnf_open ? pnf_close : pnf_open
p0 = plot(base, title='base', color=color.new(color.gray, 0))
fill(p0, p2, color=color.new(color.green, 70))
fill(p0, p1, color=color.new(color.maroon, 70))
entry() =>
base > pnf_close and barstate.isconfirmed
exit() =>
base > pnf_open and barstate.isconfirmed
alertcondition(entry(), title='buy', message='buy!')
alertcondition(exit(), title='sell', message='sell!')
strategy.risk.allow_entry_in(strategy.direction.long)
strategy.entry('Long', direction=strategy.long, when=entry())
strategy.entry('close', strategy.short, when=exit())
Take a look at the below code. It is repainting pretty badly. Can you see ways of improving the code to reduce this?
In advance, thank you for your help.
---
//@version=5
// Name: P&F Mex Scalp strat
// Revision: 0.1
// Date: 11-Jun-2018
strategy('Copy P&F scalp strat V5', shorttitle='Copy MEXlongOnly_strat V5', overlay=true)
timeframe = input('3')
box = input('Traditional')
boxsize = input.float(5)
reversal = input(1)
pnf = ticker.pointfigure(syminfo.tickerid, 'close', box, boxsize, reversal)
//non-repainting open
nonRepaintingSecurityOpen(sym, tf, src) =>
request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
pnf_open = nonRepaintingSecurityOpen(pnf, timeframe, open)
//non-repainting close
nonRepaintingSecurityClose(sym, tf, src) =>
request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
pnf_close = nonRepaintingSecurityClose(pnf, timeframe, close)
//pnf_open = request.security(pnf, timeframe, open)
//pnf_close = request.security(pnf, timeframe, close)
p1 = plot(pnf_open, title='pnf_open', color=color.new(color.green, 0))
p2 = plot(pnf_close, title='pnf_close', color=color.new(color.maroon, 0))
base = pnf_close > pnf_open ? pnf_close : pnf_open
p0 = plot(base, title='base', color=color.new(color.gray, 0))
fill(p0, p2, color=color.new(color.green, 70))
fill(p0, p1, color=color.new(color.maroon, 70))
entry() =>
base > pnf_close and barstate.isconfirmed
exit() =>
base > pnf_open and barstate.isconfirmed
alertcondition(entry(), title='buy', message='buy!')
alertcondition(exit(), title='sell', message='sell!')
strategy.risk.allow_entry_in(strategy.direction.long)
strategy.entry('Long', direction=strategy.long, when=entry())
strategy.entry('close', strategy.short, when=exit())