{#OptVar1 24;10;50;1}
var Bar,s,poz,p,p1,p2,po,i,b,per_pc,size_pc,flag: integer;
var atf1,atf10,at1,at10,at240f,at240,pane: integer;
var StpL,StpS,BuL,BuS,TpL,TpS: integer;
var time1,time2,time3,y: integer;
var stop,take,ep,pc,doliv: float;
var h,h1,h2,h3,h4,h5:float;
var l,l1,l2,l3,l4,l5:float;
var c1,c2,c3,c4,c5:float;
pane:=CreatePane(150,true,true);
i:=0;
po:=0;
y:=#OptVar1/10; // коэфициент от 10дневного атр для уровня доливки
StpL:=800; //размер стопа
StpS:=800;
BuL:=1000;  //уровень безубытка
BuS:=1000;
TpL:=9500;  //уровень тейка(люстры)
TpS:=9500;
time1:=1100;
time2:=1345;
time3:=2340;
SetScaleDaily;
atf1:=atrseries(1);
atf10:=atrseries(10);
RestorePrimarySeries;
at1:=IntradayFromDaily(atf1);
at10:=IntradayFromDaily(atf10);
 h:=0; h1:=0; h2:=0; h3:=0; h4:=0; h5:=0;
 l:=1000000; l1:=0; l2:=0; l3:=0; l4:=0; l5:=0;
 c1:=0; c2:=0; c3:=0; c4:=0; c5:=0;
for Bar := 2000 to BarCount - 1 do
begin
  if po=1 then inc(i);
  //p:=lastposition;
  If GetTime(bar)<GetTime(Bar-1) then
    Begin
      h5:=h4; h4:=h3; h3:=h2; h2:=h1; h1:=h; h:=PriceHigh(bar);
      l5:=l4; l4:=l3; l3:=l2; l2:=l1; l1:=l; l:=PriceLow(bar);
      c5:=c4; c4:=c3; c3:=c2; c2:=c1; c1:= PriceClose(bar-1);
      poz:=0;
      if (GetSeriesValue(bar,at1)>GetSeriesValue(bar,at10)) and
         (GetSeriesValue(bar-2,at1)<GetSeriesValue(bar-2,at10)) then s:=2000
      else s:=0;
    end;
  if h<PriceHigh(bar) then h:=PriceHigh(bar);
  if l>PriceLow(bar) then l:=PriceLow(bar);
  if not lastpositionactive then
    Begin
    // лонг
      if (s=0) and (poz<2) and
         (((PriceHigh(bar)-PriceLow(bar)>PriceHigh(bar-1)-PriceLow(bar-1)) and
         (PriceHigh(bar)-PriceLow(bar)>PriceHigh(bar-2)-PriceLow(bar-2)) and
         (PriceHigh(bar)-PriceLow(bar)>PriceHigh(bar-3)-PriceLow(bar-3))) or
         ((PriceHigh(bar)>PriceHigh(bar-1)) and (PriceHigh(bar-1)<PriceHigh(bar-2)))) and
         (PriceClose(bar)>PriceHigh(bar)-((PriceHigh(bar)-PriceLow(bar))/3))  and
         (ema(bar,#close,500)<ema(bar,#close,20)) and
         (PriceClose(bar)>ema(bar,#close,160)) and
         (PriceClose(bar)>Parabolic( Bar, 0.02, 0.02, 0.2 )) and
         ((time1<GetTime(bar)) and (GetTime(bar)<=time2))  then
           Begin
             buyatclose(bar,'long');
             ep:=PriceClose(bar)-1;
             stop:=ep-StpL;
             take:=ep+TpL;
             inc(poz);
             po:=1;
             p1:=lastposition;
             doliv:=ep+(GEtSeriesValue(bar,at10)/y);
             flag:=1;
           end;
     // шорт
       if (s=0) and (poz<2) and
          (((PriceHigh(bar)-PriceLow(bar)>PriceHigh(bar-1)-PriceLow(bar-1)) and
          (PriceHigh(bar)-PriceLow(bar)>PriceHigh(bar-2)-PriceLow(bar-2)) and
          (PriceHigh(bar)-PriceLow(bar)>PriceHigh(bar-3)-PriceLow(bar-3))) or
          ((PriceLow(bar)<PriceLow(bar-1)) and (PriceLow(bar-1)>PriceLow(bar-2)))) and
          (PriceClose(bar)<PriceLow(bar)+((PriceHigh(bar)-PriceLow(bar))/3)) and
          (ema(bar,#close,700)>ema(bar,#close,20)) and
          (PriceClose(bar)<ema(bar,#close,160)) and
          (PriceClose(bar)<Parabolic( Bar, 0.02, 0.02, 0.2 )) and
          ((time1<GetTime(bar)) and (GetTime(bar)<=time2))  then
            Begin
              shortatclose(bar,'short');
              ep:=PriceClose(bar)+1;
              stop:=ep+StpS;
              take:=ep-TpS;
              inc(poz);
              po:=1;
              p1:=lastposition;
              doliv:=ep-(GetSeriesValue(bar,at10)/y);
              flag:=-1;
            end;
     end
  else
     Begin
       if lastlongpositionactive then
         Begin
           if (flag=1) and (PriceHigh(bar)>=doliv) then
             Begin
               buyatstop(bar,doliv,'long doliv');
               p2:=lastposition;
               flag:=0;
             end;
           if stop<ep then
             Begin
               if PriceHigh(bar-1)>=ep+BuL then stop:=ep;
             //  if stop>=ep then stop:=ep;
             end;
           if (stop=ep) and (PriceHigh(bar)>=doliv) then stop:=(ep+doliv)/2;
           if PriceLow(bar)<=stop then
             Begin
               sellatstop(bar,stop,p1,'sl,bu');
               sellatstop(bar,stop,p2,'sl,bu');
               po:=0; i:=0; flag:=0;
             end;
           if PriceHigh(bar)>=take then
             Begin
               sellatlimit(bar,take,p1,'');
               sellatlimit(bar,take,p2,'');
               po:=0; i:=0;  flag:=0;
             end;
           if GetTime(bar)=time3 then
             Begin
               sellatclose (bar,p1,'prof');
               sellatclose (bar,p2,'prof');
               po:=0; i:=0;  flag:=0;
             end;
        end;
       if lastshortpositionactive then
         Begin
            if (flag=-1) and (PriceLow(bar)<=doliv) then
              Begin
                shortatstop(bar,doliv,'short doliv');
                p2:=lastposition;
                flag:=0;
             end;
           if stop>ep then
             Begin
               if PriceLow(bar-1)<=ep-BuS then stop:=ep;
            //   if stop<=ep then stop:=ep;
             end;
           if (stop=ep) and (PriceLow(bar)<=doliv) then stop:=(ep+doliv)/2;
           if PriceHigh(bar)>=stop then
             Begin
               coveratstop(bar,stop,p1,'sl,bu');
               coveratstop(bar,stop,p2,'sl,bu');
               po:=0; i:=0;  flag:=0;
             end;
           if PriceLow(bar)<=take then
             Begin
               coveratlimit(bar,take,p1,'');
               coveratlimit(bar,take,p2,'');
               po:=0; i:=0;   flag:=0;
             end;
           if GetTime(bar)=time3 then
             Begin
               coveratclose(bar,p1,'prof');
               coveratclose(bar,p2,'prof');
               po:=0; i:=0;  flag:=0;
             end;
     end;
     end;
end;