#define cnTaimFrame 9
extern int mTimeFrame = 3;
 
static int TimeFrame[ cnTaimFrame ] = {0,PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1 };
 
extern int mPeriod = 9;
extern double cRisk = 10; // 10%
extern double lTakeProfit = 10;
extern double sTakeProfit = 10;
extern double lTrailingStop = 30;
extern double sTrailingStop = 30;
extern int cFRI=6;
extern int cSL_P = 21;
extern int ÄÎÕÎÄ_ÏÈÏÑ=2;
extern int iTF_ôðàêòàëû = PERIOD_M30;
extern int iTF_EMA = PERIOD_M1;
 
extern int Slippage = 4;
 
extern double Lots = 0.1;
 
int order_id;
 
 
void deinit() {
}
 
static double rrr;
 
 
int start(){
 
int i;
int mCountSdelok;
double mSummaRisk;
 
double Ñâå÷àDown;
double Ñâå÷àUp;
 
 
         
// ÓäàëåíèåSTOP_ÎÐÄÅÐÎÂ();
 
   int iTF = TimeFrame[ mTimeFrame ] ; 
 
   double diClose0=iClose(NULL,iTF,0);
   double diClose2=iClose(NULL,iTF,0);
 
 
   iTF = TimeFrame[ mTimeFrame ] ; 
   double diMA1=iMA(NULL,iTF_EMA,mPeriod,0,MODE_SMA,PRICE_OPEN,0);
   double diMA3=iMA(NULL,iTF_EMA,mPeriod,0,MODE_SMA,PRICE_OPEN,0);
 
double mFRU;
double mFRD;
 
   for ( i = 0; i<= cFRI; i++)
   {
      mFRU = iFractals(NULL, iTF_ôðàêòàëû,MODE_UPPER, i  ); // ÂÅÐÕÍÈÉ ÔÐÀÊÒÀË ÈÙÅÌ
      if (mFRU != 0)
         break;
 
   }
   for ( i = 0; i<= cFRI; i++)
   {
      mFRD = iFractals(NULL, iTF_ôðàêòàëû,MODE_LOWER,  i  ); // ÍÈÆÍÈÉ ÔÐÀÊÒÀË ÈÙÅÌ
      if (mFRD != 0)
         break;
   }
 
double ÒÐÅÍÄ1,ÒÐÅÍÄ2;
int ÒðåíäÂÅÐÕ;
int ÒðåíäÂÍÈÇ;
 
   for ( i = 0; i <= 30; i++ )
   {
      ÒÐÅÍÄ1=NormalizeDouble( iMA(NULL,PERIOD_H1,30,0,MODE_EMA,PRICE_OPEN,i),4); // 30-ÿ ëèíèè äîëæíà èäòè èëè âíèç èëè ââåðõ
      ÒÐÅÍÄ2=NormalizeDouble(iMA(NULL,PERIOD_H1,30,0,MODE_EMA,PRICE_OPEN,i+1),4); // 30-ÿ ëèíèè äîëæíà èäòè èëè âíèç èëè ââåðõ
      if (ÒÐÅÍÄ1 >= ÒÐÅÍÄ2)
         ÒðåíäÂÍÈÇ++;
 
      if (ÒÐÅÍÄ1 <= ÒÐÅÍÄ2)
         ÒðåíäÂÅÐÕ++;
 
   }
//Print("íèç "+ÒðåíäÂÍÈÇ+" âåðõ "+ÒðåíäÂÅÐÕ);   
  
 //  double EMA_2 = iMA(NULL,15,21,0,MODE_SMA,PRICE_OPEN,0);
 
// Print( " BALANCE [ "+AccountBalance()+" ]    FreeMargin [ "+AccountFreeMargin()+" ] "+ rrr  );
 
 
 
 
//   if (!ExistPositions()  && ( Ñâå÷àUp >= 0.0004 || Ñâå÷àDown >= 0.0004 ) ){
 
   if (!ExistPositions()  ){
 
//
// ÒÓÒ ÏÐÎÁÓÅÌ ÎÏÐÅÄÅËÈÒÜ òðåíä!
//
 
     if ( AccountBalance() == AccountFreeMargin() )
     {
        mSummaRisk =  AccountBalance() / 100 * cRisk;
     }
     else
     {
        mSummaRisk =  (AccountBalance()+AccountFreeMargin()) / 100 * cRisk;
     }
    
     mCountSdelok = NormalizeDouble( (mSummaRisk / 100) / 4  , 0 ); // 100$ 1 - ñäåëêà
     
     if (mCountSdelok <= 1)
         mCountSdelok = 1;   
 
      if ( mCountSdelok >= 4 )
      {
         Lots  = mCountSdelok * 0.1;
         
         mCountSdelok = 4;
      }
      if (Lots >= 8)
      {
 
         mCountSdelok = 1;
         Lots = 8;
         
      }        
 
//Lots = 0.1;      // òîðìîçèì
//mCountSdelok=1;  // îãàíè÷åíèå
 
 
      if ( ( diClose0 < diMA1 ) && mFRU != 0 && ÒðåíäÂÅÐÕ > ÒðåíäÂÍÈÇ) // && EMA_2 < diClose0 )
      {
Print(" mFRU  ["+mFRU+" mFRD [ "+mFRD);
         for ( i = 1; i <= mCountSdelok; i++)
            OpenBuy();
         
//rrr = AccountBalance()-AccountFreeMargin();
//Print( " BALANCE [ "+AccountBalance()+" ]    FreeMargin [ "+AccountFreeMargin()+" ] "+ rrr  );
         return(0);
      }
 
      if ((diClose2>diMA3 )&& mFRD != 0 && ÒðåíäÂÅÐÕ < ÒðåíäÂÍÈÇ ) // && EMA_2 > diClose0 ))
      {
Print(" mFRU  ["+mFRU+" mFRD [ "+mFRD);
 
       for ( i = 1; i <= mCountSdelok; i++)
         OpenSell();
//rrr = AccountBalance()-AccountFreeMargin();
//Print( " BALANCE [ "+AccountBalance()+" ]    FreeMargin [ "+AccountFreeMargin()+" ] "+ rrr  );
         return(0);
      }
   }
   TrailingPositionsBuy(lTrailingStop);
   TrailingPositionsSell(sTrailingStop);
   return (0);
}
 
