MQL4 - automated forex trading   /  

Forum

Login | Register | Language: ru cn en
Freelancer services needed to perform 150+ jobs of traders

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

Back to topics list To post a new topic, please log in or register
avatar
53
phipho 2013.03.19 05:03
 
//+------------------------------------------------------------------+
//|                                               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();
//----
  }
//+------------------------------------------------------------------+
 
Oil boosted on China rate cut and OPEC speculation
Oil boosted on China rate cut and OPEC speculation
news | 21 November 2014, 15:13 | Views: 166
On Friday China's central bank cut its benchmark interest rates for the first time in more than two years in order to reduce borrowing costs and support an economy on track for its slowest annual growth in 24 years. This move made Brent crude oil rise to near $81. The speculation that OPEC could agree next week to reduce oil production also contributed to oil recovery.
avatar
53
phipho 2013.03.19 05:03 #
 
what happen
avatar
53
avatar
55
cyberfx.org 2013.03.19 08:24 #
 
Means the function "void TrailingStop(int iTotal)" is not called inside start, init or deinit functions or their subfunctions.
 
avatar
53
phipho 2013.03.19 11:41 #
 

so i must do what?

 

avatar
2411
deVries 2013.03.19 12:08 #
 
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());
       }

 

Back to topics list  

To add comments, please log in or register