//+------------------------------------------------------------------+
//|												 Copyright © 2010, JACreative |
//|																 KGForexWorld.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, KGForexWorld.com"
#property link      "http://www.kgforexworld.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Lime
//#property indicator_color2 Red
//#property indicator_color3 Red
//#property indicator_color4 Lime
//#property indicator_color5 Lime
//#property indicator_width1 2
//#property indicator_width2 2
//#property indicator_width3 2
//#property indicator_width4 2
//#property indicator_width5 2
extern	string		nomor_indi	= "1";
extern	int		Periode	= 10;
extern	color		Warna	= Lime;
extern   int Price_Type=PRICE_WEIGHTED;
extern	bool		ID 		= true,
						Harga		= true;
						
						//BBSD1    =true,
						//BBSD2    =false,
						//BBSD3    =false;
					
						
					
						


extern	int		Jarak			= 1;
int		CBar,Per,Par;
double	start;
double lsma[];
//double lsmaua[];
//double lsmaub[];
//double lsmada[];
//double lsmadb[];

//color		Warna;
string	SEED;
datetime	LINETIME,SAVETIME,TSearch;
string   LabelName,NamaFile;
int handle,SAVE_LINETIME;
//+------------------------------------------------------------------+
//| init()													                     |
//+------------------------------------------------------------------+
int init(){

  LabelName	= "LSMA_HILOW"+nomor_indi+Warna;
	NamaFile		= "LSMA\\"+LabelName+Symbol();


	handle	= FileOpen(NamaFile, FILE_BIN|FILE_READ);
	if(handle>=1){
		LINETIME			= FileReadInteger(handle, LONG_VALUE);
		FileClose(handle);
	}
	else if(LINETIME == 0){
		LINETIME = Time[Periode];
	}
	SAVE_LINETIME	= LINETIME;

   

	

Per = Periode;




	Warna	=Warna ;
	SetIndexBuffer(0,lsma);
   SetIndexLabel(0,nomor_indi+"LSMA");	
	SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,Warna);
	//	SetIndexBuffer(1,lsmaua);
		
		
	//	SetIndexBuffer(2,lsmaub);
	

   
	//	SetIndexBuffer(3,lsmada);
	
		
	//	SetIndexBuffer(4,lsmadb);
	


     
}


//+-------------------------------------------------------------------+
//| deinit													                      |
//+-------------------------------------------------------------------+
int deinit(){

	ObjectDelete(nomor_indi+"LSMA");

   
   
	ObjectDelete(nomor_indi+"LSMA"+" TXT");
	
		
   ObjectDelete(nomor_indi+" LINE");
}


//+------------------------------------------------------------------+
//| start															               |
//+------------------------------------------------------------------+
void start(){

	//if((Per <= 1)){
	//	return(0);
//	}
	int    i,k,J,counted_bars=IndicatorCounted();
   double deviation;
   double sum,oldval,newres;
	
	string 	Legend,Sign;
	
	double	HI,LOW,BUYER,SELLER,BUYERSELLER,SUM,F161,F261,F423;
	
   	bool		LINECREATE = false;
   
   
    

		if(ObjectFind(LabelName) == -1){

	   	
		ObjectCreate(nomor_indi+" LINE", OBJ_VLINE,0,LINETIME,0);
		ObjectSet(nomor_indi+" LINE", OBJPROP_COLOR, Warna);
		ObjectSet(nomor_indi+" LINE", OBJPROP_STYLE, STYLE_DOT);
		//LINECREATE = true;
		
//	}
	//	ObjectSet(LabelName, OBJPROP_BACK, true);
	}
	
	
	LINETIME = ObjectGet(nomor_indi+" LINE", OBJPROP_TIME1);
	if (Par!=iBarShift(NULL, 0, LINETIME, false)) {
	  for(i=0; i<=Par; i++){
        lsma[i]=0;
        // if (lsma[Par] > lsma[Par+1]) CleanPoint(Par,lsmaua,lsmaub);
         //if (lsma[Par] < lsma[Par+1]) CleanPoint(Par,lsmada,lsmadb);
			
      }
	
	
	
	}
	Par = iBarShift(NULL, 0, LINETIME, false);
  
   	if((SAVE_LINETIME != LINETIME) || (IndicatorCounted() == 0)){
	//	DELOBJ();
	//	FIRST			= true;
		// SIMPAN POSISI KE FILE
		handle	= FileOpen(NamaFile, FILE_BIN|FILE_WRITE);
		if(handle>=1){
			FileWriteInteger(handle, LINETIME, LONG_VALUE);
			FileClose(handle);
		}
	}
   
   
   for(i=0; i<=Par; i++){
    
      lsma[i]   = 3.0*iMA(NULL,0,Par+1-i,0,MODE_LWMA,Price_Type,i)-2.0*iMA(NULL,0,Par+1-i,0,MODE_SMA,Price_Type,i);
      //lsmaua[i] = EMPTY_VALUE;
     // lsmaub[i] = EMPTY_VALUE;
      //lsmada[i] = EMPTY_VALUE;
      //lsmadb[i] = EMPTY_VALUE;
         //if (lsma[i] > lsma[i+1]) PlotPoint(i,lsmaua,lsmaub,lsma);
         //if (lsma[i] < lsma[i+1]) PlotPoint(i,lsmada,lsmadb,lsma);
   	
   	}// END FOR
   
 
		
		
   


	if(Harga || ID){
		SET_LABEL(nomor_indi+"LSMA", "LS", Time[0]+(Period()*60*Jarak), lsma[0], Warna);
      }
		
}


//+------------------------------------------------------------------+
//|  SET_LABEL                                                       |
//+------------------------------------------------------------------+
void SET_LABEL(string name,string txt, datetime Tm, double Prc, color clr){
	if(Harga){
		if(ObjectFind(name) == -1){
			ObjectCreate(name, OBJ_ARROW, 0, Tm, Prc);
			ObjectSet(name, OBJPROP_COLOR, clr);
			ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
		}
  	 else{
			ObjectSet(name, OBJPROP_TIME1, Tm);
			ObjectSet(name, OBJPROP_PRICE1, Prc);
			ObjectSet(name, OBJPROP_COLOR, clr);
			ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
		}
	}

	if(ID){
		name = name + " TXT";
   	if(ObjectFind(name) == -1){
			ObjectCreate(name, OBJ_TEXT, 0, Tm, Prc);
			ObjectSetText(name, txt, 8, "Verdana", clr);
		}
   	else{
			ObjectSet(name, OBJPROP_TIME1, Tm);
			ObjectSet(name, OBJPROP_PRICE1, Prc);
			ObjectSetText(name, txt, 8, "Verdana", clr);
		}
	}

}
void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}