//+------------------------------------------------------------------+
//|                                                 Sidus_EA_VII.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#define MAGIC 1828212
extern double TakeProfit = 70;
extern double StopLoss = 35;
extern double TrailingStop = 10;
extern double Lots = 0.1;
extern double MaximumRisk    = 0.05;
extern double DecreaseFactor = 2;
extern int Slippage = 0;
extern string Name_Expert = "Sidus_EA_VII";
double spread = 1.5;

void deinit() {
   Comment("");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   if(Bars<100){
      Print("bars less than 100");
      return(0);
   }
   if(StopLoss<8){
      Print("StopLoss less than 10");
      return(0);
   }
int cnt, ticket, total;

   double RSI=iRSI(NULL,0,21,PRICE_CLOSE,0);
   double Price = iOpen(Symbol(), NULL, 0);
   double Trigger=iMA(NULL,0,6,0,MODE_EMA,PRICE_CLOSE,0);
   double MA1=iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,0);
   double MA2=iMA(NULL,0,28,0,MODE_EMA,PRICE_CLOSE,0);
   
   if(AccountFreeMargin()<(1*Lots)){
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);
   }
   if (!ExistPositions()){
   
        if ((Price>Trigger) && (MA1>MA2) && (RSI<60)){
         
         ticket = OrderSend(Symbol(),OP_BUY, LotsOptimized() ,Ask, 3, Ask - StopLoss*Point, 
                               Ask + TakeProfit*Point, "Sidus_EA_VII", 1828212, 0, Green);
         return(0);
        if(OrderType()==OP_BUY)
        {
         if(MA1<MA2) OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
           }
           return(0);
        }


        if ((Price<Trigger) && (MA1<MA2) && (RSI>40)){
         
         ticket = OrderSend(Symbol(), OP_SELL,LotsOptimized(),Bid, 3, Bid + StopLoss*Point, 
                               Bid - TakeProfit*Point, "Sidus_EA_VII", 1828212, 0, Red);
         return(0);
         if(OrderType()==OP_SELL)
        {
         if(MA1>MA2) OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
         
           }
      }
   }
   TrailingPositionsBuy(TrailingStop);
   TrailingPositionsSell(TrailingStop);
   return (0);
}
bool ExistPositions() {
	for (int i=0; i<OrdersTotal(); i++) {
		if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
			if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
				return(True);
			}
		} 
	} 
	return(false);
}
void TrailingPositionsBuy(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) { 
            if (OrderType()==OP_BUY) { 
               if (Bid-OrderOpenPrice()>trailingStop*Point) { 
                  if (OrderStopLoss()<Bid-trailingStop*Point) 
                     ModifyStopLoss(Bid-trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void TrailingPositionsSell(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) { 
            if (OrderType()==OP_SELL) { 
               if (OrderOpenPrice()-Ask>trailingStop*Point) { 
                  if (OrderStopLoss()>Ask+trailingStop*Point || OrderStopLoss()==0)  
                     ModifyStopLoss(Ask+trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void ModifyStopLoss(double ldStopLoss) { 
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);  
}
 

return(0);

double LotsOptimized()
  {
    double lot = Lots;
    int    orders = HistoryTotal();     // history orders total
    int    losses = 0;                  // number of losses orders without a break
//---- select lot size
    lot = NormalizeDouble(AccountFreeMargin()*MaximumRisk / 500, 1);
//---- calcuulate number of losses orders without a break
    if(DecreaseFactor > 0)
      {
        for(int i = orders - 1; i >= 0; i--)
          {
            if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false) 
              { 
                Print("Error in history!"); 
                break; 
              }
            //----
            if(OrderSymbol() != Symbol() || OrderType() > OP_SELL) 
                continue;
            //----
            if(OrderProfit() > 0) 
                break;
            //----
            if(OrderProfit() < 0) 
                losses++;
          }
        if(losses > 1) 
            lot = NormalizeDouble(lot - lot*losses / DecreaseFactor,1);
      }
//---- return lot size
    if(lot < 0.1) 
        lot = 0.1;
    return(lot);
  }    
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+