MQL4 - automated forex trading   /  

Forum

Function definiton unexpected, Help

Back to topics list  | 1 2 To post a new topic, please log in or register

avatar
213
Huckleberry 2011.08.12 23:53 

Hello to all

I have redone the code so as that it will only BUY. No sell actions except to liquidate the BUY positions. Sounded easy enough. Everything was going well until I got down to the iSignalClose().

The code is attached and if there is anyone who wishes to add or delete for improvement, your welcome to advise constructively.

//+------------------------------------------------------------------+
//|                                                Testing (BUY).mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Airat Safin/Ben Banta"
#property link      "http://www.metaquotes.net"

//+------------ Variables --------------
                    double MaximumRisk          = 0.1; 
                    double Lots                 = 0.1; 
                    
                                    
                    int        iBaseLag          = 5;
                    int        iBaseBar          = 1; 
                    double     dFactorTP         = 0.7; 
                    double     dFactorSL         = 1.5;

                    int        iTradeBarOnce     = 1;
                    int        iSlippage         = 1; 
                    int        iMagic            = 1;
               
                    int        iTradeBarTime     = EMPTY;

///+----------------- iSignalOpen()
int iSignalOpen ()
{
  static double dLowest;
  dLowest = Low[iLowest(0,0,MODE_LOW,iBaseLag,iBaseBar)]; 
        
double dBid;
dBid = MarketInfo(Symbol(),MODE_BID);
if (dBid < dLowest)          
return(OP_BUY);
else
return(EMPTY); 
}
//+--------------iTryOpen()----------------
int iTryOpen() 
{
  int iCommand = iSignalOpen();
   if (iCommand == EMPTY) 
return;
//}

if (iCommand == OP_BUY) 
  { 
    string sType = "Buy";  
    int  iColor = Blue;  
    int iMode = MODE_BID;
    double dPrice = MarketInfo(Symbol(),iMode);
    
OrderSend(Symbol(),iCommand,LotsOptimized(),NormalizeDouble(dPrice,Digits),iSlippage,.0050,.001,"",iMagic,0,iColor);                                            
                                           
int iTrap = GetLastError();                                            
if (iTrap == 0)                                            
  {
    Alert(sType,"Order was a Big Success"); 
  }                                            
else    
  { 
    Alert( sType,"open exception ",iTrap); 
  } 
  return();                               
} 
//+-----------------iSignalClose()-----------------
int iSignalClose()
if (OrderType() == OP_BUY)
{
  static double dATR,dProfit,dLoss;
  dATR = iATR(0,0,20,iBaseBar);
   dProfit = ((dATR*dFactorTP)*Point)+OrderOpenPrice(); 
    dLoss = ((dATR*dFactorSL)*Point)-OrderOpenPrice();
else                                    
return(EMPTY); 

if(dProfit > dATR*dFactorTP)  
return(TRUE);
if(dLoss > dATR*dFactorSL)
return(TRUE);
else
return(EMPTY);
} 
//+--------------------------iGetTicket()--------------
int iGetTicket()
{
for(int i = OrdersTotal() -1 ; i >= 0 ; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS) == TRUE)
      if (OrderMagicNumber()== iMagic)
     return(OrderTicket());
   }   
  return(EMPTY);
}
//+------------------------iTryClose()---------------
int iTryClose()
{
  int iCommand = iSignalClose ();
   if (iCommand == EMPTY)
return;

if (iCommand == OP_BUY) &&
if (OrderType() == OP_BUY) &&
if (OrderProfit() > 0)  
string SELL = "TakeProfit";
else   
SELL = "StopLoss";

double dPrice = OrderClosePrice();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);

int iTrap = GetLastError();
if (iTrap == 0) 
{ 
   Alert(SELL,"closed with Hard StopLoss"); 
  } 
else    
  {
   Alert(SELL,"close exception",iTrap); 
  }
} 
//+-------------------------Calculate Position-------------
double LotsOptimized()
  {
   double lot=Lots;
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/500.0,1);
   if(lot<0.1) lot=0.1;
   return(lot);
  } 

int init()
{
Alert("","Start",UninitializeReason());
}

