24
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pine Script Tips

Posted at

PineScriptのよく使うコードをメモっていきます

  • パラメータを可変にする
    input関数を使う
short_ma_term = input(title = "短期MA期間",defval=9)
middle = input(title = "中期MA",defval=25)
long_ma_term = input(title = "長期MA期間",defval=75)
  • テスト期間の日時を可変にする
////テスト期間の設定
sYear = input(2018, "テスト開始年") 
sMonth = input(1, "テスト開始月")
sDay = input(1, "テスト開始日")
sHour = input(0, "テスト開始時")
//分も指定したいときは追加する
sPeriod = timestamp("Asia/Tokyo",sYear,sMonth,sDay,sHour,0)
eYear = input(2018, "テスト終了年") 
eMonth = input(1, "テスト終了月")
eDay = input(1, "テスト終了日")
eHour = input(0, "テスト終了時")
//分も指定したいときは追加する
ePeriod = timestamp("Asia/Tokyo",eYear,eMonth,eDay,eHour,0)
testPeriod = (time > sPeriod and time < ePeriod)

////取引時の設定
//方法1 strategyにwhen optionを指定する
strategy.entry("SELL", strategy.short, when=testPeriod)

//方法2 ifで区切る
if timescale
    strategy.entry("SELL", strategy.short)
  • plotの線を目立つように、太さを変える
    linewidth オプションを使う
plot(bb_bottom,linewidth = 2,color = green)
  • 他の取引所の価格を使う
    これはTradingViewの大きな優位性
    為替も金も株もすぐに参照できる
//hlc3の30分足
sub_hlc3=security("BITFLYER:FXBTCJPY","30",hlc3)
//close
sub_close=security("BITFLYER:FXBTCJPY","30",close)
  • エントリーして、一定時間後にポジションを閉じる
    変数の保持方法がポイント
time_limit = input(10, "イグジット時間")
passed_time = 0
if strategy.position_size[1] == 0 and strategy.position_size != 0
    passed_time := 1
if passed_time[1] >= 1
    passed_time := nz(passed_time[1]) + 1
if strategy.position_size[1] != 1 and strategy.position_size == 0
    passed_time := 0
    
if passed_time > time_limit
    strategy.close_all()
  • for 文の使い方
my_sma(price, length) =>
    sum = price
    for i = 1 to length-1
        sum := sum + price[i]
    sum / length

plot(my_sma(close,14))
  • 下位足で日足の高値、安値を簡単に確認する
//@version=3
study("日足の高安値",overlay = true)
//日足のデータを読み込む
dMin = security(ticker, "D", low, lookahead=barmerge.lookahead_on)
dMax = security(ticker, "D", high, lookahead=barmerge.lookahead_on)
//プロットする
plot(dMin, color=yellow,linewidth = 2)
plot(dMax, color=red,linewidth = 2)

//ticker : 現在と同じ銘柄を読み込む
//"D" : 日足のデータを読み込む
//"lookahead=barmerge.lookahead_on" : 未来のデータを使って表示を更新する
24
28
0

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
24
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?