/*[[
        Name := MedianTraderSilver
        Author := ERB
        Link :=
        Notes := Eur Daily only
        Lots := 1.00
        Stop Loss := 150
        Take Profit := 150
        Trailing Stop := 0
]]*/

Define: MaxOpenTrades(6);
Define: Compounding(0);
Define: GearingToOne(1);
Var:    a002(0);
Var:    r1(0);
Var:    r2(0);
Var:    tvi(0);
Var:    s1(0);
Var:    s2(0);
Var:    step(0);
Var:    ppl(30);
Var:    cnt(0);
Var:    t1(0);
Var:    t2(0);
Var:    t3(0);
Var:    t4(0);
Var:    t5(0);
Var:    ShowGrid(0);
Var:    SLSell(0);
Var:    SLBuy(0);
Var:    TradeVol(1);

if CurTime < StrToTime("2006.12.12") then Exit;
//if CurTime > StrToTime("2005.09.30") then Exit;

if IsTesting == False then ShowGrid = 1;
//Comment("This EA is for Demo accounts only");

if Compounding == 1 then TradeVol = 0.1 * Round(Equity / (10000 / 
GearingToOne));
if Compounding == 0 then TradeVol = Lots;

tvi = Abs(iMA(3,0,1) - iMA(3,0,3)) / (High[Highest(2,3,3)] - Low[Lowest(1,3,3)]) * 100;
if tvi < 40 then step = 150;
if tvi > 40 then step = 200;

a002 = GetGlobalVariable("a002");
if Low[Lowest(1,2,2)]   > a002 + 50 * Point then a002 = Round((a002 + step * 
Point) * 10000) / 10000;
if High[Highest(2,2,2)] < a002 - 50 * Point then a002 = Round((a002 - step * 
Point) * 10000) / 10000;
SetGlobalVariable("a002",a002);

r2 = Round((a002 + 45 * Point) * 10000) / 10000;
r1 = Round((a002 + ppl * Point) * 10000) / 10000;
s1 = Round((a002 - ppl * Point) * 10000) / 10000;
s2 = Round((a002 - ppl * 2 * Point) * 10000) / 10000;
SLBuy  = a002 - StopLoss * Point;
SLSell = a002 + StopLoss * Point;

if ShowGrid == 1 then
   {
   
MoveObject("Sell_StopLoss_line",0,Time[0],SLSell,Time[0],SLSell,16776960,1,2);
   MoveObject("Median_line",0,Time[0],a002,Time[0],a002,16711935,1,1);
   
MoveObject("Buy_StopLoss_line",0,Time[0],SLBuy,Time[0],SLBuy,16776960,1,2);
   SetObjectText("SLSell_txt","Sell StopLoss        ","Arial",8,White);
   MoveObject("SLSell_txt",4,Time[44],SLSell,Time[44],SLSell,16777215);
   SetObjectText("Median_txt","Median        ","Arial",8,White);
   MoveObject("Median_txt",4,Time[44],"a002",Time[44],a002,16777215);
   SetObjectText("SLBuy_txt","Buy StopLoss        ","Arial",8,White);
   MoveObject("SLBuy_txt",4,Time[44],SLBuy,Time[44],SLBuy,16777215);
   }

for cnt = 1 to TotalTrades
   {
   if Ord(cnt,VAL_OPENTIME) > Time[0] then
      {
      if Ord(cnt,VAL_OPENPRICE) == s2 then t1 = 1;
      if Ord(cnt,VAL_OPENPRICE) == s1 then t2 = 1;
      if Ord(cnt,VAL_OPENPRICE) == a002 then t3 = 1;
      if Ord(cnt,VAL_OPENPRICE) == r1 then t4 = 1;
      if Ord(cnt,VAL_OPENPRICE) == r2 then t5 = 1;
      }
   }

if TotalTrades < MaxOpenTrades then
   {
   if DayOfWeek > 2 then
      {
      if Hour == 1 and Minute > 5 then
         {
         if Ask < s2 - 7 * Point and t1 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,s2,0,SLBuy,s2 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Ask < s1 - 7 * Point and t2 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,s1,0,SLBuy,s1 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Ask < a002 - 7 * Point and t3 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,a002,0,SLBuy,a002 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Ask < r1 - 7 * Point and t4 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,r1,0,SLBuy,r1 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Bid > r2 + 7 * Point and t5 == 0 then
            {
            SetOrder(OP_SELLSTOP,TradeVol,r2,0,SLSell,r2 - TakeProfit * 
Point,Black);
            Exit;
            }
         }

      if Hour == 1 and Minute < 5 then
         {
         t1 = 0;
         t2 = 0;
         t3 = 0;
         t4 = 0;
         t5 = 0;
         }
      }

   if DayOfWeek == 2 then
      {
      if Hour == 2 and Minute > 5 then
         {
         if Ask < s2 - 7 * Point and t1 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,s2,0,SLBuy,s2 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Ask < s1 - 7 * Point and t2 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,s1,0,SLBuy,s1 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Ask < a002 - 7 * Point and t3 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,a002,0,SLBuy,a002 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Ask < r1 - 7 * Point and t4 == 0 then
            {
            SetOrder(OP_BUYSTOP,TradeVol,r1,0,SLBuy,r1 + TakeProfit * 
Point,Black);
            Exit;
            }
         if Bid > r2 + 7 * Point and t5 == 0 then
            {
            SetOrder(OP_SELLSTOP,TradeVol,r2,0,SLSell,r2 - TakeProfit * 
Point,Black);
            Exit;
            }
         }
      if Hour == 1 and Minute < 5 then
         {
         t1 = 0;
         t2 = 0;
         t3 = 0;
         t4 = 0;
         t5 = 0;
         }
      }
   }

for cnt = 1 to TotalTrades
   {
   if Ord(cnt,VAL_TYPE) > OP_SELL then
      {
      if Hour == 0 and Minute >= 30 then
         {
         DeleteOrder(Ord(cnt,VAL_TICKET),Red);
         Exit;
         }
      if DayOfWeek == 6 then
         {
         if Hour == 22 and Minute >= 30 then
            {
            DeleteOrder(Ord(cnt,VAL_TICKET),Red);
            Exit;
            }
         }
      }
   }