MT4 ADR TO Trading Station

If you need your own private indicator, signal or strategy, or any kind of development using various trading APIs - please visit this forum.

Moderator: admin

Re: MT4 ADR TO Trading Station

Postby Apprentice » Fri Sep 12, 2014 12:30 am

Can you provide code, description, formula or web reference.
Something like Average Daily Range (ADR) Projection
viewtopic.php?f=17&t=2799&hilit=ADR
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: MT4 ADR TO Trading Station

Postby fromthewest » Fri Sep 12, 2014 8:23 pm

Code: Select all
#property indicator_chart_window




int ADROpenHour= 0; // start time for range calculation (LEAVE AT 0. PROGRAM DOESN'T WORK PROPERLY OTHERWISE.)
int ADRCloseHour= 24; // end time for range calculation (LEAVE AT 24. PROGRAM DOESN'T WORK PROPERLY OTHERWISE.)

int ATRTimeFrame= PERIOD_D1; // timeframe for ATR (LEAVE AT PERIOD_D1)
extern int ADRPeriod= 14; // period for ATR

// for time difference
extern int LookBack = 0;

extern string LineStyle_Help = "0:SOLID; 1:DASH; 2:DOT; 3:DASHDOT; 4:DASHDOTDOT";
extern int LineStyle= 2;
extern int LineThickness1= 1; // normal thickness
extern color LineColor1= Blue; // normal color
extern int LineHitThickness= 3; // thickness for range reached state
extern color LineHitColor= Red; // color for range reached state

extern bool ShowLabel= true;
extern bool ShowLevelPrices= true; // show prices on h-lines
extern int LabelDistant= 19; // number of bars from right, where lines labels will be shown
extern int PriceFontsize = 10;




double mPoint;




//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
Print("Period= ", Period());
Print("Point= ", Point);



if(Point==0.00001) {mPoint=0.0001;}//Alpari
else if(Point==0.001) {mPoint=0.01;}
else {mPoint = Point;}//InterBankFX



return(0);
}


