Download MetaTrader 5

Function "TrailingStop" is not referenced and will be removed from exp-file

To add comments, please log in or register
phipho
165
phipho  
//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20050610

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.02;
extern double DecreaseFactor     = 3;
extern int     iTakeProfit    = 100;
extern int     iTrailingStop  = 35;
extern int     iStopLoss      = 30;
extern int     iMagicNumber   = 12444;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
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/1000.0,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);
  }
  
  //---- trailin stop
void TrailingStop(int iTotal){

   int iCount;
   
   if(iTrailingStop < 1)return(-1); // error
   
   for(iCount=0;iCount<iTotal;iCount++){

      OrderSelect(iCount, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber)
         switch(OrderType()){
            case OP_BUY:
               if(Bid-OrderOpenPrice()>Point*iTrailingStop){
                  if(OrderStopLoss()<Bid-Point*iTrailingStop){
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*iTrailingStop,OrderTakeProfit(),0,Green);
                  }
               }
               break;
            case OP_SELL:
               if((OrderOpenPrice()-Ask)>(Point*iTrailingStop)){
                  if((OrderStopLoss()>(Ask+Point*iTrailingStop)) || (OrderStopLoss()==0)){
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*iTrailingStop,OrderTakeProfit(),0,Red);
                  }
               }
               break;
         }
      }
   
      return(0);
}
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma5,ma10,v1;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
  
    
   
      
   // get the current prices for each moving average
   ma5= iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0);
   ma10=iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);
   v1=iRSI (NULL, 0,14, PRICE_MEDIAN, 0);
//---- sell conditions
   if(ma5<ma10 && v1<50)  
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Ask+iStopLoss*Point,Bid-iTakeProfit*Point,"",MAGICMA,0,Red);
      return;
     }
//---- buy conditions
   if( ma5>ma10 && v1>50)  
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Bid-iStopLoss*Point,Ask+iTakeProfit*Point,"",MAGICMA,0,Blue);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
    double ma5,ma10,v1;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
    ma5= iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0);
   ma10=iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);
   v1=iRSI (NULL, 0,14, PRICE_MEDIAN, 0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(ma5 < ma10) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(ma5>ma10) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+
phipho
165
phipho  
what happen
phipho
165
phipho  
and it have many order when it finish. i want read to one order when have signal but when it trakeprofig so it have new order. 
Asparuh Nestorov
354
Asparuh Nestorov  
Means the function "void TrailingStop(int iTotal)" is not called inside start, init or deinit functions or their subfunctions.
phipho
165
phipho  

so i must do what?

Tjipke de Vries
6398
Tjipke de Vries  
phipho:

so i must do what?

 


void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();

.....  you can try

void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else{
       CheckForClose();
       TrailingStop(OrdersTotal());
       }
To add comments, please log in or register