Writing a semi-automated trading tool using Pine language

Author: The Little Dream, Created: 2022-09-29 14:36:32, Updated: 2023-09-15 20:53:45

img

Writing a semi-automated trading tool using Pine language

Although there are more and more traders who write programs to do fully automated trading, a larger group of traders are still manual traders. In fact, manual subjective traders can also help their subjective trading by writing some small tools. For example, sometimes they find a good entry position, plan to set a fixed stop loss for the initial position, track the stop. Then, without the comparatively energy-consuming things like subsequent spreadsheets, completely follow their established stop loss spreadsheets, and let the program help their own spreadsheets.

Parameter design

The strategy for designing such requirements using the Pine language is quite simple, with the following parameters designed depending on the requirements:

1, offset: when triggering the tracking stop, shift the highest price, the lowest price to define the deviation distance of the stop line. 2, limit: Parameters used to control A. Initial underlying purchase directly; B. Specify price waiting for purchase; C. Do nothing. 3, amount: the amount of the order when the underlying is opened. 4, loss: number of stop loss points. 5. targetOffset: The price difference that moves the price of the open position when the tracking stop occurs. 6 min Tick: The price is going up. 7, direction: the direction of the bottom of the stock and the opening of the stock.

Design strategies

/*backtest
start: 2022-09-24 00:00:00
end: 2022-09-27 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
args: [["v_input_1",20],["v_input_2",0],["v_input_4",50],["v_input_5",20],["RunMode",1,358374],["ZPrecision",0,358374],["XPrecision",3,358374]]
*/

strategy("跟踪止损止盈委托", overlay = true)

varip targetPrice = na
varip high_lowPrice = na
varip isTrade = false 
varip isAlert = false
varip isAlertMinTick = false
varip isAlertFinished = false 

varip offset = input(30, "offset", "跟踪止损止盈偏移")
varip limit = input(-1, "limit", "初始开仓价格,-1为不开仓,0为立即开仓,其它具体数值为限价价格")
varip amount = input(1, "amount", "开仓量")
varip loss = input(30, "loss", "止损")
varip targetOffset = input(30, "targetOffset", "触发跟踪止盈止损偏移量")
varip minTick = input(1, "minTick", "价格一跳")
tradeType = input.string("long", "direction", tooltip="下单方向,long做多,short做空", options=["long", "short"])

if not barstate.ishistory and not isAlertMinTick
    runtime.log("检查syminfo.mintick是否正确!syminfo.mintick:", syminfo.mintick, "#FF0000")
    if syminfo.mintick < minTick 
        runtime.error("系统syminfo.mintick < minTick参数", "#FF0000")
    isAlertMinTick := true 

if not barstate.ishistory and limit == -1 and not isAlert
    runtime.log("没有设置开仓价格,当前limit为-1(防止误开仓,初始默认limit为-1),禁止开仓", "#FF0000")
    isAlert := true 

if isTrade and strategy.position_size == 0 and not isAlertFinished
    runtime.log("所有委托流程执行完毕,仓位为0", "#FF0000")
    isAlertFinished := true 

if not barstate.ishistory and not isTrade and limit != -1
    if limit == 0 
        strategy.entry("open", tradeType == "long" ? strategy.long : strategy.short, amount)
    else if limit > 0 
        strategy.entry("open", tradeType == "long" ? strategy.long : strategy.short, amount, limit=limit)
    
    if tradeType == "long"
        targetPrice := (limit == 0 ? close : limit) + targetOffset
    else 
        targetPrice := (limit == 0 ? close : limit) - targetOffset
    strategy.exit("exit", "open", amount, loss=loss, trail_price=targetPrice, trail_offset=offset)
    runtime.log("每点价格为:", syminfo.mintick, ",当前close:", close)
    isTrade := true 

if ((close > targetPrice and strategy.position_size > 0) or (close < targetPrice and strategy.position_size < 0)) and not barstate.ishistory
    high_lowPrice := na(high_lowPrice) ? close : high_lowPrice
    if strategy.position_size > 0 
        high_lowPrice := close > high_lowPrice ? close : high_lowPrice
    else 
        high_lowPrice := close < high_lowPrice ? close : high_lowPrice

plot(targetPrice, "trail_price 触发线")    
plot(strategy.position_size!=0 ? high_lowPrice : na, "当前最高价/最低价")
plot(strategy.position_size!=0 ? (strategy.position_size > 0 ? high_lowPrice-syminfo.mintick*offset : high_lowPrice+syminfo.mintick*offset) : na, "移动止损触发线")

The strategy design is also not complicated, and when used it is usually set to a "real-time price model" because it is necessary to detect the price at all times.

img

Note that the stop loss in the parameter is represented by a point (i.e. one price jump), and the offset tracking stop-gap deviation is also represented by a point (i.e. one price jump). The offset of the target tracking stop-gap trigger line is represented by the price distance (i.e. set to 30, i.e. 30 yuan distance). When the price jump is 1, the offset is 30 yuan distance.

This assignment strategy is designed to not only do more of the initial base position, but also to empty the initial base position. Then stop loss and track stop throttle are handled according to the empty direction.

Let's demonstrate the features of the design implementation below:

1 ⇒ When this policy is run, immediately open the base position and then set the stop loss, track stop and stop in accordance with the parameters.

img

direction is set to long, limit parameter is set to 0, i.e. the strategy is executed immediately, amount is set to 1, strategy is set to 1 contract.

img

2, specify the limit parameter, specify the entry price

Other parameters are unchanged, just specify the limit parameter price as:1276

img

3, the default limit parameter is -1, nothing works to prevent false starts.

img

THE END

When using the Pine language strategy, pay special attention to this data; how much of the system's price jump is specifically related to the "price currency accuracy" in the parameter.

img

The parameter "Priceed Currency Accuracy" is set to 0, which means that the price data is numerically accurate to one digit (i.e. the decimal digit is 0); then the smallest unit of change in price is 1.

OK, that's the whole design of this semi-automated delegation policy, although I've also got it for real-world use. But it's also important to understand how to use such tools based on your trading habits, which can be modified and optimized by yourself. Here the policy code is just publicly shared, exchange learning design, logic.

As you can see, Pine is very easy to use, easy to use, and easy to learn. We can quickly design the tools we want without having to bother with complex programming.


Related

More