파인스크립트 전략(strategy)을 사용하여 자동매매하는 방법

파인스크립트의 전략(strategy)를 사용해서 자동매매하는 방법에 대한 내용입니다.
어느정도 파인스크립트에 대한 기본지식이 필요하니 파인스크립트 기초 를 참고해 주세요.

전략(strategy) 스크립트 준비하기

※ 파인스크립트 버전 4에서만 사용가능합니다.(//@version=4)

[Pine스크립트] ※ 설명을 위한 샘플입니다.실제 매매에는 사용하지 마세요.
 1//@version=4
 2strategy("샘플전략", overlay=true)
 3
 4// Create Indicator's
 5shortSMA = sma(close, 10)
 6longSMA = sma(close, 30)
 7rsi = rsi(close, 14)
 8atr = atr(14)
 9
10// Specify crossover conditions
11longCondition = crossover(shortSMA, longSMA)
12shortCondition = crossunder(shortSMA, longSMA)
13
14// Execute trade if condition is True
15if (longCondition)
16    stopLoss = low - atr * 2
17    takeProfit = high + atr * 2
18    strategy.entry("long", strategy.long, 100, when = rsi > 50)
19    strategy.exit("exit", "long", stop=stopLoss, limit=takeProfit)
20
21if (shortCondition)
22    stopLoss = high + atr * 2
23    takeProfit = low - atr * 2
24    strategy.entry("short", strategy.short, 100, when = rsi < 50)
25    strategy.exit("exit", "short", stop=stopLoss, limit=takeProfit)
26
27// Plot Moving Average's to chart
28plot(shortSMA)
29plot(longSMA, color=color.black)

앞으로 수정할 코드는 위의 소스에서 18번째,19번째,24번째,25번째줄입니다.

설명
  • 18번째줄에서 매수진입을 하여 19번째에서 매수청산을 합니다.
  • 24번째줄에서 매도진입을 하여 25번째에서 매도청산을 합니다.

바이빗 거래소 15분차트의 백테스트 결과입니다.

전략(strategy에 주문메시지 설정하기

위의 전략에 맞게 3개의 주문메시지를 작성합니다.(바아빗기준)

매수용 주문메시지
1
TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"buy","bal_pct":50,"token":"토큰"}:MVT
매도용 주문메시지
1
TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"sell","bal_pct":50,"token":"토큰"}:MVT
청산용 주문메시지
1
TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"close","token":"토큰"}:MVT

위의 주문메시지를 아래와 같이 전략 스크립트에 추가합니다.
 1//@version=4
 2strategy("샘플전략", overlay=true)
 3
 4// Create Indicator's
 5shortSMA = sma(close, 10)
 6longSMA = sma(close, 30)
 7rsi = rsi(close, 14)
 8atr = atr(14)
 9
10// Specify crossover conditions
11longCondition = crossover(shortSMA, longSMA)
12shortCondition = crossunder(shortSMA, longSMA)
13
14// Execute trade if condition is True
15if (longCondition)
16    stopLoss = low - atr * 2
17    takeProfit = high + atr * 2
18    strategy.entry("long", strategy.long, 100, when = rsi > 50, alert_message='TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"buy","bal_pct":50,"token":"토큰"}:MVT')
19    strategy.exit("exit", "long", stop=stopLoss, limit=takeProfit, alert_message='TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"close","token":"토큰"}:MVT')
20
21if (shortCondition)
22    stopLoss = high + atr * 2
23    takeProfit = low - atr * 2
24    strategy.entry("short", strategy.short, 100, when = rsi < 50, alert_message='TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"sell","bal_pct":50,"token":"토큰"}:MVT')
25    strategy.exit("exit", "short", stop=stopLoss, limit=takeProfit, alert_message='TVM:{"exchange":"bybit-testnet","account":"*","symbol":"BTC/USD","type":"market","side":"close","token":"토큰"}:MVT')
26
27// Plot Moving Average's to chart
28plot(shortSMA)
29plot(longSMA, color=color.black)

strategy.entry함수와 strategy.exit함수에 alert_message 속성을 추가한뒤 위의 주문메시지를 설정합니다.
※ 여기서 중요한건 alert_message에 주문메시지를 설정할때 쌍따옴표(")가 아니라 따움표(')로 해야합니다. (예) alert_message='주문메시지'

얼러트 설정하기

아래와 같이 얼러트를 추가합니다.

메시지란에 {{strategy.order.alert_message}} 을 설정합니다.

※ 4개의 시그널이 발생하지만 얼러트는 하나만 설정하면 됩니다.