简体   繁体   中英

(Anybody, please? I tried figuring it out but still stuck) Strategy Entry when Multiple Conditions are met

I have tried whatever I can but cannot figure out how to do this. I have no experience in coding but I am decently proficient in excel and know VBA to some extent, so somehow I manage to wiggle my way with different coding languages for different requirements. I can say that I am good at Googling, so I just google what I exactly want and get the answers for the task in any language that I am working on. And then simply copy paste the solutions with the adjustments that are specific to my task. But can't figure this one out, and I have googled different questions and searched for solutions on all the result pages. Didn't get lucky as usual. Just started with Pine Code last week.

I have the following query.

So I have an indicator that runs from 0 - 100. It can stay at 100 or 0 for multiple bars. I want to do the following -

When the indicator reaches 100, I want to find the closing price of the bar. Then until it stays at 100, this condition should be open ( this is the first condition ). Then once the indicator value goes below 98, I want to find the closing price of the bar ( this is the second condition ). If the closing price of the bar when the indicator went below 98 was higher than the closing price when the indicator reached 100 the first time, then a strategy.long will execute.

If the above two conditions are met but the closing price on condition1 was higher than the closing price on condition 2, then a strategy.short will execute.

When the indicator reaches 0, I want to find the closing price of the bar. Then until it stays at 0, this condition should be open ( this is the first condition ). Then once the indicator value goes above 2, I want to find the the closing price of the bar ( this is the second condition ). If the closing price of the bar when the indicator went above 2 was higher than the closing price when the indicator reached 0 the first time, then a strategy.long will execute.

If the above two conditions are met but the closing price on condition1 was higher than the closing price on condition 2, then a strategy.short will execute.

Also, I want to add a limit of 0.8% on the price when the startegy is long/short.

If you read all of this, thank you for your time.

Hoping you can help me with a solution.

Thank You once again, Cheers: :)

 This is my code (it currently is an indicator but I want a strategy).- 


mom = ta.mom(close,100)
percentile = (ta.percentrank(mom, 100))

x = false
if percentile == 100
    x:=true

if percentile < 99 and percentile >98
    x:=false

    
plotshape(percentile, title = "Buy", style = shape.arrowup, location = location.belowbar, color = color.green, text = "Buy", textcolor = color.green, size = size.large)


It resulted in multiple buy signals.

So after two days, I finally did it. The results aren't the way I expected them to be, but that doesn't matter because I learnt something new and this has opened up a lot of different other ideas to me. Also, I didn't figure it out on my own, I had posted the same query on the pinescript subreddit and a fellow redditor helped with a part of the code and I took it from there. Here is the link 1 .

And here is my full code -

//@version=5
indicator("percentile indicator", overlay = true)

mom = ta.mom(close,100)
percentile = (ta.percentrank(mom, 100))

c1 = 0.0
c1 := nz(c1[1])
c1 := (percentile == 100 and percentile[1] < 100) ? close : c1

c2 = 0.0
c2 := nz(c2[1])
c2 := (percentile[1] == 100 and percentile < 100) ? close : c2

event = 0
event := nz(event[1])
event := percentile == 100 ? 1 : event
event := event ==1 and percentile < 100 ? 2 : event

long = event == 2 and c2 > c1 ? 1 : 0
short = event == 2 and c2 < c1 ? 1 : 0

event := event == 2 ? 0 : event

plotshape(long, title = "Long", style = shape.labelup, location = location.belowbar, color = color.green, text = "Long", textcolor = color.white, size = size.tiny)
plotshape(short, title = "Short", style = shape.labelup, location = location.belowbar, color = color.red, text = "Short", textcolor = color.white, size = size.tiny)


c3 = 0.0
c3 := nz(c3[1])
c3 := (percentile == 0 and percentile[1] > 0) ? close : c3

c4 = 0.0
c4 := nz(c4[1])
c4 := (percentile[1] == 0 and percentile > 0) ? close : c4

event1 = 0
event1 := nz(event1[1])
event1 := percentile == 0 ? 1 : event1
event1 := event1 == 1 and percentile > 0 ? 2 : event1

long1 = event1 == 2 and c4 > c3 ? 1 : 0
short1 = event1 == 2 and c4 < c3 ? 1 : 0

event1 := event1 == 2 ? 0 : event1

plotshape(long1, title = "Long", style = shape.labelup, location = location.belowbar, color = color.green, text = "Long", textcolor = color.white, size = size.tiny)
plotshape(short1, title = "Short", style = shape.labelup, location = location.belowbar, color = color.red, text = "Short", textcolor = color.white, size = size.tiny)

EDIT - the above was an indicator and below is the strategy with limits and stops.

//@version=5
strategy("ABC")

mom = ta.mom(close,100)
percentile = (ta.percentrank(mom, 100))

c1 = 0.0
c1 := nz(c1[1])
c1 := (percentile == 100 and percentile[1] < 100) ? close : c1

c2 = 0.0
c2 := nz(c2[1])
c2 := (percentile[1] == 100 and percentile < 100) ? close : c2

event = 0
event := nz(event[1])
event := percentile == 100 ? 1 : event
event := event ==1 and percentile < 100 ? 2 : event

long = event == 2 and c2 > c1 ? 1 : 0
short = event == 2 and c2 < c1 ? 1 : 0

longprice = ta.valuewhen(long,close,0)
shortprice = ta.valuewhen(short,close,0)
limitlong = longprice * 1.008
stoplong = longprice * 0.992
limitshort = shortprice * 0.992
stopshort = shortprice * 1.008

event := event == 2 ? 0 : event

if long
    strategy.entry("Enter Long", strategy.long)
    strategy.exit("Long Exit","Enter Long", limit = limitlong, stop = stoplong)
if short
    strategy.entry("Enter Short", strategy.short)
    strategy.exit("Short Exit","Enter Short",limit = limitshort, stop = stopshort)

c3 = 0.0
c3 := nz(c3[1])
c3 := (percentile == 0 and percentile[1] > 0) ? close : c3

c4 = 0.0
c4 := nz(c4[1])
c4 := (percentile[1] == 0 and percentile > 0) ? close : c4

event1 = 0
event1 := nz(event1[1])
event1 := percentile == 0 ? 1 : event1
event1 := event1 == 1 and percentile > 0 ? 2 : event1

long1 = event1 == 2 and c4 > c3 ? 1 : 0
short1 = event1 == 2 and c4 < c3 ? 1 : 0

long1price = ta.valuewhen(long1,close,0)
short1price = ta.valuewhen(short1,close,0)
limit1long = long1price * 1.008
stop1long = long1price * 0.992
limit1short = short1price * 0.992
stop1short = short1price * 1.008

event1 := event1 == 2 ? 0 : event1

if long1
    strategy.entry("Enter Long", strategy.long)
    strategy.exit("Long Exit","Enter Long", limit = limit1long, stop = stop1long)
if short1
    strategy.entry("Enter Short", strategy.short)
    strategy.exit("Short Exit","Enter Short",limit = limit1short, stop = stop1short)

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