int deinit()
{
Alert("","Stop" ,UninitializeReason());
}

int start()
{
/*
static int iTrigger = 0;
if ( iTrigger == 0 ) 
{
  if ((iTime(0,0,0) == 0)
  ||
  (iBars(0,0) < iBaseLag + iBaseBar))
          return; 
       else 
         iTrigger = 1; 
}*/

int iTicket = iGetTicket();

if (iTicket < 0)
iTryOpen();
else   
iTryClose();
}
int iTrap = GetLastError);
if (iTrap > 0)
Alert("Exception",iTrap);
}  

Orders Management - It's Simple

Orders Management - It's Simple

The article deals with various ways of how to control open positions and pending orders. It is devoted to simplifying of writing Expert Advisors.


avatar
213
Huckleberry 2011.08.12 23:54 

Thank you all in advance for any info and reading. My eyes are ready to fall out of my head. Sure could use some help.

Regards Huckleberry


avatar
4224
RaptorUK 2011.08.13 00:59 

You are missing a } in iTryOpen()

You need a { immediately after iSignalClose()

iTryOpen() is type int so it has to return an int, return(int ); not return();

if (iCommand == OP_BUY) &&
if (OrderType() == OP_BUY) &&
if (OrderProfit() > 0)  
string SELL = "TakeProfit";

is wrong. . .  did you mean . . 

if ( (iCommand == OP_BUY) &&
 (OrderType() == OP_BUY) &&
 (OrderProfit() > 0) )  
string SELL = "TakeProfit";

You have a pair of { } in start() that you don't need . .


avatar
213
Huckleberry 2011.08.13 02:14 

Hello Raptor UK

Thank you for your prompt attention and knowledge. I will get on this ASAP. And provide feedback.

Regards Huckleberry


avatar
213
Huckleberry 2011.08.13 02:18 

And Yes. I did mean


avatar
213
Huckleberry 2011.08.13 12:29 

Hey RaptorUK,

Sorry for the incomplete message above. Yes, I did mean to following, with a small edit.

if ((iCommand == OP_SELL) && (OrderType() == OP_SELL))

This message is onlt to just keep you informed. I have done many improvements. There are no errors or warnings. If you can recall, this code is meant to BUY only and liquidate all lots at a given price. The code at this time will BUY, but will not liquidate. I have the internet again and will continue working. Write again later. But here is what I have done so far.

//+------------------------------------------------------------------+
//|                                                Testing (BUY).mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Airat Safin/Ben Banta"
#property link      "http://www.metaquotes.net"

//+------------ Variables --------------
                    double MaximumRisk          = 0.1; 
                    double Lots                 = 0.1; 
                    
                                    
                    int        iBaseLag          = 5;
                    int        iBaseBar          = 1; 
                    double     dFactorTP         = 0.7; 
                    double     dFactorSL         = 1.5;
                    int        iGProfit          = 44;
                    int        iGLoss            = 20;

                    int        iTradeBarOnce     = 1;
                    int        iSlippage         = 1; 
                    int        iMagic            = 1;
               
                    int        iTradeBarTime     = EMPTY;

///+----------------- iSignalOpen() --------------------------
int iSignalOpen ()
{
  static double dLowest;
  dLowest = Low[iLowest(0,0,MODE_LOW,iBaseLag,iBaseBar)]; 
        
double dBid;
dBid = MarketInfo(Symbol(),MODE_ASK);
if (dBid < dLowest)          
return(OP_BUY);
else
return(EMPTY); 
}

//+--------------------iTryOpen()-----------------------------
int iTryOpen() 
{
  int iCommand = iSignalOpen();
   if (iCommand == EMPTY) 
return;

if (iCommand == OP_BUY) 
  { 
    string sType = "Buy";  
    int  iColor = Blue;  
    int iMode = MODE_ASK;
    double dPrice = MarketInfo(Symbol(),iMode);
    
OrderSend(Symbol(),iCommand,LotsOptimized(),NormalizeDouble(dPrice,Digits),iSlippage,0,0,"",iMagic,0,iColor);// ?????????                                           
                                           
int iTrap = GetLastError();                                            
if (iTrap == 0)                                            
  {
    Alert(sType,"Order was a Big Success"); 
  }                                            
else    
  { 
    Alert( sType,"open exception ",iTrap); 
  } 
  return(0);                               
} 
}

//+--------------------------iGetTicket()--------------
int iGetTicket()
{
for(int i = OrdersTotal() -1 ; i >= 0 ; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS) == TRUE)
      if (OrderMagicNumber()== iMagic)
     return(OrderTicket());
   }   
  return(EMPTY);
}

