//+-----------+ //| LSMA | //+-----------+ #property copyright "Copyright 2005 Ron Thompson" #property link "http://www.forexmt4.com/" // indicator settings #property indicator_separate_window // buffers #property indicator_buffers 2 #property indicator_color1 Gray #property indicator_color2 Aqua #property indicator_level1 1 #property indicator_level2 2 #property indicator_level3 3 #property indicator_level4 4 // user input extern int xbars = 5000 ; extern int PeriodGo = 1 ; extern int PeriodStop = 200 ; extern int PeriodStep = 10 ; extern int MAperiod = 14 ; extern bool AlertMA = false ; extern bool AlertPrice = false ; extern bool Signals = true ; extern double upmax = 18; extern double upline = 16; extern double dnline = -16; extern double dnmax = -18; bool beenuptomax=true; bool beendntomax=true; // bar counting datetime bartime=0; datetime alerttime=0; // chart period string chartperiod; // Objects int uniq=0; string Object_ID = "myx"; // LSMA int per; // buffers double buff1[]; double buff2[]; double myPoint; string Id = "Vote"; //+-----------+ //| Init | //+-----------+ int init() { // 233 up arrow // 234 down arrow // 158 little dot // 159 big dot // 168 open square // 120 box with X SetIndexBuffer(0,buff1); SetIndexStyle(0, DRAW_LINE); //SetIndexArrow(0,159); SetIndexBuffer(1,buff2); SetIndexStyle(1, DRAW_LINE); //SetIndexArrow(1,159); myPoint = SetPoint(); switch (Period()) { case 1: chartperiod = "M1"; break; case 5: chartperiod = "M5"; break; case 15: chartperiod = "M15"; break; case 30: chartperiod = "M30"; break; case 60: chartperiod = "H1"; break; case 240: chartperiod = "H4"; break; case 1440: chartperiod = "D1"; break; } // set the lines on the indicator // to match the user input //SetLevelStyle(STYLE_DASH, 1, Gray); SetLevelValue(0,upmax); SetLevelValue(1,upline); SetLevelValue(2,dnline); SetLevelValue(3,dnmax); Print("Init complete"); } //+-----------+ //| DE-Init | //+-----------+ int deinit() { myObjectsDeleteAll(); Print("DE-Init complete"); } //+-----------+ //| Each Tick | //+-----------+ int start() { // draw once at open of bar if(bartime==Time[0]) return(0); bartime=Time[0]; int pos; int vote; double wt; //LSMA current value double wtp; //LSMA previous value //clear the array for(pos = xbars; pos >= 0; pos--) { buff1[pos]=0; } myObjectsDeleteAll(); for(per = PeriodGo; per <= PeriodStop; per+=PeriodStep) { for(pos = xbars; pos >= 0; pos--) { wtp = wt; wt = LSMA2(pos); if(wtwtp) buff1[pos]++; }//for pos }//for per // if user wants an MA, draw now if(MAperiod>0) { ArraySetAsSeries(buff1,true); for(pos = xbars; pos >= 0; pos--) { buff2[pos]=iMAOnArray(buff1,0,MAperiod,0,MODE_LWMA,pos); } } for(pos = xbars; pos >= 0; pos--) { if(buff2[pos]>upmax) beenuptomax=true; if(buff2[pos]=upline && buff2[pos]<=upmax) { //draw number on chart ObjectCreate (Object_ID+DoubleToStr(pos,0)+"_"+DoubleToStr(uniq,0), OBJ_TEXT, 0, Time[pos], Open[pos] ); ObjectSetText(Object_ID+DoubleToStr(pos,0)+"_"+DoubleToStr(uniq,0), ".",48,"Arial",White); uniq++; } if(buff2[pos]dnmax) { //draw number on chart ObjectCreate (Object_ID+DoubleToStr(pos,0)+"_"+DoubleToStr(uniq,0), OBJ_TEXT, 0, Time[pos], Open[pos] ); ObjectSetText(Object_ID+DoubleToStr(pos,0)+"_"+DoubleToStr(uniq,0), ".",48,"Arial",Red); uniq++; } // dropping if(Signals && beenuptomax && buff2[pos+1]>upline && buff2[pos]dnline) { beendntomax=false; //draw number on chart ObjectCreate (Object_ID+DoubleToStr(pos,0)+"_"+DoubleToStr(uniq,0), OBJ_TEXT, 0, Time[pos], Close[pos]+(myPoint*7) ); ObjectSetText(Object_ID+DoubleToStr(pos,0)+"_"+DoubleToStr(uniq,0), "LO-NG",10,"Arial",White); uniq++; } } // bar counting if(alerttime!=iTime(Symbol(), 0, 0) ) { alerttime=iTime(Symbol(), 0, 0) ; Print("-----Alerts checking"); if(AlertMA && MAperiod>0) { if(buff2[1]<0.0 && buff2[0]>0.0) Alert(Symbol()+" on MA "+chartperiod+" is BUY"); if(buff2[1]>0.0 && buff2[0]<0.0) Alert(Symbol()+" on MA "+chartperiod+" is SELL"); } if(AlertPrice) { if(buff1[1]<0.0 && buff1[0]>0.0) Alert(Symbol()+" on PRICE "+chartperiod+" is BUY"); if(buff1[1]>0.0 && buff1[0]<0.0) Alert(Symbol()+" on PRICE "+chartperiod+" is SELL"); } } }//start /* double LSMA(int mybar) { int i; double sum=0; double lengthvar = ( per + 1.0 ) / 3.0; for(i = per; i >= 1 ; i--) { sum += ( i - lengthvar)*Open[per - i + mybar]; } return(sum * 6 / (per*(per + 1)) ); } */ double LSMA2(int shift) { double wt; double ma1=iMA(Symbol(),0,per,0,MODE_SMA ,PRICE_OPEN,shift); double ma2=iMA(Symbol(),0,per,0,MODE_LWMA,PRICE_OPEN,shift); wt = MathFloor((3.0*ma2-2.0*ma1)/myPoint)*myPoint; return(wt); } void myObjectsDeleteAll() { int obj = ObjectsTotal(OBJ_TEXT); int i, limit; string objName; if (obj > 0) { for (i = obj; i >= 0;i--) { objName = ObjectName(i); if (StringFind(objName,Object_ID, 0) >= 0) ObjectDelete(objName); } } limit=1500; for(i=limit; i>=0; i--) ObjectDelete(Id+i); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); }