chatGPTでPINEscriptのインジケータ書いてみた。 成果物。

6日目を終えて、chatGPTはしょっちゅう悪びれずに嘘つくが、それでも自分一人でやるよりも、相棒として採用するほうが集中力が持続して成果物を仕上げやすい、との結論になりました。
使い勝手検証は本日で最終日としますので、成果物を公開します。

インジケータを1から作り始めて、ストラテジーに変換して動くようにするまで約3時間でした。

お題「RSIで逆張り」
インジケータのソースコード

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yanyancha
//@version=4
study("Study SMA(rsi) and cross")
// RSI ---------------------------------
rsiKikan = input(14, title="RSI期間本数")
smaKikan =  input(3, title="SMA期間本数")
wRsi = rsi(close, rsiKikan)
wSmaRsi = sma(wRsi, smaKikan)

// Check for cross-over: macd>signal ?
crossUp = crossover(wRsi, wSmaRsi)
// Check for cross-under: macd<signal ?
crossDn = crossunder(wRsi, wSmaRsi)
// 
rsiOver = (wRsi > wSmaRsi) ? true : false
rsiUnder = (wRsi < wSmaRsi) ? true : false

rsiColor = rsiOver ? color.new(color.green, 10) : rsiUnder ? color.new(color.red, 10) : color.new(color.white, 50)  

// Plot MACD in a subwindow
plot(wRsi, color=rsiColor, linewidth=2, title="RSI")
plot(wSmaRsi, color=color.new(color.gray, 30), linewidth=1, title="SmaRsi")

// Define the variable to hold the gobuy signal and gosell signal
highLine = input(75, title="買われすぎしきい値")
lowLine = input(25, title="売られすぎしきい値")
chSikiti = input(10, title="しきい値チェック本数")
hline(highLine)
hline(lowLine)

gobuy = false
gosell = false
goGyakubariSell = false
goGyakubariBuy  = false
// Check if the RSI value is greater than 70 in the last 10 bars
for i = 1 to chSikiti
    if (wRsi[i] >= highLine)
        goGyakubariSell := true
        break
    if (wRsi[i] <= lowLine)
        goGyakubariBuy := true
        break

goSell = (goGyakubariSell and crossDn) ? true:false
goBuy = (goGyakubariBuy and crossUp) ? true:false
// Set background color
bgcolor(goBuy ? color.new(color.green, 40) : na)
bgcolor(goSell ? color.new(color.red, 40) : na)
// 

これをchatGPT使ってストラテジーに変換してバグ取りと資産残高に対するストップを入れたコードがこちら。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yanyancha
//@version=4
strategy("Strategy SMA(RSI) and cross")
// RSI ---------------------------------
rsiKikan = input(14, title="RSI期間本数")
smaKikan = input(3, title="SMA期間本数")
wRsi = rsi(close, rsiKikan)
wSmaRsi = sma(wRsi, smaKikan)

// Check for cross-over: macd>signal ?
crossUp = crossover(wRsi, wSmaRsi)
// Check for cross-under: macd<signal ?
crossDn = crossunder(wRsi, wSmaRsi)
//
rsiOver = (wRsi > wSmaRsi) ? true : false
rsiUnder = (wRsi < wSmaRsi) ? true : false

rsiColor = rsiOver ? color.new(color.green, 10) : rsiUnder ? color.new(color.red, 10) : color.new(color.white, 50)

// Plot MACD in a subwindow
plot(wRsi, color=rsiColor, linewidth=2, title="RSI")
plot(wSmaRsi, color=color.new(color.gray, 30), linewidth=1, title="SmaRsi")

// Define the variable to hold the gobuy signal and gosell signal
highLine = input(75, title="買われすぎしきい値")
lowLine = input(25, title="売られすぎしきい値")
chSikiti = input(10, title="しきい値チェック本数")
hline(highLine)
hline(lowLine)
closeBuyLine = input(65, title="買いポジクローズ基準")
closeSellLine = input(35, title="売りポジクローズ基準")

gobuy = false
gosell = false
goGyakubariSell = false
goGyakubariBuy = false
// Check if the RSI value is greater than 70 in the last 10 bars
for i = 1 to chSikiti
    if (wRsi[i] >= highLine)
        goGyakubariSell := true
        break
    if (wRsi[i] <= lowLine)
        goGyakubariBuy := true
        break

goSell = (goGyakubariSell and crossDn) ? true:false
goBuy = (goGyakubariBuy and crossUp) ? true:false

// Entry
if goBuy
    strategy.entry("Buy", strategy.long, qty=1.0)
if goSell
    strategy.entry("Sell", strategy.short, qty=1.0)

// Exit
closeBuyPosi = wRsi>closeBuyLine
closeSellPosi = wRsi<closeSellLine

if strategy.position_size > 0 and closeBuyPosi
    strategy.close("Buy")
if strategy.position_size < 0 and closeSellPosi
    strategy.close("Sell")

// stop
// strategy.openprofitの損失が
// (strategy.initial_capital + strategy.netprofit)の合計値の
// 変数n%を越えたら、ポジションクローズする。
n = input(1.0, minval = 0.0, maxval = 50.0, title = "含み損のしきい値(%)")
stopLoss = (n/100) * (strategy.initial_capital + strategy.netprofit)

if strategy.openprofit < -stopLoss
    strategy.close("Buy")

// Set background color
bgcolor(goBuy ? color.new(color.green, 40) : na)
bgcolor(goSell ? color.new(color.red, 40) : na)
//

ある程度は自分でコードかける人なら、嘘つきなchatGPTを使っても、気づき増えて結果として生産性が上がることがわかりました。

ストラテジーの成績はこちら、5分足で動かしてのレンジトレードなら強いです。実戦で使うにはストップを工夫しないとこのままでは使えません。
5分以外の時間軸ではボロボロです。パラメータチューニングしないといけません。

昔の格言 「バカとはさみは使いよう」 まさにこれです。

このコードでリバーストレードする場合はBTCUSDでは1時間足を使うのがよさげです。

リバース向け、時間軸はH1
US30USD
DXY
BTCUSD
BTCJPY
XRPUSD

このまま順張りむけ
AUDUSD _M30 めっちゃ勝ててる、勝率68% PF1.4 ポジ数148
オジドルで負けてる人は参考にしてみてはいかが。

チップはこちらからお願いします