//+-----------------iSignalClose()-----------------
int iSignalClose()
{

double dProfit;

if (OrderType() == OP_SELL)
{
  dProfit = (MarketInfo(Symbol(),MODE_BID)); //To determine the current price
if (dProfit >= (iGProfit*Point)+OrderOpenPrice())// If current price is >= to iGProfit plus OrderOpenPrice
  return(TRUE);
}

  double dLoss;
  
if (OrderType() == OP_SELL)
{
  dLoss = (MarketInfo(Symbol(), MODE_BID)); //To determine the current price   
if (dLoss <= (iGLoss*Point)-OrderOpenPrice())//If current price is <= minus OrderOpenPrice
  return(TRUE);   
}  
else
{                                    
return(EMPTY); 
} 
}

//+------------------------iTryClose()---------------
int iTryClose()
{
  int iCommand = iSignalClose ();
   if (iCommand == EMPTY)
return;

if ((iCommand == OP_SELL) && (OrderType() == OP_SELL))// && (OrderProfit() > 0))
{  
string SELL = "TakeProfit";
}
else
{   
SELL = "StopLoss";
}
double dPrice = OrderClosePrice();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);

int iTrap = GetLastError();
if (iTrap == 0) 
{ 
   Alert(SELL,"Closed Properly"); 
} 
else    
  {
   Alert(SELL,"Close Exception",iTrap); 
  }
}

//+-------------------------Calculate Position-------------
double LotsOptimized()
  {
   double lot=Lots;
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/500.0,1);
   if(lot<0.1) lot=0.1;
   return(lot);
  } 
  
//+----------------- init()------------
int init()
{
Alert("","Start",UninitializeReason());
}

//+------------------deinit()-----------
int deinit()
{
Alert("","Stop" ,UninitializeReason());
}

//+------------------start()--------------
int start()
{

int iTicket = iGetTicket();

if (iTicket < 0)
iTryOpen();
else   
iTryClose();
}
/*
//+-------------------GetLastError()--------------------
int iTrap = GetLastError();
if (iTrap == 0) 
{ 
   Alert(iTicket,"All is Well"); 
  } 
else    
  {
   Alert(iTicket,"Problems with Ticket",iTrap); 
}
*/ 
Best Regards Huckleberry

avatar
4224
RaptorUK 2011.08.13 13:16 

I think you have a lot of work to do on your code, but keep learning and you will get there . . . for example,

int iSignalClose()     //  <------- type int
.
.
.
.
.
 
 return(TRUE);         // <----- trying to return a bool ?
}  
else
{                                    
return(EMPTY); 
} 
}

//+------------------------iTryClose()---------------
int iTryClose()
{
  int iCommand = iSignalClose ();
   if (iCommand == EMPTY)                  //  from above, this is an int returned as a bool
return;

if ((iCommand == OP_SELL) && (OrderType() == OP_SELL))// && (OrderProfit() > 0))    //  so it can't be OP_SELL,  it can only be what was returned by iSignalClose()


avatar
213
Huckleberry 2011.08.14 01:45 

Thank you RaptorUK. That helps to clear up bit more. I am plugging in and deleteing out a variety of options. The iGLoss and iGProfit was also changed from an"int" to a "double". The broker that I work with has a 5 digit quote, therefore,

  dProfit = (MarketInfo(Symbol(),MODE_BID)); //To determine the current price
if (dProfit >= (iGProfit*Point)+OrderOpenPrice())// If current price is >= to iGProfit plus OrderOpenPrice returns OP{SELL)
  return(OP_SELL);
would be correct.....Or would this be better
int iCommand = iSignalClose ();//iCommand equals above function
   if (iCommand == EMPTY)//if iCommand IS "EMPTY", therefore, it could not be iSignalClose()
return;                 //reurn; is the only response
  
if (iCommand == OP_SELL && (OrderType() == OP_SELL)) //OP_SELL is in the iSignalClose(); return(OP_SELL); And 
                                                     //the OrderType() == OP_SELL; Therefore this "if" line 
                                                     //would be correct..?? I've tried both (iCommand == 
                                                     //OP_SELL and iSignalClose() with no success yet. 

There is alao the GetLastError() I am working on. Certainly do not want to screw up a line if it is correct only to find out I should be working on a different function or line which was the problem area. :-) The nose is back to the grind stone.

