#property description "TMA bands"
#property link "http://ea.kitgain.com/"
//2018.02.16 原型是TMALine_2_for_Mix.mq4
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 DodgerBlue
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 2.6;
extern int ATRPeriod = 110;
extern bool Interpolate = TRUE;
extern bool alertsOn = FALSE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = FALSE;
double tma[];
double Upper[];
double Lower[];
double indi[];
string EAname;
bool 计算值;
bool 返回柱数;
int 工作周期;
//string Gs_nothing_168 = "nothing";
datetime 现时;
string 周期字[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int 周期[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int init()
{
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, tma);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, Upper);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, Lower);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, indi);
EAname = WindowExpertName();
返回柱数 = TimeFrame == "returnBars";
if (返回柱数) return (0);
计算值 = TimeFrame == "calculateValue";
if (计算值) return (0);
工作周期 = 找周期(TimeFrame);
IndicatorShortName(周期文字(工作周期) + " TMA bands (" + HalfLength + ")");
return (0);
}
int deinit() {
return (0);
}
int start()
{
int k;
double 分;
double 母;
double ATRbb;
int st;
int datetime_48;
int 已计 = IndicatorCounted();
if (已计 < 0) return (-1);
if (已计 > 0) 已计--;
int 未计 = MathMin(Bars - 1, Bars - 已计 + HalfLength);
if (返回柱数)
{
tma[0] = 未计 + 1;
return (0);
}
if (计算值 || 工作周期 == Period())
{
for (int i = 未计; i >= 0; i--)
{
分 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
母 = HalfLength + 1;
k = 1;
for (int j = HalfLength; k <= HalfLength; j--)
{
分 += j * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + k);
母 += j;
if (k <= i)
{
分 += j * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - k);
母 += j;
}
k++;
}
// 110 2.6
ATRbb = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
tma[i] = 分 / 母;
Upper[i] = tma[i] + ATRbb;
Lower[i] = tma[i] - ATRbb;
indi[i] = 0;
if (alertsOnHighLow) //false
{
if (High[i] > Upper[i]) indi[i] = 1;
if (Low[i] < Lower[i]) indi[i] = -1;
}
else
{
if (Close[i] > Upper[i]) indi[i] = 1;
if (Close[i] < Lower[i]) indi[i] = -1;
}
}
if (计算值) return (0);
显示箭头();
return (0);
}
未计 = MathMax(未计, MathMin(Bars - 1, iCustom(NULL, 工作周期, EAname, "returnBars", 0, 0) * 工作周期 / Period()));
for (i = 未计; i >= 0; i--)
{
st = iBarShift(NULL, 工作周期, Time[i]);
tma[i] = iCustom(NULL, 工作周期, EAname, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 0, st);
Upper[i] = iCustom(NULL, 工作周期, EAname, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 1, st);
Lower[i] = iCustom(NULL, 工作周期, EAname, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 2, st);
indi[i] = iCustom(NULL, 工作周期, EAname, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 3, st);
if (工作周期 <= Period() || st == iBarShift(NULL, 工作周期, Time[i - 1])) continue;
if (Interpolate) //true
{
datetime_48 = iTime(NULL, 工作周期, st);
for (int o = 1; i + o < Bars && Time[i + o] >= datetime_48; o++)
{
}
for (j = 1; j < o; j++)
{
tma[i + j] = tma[i] + (tma[i + o] - tma[i]) * j / o;
Upper[i + j] = Upper[i] + (Upper[i + o] - Upper[i]) * j / o;
Lower[i + j] = Lower[i] + (Lower[i + o] - Lower[i]) * j / o;
}
}
}
显示箭头();
return (0);
}
void 显示箭头()
{
int i;
if (alertsOn) //false
{
if (alertsOnCurrent) i = 0;//false
else i = 1;
i = iBarShift(NULL, 0, iTime(NULL, 工作周期, i));
if (indi[i] != indi[i + 1])
{
if (indi[i] == 1.0) 生成信息(i, "up");
if (indi[i] == -1.0) 生成信息(i, "down");
}
}
}
void 生成信息(int ofs, string As_4)
{
string att;
if (现时 != Time[ofs])
{
//Gs_nothing_168 = As_4;
现时 = Time[ofs];
att = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " + 周期文字(工作周期) + " TMA bands price penetrated ", As_4, " band");
Print(att);
//if (alertsMessage) Alert(att);
//if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), att);
//if (alertsSound) PlaySound("alert2.wav");
}
}
int 找周期(string 命令字) //"All tf"
{
命令字 = 大写(命令字);
for (int i = ArraySize(周期) - 1; i >= 0; i--)
if (命令字 == 周期字[i] || 命令字 == "" + 周期[i]) return (MathMax(周期[i], Period()));
return (Period());
}
string 周期文字(int Ai_0)
{
for (int i = ArraySize(周期) - 1; i >= 0; i--)
if (Ai_0 == 周期[i]) return (周期字[i]);
return ("");
}
string 大写(string 命令字)
{
int 码;
string 处理串 = 命令字;//"All tf"
for (int i = StringLen(命令字) - 1; i >= 0; i--)
{
码 = StringGetChar(处理串, i);//逐个处理,从后到前
// 96 a~z 123
if ((码 > '`' && 码 < '{') || (码 > 223 && 码 < 256))
处理串 = StringSetChar(处理串, i, 码 - 32);//转大写
else
if (码 > -33 && 码 < 0) 处理串 = StringSetChar(处理串, i, 码 + 224);
}
return (处理串);
}
发表评论