Function definiton unexpected, Help - page 2

 

Looking into the journal, when I do a backtest, there are no errors concerning OrderClose().

I make a note of this for troubleshooting

 

Hello RaptorUK, I did not know you were making a reply so soon

 

I'll look into your message for what to do and not.

 
Huckleberry:

Hello RaptorUK, I did not know you were making a reply so soon

I'm testing my code through the Strategy tester so I have time between runs . . . ;-)
 

Thank you for your comments and time.

 

Hi RaptorUK, and all

Attached is the newest version.

//|                                                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; 
                    
                                    
            extern  int        iBaseLag          = 5;
                    int        iBaseBar          = 1; 
                    double     dFactorTP         = 0.7; 
                    double     dFactorSL         = 1.5;
             extern int        iGProfit          = 40;
             extern int        iGLoss            = 70;

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

///+----------------- iSignalOpen() --------------------------
int iSignalOpen ()
{
/*if ((iTradeBarTime == iTime(0,0,0)) 
      && 
    (iTradeBarOnce == 1))
   return(EMPTY); 
                   
static int iTime_0 = EMPTY;

if (iTime_0 < iTime(0,0,0))   
{

  iTime_0 = iTime(0,0,0);
  iTradeBarTime = EMPTY; */
  
  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);
    int Digits = Get_Brokers_Digits();
OrderSend(Symbol(),iCommand,LotsOptimized(),Ask,3*Digits,Ask-iGLoss*Point,Ask+iGProfit*Point,"",iMagic,0,iColor);// ?????????                                           
                                           
int iTrap = GetLastError();                                            
if (iTrap == 0)                                            
  {
    Alert(sType,"Order was a Big Success"); 
  }                                            
else    
  { 
    Print( sType,"error: code#",iTrap); 
  } 
  return(0);                               
} 
}

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

//+------------------------iTryClose()--------------
int iTryClose()
{
/*
static int iTime_0 = EMPTY;
if (iTime_0 < iTime(0,0,0))
{
  iTime_0 = iTime(0,0,0);*/
  
if(OrderType() == OP_SELL)

OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
return;
}
//}
//+-------------------------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());
return;
}

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

//+------------------start()--------------
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();
} 
//}

The latest problem was that the code would BUY, but would not liquidate. By inserting the iGProfit and iGLoss with correct settings the code will now Liquidate. So that problem has been corrected.

But the code will BUY many times inbetween liquidation and liquidation continues until BUYING signals crop up again. Therefore, in the time span of 5 days, there were over 10,000 trades, until the account was dried out.

What I was attempting for the code to do, was for it to trade only once per bar. e.g. That if there was a BUY signal and a SELL signal on the same bar, no other trade signals would be generated from that bar. No trades until the next bar, etc.

From an old code (one that is public) I inserted portions that I thought would help. They did not. I have left them inserted, but with /* */. Otherwise, code is intact. If anyone could have a look and give advise or critic, I would be grateful.

Cheers

 
Huckleberry:


What I was attempting for the code to do, was for it to trade only once per bar. e.g. That if there was a BUY signal and a SELL signal on the same bar, no other trade signals would be generated from that bar. No trades until the next bar, etc.


OK, here is what I would do (without looking at your code so bear in mind I might be a little off . . .):

Set a variable to true on the first tick of the new bar, maybe NewBar_AllowTrade = true; test that variable when your code wants to trade, if ( . . . . && NewBar_AllowTrade == true && . . . . . ) then when the trade has been placed successfully set it to false . . . NewBar_AllowTrade = false;

if you want to allow 1 BUY and 1 SELL per new bar you can do something similar but you will have to use an int rather than a bool, on the first tick of a new bar set it to 0, NewBar_AllowTrade = 0; . . and modify the int once when the BUY is placed, NewBar_AllowTrade |= 1; and again when the SELL is placed NewBar_AllowTrade |= 2; to test if a BUY can be placed: NewBar_AllowTrade & 1 == 1 to test if a SELL can be placed: NewBar_AllowTrade & 2 == 2

Reason: