input int periodAMA = 80;
input int nfast = 80;
input int nslow = 10;
input double G = 0.8;
input double dK = 0.5;
input bool Alerts = true;
input bool EmailAlerts = false;
input bool PushAlerts = true;
input enum_candle_to_check TriggerCandle = Previous;
datetime LastAlertTime = D'01.01.1970';
double AMAbuffer[];
double AMAupsig[];
double AMAdownsig[];
double slowSC, fastSC, dSC;
extern ENUM_BASE_CORNER Btn_Corner = CORNER_LEFT_UPPER; // Button corner
extern int Btn_Subwindow = 0; // Button subwindow
extern string Btn_Text = "Aku"; // Button text
extern int Btn_FontSize = 10; // Button font size
extern string Btn_FontType = "Berlin Sans FB"; // Button font type
extern color Btn_Bg_Color = clrDimGray; // Button background color
extern color Btn_BorderColor = clrBlack; // Button border color
extern color Btn_TextColorPressed = clrYellow; // Button pressed text color
extern color Btn_TextColorUnpressed = clrLavender; // Button unpressed text color
extern int Btn_x = 20; // Button X coordinate
extern int Btn_y = 13; // Button Y coordinate
extern int Btn_Width = 60; // Button width
extern int Btn_Height = 20; // Button height
//---- indicator buffers
double ResistanceBuffer[];
double BuyerBallanceBuffer[];
double MidleBuffer[];
double SellerBallanceBuffer[];
double SupportBuffer[];
bool show_data = true; //button related
bool recalc = true; //button related
string IndicatorName, IndicatorObjPrefix, buttonId; //button related
string indicatorFileName;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
//button related
string GenerateIndicatorName(const string target)
{
string name = target;
int try = 2;
while (WindowFind(name) != -1)
{
name = target + " #" + IntegerToString(try++);
}
return name;
}
//
int OnInit()
{
IndicatorName = GenerateIndicatorName(Btn_Text);
IndicatorObjPrefix = "__" + IndicatorName + "__";
IndicatorShortName(IndicatorName);
IndicatorDigits(Digits);
double val;
if (GlobalVariableGet(IndicatorName + "_visibility", val))
show_data = val != 0;
SetIndexArrow(1, 159);
SetIndexArrow(2, 159);
SetIndexDrawBegin(0, periodAMA + 2 );
SetIndexDrawBegin(1, periodAMA + 2);
SetIndexDrawBegin(2, periodAMA + 2);
SetIndexBuffer(0, AMAbuffer);
SetIndexBuffer(1, AMAupsig);
SetIndexBuffer(2, AMAdownsig);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0);
IndicatorDigits(_Digits);
slowSC = 2.0 / (nslow + 1);
fastSC = 2.0 / (nfast + 1);
dSC = fastSC - slowSC;
ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1);
buttonId = IndicatorObjPrefix + (Btn_Text);
createButton(buttonId, Btn_Text, Btn_Width, Btn_Height, Btn_FontType, Btn_FontSize, Btn_Bg_Color, Btn_BorderColor, Btn_TextColorPressed);
ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, Btn_y);
ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, Btn_x);
return(INIT_SUCCEEDED);
}
void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
ObjectDelete (0,buttonID);
ObjectCreate (0,buttonID,OBJ_BUTTON, Btn_Subwindow, 0, 0);
ObjectSetInteger(0,buttonID,OBJPROP_COLOR, Btn_TextColorPressed);
ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR, Btn_Bg_Color);
ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR, Btn_BorderColor);
ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
ObjectSetInteger(0,buttonID,OBJPROP_XSIZE, Btn_Width);
ObjectSetInteger(0,buttonID,OBJPROP_YSIZE, Btn_Height);
ObjectSetString (0,buttonID,OBJPROP_FONT, Btn_FontType);
ObjectSetString (0,buttonID,OBJPROP_TEXT, Btn_Text);
ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE, Btn_FontSize);
ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE, 0);
ObjectSetInteger(0,buttonID,OBJPROP_CORNER, Btn_Corner);
ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN, 1);
ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE, 9999);
ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE, 9999);
}
//button related
void handleButtonClicks()
{
if(ObjectGetInteger(0, buttonId, OBJPROP_STATE))
{
ObjectSetInteger(0, buttonId, OBJPROP_STATE, false);
show_data = !show_data;
GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
recalc = true;
// start();
}
}
//
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
handleButtonClicks();
if(id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
SetIndexArrow(1, 159);
SetIndexArrow(2, 159);
SetIndexDrawBegin(0, periodAMA + 2 );
SetIndexDrawBegin(1, periodAMA + 2);
SetIndexDrawBegin(2, periodAMA + 2);
SetIndexBuffer(0, AMAbuffer);
SetIndexBuffer(1, AMAupsig);
SetIndexBuffer(2, AMAdownsig);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0);
IndicatorDigits(_Digits);
slowSC = 2.0 / (nslow + 1);
fastSC = 2.0 / (nfast + 1);
dSC = fastSC - slowSC;
if(show_data)
{
handleButtonClicks();
ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,Btn_TextColorPressed);
}
else
{
ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,Btn_TextColorUnpressed);
SetIndexStyle(0,DRAW_NONE);
SetIndexStyle(1,DRAW_NONE);
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
SetIndexStyle(4,DRAW_NONE);
SetIndexStyle(5,DRAW_NONE);
SetIndexStyle(6,DRAW_NONE);
SetIndexStyle(7,DRAW_NONE);
}
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if (Bars <= periodAMA + 2) return 0; // Not enough bars.
int counted_bars = IndicatorCounted();
if (counted_bars > 0) counted_bars--;
int limit = Bars - counted_bars;
if (limit > Bars - periodAMA - 2) limit = Bars - periodAMA - 2;
for (int pos = limit; pos >= 0; pos--)
{
double AMA0;
if (pos == Bars - periodAMA - 2) AMA0 = Close[pos + 1];
else AMA0 = AMAbuffer[pos + 1];
double signal = MathAbs(Close[pos] - Close[pos + periodAMA]);
double noise = 0.000000001; // To avoid division by zero if it is unchanged.
for (int i = 0; i < periodAMA; i++)
{
noise += MathAbs(Close[pos + i] - Close[pos + i + 1]);
}
double ER = signal / noise;
double ERSC = ER * dSC;
double SSC = ERSC + slowSC;
double ddK = MathPow(SSC, G) * (Close[pos] - AMA0);
double AMA = AMA0 + ddK;
AMAbuffer[pos] = AMA;
if ((MathAbs(ddK) > dK * Point()) && (ddK > 0)) AMAupsig[pos] = AMA;
else AMAupsig[pos] = 0;
if ((MathAbs(ddK) > dK * Point()) && (ddK < 0)) AMAdownsig[pos] = AMA;
else AMAdownsig[pos] = 0;
AMA0 = AMA;
}
// Alerts
if (((TriggerCandle > 0) && (Time[0] > LastAlertTime)) || (TriggerCandle == 0))
{
string Text;
// Buy signal.
if ((AMAupsig[TriggerCandle] > 0) && (AMAupsig[TriggerCandle + 1] == 0))
{
Text = "点点穿越: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - 点点快线涨";
if (Alerts) Alert(Text);
if (EmailAlerts) SendMail("VarMovAvg Alert", Text);
if (PushAlerts) SendNotification(Text);
LastAlertTime = Time[0];
}
// Sell signal.
else if ((AMAdownsig[TriggerCandle] > 0) && (AMAdownsig[TriggerCandle + 1] == 0))
{
Text = "点点穿越: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - 点点快线跌";
if (Alerts) Alert(Text);
if (EmailAlerts) SendMail("VarMovAvg Alert", Text);
if (PushAlerts) SendNotification(Text);
LastAlertTime = Time[0];
}
}
return rates_total;
|