bool ExistPositions() {
   for (int i=0; i<OrdersTotal(); i++) 
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      {
 
 
         if (OrderSymbol()==Symbol()) 
         {
           return(True);
         }
 
 
      } 
   } 
   return(false);
}
 
 
void TrailingPositionsBuy(int trailingStop) 
{ 
 
   string sCommentSL;
   int order_opp;
   double orderU;
   double oppositeProfit;
   
   for (int i=0; i<OrdersTotal(); i++) 
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      { 
         if (OrderSymbol()==Symbol()) 
         { 
            if (OrderType()==OP_BUY) 
            { 
            /*
                if ( OrderComment() == "tss" || OrderComment() == "tbs")
                {
                    order_id = OrderTicket(); 
                    if ( OrderProfit() >= 30 ) 
                       OrderClose(order_id,0.3,Bid,3,Red);
                }
                else
                {
                */
               if (OrderProfit() >= ÄÎÕÎÄ_ÏÈÏÑ  ) 
               {
                  order_id = OrderTicket();  
                  sCommentSL = OrderComment();
                  if ( StringSubstr(sCommentSL,0,2) == "SL" )
                  {
                     order_opp = order_id; // OrderTicket();  
                     order_id = StrToInteger ( StringSubstr(sCommentSL,2,StringLen(sCommentSL) ) );
                     if ( OrderSelect(order_id , SELECT_BY_TICKET, MODE_TRADES) == true )
                     { 
                        orderU = OrderProfit();
                        OrderSelect(order_opp , SELECT_BY_TICKET, MODE_TRADES);
                        oppositeProfit = OrderProfit();
//   Print("<<<***>>>"+TimeToStr ( CurTime() )+" ÎÏÏ "+order_opp+"  [ "+sCommentSL+"] "+oppositeProfit+"   ÇÀÊ "+order_id+" "+orderU);
                        if ( oppositeProfit >= MathAbs(orderU) )
                        {
//   Print("      >>>"+TimeToStr ( CurTime() )+" ÎÏÏ "+order_opp+"  [ "+sCommentSL+"] "+oppositeProfit+"   ÇÀÊ "+order_id+" "+orderU);
                            OrderCloseBy(order_id,order_opp);      
                        }
                     }
                  }
                  else 
                  {
                     OrderClose(order_id,Lots,Bid,3,Red);
                     ÓäàëåíèåSTOP_ÎÐÄÅÐÎÂ();
                  }
               }
               else
               {
                  if (Bid-OrderOpenPrice()>trailingStop*Point) 
                  { 
                     if (OrderStopLoss()<Bid-trailingStop*Point) 
                        ModifyStopLoss(Bid-trailingStop*Point); 
                  } 
               }
            } 
         } 
      } 
   } 
} 
 
