0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PineScriptメモ

Last updated at Posted at 2024-11-26

Pine Scriptで移動平均を使用するには、ta.sma(単純移動平均)、ta.ema(指数移動平均)などの関数を使用する。


指数移動平均(EMA)

//@version=5
indicator("Exponential Moving Average (EMA)", overlay=true)
length = input.int(14, title="EMA Length")  // 移動平均の期間を設定
ema_value = ta.ema(close, length)          // 終値を基にしたEMAを計算
plot(ema_value, color=color.red, title="EMA")

複数の移動平均を表示

複数の移動平均を1つのチャートに表示する例です。

//@version=5
indicator("SMA & EMA", overlay=true)
sma_length = input.int(20, title="SMA Length")
ema_length = input.int(50, title="EMA Length")

sma_value = ta.sma(close, sma_length)
ema_value = ta.ema(close, ema_length)

plot(sma_value, color=color.blue, title="SMA")
plot(ema_value, color=color.red, title="EMA")

移動平均と価格のクロスを検出

価格が移動平均を上抜け・下抜けする条件を追加します。

//@version=5
indicator("SMA with Cross Alerts", overlay=true)

length = input.int(20, title="SMA Length")
sma_value = ta.sma(close, length)

// 上抜け・下抜けの条件を定義
cross_up = ta.crossover(close, sma_value)
cross_down = ta.crossunder(close, sma_value)

// プロット
plot(sma_value, color=color.blue, title="SMA")
plotshape(cross_up, style=shape.labelup, location=location.belowbar, color=color.green, title="Cross Up")
plotshape(cross_down, style=shape.labeldown, location=location.abovebar, color=color.red, title="Cross Down")

応用例:移動平均を利用した戦略

例えば、短期移動平均と長期移動平均のクロスを使ったトレンド戦略。

//@version=5
strategy("Moving Average Crossover Strategy", overlay=true)

short_length = input.int(10, title="Short MA Length")
long_length = input.int(50, title="Long MA Length")

short_ma = ta.sma(close, short_length)
long_ma = ta.sma(close, long_length)

// クロスの条件
long_condition = ta.crossover(short_ma, long_ma)
short_condition = ta.crossunder(short_ma, long_ma)

// エントリーとエグジット
if (long_condition)
    strategy.entry("Long", strategy.long)
if (short_condition)
    strategy.close("Long")

// プロット
plot(short_ma, color=color.green, title="Short MA")
plot(long_ma, color=color.red, title="Long MA")
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?