简体   繁体   中英

Exit all trades at 1% profit of account balance or 1 dollar

I am newbie just started learning pine coding. I am developing a simple strategy of multiple buys and sells based on market flow. How can I exit all open trades when my account balance reaches a net profit of either 1% percentage of account balance or just at 1 dollar profit?

//buy-sell conditions
buy_signal  = close >= open[1]
sell_signal = close <= open[1]

//execution code
strategy.entry('Buy',  strategy.long,  when = buy_signal)
strategy.entry('Sell', strategy.short, when = sell_signal)

//collective trades exit code
if ( )
    strategy.close_all()

Use strategy.position_avg_price to get your average position price. Then calculate your target profit price using this variable.

long_profit_price = strategy.position_avg_price * 1.01  // 1% profit

if (strategy.position_size > 0)
    strategy.exit("Buy Exit", "Buy", limit=long_profit_price)

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