LoginSignup
1
2

More than 3 years have passed since last update.

[暗号通貨自動売買] TVExtBotスタートガイド2(Pineスクリプト編)

Last updated at Posted at 2019-04-23

TVExtBot for 暗号通貨(以下、TVExtBot)で使用するPineスクリプトについて説明します。
前回をまだ見ていない方はこちらをどうぞ
:point_right: TVExtBotスタートガイド1(設定編)

🎁Bybit 取引手数料15%割引&特典
Bybit取引所のアフィリエイトリンクを使ってアカウントを新規登録していただくと、取引手数料15%割引最大90ドルの登録特典がいただけます。
:point_right: アフィリエイトリンク
:point_right: 登録特典ページ

Pineスクリプト準備

トレーディングビューサイト(tradingview.com)ではユーザーが自分のアイデアを公開したり、TradingViewの言語であるpineスクリプトを使用し作成したインジケーターやストラテジーを公開することができます。
TradingViewのユーザーは公開されたインジケーターを自由に自分のチャート上に表示することができます。
今回は説明のために予め準備したPineスクリプトを使用します。
※ 実際に売買には使用しないでください!

① 『インジケーター&ストラテジー』を選択します。
スクリーンショット 2019-04-21 10.42.11.png

② 『TVExtBot』を入力し、一覧から『SMA Strategy for TVExtBot』Pineスクリプト(※1)をクリックします。
(※1) Pineエディタの『新規作成−空のインジケータースクリプト』を実行するとデフォルトとして提供されるコードにバックテストの期間を指定してシミュレーションできるように変更しているコードになります。
(バックテストのデフォルト期間は2019年1月1日〜2019年12月31日です)
スクリーンショット 2019-04-22 22.09.11.png

③ 『SMA Strategy for TVExtBot』のバックテスト結果が『投資戦略テスター』タブに表示されます。
スクリーンショット 2019-04-22 21.35.33.png
ビットフライヤーBTC/JPYチャートで15分足の場合、2019年1月1日〜4月22日までの勝率が『34.97%』です。
他にも、『パフォーマンスサマリー』『トレード一覧』も確認できます。
スクリーンショット 2019-04-22 21.43.49.png

Pineスクリプト編集

アラートを設定するために、以下の2ステップが必要になります。
1.strategy関数をstudy関数に変更する。
2.カスタムアラート設定のalertcondition関数を追加する。
alertconditionについての公式の解説はこちら
※ strategy関数を使用する場合、アラート設定を行うことができません。

① Pineスクリプトの変更前と変更後は以下の通りです。
【Pineスクリプトのコード内容】
 移動平均(sma)の14日線、28日線が下から上にクロスした場合はLongエントリ、上から下にクロスした場合はShortエントリの戦略です。

変更前
//------------------------------------
//【説明】TVExtBot for Cryptocurrency 戦略サンプル
//------------------------------------
//@version=3
strategy("SMA Strategy for TVExtBot", overlay=true)

FromYear  = input(defval = 2019, title = "From Year", minval = 2010)
FromMonth = input(defval = 1, title = "From Month", minval = 1)
FromDay   = input(defval = 1, title = "From Day", minval = 1)
ToYear    = input(defval = 2019, title = "To Year", minval = 2010)
ToMonth   = input(defval = 12, title = "To Month", minval = 1)
ToDay     = input(defval = 31, title = "To Day", minval = 1)

FromDate = timestamp("GMT+9",FromYear,FromMonth,FromDay, 0, 1)
ToDate = timestamp("GMT+9",ToYear,ToMonth,ToDay, 0, 1)

BGColor =  (time >= FromDate) and (time <= ToDate) ? #FFFF4D : na
bgcolor(BGColor, transp=94)
BackTest() =>
    time >= FromDate and time <= ToDate ? true : false

longCondition = crossover(sma(close, 14), sma(close, 28))
if (BackTest() and longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (BackTest() and shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

【Pineスクリプトのコード内容】
移動平均(sma)の14日線、28日線が下から上にクロスした場合は買いアラートを、上から下にクロスした場合は売りアラートを設定できるように変更しました。

変更後
//------------------------------------
//【説明】TVExtBot for Cryptocurrency スタートガイドサンプル
//------------------------------------
//@version=3
study("SMA Indicator for TVExtBot", overlay=true)

longCondition = crossover(sma(close, 14), sma(close, 28))
shortCondition = crossunder(sma(close, 14), sma(close, 28))

plotshape(longCondition,title="Buy",style=shape.triangleup,text="Buy",color=green,textcolor=green,location=location.belowbar)
plotshape(shortCondition,title="Sell",style=shape.triangledown,text="Sell",color=red,textcolor=red,location=location.abovebar)
alertcondition(longCondition, title = "Buy", message = '買い注文メッセージ入力')
alertcondition(shortCondition, title = "Sell", message = '売り注文メッセージ入力')

② Pineエディタの『新規作成−空のインジケータースクリプト』をクリックします。
スクリーンショット 2019-04-22 22.47.44.png

③ 上記①変更後のPineスクリプトをコピー&ペーストします。
スクリーンショット 2019-04-22 22.55.27.png

④ 『チャートに追加』をクリックします。
スクリーンショット 2019-04-22 22.58.32.png

これで自動売買するためのPineスクリプトが準備できました。
次回は、自動売買するために必要な注文メッセージ作成及び設定について説明させていただきます。
:point_right: TVExtBotスタートガイド(注文メッセージ作成及び設定編)

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2