Sarotobi
Sarotobi

Reputation: 841

Pinescript how to place order when price touch the lines

I am attempting to place a buy and sell orders on each pivotHigh and pivotLow lines using below script, but i only manage some orders to be placed, how can i place my orders when the prices touched the lines.

//variable declartions
plotPivots = true
hih = pivothigh(high, pivotLookup, pivotLookup)
lol = pivotlow (low , pivotLookup, pivotLookup)
top = valuewhen(hih, high[pivotLookup], 0)
bottom = valuewhen(lol, low [pivotLookup], 0)
plot(top, offset=-pivotLookup, linewidth=2, color=(top != top[1] ? na : (plotPivots ? color.silver : na)), title="pivotTop")
plot(bottom, offset=-pivotLookup, linewidth=2, color=(bottom != bottom[1] ? na : (plotPivots ? color.silver : na)), title="pivotBottom")

if crossover(close, top)
    box.new(left=bar_index, top=top, right=bar_index+boxLength, bottom=bottom, bgcolor=color.new(color.green, boxTransparency), border_color=color.new(color.green, 80))
    strategy.entry("BUY", strategy.long, comment="BUY STOP", alert_message="BUY")
    
if crossunder(close, bottom)
    box.new(left=bar_index, top=top, right=bar_index+boxLength, bottom=bottom, bgcolor=color.new(color.red, boxTransparency), border_color=color.new(color.red, 80))
    strategy.entry("SELL", strategy.short,  comment="SELL STOP", alert_message="SELL")
 

Output enter image description here

Upvotes: 0

Views: 1814

Answers (1)

vitruvius
vitruvius

Reputation: 21382

You should place limit orders in the global scope for this. Use the stop argument of the strategy.entry and set your buy price there.

//@version=5
strategy("test", "test", true)
//variable declartions
plotPivots = true
pivotLookup = 5
hih = ta.pivothigh(high, pivotLookup, pivotLookup)
lol = ta.pivotlow (low , pivotLookup, pivotLookup)
top = ta.valuewhen(hih, high[pivotLookup], 0)
bottom = ta.valuewhen(lol, low [pivotLookup], 0)
plot(top, offset=-pivotLookup, linewidth=2, color=(top != top[1] ? na : (plotPivots ? color.silver : na)), title="pivotTop")
plot(bottom, offset=-pivotLookup, linewidth=2, color=(bottom != bottom[1] ? na : (plotPivots ? color.silver : na)), title="pivotBottom")

strategy.entry("BUY", strategy.long, stop=top, comment="BUY STOP", alert_message="BUY")
strategy.entry("SELL", strategy.short, stop=bottom,  comment="SELL STOP", alert_message="SELL")

enter image description here

Upvotes: 1

Related Questions