int deinit()
{
int obj_total= ObjectsTotal();

for (int i= obj_total; i>=0; i--) {
string name= ObjectName(i);

if (StringSubstr(name,0,5)=="[ADR]")
ObjectDelete(name);
}

return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{

static datetime timelastupdate= 0;
static int lasttimeframe= 0,
lastfirstbar= -1;

int idxfirstbaroftoday= 0,
idxfirstbarofyesterday= 0,
idxlastbarofyesterday= 0;


//---- exit if period is greater than daily charts
if(Period() > 1440) {
Alert("Error - Chart period is greater than 1 day.");
return(-1); // then exit
}




// let's find out which hour bars make today and yesterday
ComputeDayIndices(0,LookBack,idxfirstbaroftoday, idxfirstbarofyesterday, idxlastbarofyesterday);


// no need to update these buggers too often (the code below is a bit tricky, usually just the
// timelastupdate would be sufficient, but when turning on MT after the night there is just
// the newest bar while the rest of the day is missing and updated a bit later). Don't mess
// with this unless you are absolutely sure you know what you're doing.
if (Time[0]==timelastupdate && Period()==lasttimeframe && lastfirstbar==idxfirstbaroftoday) {
// return (0);
}


lasttimeframe= Period();
timelastupdate= Time[0];
lastfirstbar= idxfirstbaroftoday;



//
// okay, now we know where the days start and end
//


// int tzdiff= TimeZoneOfData + TimeZoneOfSession,
// tzdiffsec= tzdiff*3600;


datetime startofday= Time[idxfirstbaroftoday]; // datetime (x-value) for labes on horizontal bars

double adr= iATR(NULL, ATRTimeFrame, ADRPeriod, 1);



//
// walk forward through today and collect high/lows within the same day
//
double today_high,
today_low,
today_open= 0,
today_range,
lasthigh, lastlow,last,
to_long_adr= 0,
to_short_adr= 0,
adr_high= 0,
adr_low= 0;
bool adr_reached= false, lastreached;

// new-start
for (int j= idxfirstbaroftoday; j>=0; j--) {

datetime bartime= Time[j];//-tzdiffsec;

if (TimeHour(bartime)>=ADROpenHour && TimeHour(bartime)<ADRCloseHour) {

if (today_open==0) {
today_open= Open[idxfirstbaroftoday]; // should be open of today start trading hour
adr_high= today_open + adr;
adr_low= today_open - adr;
today_high= today_open;
today_low= today_open;

}

for (int k= 0; k<3; k++) {

double price;

switch (k) {
case 0: price= Low[j]; break;
case 1: price= High[j]; break;
case 2: price= Close[j]; break;
}

lasthigh= today_high;
lastlow= today_low;
lastreached= adr_reached;

today_high= MathMax(today_high, price);
today_low= MathMin(today_low, price);

today_range= today_high - today_low;
adr_reached= today_range >= adr - Point/2; // "Point/2" to avoid rounding problems (double variables)


// adr-high
if (!lastreached && !adr_reached) {
adr_high= today_low + adr;
}
else
if (!lastreached && adr_reached && price>=lasthigh) {
adr_high= today_low + adr;
}
else
if (!lastreached && adr_reached && price<lasthigh) {
adr_high= lasthigh;
}
else {
adr_high= adr_high;
}


// adr-low
if (!lastreached && !adr_reached) {

adr_low= today_high - adr;
}
else
if (!lastreached && adr_reached && price>=lastlow) {

adr_low= today_low;
}
else
if (!lastreached && adr_reached && price<lastlow) {

adr_low= lasthigh - adr;
}
else {

adr_low= adr_low;
}

to_long_adr= adr_high - Close[j];
to_short_adr= Close[j] - adr_low;


}
}
}
// new-end





// draw the vertical bars that marks the time span
SetTimeLine(" ", " ", idxfirstbaroftoday, CadetBlue, Low[idxfirstbaroftoday]- 10*Point);

color col= LineColor1;
int thickness= LineThickness1;

if (adr_reached) {
col= LineHitColor;
thickness= LineHitThickness;
}

SetLevel("DTFL ADR High", adr_high, col, LineStyle, thickness, startofday);
SetLevel("DTFL ADR Low", adr_low, col, LineStyle, thickness, startofday);


string reached_str= "Yes";
if (!adr_reached)
reached_str= "No";



string comment=


"ADR: " + DoubleToStr(MathRound((adr/Point)),0) +
" Todays Range: " + DoubleToStr(MathRound(((today_high-today_low)/Point)),0) +
" To ADR High: " + DoubleToStr(MathRound(((to_long_adr)/Point)),0) +
" To ADR Low: " + DoubleToStr(MathRound(((to_short_adr)/Point)),0);





Comment(comment);


return(0);
}


//+------------------------------------------------------------------+
//| Compute index of first/last bar of yesterday and today |
//+------------------------------------------------------------------+
void ComputeDayIndices(int tzlocal, int tzdest, int &idxfirstbaroftoday, int &idxfirstbarofyesterday, int &idxlastbarofyesterday)
{
int tzdiff= tzlocal + tzdest,
tzdiffsec= tzdiff*3600,
dayminutes= 24 * 60,
barsperday= dayminutes/Period();

int dayofweektoday= TimeDayOfWeek(Time[0] - tzdiffsec), // what day is today in the dest timezone?
dayofweektofind= -1;

//
// due to gaps in the data, and shift of time around weekends (due
// to time zone) it is not as easy as to just look back for a bar
// with 00:00 time
//

idxfirstbaroftoday= 0;
idxfirstbarofyesterday= 0;
idxlastbarofyesterday= 0;

switch (dayofweektoday) {
case 6: // sat
case 0: // sun
case 1: // mon
dayofweektofind= 5; // yesterday in terms of trading was previous friday
break;

default:
dayofweektofind= dayofweektoday -1;
break;
}




// search backwards for the last occrrence (backwards) of the day today (today's first bar)
for (int i= 0; i<=barsperday+1; i++) {
datetime timet= Time[i] - tzdiffsec;
// Print(Symbol(), " DayofWeek[", i, ,"]= ", TimeDayOfWeek(timet), " (", dayofweektoday, ") ", TimeToStr(timet));
if (TimeDayOfWeek(timet)!=dayofweektoday) {
idxfirstbaroftoday= i-1;
break;
}
}


// Print(Symbol(), " idxfirstoftoday ", idxfirstbaroftoday);

// search backwards for the first occrrence (backwards) of the weekday we are looking for (yesterday's last bar)
for (int j= 0; j<=2*barsperday+1; j++) {
datetime timey= Time[i+j] - tzdiffsec;
if (TimeDayOfWeek(timey)==dayofweektofind) { // ignore saturdays (a Sa may happen due to TZ conversion)
idxlastbarofyesterday= i+j;
break;
}
}


// search backwards for the first occurrence of weekday before yesterday (to determine yesterday's first bar)
for (j= 1; j<=barsperday; j++) {
datetime timey2= Time[idxlastbarofyesterday+j] - tzdiffsec;
if (TimeDayOfWeek(timey2)!=dayofweektofind) { // ignore saturdays (a Sa may happen due to TZ conversion)
idxfirstbarofyesterday= idxlastbarofyesterday+j-1;
break;
}
}



}


//+------------------------------------------------------------------+
//| Helper |
//+------------------------------------------------------------------+
void SetLevel(string text, double level, color col1, int linestyle, int thickness, datetime startofday)
{
int digits= Digits;
string labelname= "[ADR] " + text + " Label",
linename= "[ADR] " + text + " Line",
pricelabel;

// create or move the horizontal line
if (ObjectFind(linename) != 0) {
ObjectCreate(linename, OBJ_TREND, 0, startofday, level, Time[0],level);
}

ObjectSet(linename, OBJPROP_BACK, true);
ObjectSet(linename, OBJPROP_STYLE, linestyle);
ObjectSet(linename, OBJPROP_COLOR, col1);
ObjectSet(linename, OBJPROP_WIDTH, thickness);
ObjectMove(linename, 1, Time[0],level);
ObjectMove(linename, 0, startofday, level);



// put a label on the line
if (ShowLabel && StrToInteger(text)==0){
if (ObjectFind(labelname) != 0)
ObjectCreate(labelname, OBJ_TEXT, 0, Time[0]/* MathMin(Time[BarForLabels], startofday + 2*Period()*60)*/, level);
}

ObjectMove(labelname, 0, Time[0] - LabelDistant*Period()*60, level);



pricelabel= " " + text;

if (ShowLevelPrices && StrToInteger(text)==0)
pricelabel= pricelabel + ": "+DoubleToStr(level, Digits);


ObjectSetText(labelname, pricelabel, PriceFontsize, "Times New Roman", col1);
}


//+------------------------------------------------------------------+
//| Helper |
//+------------------------------------------------------------------+
void SetTimeLine(string objname, string text, int idx, color col1, double vleveltext)
{
string name= "[ADR] " + objname;
int x= Time[idx];

if (ObjectFind(name) != 0)
ObjectCreate(name, OBJ_TREND, 0, x, 0, x, 100);

ObjectMove(name, 0, x, 0);
ObjectMove(name, 1, x, 100);
ObjectSet(name, OBJPROP_BACK, true);
ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
ObjectSet(name, OBJPROP_COLOR, DarkGray);

if (ObjectFind(name + " Label") != 0)
ObjectCreate(name + " Label", OBJ_TEXT, 0, x, vleveltext);

ObjectMove(name + " Label", 0, x, vleveltext);
ObjectSetText(name + " Label", text, 8, "Times New Roman", col1);
}
fromthewest
 
Posts: 3
Joined: Thu Sep 11, 2014 6:46 pm

Re: MT4 ADR TO Trading Station

Postby Apprentice » Sun Sep 14, 2014 3:36 am

I have given you my offer via email.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Return to Premium Development Services

Who is online

Users browsing this forum: No registered users and 9 guests