Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satomacoto/f01317f94080b4ea87da to your computer and use it in GitHub Desktop.
Save satomacoto/f01317f94080b4ea87da to your computer and use it in GitHub Desktop.
MyLWMA
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
// 指標バッファ
double BufMA[];
// パラメータ
extern int MA_Period = 20;
// 初期化関数
int init()
{
// 指標バッファ割り当て
SetIndexBuffer(0,BufMA);
// 指標ラベルの設定
SetIndexLabel(0, "LWMA("+MA_Period+")");
return 0;
}
// 指標処理関数
// LWMA[i] = {N*Close[i]+(N-1)*Close[i-1]+...+Close[i+N-1]}/{1+2+...+N}
int start()
{
int limit = Bars-IndicatorCounted();
if (limit == Bars) limit -= MA_Period - 1;
for (int i = limit - 1; i >= 0; i--)
{
double sum = 0;
int weight = 0;
for (int k = 0; i < MA_Period; k++)
{
int w = MA_Period - k; // w = 1とするとSMA
sum += w * Close[i+k];
weight += w;
}
BufMA[i] = sum/weight;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment