- //+-------------------------------------------------------------------+
- //| EmacRectangle.mq4 |
- //| 3EMA Cross Rectangle |
- //+-------------------------------------------------------------------+
- #property indicator_chart_window
- #property indicator_buffers 2
- #property indicator_color1 clrLime
- #property indicator_width1 2
- #property indicator_color2 clrMagenta
- #property indicator_width2 2
- extern int EMA1 = 3;
- extern int EMA2 = 21;
- extern int EMA3 = 63;
- extern double arrowDistance = 0.0003;
- extern color upRectColor = clrYellow;
- extern color dnRectColor = clrDodgerBlue;
- extern color touchRectColor = clrDarkGray;
- //---
- extern double rectHigh = 0.0001;
- extern int rectDays = 3;
- extern int bars_limit = 2000;
- extern bool showRectangle = true;
-
- double upArrow[];
- double dnArrow[];
- double prev2EMA1, prev2EMA2, prev2EMA3;
- double prevEMA1, prevEMA2, prevEMA3;
- double curEMA1, curEMA2, curEMA3;
- double prevOpenPrc, prevClosePrc, prevLowPrc, prevHighPrc;
- string objRectName;
- string rectArray[];
- string indiName = "Emac";
- int rectValidity = 0;
- int arrayMaxAmounts = 99999;
- int arrayCurAmount = 0;
- int arrayLastAmount = 0;
- //+------------------------------------------------------------------+
- //| INIT() |
- //+------------------------------------------------------------------+
- int init() {
- if (Period() != PERIOD_H1) {
- alert("Use only [url=home.php?mod=space&uid=73392]@[/url] TF: H1");
- return(0);
- }
- ArrayResize(rectArray,arrayMaxAmounts,arrayMaxAmounts);
- IndicatorBuffers(2);
- SetIndexBuffer(0,upArrow);
- SetIndexBuffer(1,dnArrow);
- SetIndexStyle(0,DRAW_ARROW);
- SetIndexStyle(1,DRAW_ARROW);
- SetIndexArrow(0,233);
- SetIndexArrow(1,234);
- SetIndexEmptyValue(0,0.0);
- SetIndexEmptyValue(1,0.0);
- SetIndexLabel(0,"Up");
- SetIndexLabel(1,"Dn");
- return(0);
- }
- //+------------------------------------------------------------------+
- //| DEINIT() |
- //+------------------------------------------------------------------+
- int deinit() {
- int k=0;
- while (k<ObjectsTotal()) {
- string objname = ObjectName(k);
- if (StringSubstr(objname,0,StringLen("Emac")) == "Emac")
- ObjectDelete(objname);
- else
- k++;
- }
- return(0);
- }
- //+------------------------------------------------------------------+
- //| START() |
- //+------------------------------------------------------------------+
- int start() {
- if (Period() != PERIOD_H1) {
- alert("Use only @ TF: H1");
- return(0);
- }
- int limit,i,k;
- int counted_bars=IndicatorCounted();
- //---- last counted bar will be recounted
- if(counted_bars>0) counted_bars--;
- limit=Bars-counted_bars;
- //---
- if (limit>bars_limit-1 && bars_limit!=0) limit=bars_limit-1;
- if (Bars<EMA1 || Bars<EMA2 || Bars<EMA3 || (bars_limit>0 && bars_limit<EMA1) || (bars_limit>0 && bars_limit<EMA2) || (bars_limit>0 && bars_limit<EMA3)) {
- alert("Adjust limit settings or put more bars on your chart!");
- return(0);
- }
- //---
- for (i=0; i<limit; i++) {
- prev2EMA1 = iMA(NULL,0,EMA1,0,MODE_EMA,PRICE_CLOSE,i+2);
- prev2EMA2 = iMA(NULL,0,EMA2,0,MODE_EMA,PRICE_CLOSE,i+2);
- prev2EMA3 = iMA(NULL,0,EMA3,0,MODE_EMA,PRICE_CLOSE,i+2);
- prevEMA1 = iMA(NULL,0,EMA1,0,MODE_EMA,PRICE_CLOSE,i+1);
- prevEMA2 = iMA(NULL,0,EMA2,0,MODE_EMA,PRICE_CLOSE,i+1);
- prevEMA3 = iMA(NULL,0,EMA3,0,MODE_EMA,PRICE_CLOSE,i+1);
- curEMA1 = iMA(NULL,0,EMA1,0,MODE_EMA,PRICE_CLOSE,i);
- curEMA2 = iMA(NULL,0,EMA2,0,MODE_EMA,PRICE_CLOSE,i);
- curEMA3 = iMA(NULL,0,EMA3,0,MODE_EMA,PRICE_CLOSE,i);
- prevOpenPrc = iOpen(NULL,0,i+1);
- prevClosePrc = iClose(NULL,0,i+1);
- prevLowPrc = iLow(NULL,0,i+1);
- prevHighPrc = iHigh(NULL,0,i+1);
- rectValidity = rectDays*86400;
-
- if ( (prev2EMA1<prev2EMA2)&&(prevEMA1>=prevEMA2)&&(curEMA1>curEMA2)&&(prevEMA2>=prevEMA3)&&(curEMA2>curEMA3) ) { // up
- upArrow[i+1] = prevLowPrc - arrowDistance;
- if (showRectangle == true) {
- //----------------------- Draw Rectangle ----------------------
- objRectName = StringConcatenate(indiName,"_",FuncPeriodToStr(Period()),"_",DoubleToStr(Time[i+1],0));
- if (ObjectFind(objRectName) == -1) { // not exists
- ObjectCreate(objRectName, OBJ_RECTANGLE, 0,Time[i+1],prevLowPrc,Time[i+1]+rectValidity,prevLowPrc+rectHigh);
- ObjectSet(objRectName, OBJPROP_COLOR, upRectColor);
- ObjectSet(objRectName, OBJPROP_WIDTH, 0);
- ObjectSet(objRectName, OBJPROP_STYLE, STYLE_SOLID);
- //--- put information to Array ---
- arrayCurAmount = ArraySize(rectArray);
- rectArray[arrayCurAmount] = StringConcatenate(objRectName,"$up$",Time[i+1],"$",prevClosePrc,"$0"); // 0=new, not yet touched
-
- } // if
- } // if
- } // if
-
- if ( (prev2EMA1>prev2EMA2)&&(prevEMA1<=prevEMA2)&&(curEMA1<curEMA2)&&(prevEMA2<=prevEMA3)&&(curEMA2<curEMA3) ) { // down
- dnArrow[i+1] = prevHighPrc + arrowDistance;
- if (showRectangle == true) {
- //----------------------- Draw Rectangle ----------------------
- objRectName = StringConcatenate(indiName,"_",FuncPeriodToStr(Period()),"_",DoubleToStr(Time[i+1],0));
- if (ObjectFind(objRectName) == -1) { // not exists
- ObjectCreate(objRectName, OBJ_RECTANGLE, 0, Time[i+1],prevHighPrc,Time[i+1]+rectValidity,prevHighPrc-rectHigh);
- ObjectSet(objRectName, OBJPROP_COLOR, dnRectColor);
- ObjectSet(objRectName, OBJPROP_WIDTH, 0);
- ObjectSet(objRectName, OBJPROP_STYLE, STYLE_SOLID);
- //--- put information to Array ---
- arrayCurAmount = ArraySize(rectArray);
- rectArray[arrayCurAmount] = StringConcatenate(objRectName,"$dn$",Time[i+1],"$",prevClosePrc,"$0"); // 0=new, not yet touched
-
- } // if
- } // if
- } // if
- } // for
-
- //----------------------- Edit Rectangle, change time2 & color, if price touched ----------------------
-
-
- return(0);
- } // start
- //+------------------------------------------------------------------+
- void DelObjects(string id) {
- int ot1=ObjectsTotal();
- while(ot1>=0) {
- if (StringFind(ObjectName(ot1),id,0)>-1) {
- ObjectDelete(ObjectName(ot1));
- }
- ot1--;
- }
- return;
- }
-
- //+------------------------------------------------------------------+
- string FuncPeriodToStr(int thePeriod) {
- switch(thePeriod) {
- case 1: return("M1");
- case 5: return("M5");
- case 15: return("M15");
- case 30: return("M30");
- case 60: return("H1");
- case 240: return("H4");
- case 1440: return("D1");
- case 10080: return("W1");
- case 43200: return("MN1");
- default: return("MN1");
- }
- }
- //+------------------------------------------------------------------+
|