简体   繁体   中英

% based profit target stragety to Alert with pinescript

im trying to convert strategy to study with percentage based profit target. but i dont know how to convert. Actually im a newbie in this field. Im learning script with other users questions or answers.

Exactly, i want to create Alert "stragety.exit of x0 to x5"

Many Thanks~

stopPer = input(1.0, title='Stop Loss %', type=input.float) / 100
takePer = input(26.0, title='Take Profit %', type=input.float) / 100

longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)

strategy.entry("Buy", strategy.long, when = buy)
strategy.entry("sell", strategy.short, when =sell)

if strategy.position_size > 0 
    strategy.exit(id="Close Long", stop=longStop, limit=longTake)
if strategy.position_size < 0 
    strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)


strategy.exit("x0", qty_percent = 10, profit = percentAsPoints(1))
strategy.exit("x1", qty_percent = 10, profit = percentAsPoints(3))
strategy.exit("x2", qty_percent = 10, profit = percentAsPoints(5))
strategy.exit("x3", qty_percent = 15, profit = percentAsPoints(10))
strategy.exit("x4", qty_percent = 20, profit = percentAsPoints(15))
strategy.exit("x5", profit = percentAsPoints(20))`

You can create an alert on strategy directly. You don't need convert it to a study for this. I suggests to start from learning the official docs .

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