Cheers


avatar
4224
RaptorUK 2011.08.14 13:54 
if iGProfit and iGLoss are specified in PIPS then you will need to adjust for the fact that you are using a 5 digit broker, what you currently have are 4.4 and 2.0 pips, Plenty has been written about that already, some very good code examples have been posted too . . http://crum.be/45digit

avatar
213
Huckleberry 2011.08.14 22:10 

Yes. Currently it is set at 4.4 and 2.0 pips. I built the code originally as a "int' with 4400 and 2000 pips. Much to far for reality. I deleted out the "int" 44 and 20 pips and plugged in "double" 0.00044 and 0.00020. I mentioned this for the public and for you. I did check into the Hyperlink. And added it to my favorites. Thank you.

GetLastError() has a problem.

if (iCommand == OP_SELL && (OrderType() == OP_SELL)) //OP_SELL is in the iSignalClose(); return(OP_SELL); And 
                                                     //the OrderType() == OP_SELL; Therefore the "if" line 
                                                     //would be correct..?? I've tried both (iCommand == 
                                                     //OP_SELL and iSignalClose() with no success yet.                                                    
{  
string SELL = "TakeProfit";
}
else
{   
SELL = "StopLoss";
}
double dPrice = OrderClosePrice();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);

int iTrap = GetLastError();
if (iTrap == 0) 
{ 
   Alert(SELL,"Closed Properly ",iTrap); 
} 
else    
  {
   Alert(SELL,"Close Exception",iTrap); 
  }
}

Originally, the above GetLastError() code was to Alert () if the trade was 'TakeProfit' else 'StopLoss'. But asking for Alert() 'TakeProfit' or 'StopLoss' will NOT giveing errors. So, to get errors, I have done the following instead. Maybe I am off course, but practice is a learning tool.

//+------------------------iTryClose()---------------
int iTryClose()
{
  int iCommand = iSignalClose ();//iCommand equals above function
   if (iCommand == EMPTY)//if iCommand IS "EMPTY", therefore, it could not be iSignalClose()
return;                 //return; is the only response


if (iCommand == OP_SELL && (OrderType() == OP_SELL)) //OP_SELL is in the iSignalClose(); return(OP_SELL); Since 
                                                     //iCommand() and OrderType() == OP_SELL; Therefore the "if" line 
                                                     //would be correct.

double dPrice = OrderClosePrice();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);
return;


int iTrap = GetLastError();
if(iTrap > 0)
//if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);
  {  
     Print("error: code #",iTrap);
     return(0);
}
} 

By doing the above, it still does not liquidate the BUY position, but I believe it gives a better representation to collecting errors that may occur for OrderClose(). Asking for your advise or critic.

Thank you for your time and info.

Cheers


avatar
4224
RaptorUK 2011.08.14 22:35 

A few points:

You don't need to do this . . .

double dPrice = OrderClosePrice();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);

. . you can do this instead . .

OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),iSlippage,Blue);

assuming you have already selected the correct order using OrderSelect( int index, int select, int pool=MODE_TRADES)

In the 2nd version, if you do this . . . .

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue);
return;                                  //    <------------------------------------------  the function is exited here


int iTrap = GetLastError();               //  <-----   this won't happen

. . maybe you meant this . . .

if ( OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(dPrice,Digits),iSlippage,Blue) )
return; 

In the first version: if you aren't getting either Alert then you aren't getting to that part of the code and you need to check your conditional statements that should take you there.

Back to topics list   | 1 2  

To add comments, please log in or register