//+------------------------------------------------------------------+
//|                                                        NDuet.mq4 |
//|                      Copyright � 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 LightSeaGreen
#property indicator_color2 HotPink


extern int sper=55;
extern int fper=21;
extern int nBar=300;
extern int per=14;

double mas[];
double maf[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{//1
   SetIndexBuffer(0,mas);
   SetIndexBuffer(1,maf);
   return(0);
}//1
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{//1
   ObjectsDeleteAll(0);
   return(0);
}//1
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{//1
   int limit=0,i=0,blokb=0,bloks=0;
   double mstwo=0,mftwo=0,cci=0,trend=0;
   int counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(i=0; i<limit; i++)
   {//2
      cci=iCCI(NULL,0,per,PRICE_CLOSE,i);
      trend=((Close[i+fper]-Close[i])/Point);
      mas[i]=iMA(NULL,0,sper,0,MODE_EMA,PRICE_CLOSE,i);
      mstwo =iMA(NULL,0,sper,0,MODE_EMA,PRICE_CLOSE,i+2);

      maf[i]=iMA(NULL,0,fper,0,MODE_EMA,PRICE_CLOSE,i);
      mftwo =iMA(NULL,0,fper,0,MODE_EMA,PRICE_CLOSE,i+2);

      if((mas[i]>maf[i])&&((mas[i]-maf[i])/Point<10)&&((mas[i]-maf[i])/Point>=1)
      &&(mstwo<mftwo)&&(cci<0)&&(trend>0)&&(bloks==0))
      {//3
         string Name1="S"+i;
         double Price1=NormalizeDouble(High[i]+10*Point,Digits);
         ObjectCreate(Name1,OBJ_ARROW,0,Time[i],Price1);
         ObjectSet(Name1,OBJPROP_ARROWCODE,242);
         ObjectSet(Name1,OBJPROP_COLOR,Aqua);
         ObjectSetText(Name1,"Sell",10,"Times New Roman",Aqua);         
         bloks=1;
         blokb=0;
      }//3
      
      if((mas[i]<maf[i])&&((maf[i]-mas[i])/Point<10)&&((maf[i]-mas[i])/Point>=1)
      &&(mstwo>mftwo)&&(cci>0)&&(trend<0)&&(blokb==0))
      {//3
         string Name2="B"+i;
         double Price2=NormalizeDouble(Low[i]-10*Point,Digits);
         ObjectCreate(Name2,OBJ_ARROW,0,Time[i],Price2);
         ObjectSet(Name2,OBJPROP_ARROWCODE,241);
         ObjectSet(Name2,OBJPROP_COLOR,Red);
         ObjectSetText(Name2,"Buy",10,"Times New Roman",Aqua);         
         blokb=1;
         bloks=0;
      }//3
   }//2   
   return(0);
}//1
//+------------------------------------------------------------------+


/*[[
	Name := NDuet
	Author := Copyright � 2004, HomeSoft-Tartan Corp.
	Link := spiky@transkeino.ru
	Separate Window := Yes
	First Color := Lime
	First Draw Type := Line
	First Symbol := 217
	Use Second Data := Yes
	Second Color := Red
	Second Draw Type := Line
	Second Symbol := 218
]]*/

/*
Variable : shift(0),i(0),mas(0),maf(0),mstwo(0),mftwo(0),blokb(0),bloks(0),cci(0),trend(0);

SetLoopCount(0);
// loop from first bar to current bar (with shift=0)
For shift=Bars-1 Downto 0 Begin
	SetIndexValue(shift, 0);
	SetIndexValue2(shift, 0);
End;

For shift=nBar+fper Downto 0 Begin
cci=iCCI(per,shift);
trend=((C[shift+fper]-C[shift])/point);
mas=iMA(sper,MODE_EMA,shift);
mstwo=iMA(sper,MODE_EMA,shift+2);
SetIndexValue(shift,mas);
maf=iMA(fper,MODE_EMA,shift); 
mftwo=iMA(fper,MODE_EMA,shift+2);
SetIndexValue2(shift,maf);
if mas>maf and ((mas-maf)/point)<10 and ((mas-maf)/point)>=1 and mstwo<mftwo and cci<0 and trend>0 and bloks=0 then Begin
   bloks=1;blokb=0;SetArrow(Time[shift],H[shift]+10*Point,242,Aqua);end;
if mas<maf and ((maf-mas)/point)<10 and ((maf-mas)/point)>=1 and mstwo>mftwo and cci>0 and trend<0 and blokb=0 then Begin
   blokb=1;bloks=0;SetArrow(Time[shift],L[shift]-10*Point,241,Red);end;
end;
*/