MQL Guide - page 3

 
PhilWill:

Hello,

I have made a Custom Indicator which works. It displays on the Chart and everything is fine.

I am now wanting to use that in an Expert Advisor and the only value I get is 2147483647.

It has one parameter which I pass in and that is all. (Below is the code

iCustom(NULL,0,"myCustomIndicator1",11,0,0);

What am I doing wrong??

Thanks in advance,

Phil

2147483647 = EMPTY_VALUE.

void SetIndexEmptyValue( int index, double value)
 
Sets drawing line empty value. Empty values are not drawn or shown in the DataWindow. By default, empty value is EMPTY_VALUE
 
golden188:
Could someone please help me with some language to make my Expert Advisor trade a percentage of my balance? For example, I would like each trade, instead of being x number of lots, to be 10% of my current balance. Does anyone know how to do this? The more explicit the details the more helpful it would be.

thanks.
I use the following function:

extern double Lots = 0.1;
extern double MinLot = 0.1;
extern double MaxLot = 5.0;
extern int LotPrec = 1;
extern bool DynamicLot = false;
extern double LotStep = 0.1;
extern double BalanceStep = 500;
 
double GetLots() 
{
  double lots = Lots;
 
  if (DynamicLot)
  {
    lots = NormalizeDouble(LotStep*AccountBalance()/BalanceStep, LotPrec);  
  }
 
  lots = MathMax(lots, MinLot);
  lots = MathMin(lots, MaxLot);
  return (lots);
}
 
Hi, can U tell why this EA is not closing trades? And how to fix it?

//+------------------------------------------------------------------+
//|                                                           Fi.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
 
//EXTERNAL VALUES+++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
extern double Lots = 0.1;
extern double TrailingStop = 0;
 
 
 
 
datetime Time0 = 0;
 
 
//COLLECTED DATA++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double   ForceCurrent;
double   ForcePrevious1;
double   ForcePrevious2;
double   ForcePrevious3;
double   ForcePrevious4;
double   ForcePrevious5;
double   ForcePrevious6;
 
 
int cnt, ticket, total;
int BarsCount=0;
 
//EA IDENTIFIER+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
extern string nameEA       = "Fi";
 
 
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
//CALCULATING INDICATORS++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
double ForceCurrent=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,0);
double ForcePrevious1=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,1);   
double ForcePrevious2=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,2);
double ForcePrevious3=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,3);
double ForcePrevious4=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,4);
double ForcePrevious5=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,5);
double ForcePrevious6=iForce(NULL, 0, 14,MODE_LWMA,PRICE_WEIGHTED,6);
 
 
   
//TRADING+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
 
if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
   total=OrdersTotal();
   
   
if (Time0 != Time[0])
{
      // no opened orders identified
      
      
      // check for short position (SELL) possibility
      if(ForceCurrent<0.03 && ForcePrevious1<0.03 && ForcePrevious4>0.03 && ForcePrevious5>0.03 && ForcePrevious1<=ForcePrevious1<=ForcePrevious2<=ForcePrevious3<=ForcePrevious4<=ForcePrevious5)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Fi",16384,0,Red);
         Time0 = Time[0];
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
}        
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
 
 
   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
        {
            // long position is opened         
           {
            // should it be closed?
            
            
            if(ForceCurrent>-0.03 && ForcePrevious1>-0.03 && ForcePrevious4<-0.03 && ForcePrevious5<-0.03 && ForcePrevious1>=ForcePrevious1>=ForcePrevious2>=ForcePrevious3>=ForcePrevious4>=ForcePrevious5)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
              
           }
        }
     }
     
    
 
   return(0);
  }
// the end.
 
wonderkey:
many thanks for being so helpful here. You couldn´t be so kind to introduce the solution of the following problem to me?
My EA sometimes gets stopped out in a winning trade and a few seconds later it sets up the same trade once again, which is not only senseless but very often results in losses. How can I tell the EA to wait with a new order 1 hour, or any other time?

Thanks in advance!

-wonderkey

Hi,

Select the last order and don't trade some time.


int MinTimeSec = 60; //no trade 60 seconds after last close
int Magic = ...
 
void start()
{
  ...
 
  if (LastOrderSelect(MODE_HISTORY, OP_BUY, OP_SELL))
  {
    if (TimeCurrent() - OrderCloseTime() < MinTimeSec) return;
  }
 
  ...
}
 
 
bool LastOrderSelect(int pool, int type1, int type2 = -1)
{
  datetime tm = -1;
  int ticket = -1;
 
  if (pool == MODE_TRADES)
  {
    int cnt = OrdersTotal();
    for (int i=0; i < cnt; i++) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
        
      int type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderOpenTime() > tm)
        {
          tm = OrderOpenTime();
          ticket = OrderTicket();
        }
      }
    }
  
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }
 
  if (pool == MODE_HISTORY)
  {
    cnt = OrdersHistoryTotal();
    for (i=0; i < cnt; i++) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
        
      type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderCloseTime() > tm)
        {
          tm = OrderCloseTime();
          ticket = OrderTicket();
        }
      }
    }
  
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }
    
  return (false);
}
 

Hello I have this problem:

 

 

void delete(int type){

   if(OrdersTotal()>0){

      for(i=OrdersTotal();i>=0;i--){

         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

         if(type!=6 && type!=7 && type!=8)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==type)OrderDelete(OrderTicket());

         if(type==6)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)

            OrderDelete(OrderTicket());

         if(type==7)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)OrderDelete(OrderTicket());

         if(type==8)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)OrderDelete(OrderTicket());

      }

 

 

and the description tells my that "delete" name expected..... I think that need a name specification....

 

Thanks you very much for your help... 

 
  1. delete is a keyword since build 600 17.2.2014. You'll have to rename it.
  2. for(i=OrdersTotal();i>=0;i--){
       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    If there are 3 orders, their positions are 0, 1, 2. The first time in your loop the OrderSelect will fail but you don't check. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. Next time don't post to unrelated threads. Create a new one.
Reason: