AIとファイナンス

AIとファイナンスの架け橋、それがこのブログの目指すところです。兼業投資家向けに、Pythonを駆使して株やFXの分析を「自分で」行えるようになるための情報を提供します。ニューラルネットワークを活用した市場予測から、実証済みの金融理論まで、全てのコードを公開し、誰もが活用できるように!是非色々なコードで遊んでみてください!

【ChatGPT】GPTが書いてくれたPinescrtiptは動くのか?【Trading View】

前回の記事ではトレーディング戦略を口語で説明するとPinescrtiptでStrategyを書いてくれるChatGPTを作ってみました。今回はそれが果たして本当に使えるのかを調べてみましょう!ちなみにこのGPTへのリンクはこちらです。

さて、こちらがスタートした時の画面になります。ここに口語でどんどん作戦の内容を入れていきます。

Trading Strategy Pinescripter 入力画面

このようにどんなものがあるかを聞けば具体例を列挙してくれます。今回はFXの作戦でよくあるMACDを使ったトレーディング戦略をやってみましょう!

トリガーの種類

このようにGPTに指示を出すと以下のように戦略をまとめてくれたうえでPinescrtiptを出力してくれます。今回出力されたScriptは最下部に丸々載せているので「GPTはちょっと」、という方もTrading Viewにコピーして使ってみてください!

戦略のまとめ

 

バックテスト結果

バックテスト結果は、、、あれ、意外と悪くない?
今回この結果に至るまでにかかった時間はわずか10分。そう考えるとこれは悪くないですよね。

皆さまも是非使ってみてください。

 

 

ーーーーーーーーーーーここからコードーーーーーーーーーーーーー

//@version=5
strategy("My Strategy", overlay=true, margin_long=25, margin_short=25)

// Define strategy settings
atr = ta.atr(14)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
prev_close = request.security(syminfo.tickerid, "D", close[2])

// Calculate strategy values
longCondition = ta.crossover(macdLine, signalLine) and close > prev_close
shortCondition = ta.crossunder(macdLine, signalLine) and close < prev_close

// Output strategy data
plotshape(series=longCondition, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=shortCondition, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// Determine long trading conditions
if (longCondition)
    strategy.entry("Long", strategy.long)

// Code short trading conditions
if (shortCondition)
    strategy.entry("Short", strategy.short)

// Submit entry orders
// Note: The position size is dynamically calculated based on account balance and leverage, which is handled internally by TradingView.

// Submit exit orders
strategy.exit("Exit Long", "Long", trail_points=atr*3, trail_offset=atr*3)
strategy.exit("Exit Short", "Short", trail_points=atr*3, trail_offset=atr*3)