[STOCK] [Strategy][TRADINGVIEW][Tutorial] Using strategy.order in PINE SCRIPT to complete multiple trades strategy

Objective

In a trading strategy, if you want to add positions midway, you can use the strategy.order function to achieve this. The following example will be:

1. Buy once when it rises above the quarterly line, and the order quantity is 0.

2. Buy again when it rises above the 10-day line.

3. Sell all when it falls below the 60-day line.

P.S. If you don't want to sell all at once, you can adjust the quantity to be sold by adding the qty parameter to the strategy.close function.

  strategy.close("buy", comment = '全賣', qty = 1 )

if the article is helpful to you, please click on an advertisement to provide me with additional income, which is also an encouragement for me.

Strategy Script

For instructions on how to add the script to the chart, please refer to [STOCK][TRADINGVIEW][Tutorial] Adding your own indicators & strategies to TRADINGVIEW charts.


// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CarterTsai

//@version=5
strategy("加碼範例", overlay=true, margin_long=1000, margin_short=100)

sma10 = ta.sma(close, 10)
sma60 = ta.sma(close, 60)

buycondition = ta.crossover(close, sma60)
additioncondition = ta.crossover(close, sma10)
sellcondition = ta.crossunder(close, sma60)

// 漲過季線買一次, 並且訂單數量應該是0
if (strategy.position_size == 0 and buycondition)
    strategy.entry('buy', strategy.long, comment='第一次買', qty=1)

// 漲過10線買一次
if ( strategy.position_size > 0 and additioncondition )
    strategy.order('buy', strategy.long, comment='漲破10線再買一次', qty= 1)

// 跌破季線
if (strategy.position_size > 0 and sellcondition)
    strategy.close("buy", comment = '全賣')


plot(sma60, color=color.yellow, linewidth=2)
plot(sma10, color=color.blue, linewidth=2)

結果














留言

熱門文章