void TrailingPositionsSell(int trailingStop) 
{ 
   string sCommentSL;
   int order_opp;
   double orderU;
   double oppositeProfit;
 
   for (int i=0; i<OrdersTotal(); i++) 
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      { 
         if (OrderSymbol()==Symbol()) 
         { 
            if (OrderType()==OP_SELL) 
            { 
               /*
               if ( OrderComment() == "tss" ||  OrderComment() == "tbs" )
               {
                  order_id = OrderTicket(); 
                  if ( OrderProfit() >= 30 ) 
                       OrderClose(order_id,0.3,Ask,3,Red);
               }
               else
               {            
               */
               if (OrderProfit() >= ÄÎÕÎÄ_ÏÈÏÑ  ) 
               {
                  order_id = OrderTicket();  
                  sCommentSL = OrderComment();
                  if ( StringSubstr(sCommentSL,0,2) == "SL" )
                  {
                     order_opp = order_id; // OrderTicket();  
                     order_id = StrToInteger ( StringSubstr(sCommentSL,2,StringLen(sCommentSL) ) );
                     if ( OrderSelect(order_id , SELECT_BY_TICKET, MODE_TRADES) == true )
                     { 
                        orderU = OrderProfit();
                        OrderSelect(order_opp , SELECT_BY_TICKET, MODE_TRADES);
                        oppositeProfit = OrderProfit();
 //  Print("<<<***>>>"+TimeToStr ( CurTime() )+" ÎÏÏ "+order_opp+"  [ "+sCommentSL+"] "+oppositeProfit+"   ÇÀÊ "+order_id+" "+orderU);
                        if ( oppositeProfit >= MathAbs(orderU) )
                        {
 // Print("      >>>"+TimeToStr ( CurTime() )+" ÎÏÏ "+order_opp+"  [ "+sCommentSL+"] "+oppositeProfit+"   ÇÀÊ "+order_id+" "+orderU);
                            OrderCloseBy(order_id,order_opp);      
                        }
                     }
                  }
                  else 
                  {
                     OrderClose(order_id,Lots,Ask,3,Red);
                     ÓäàëåíèåSTOP_ÎÐÄÅÐÎÂ();
                  }
               }
               else
               {
                  if (OrderOpenPrice()-Ask>trailingStop*Point) 
                  { 
                     if (OrderStopLoss()>Ask+trailingStop*Point || OrderStopLoss()==0)  
                        ModifyStopLoss(Ask+trailingStop*Point); 
                  }
               }
            } //(OrderType()==OP_SELL) 
         } 
      } 
   } 
} 
void ModifyStopLoss(double ldStopLoss) { 
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); 
  // if (fm && UseSound) PlaySound(NameFileSound); 
} 
 
void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   string sComment;
   
   ldLot = Lots; // GetSizeLot(); 
   ldStop = 0; 
//   ldTake = Ask+lTakeProfit*Point; // GetTakeProfitBuy(); 
   lsComm = ""; // ;GetCommentForOrder(); 
   order_id = OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,0,0, Red); 
   if (order_id != -1)
   {
      sComment = "SL"+order_id ;
      OrderSend(Symbol(),OP_SELLSTOP,ldLot*3,Bid-cSL_P*Point,3,0,0,sComment,order_id,0,0); 
   }
   // if (UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   string sComment;
   
   ldLot = Lots; // GetSizeLot(); 
   ldStop = 0; 
//   ldTake = Bid-sTakeProfit*Point; // GetTakeProfitSell(); 
   lsComm = ""; // GetCommentForOrder(); 
   order_id = OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,0,0,SandyBrown); 
   if (order_id != -1)
   {
      sComment = "SL"+order_id ;
      OrderSend(Symbol(),OP_BUYSTOP,ldLot*3,Ask+cSL_P*Point,3,0,0,sComment,order_id,0,0); 
   }
    // if (UseSound) PlaySound(NameFileSound); 
} 
//string GetCommentForOrder() { return(Name_Expert); } 
//double GetSizeLot() { return(Lots); } 
//double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); } 
//double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); } 
 
 
void ÓäàëåíèåSTOP_ÎÐÄÅÐÎÂ()
{
   int i,j;
   int tiket,tiketSTOP; 
   int magicSTOP;
   int flag;
 
   for (i=0; i<OrdersTotal(); i++) 
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      { 
         if (OrderType()==OP_SELLSTOP) 
         {
             tiketSTOP = OrderTicket();
             magicSTOP = OrderMagicNumber(); // 
             flag = 0;
             for ( j=0; j<OrdersTotal(); j++) 
             {
                if ( OrderType() != OP_SELLSTOP && OrderType() != OP_BUYSTOP )
                {
                   tiket = OrderTicket();
                   if ( magicSTOP == tiket)
                   {            
                      flag = 1;
                   }
                }
             }
             if (flag ==0)
                OrderDelete(tiketSTOP);
         }
         if (OrderType()==OP_BUYSTOP) 
         {
             tiketSTOP = OrderTicket();
             magicSTOP = OrderMagicNumber(); // 
             flag = 0;
             for ( j=0; j < OrdersTotal(); j++ ) 
             {
                if ( OrderType() != OP_BUYSTOP )
                {
                   tiket = OrderTicket();
                   if ( magicSTOP == tiket)
                   {            
                      flag = 1;
                   }
                }
             }
             if (flag ==0)
                OrderDelete(tiketSTOP);
         }
      }   
   }
}
             
            
Comments