Помогите доделать советника.Плиззз!!!

 

Вообщем я переделал немного соваетник и хотелось бы добавить в него еще функцию автоматического  трейлинг стопа.   

 PS: советник работает по индикатору ССI

//+------------------------------------------------------------------+
//|                                                       Ruslan.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#define magic  1052009

//---- input parameters
extern int period=720;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
 int period=14;
   double val = iCCI(NULL ,0,period,PRICE_TYPICAL,0);

   int ticket, cnt, total=OrdersTotal();
     
   if(total<1)
      {
         if(val<-150)
            {
               ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"CCI_TRADER",magic,0,Green);
               if(val>-250)
                  {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
                  }
               else
                  {
                     Print("Error opening BUY order : ",GetLastError());
                     return(0);
                  }
            }
         else if(val>150)
            {
               ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"CCI_TRADER",magic,0,Red);
               if(val<250)
                  {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
                  }
               else
                  {
                     Print("Error opening SELL order : ",GetLastError());
                     return(0);
                  }
            }
      }
      for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(val>150)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(150); // exit
                }
           }
         else // go to short position
           {
            // should it be closed?
            if(val<-150)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(-150); // exit
 }
           }
        }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

_____________

Вот так можно:

//+------------------------------------------------------------------+
//|                                                       Ruslan.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#define magic  1052009

//---- input parameters

extern int period=14;
extern string  __________ = "=== Функция Трейлинг Стоп ====";
extern bool      UseTrailing = false;//Выключатель трейлинг стопа
extern int    MinProfit = 250;//порог включения трейлин стопа
extern int    TrailingStop = 250;// величина трейлинг стопа
extern int    TrailingStep = 50; // шаг трейлинг стопа



//-- Подключаемые модули --
#include <stderror.mqh> #include <stdlib.mqh>
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

if (UseTrailing) TrailPositions(); // выключатель трейлинг стопа
//----
 //int period=14;
   double val = iCCI(NULL ,0,period,PRICE_TYPICAL,0);

   int ticket, cnt, total=OrdersTotal();
     
   if(total<1)
      {
         if(val<-150)
            {
               ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"CCI_TRADER",magic,0,Green);
               if(val>-250)
                  {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
                  }
               else
                  {
                     Print("Error opening BUY order : ",GetLastError());
                     return(0);
                  }
            }
         else if(val>150)
            {
               ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"CCI_TRADER",magic,0,Red);
               if(val<250)
                  {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
                  }
               else
                  {
                     Print("Error opening SELL order : ",GetLastError());
                     return(0);
                  }
            }
      }
      for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(val>150)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(150); // exit
                }
           }
         else // go to short position
           {
            // should it be closed?
            if(val<-150)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(-150); // exit
 }
           }
        }
     }
//----
   return(0);
  }
//+------------------------------

//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
void TrailPositions() // функция трейлинг стоп
{
  int Orders = OrdersTotal();
  for (int i=0; i<Orders; i++) {
    if (!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
    if (OrderSymbol() != Symbol()) continue;
     if (OrderMagicNumber() == magic ){    
     if (OrderType() == OP_BUY )  {
      if (Bid-OrderOpenPrice() > MinProfit*Point) {
        if (OrderStopLoss() < Bid-(TrailingStop+TrailingStep-1)*Point) {
          OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point,
                                                     OrderTakeProfit(), 0, Blue);
        }      }    }
    if (OrderType() == OP_SELL)  {
      if (OrderOpenPrice()-Ask >MinProfit*Point) {
        if (OrderStopLoss() > Ask+(TrailingStop+TrailingStep-1)*Point 
                                                       || OrderStopLoss() == 0) {
          OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point,
                                                      OrderTakeProfit(), 0, Blue);
        }   }   }    }   }  }

Непонятно, зачем вы два раза задаете

extern int period=720; - в глобальных переменных

и еще раз в ф-и СТАРТ 

int period=14;   ???????????? 

 
Спасибо огромное за советника!!!! истинно благодарен вам!!!
Причина обращения: