I am an absolute rooky in “programming” indicators or expert adisors. I am not a programmer, but I will do my very best for understanding the code, before combining it with code from other indicators. So I am not programmer, but rather a “combiner”.
But now I have a problem I cannot solve for myself and I hope that somebody can help me.
In the attachment you will find a simple regression indicator. I added an alert what will be executed when the price touches the borderline of the regression channel. Now I want to execute not only an alert, but also an order.For this I need an EA not an indicator. I can add a standard indicator in MT4 with “iMA” for example, if I want to use a moving average. But how can I implement my own indicator??? For the EA I need the calculation of the regression indicator. Can somebody show me please, how to implement the code of the indicator in the EA? I really don´t know what to do.
//±-----------------------------------------------------------------+
//| Linear Regression.mq4 |
//| Copyright © 2006, tageiger, aka
//|
//±-----------------------------------------------------------------+
#property copyright "Copyright © 2006, tageiger,
#property link
#property indicator_chart_window
extern int period=0;
/default 0 means the channel will use the open time from “x” bars back on which ever time period
the indicator is attached to. one can change to 1,5,15,30,60…etc to “lock” the start time to a specific
period, and then view the “locked” channels on a different time period…/
extern int line.width=2;
extern int LR.length=100; // bars back regression begins
extern color LR.c=Magenta;
extern double std.channel.1=2.0;
extern color c.1=Magenta;
extern double AlertChannelPips=0.003;
//extern double SL_Pips=0.005 //will be needed when open a position
extern int AlertOn=1;
int init(){return(0);}
int deinit(){ ObjectDelete(period+“m “+LR.length+” TL”);
ObjectDelete(period+“m “+LR.length+” +”+std.channel.1+“d”); ObjectDelete(period+“m “+LR.length+” -”+std.channel.1+“d”);
return(0);}
int start(){//refresh chart
ObjectDelete(period+“m “+LR.length+” TL”);
ObjectDelete(period+“m “+LR.length+” +”+std.channel.1+“d”); ObjectDelete(period+“m “+LR.length+” -”+std.channel.1+“d”);
//linear regression calculation
int start.bar=LR.length, end.bar=0;
int n=start.bar-end.bar+1;
//---- calculate price values
double value=iClose(Symbol(),period,end.bar);
double a,b,c;
double sumy=value;
double sumx=0.0;
double sumxy=0.0;
double sumx2=0.0;
for(int i=1; i<n; i++)
{
value=iClose(Symbol(),period,end.bar+i);
sumy+=value;
sumxy+=valuei;
sumx+=i;
sumx2+=ii;
}
c=sumx2n-sumxsumx;
if(c==0.0) return;
b=(sumxyn-sumxsumy)/c;
a=(sumy-sumxb)/n;
double LR.price.2=a;
double LR.price.1=a+bn;
//---- maximal deviation calculation (not used)
double max.dev=0;
double deviation=0;
double dvalue=a;
for(i=0; i<n; i++)
{
value=iClose(Symbol(),period,end.bar+i);
dvalue+=b;
deviation=MathAbs(value-dvalue);
if(max.dev<=deviation) max.dev=deviation;
}
//Linear regression trendline
ObjectCreate(period+“m “+LR.length+” TL”,OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1,Time[end.bar],LR.price.2);
ObjectSet(period+“m “+LR.length+” TL”,OBJPROP_COLOR,LR.c);
ObjectSet(period+“m “+LR.length+” TL”,OBJPROP_WIDTH,line.width);
ObjectSet(period+“m “+LR.length+” TL”,OBJPROP_RAY,false);
//…standard deviation…
double x=0,x.sum=0,x.avg=0,x.sum.squared=0,std.dev=0;
for(i=0; i<start.bar; i++) {
x=MathAbs(iClose(Symbol(),period,i)-ObjectGetValueByShift(period+“m “+LR.length+” TL”,i));
x.sum+=x;
if(i>0) {
x.avg=(x.avg+x)/i;
x.sum.squared+=(x-x.avg)(x-x.avg);
std.dev=MathSqrt(x.sum.squared/(start.bar-1)); } }
//Print("LR.price.1 “,LR.price.1,” LR.Price.2 “,LR.price.2,” std.dev ",std.dev);
//…standard deviation channels…
ObjectCreate(period+“m “+LR.length+” +”+std.channel.1+“d”,OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1+std.devstd.channel.1,
Time[end.bar],LR.price.2+std.dev*std.channel.1);
ObjectSet(period+“m “+LR.length+” +”+std.channel.1+“d”,OBJPROP_COLOR,c.1);
ObjectSet(period+“m “+LR.length+” +”+std.channel.1+“d”,OBJPROP_WIDTH,line.width);
ObjectSet(period+“m “+LR.length+” +”+std.channel.1+“d”,OBJPROP_RAY,false);
ObjectCreate(period+“m “+LR.length+” -”+std.channel.1+“d”,OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1-std.devstd.channel.1,
Time[end.bar],LR.price.2-std.devstd.channel.1);
ObjectSet(period+“m “+LR.length+” -”+std.channel.1+“d”,OBJPROP_COLOR,c.1);
ObjectSet(period+“m “+LR.length+” -”+std.channel.1+“d”,OBJPROP_WIDTH,line.width);
ObjectSet(period+“m “+LR.length+” -”+std.channel.1+“d”,OBJPROP_RAY,false);
// ALERT
// Alert channel width
double DefChannelBorderTop.1=LR.price.2+std.devstd.channel.1;
double DefChannelBorderBottom.1=LR.price.2-std.devstd.channel.1;
// Trend DOWN
if (
(LR.price.1 > LR.price.2) &&
((DefChannelBorderTop.1)-(DefChannelBorderBottom.1)) > (AlertChannelPips) &&
(Ask > LR.price.2+std.dev*std.channel.1) &&
(AlertOn > 0)
)
{
PlaySound(“alert.wav”);
AlertOn = 0; // Alert will be switched off after trigger!
//---------------------
//Instead of the Alert an order should be executed:
//ONLY IF POSITION IS FLAT!! NO OPEN ORDERS!!
//EXECUTION WHEN PRICE TOUCHES LINE! NOT AFTER CLOSING THE BAR!
//Direction: SHORT
//Stopp Loss: SL_Pips
//Position size: By using SL_Pips, the risk must be 1% of the account balance.
//Take profit: DefChannelBorderBottom.1, if possible the TP should be adjusted with every new bar.
//---------------------
}
// Trend UP
if (
(LR.price.1 < LR.price.2) &&
((DefChannelBorderTop.1)-(DefChannelBorderBottom.1)) > (AlertChannelPips) &&
(Bid < LR.price.2-std.dev*std.channel.1) &&
(AlertOn > 0)
)
{
PlaySound(“alert.wav”);
AlertOn = 0; // Alert will be switched off after trigger!
//---------------------
//Instead of the Alert an order should be executed:
//ONLY IF POSITION IS FLAT!! NO OPEN ORDERS!!
//EXECUTION WHEN PRICE TOUCHES LINE! NOT AFTER CLOSING THE BAR!
//Direction: LONG
//Stopp Loss: SL_Pips
//Position size: By using SL_Pips, the risk must be 1% of the account balance.
//Take profit: DefChannelBorderTop.1, if possible the TP should be adjusted with every new bar.
//---------------------
}
return(0);}
//±-----------------------------------------------------------------+