have a bug [bug?] in the code, which bias'es the whole EA.

 

hi there!

 

so, as the subject mentions - theres a bug in this code - annoying me,  quite a bit - might anyone experienced point out where the issue is?

the issue is - when the "flush", or the restart of the EA is activated - for some reason it opens the two initial trades, but with one extra trade - and thus - bias'ing the whole idea.

might work differently for other people, but as far as ive seen - it opens an extra "buy" order right off the bat.

the issue only occurs if "hedger" is on.

 

any one kind enough to lend a hand?

 

p.s.

 

feel free to point out any mistakes, errors and so on..

for the greater good.

cheerios. 

//+------------------------------------------------------------------+
//|                                        Naurismucenieks_EA_V2.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Iwori Fx"
#property link      "http://www.mql5.com"
#property version   "1.00"
#property description "\nPowered By: Iwori Fx"


//--- input parameters
extern int      MagicNumber=12345679;

extern string   sOM="---ORDER MANAGEMENT---";
extern double   LotSize=0.1;   //Standard lotsize
extern bool     auto_lots   =  true; //Use dynamic lotsize?
extern double   min_lotsize =  0.01; //Minimum lotsize
extern double   max_lotsize =  10.00; //Maximum lotsize
extern double   base_deposit   =  100.00; //Base deposit
extern bool     use_holdback   =  true; //Hold back cash?
extern double   base_extra     =  1000.00; //Cash holdback.
extern double   TP=20.0;
extern double   ReOpenDist=20.0;
extern int      Slippage=3;
extern double   close_d = 10.0;//CLOSE DISTANCE
extern int      close_x = 30;//CLOSE %
input  bool     partialclose   = false;//partial close
input int       DSL = 20;      //distance the SL
input bool      lotReset = false;//LOT reset
input bool      hedge = true;//HEDGEt
extern bool FLUSH=true;
extern double FLUSH_Percent=1;
extern int SleepXMinutes=15;
extern bool REBOO=false;
extern double REBOO_Percent=5;
string EaName="Naurismucenieks_EA_V1";
int   nAll;
int   nBuy;
int   nSell;
double accountbalance;
double   LastBuy;
double   LastSell;

double   mult;
bool     first;
int      lastTpTicket = 0;
datetime time;
double point,close;
int digits,Q;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---

           if(Digits==5 || Digits==3)Q=10;  
   else Q=1;
   
   
   if(Digits<4)
     {
      point=0.01;
      digits=2;
     }
   else
     {
      point=0.0001;
      digits=4;
      }
      
   first=true;
   nAll=nBuy=nSell=0;
   LastBuy=LastSell=0.0;
   mult=(_Digits==1 || _Digits==3 || _Digits==5)? 10.0:1.0;
   
   if(!IsTradeAllowed())
   {
      Alert("ERROR - TRADE NOW ALLOWED");
      return(INIT_FAILED);
   }
   accountbalance=AccountBalance();
   time=TimeCurrent();
//---
   return(INIT_SUCCEEDED);
}


//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   Comment("");
//---
}


//+------------------------------------------------------------------+
//| CountOrder function                                              |
//+------------------------------------------------------------------+
void CountOrder()
{
//---
   nAll = nSell = nBuy = 0;
   LastBuy=100000.0;
   LastSell=0.0;
   
   for (int i = 0; i < OrdersTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      if(OrderType() < 2 && OrderSymbol() == _Symbol)
      {
         if (OrderMagicNumber() == MagicNumber)
         {
            nAll++;
            if(OrderType() == OP_BUY)
            {
               nBuy++;
               if(OrderOpenPrice()<=LastBuy) LastBuy=OrderOpenPrice();
            }
            if(OrderType() == OP_SELL) 
            {
               nSell++;
               if(OrderOpenPrice()>=LastSell) LastSell=OrderOpenPrice();
            }
         }
      }
   }
//---
}


//+------------------------------------------------------------------+
//| Expert GetLot function                                           |
//+------------------------------------------------------------------+
double GetLotb()
{
//---
   double lot1b,lot1s;
   int tOb=0,tCb=0;
   datetime timeOpenb,timeCloseb;
   int tOs=0,tCs=0;
   datetime timeOpens,timeCloses;
   lot1s=lot1b=0.0;
   timeOpenb=timeCloseb=0;
   timeOpens=timeCloses=0;
   if(first)
   {
      first=false;
      return(LotSize);
   }
   
   for (int i = 0; i < OrdersHistoryTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderType()!=OP_BUY) continue;
      if(OrderSymbol() == _Symbol)
      {
         if (OrderMagicNumber() == MagicNumber&&OrderType()==OP_BUY)
         {
            if(OrderCloseTime()!=0 && OrderCloseTime()>timeCloseb&&OrderProfit()>0)
            {
               lot1b=OrderLots();
               timeCloseb=OrderCloseTime();
               tCb=OrderTicket();
            }
            //if(OrderOpenTime()>timeOpenb)
            //{
            //   timeOpenb=OrderOpenTime();
            //   tOb=OrderTicket();
            //}
         }
         
      }
   }
return(lot1b+LotSize);
//---
}

double hedge_GetLotb()
{
//---
   double lot1b=0,lot1s=0;
   if(first)
   {
      first=false;
      return(LotSize);
   }
   
   for (int i = 0; i < OrdersTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderType()==OP_BUY) lot1b +=OrderLots();
      if(OrderType()==OP_SELL) lot1s +=OrderLots();
      }
      Print("type=buy lots b:",lot1b,",s:",lot1s);
      if ((lot1s - lot1b)<LotSize) return(NormalizeDouble(LotSize,2));
return(NormalizeDouble((lot1s - lot1b),2));
//---
}

double hedge_GetLots()
{
//---
   double lot1b=0,lot1s=0;
   if(first)
   {
      first=false;
      return(LotSize);
   }
   
   for (int i = 0; i < OrdersTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderType()==OP_BUY) lot1b +=OrderLots();
      if(OrderType()==OP_SELL) lot1s +=OrderLots();
      }
      Print("type=sell lots b:",lot1b,",s:",lot1s);
      if ((lot1b-lot1s)<LotSize) return(NormalizeDouble(LotSize,2));
return((NormalizeDouble((lot1b-lot1s),2)));
//---
}

double GetLots()
{
//---
   double lot1b,lot1s;
   int tOb=0,tCb=0;
   datetime timeOpenb,timeCloseb;
   int tOs=0,tCs=0;
   datetime timeOpens,timeCloses;
   lot1s=lot1b=0.0;
   timeOpenb=timeCloseb=0;
   timeOpens=timeCloses=0;
   if(first)
   {
      first=false;
      return(LotSize);
   }
   
   for (int i = 0; i < OrdersHistoryTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderType()!=OP_SELL) continue;
      if(OrderSymbol() == _Symbol)
      {
  
         if (OrderMagicNumber() == MagicNumber&&OrderType()==OP_SELL)
         {
            if(OrderCloseTime()!=0 && OrderCloseTime()>timeCloses&&OrderProfit()>0)
            {
               lot1s=OrderLots();
               timeCloses=OrderCloseTime();
               tCs=OrderTicket();
            }
            //if(OrderOpenTime()>timeOpens)
            //{
            //   timeOpens=OrderOpenTime();
            //   tOs=OrderTicket();
            //}
         }
         
      }
   }
   return(lot1s+LotSize);
//---
}


double GetLot(double dir)
{
//---
   int type = (dir==1.0)? OP_BUY:OP_SELL;
   double lot1;
   int tO=0,tC=0;
   datetime timeOpen,timeClose;
   lot1=0.0;
   timeOpen=timeClose=0;
   
   if(first)
   {
      first=false;
      return(LotSize);
   }
   
   for (int i = 0; i < OrdersHistoryTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderType()!=type) continue;
      if(OrderSymbol() == _Symbol)
      {
         if (OrderMagicNumber() == MagicNumber)
         {
            if(OrderCloseTime()!=0 && OrderCloseTime()>timeClose&&OrderProfit()>0)
            {
               lot1=OrderLots();
               timeClose=OrderCloseTime();
               tC=OrderTicket();
            }
            if(OrderOpenTime()>timeOpen)
            {
               timeOpen=OrderOpenTime();
               tO=OrderTicket();
            }
         }
      }
   }
   
   if(tC==tO)return(lot1+LotSize);
   
   return(LotSize);
//---
}

int Check_tp(){
   int tO=0,tC=0;
   datetime timeOpen=0,timeClose=0;
   int type=0;
   for (int i = 0; i < OrdersHistoryTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
   //   if(OrderType()!=type) continue;
      if(OrderSymbol() == _Symbol)
      {
         if (OrderMagicNumber() == MagicNumber)
         {
            if(OrderCloseTime()!=0 && OrderCloseTime()>timeClose&&OrderProfit()>0)
            {
            //   lot1=OrderLots();
               timeClose=OrderCloseTime();
               tC=OrderTicket();
            }
            if(OrderOpenTime()>timeOpen)
            {
               timeOpen=OrderOpenTime();
               tO=OrderTicket();
               type = OrderType();
            }
         }
      }
   }
   if(tC==tO&&lastTpTicket!=tO) {
   lastTpTicket=tO;
   return(type);
   }
return(-1);
}
//+------------------------------------------------------------------+
//| Expert OpenOrder function                                        |
//+------------------------------------------------------------------+
void OpenOrder(int type, double tp,int first)
  {
//---
   double dir = 0.0;
   double sl = 0.0;
   double price = 0.0;
   color  clr = clrNONE;
   double lotb = LotSize;
   double lots = LotSize;
   double lot = LotSize;
   int result = -1;
   
   while(IsTradeContextBusy()) Sleep(50);
   RefreshRates();
   
   if(type==OP_BUY)
   {
      dir = 1.0;
      price = Ask;
      clr = clrBlue;
   }
   if(type==OP_SELL)
   {
      dir = -1.0;
      price = Bid;
      clr = clrRed;
   }
   
   
  if (!lotReset){
  if (hedge){
  if (type==OP_SELL&&first==0)if (tp==-1.0 ) lots=hedge_GetLots();
  if (type==OP_BUY&&first==0) if (tp==-1.0 ) lotb=hedge_GetLotb();
  

 // Print(lotb,lots,tp);
  }else{
  if (tp==-1.0 ) lotb=GetLotb();
  if (tp==-1.0 ) lots=GetLots();
  }
  }else{
  if (tp==-1.0 ) lot=GetLot(dir);
  
  }
   
   if(TP>0.0 && tp==-1.0) tp = price + dir*TP*_Point*mult;
   
   //if (partialclose)OpenOrders(_Symbol,type,lot,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,clr,dir);
   //else
   if (!lotReset){
   if (type==OP_BUY){ 
   if (partialclose)OpenOrders(_Symbol,type,lotb,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,clr,dir);
      else OrderSend(_Symbol,type,lotb,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,0,clr);}
   if (type==OP_SELL) {
   if (partialclose)OpenOrders(_Symbol,type,lots,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,clr,dir);
      else OrderSend(_Symbol,type,lots,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,0,clr);}
  // if (type==OP_SELL) OrderSend(_Symbol,type,lots,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,0,clr);}
   }else{
      if (partialclose)OpenOrders(_Symbol,type,lot,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,clr,dir);
      else OrderSend(_Symbol,type,lot,price,Slippage,sl,tp,(string)MagicNumber,MagicNumber,0,clr);
    }
//---
}
void OpenOrders(string symbol, int cmd, double volume, double price, int  slippage,double stoploss,double takeprofit,string comment=NULL,int magic=0, color    arrow_color=clrNONE ,double dir=0.0){
double    tmp_d   = close_d*100/close_x;
double tmp_vol = NormalizeDouble(volume*close_x/100,2);//MathRound(NormalizeDouble(volume*close_x/100,8)); 
int    tmp_i =0,m=0;
if (DSL==0) m=1;
//printf("%f",tmp_vol);
for(int i=1;i*close_d<tmp_d;i++){
stoploss = price - dir*(i-1+m)*close_d*_Point*mult- dir*DSL*_Point*mult;
OrderSend(symbol,cmd,tmp_vol,price,Slippage,stoploss,takeprofit,(string)MagicNumber,MagicNumber,0,arrow_color);
tmp_i++;
}

tmp_vol = volume - tmp_vol*tmp_i;
if (tmp_i==0) tmp_i++;
stoploss = price - dir*(tmp_i)*close_d*_Point*mult- dir*DSL*_Point*mult;
OrderSend(symbol,cmd,tmp_vol,price,Slippage,stoploss,takeprofit,(string)MagicNumber,MagicNumber,0,arrow_color);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  

    
  if(FLUSH){
  if(AccountEquity()>=(accountbalance/100*FLUSH_Percent)+accountbalance){
  Sleep((SleepXMinutes*60)*1000);
     first=true;
   nAll=nBuy=nSell=0;
   LastBuy=LastSell=0.0;
  close=CloseOrders();


  accountbalance=AccountBalance();
  MagicNumber=MagicNumber+1;
  }}
  
  
   
  

    
  if(REBOO){
  if(AccountEquity()<=accountbalance-(accountbalance/100*REBOO_Percent)){
 // Sleep((SleepXMinutes*60)*1000);
     first=true;
   nAll=nBuy=nSell=0;
   LastBuy=LastSell=0.0;
  close=CloseOrders();

Sleep(3000);
  accountbalance=AccountBalance();
  MagicNumber=MagicNumber+1;
  }}
  
 
  
  
  
  


//------------
   string text7="Last Reset Balance= "+accountbalance;
   int WhichCorner7=1;//0,1,2 or 3
   string name7="Last Reset Balance";
   string nObjName="Last Reset Balance";
   ObjectCreate(nObjName,OBJ_LABEL,0,0,0);
   ObjectSetText(nObjName,text7,13,"Consolas",Yellow);
   ObjectSet(nObjName,OBJPROP_CORNER,WhichCorner7);
   ObjectSet(nObjName, OBJPROP_XDISTANCE, 10);//left to right
   ObjectSet(nObjName, OBJPROP_YDISTANCE, 30);//top to bottom
   
   //------------
   string text9="Required Equity to Reset= "+(accountbalance+(accountbalance/100*FLUSH_Percent));
   int WhichCorner9=1;//0,1,2 or 3
   string name9="Required Equity to Reset";
   string kObjName="Required Equity to Reset";
   ObjectCreate(kObjName,OBJ_LABEL,0,0,0);
   ObjectSetText(kObjName,text9,13,"Consolas",Yellow);
   ObjectSet(kObjName,OBJPROP_CORNER,WhichCorner9);
   ObjectSet(kObjName, OBJPROP_XDISTANCE, 10);//left to right
   ObjectSet(kObjName, OBJPROP_YDISTANCE, 70);//top to bottom
   //------------
   string text10="Live Equity= "+AccountEquity();
   int WhichCorner10=1;//0,1,2 or 3
   string name10="Live Equity";
   string ObjName="Live Equity";
   ObjectCreate(ObjName,OBJ_LABEL,0,0,0);
   ObjectSetText(ObjName,text10,13,"Consolas",Yellow);
   ObjectSet(ObjName,OBJPROP_CORNER,WhichCorner10);
   ObjectSet(ObjName, OBJPROP_XDISTANCE, 10);//left to right
   ObjectSet(ObjName, OBJPROP_YDISTANCE, 50);//top to bottom
   
   //------------
      string text100="Equity to REBOO= "+(accountbalance-(accountbalance/100*REBOO_Percent));
   int WhichCorner100=1;//0,1,2 or 3
   string name100="Equity to REBOO";
   string ObjName0="Equity to REBOO";
   ObjectCreate(ObjName0,OBJ_LABEL,0,0,0);
   ObjectSetText(ObjName0,text100,13,"Consolas",Yellow);
   ObjectSet(ObjName0,OBJPROP_CORNER,WhichCorner100);
   ObjectSet(ObjName0, OBJPROP_XDISTANCE, 10);//left to right
   ObjectSet(ObjName0, OBJPROP_YDISTANCE, 90);//top to bottom
   
   //------------


    //Generate dynamic lotsizes?
    if(auto_lots==true){dynamic_lots();}
//---
   CountOrder();
 //  if (Check_tp()==1) Print("tp re");
   if(nAll==0)
   {
      OpenOrder(OP_BUY,-1.0,1);
      OpenOrder(OP_SELL,-1.0,1);
   }
   
   if(nAll>0)
   {
      first=false;
      if (hedge){
      int type = Check_tp();
      if (type>=0) OpenOrder(type,-1.0,0);
      }else{
      if(nBuy==0) OpenOrder(OP_BUY,-1.0,0);
      if(nSell==0) OpenOrder(OP_SELL,-1.0,0);
      }
      if(nBuy>0 && (LastBuy-Ask)>=ReOpenDist*_Point*mult) OpenOrder(OP_BUY,LastBuy,0);
      if(nSell>0 &&( Bid-LastSell)>=ReOpenDist*_Point*mult) OpenOrder(OP_SELL,LastSell,0);
   }

   // call the display function.
   display();

   
//---
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//+ dynamic lotsize calculation function. 
//+ coded by smarterai
//+------------------------------------------------------------------+
void  dynamic_lots(){
   //Get the account balance.
   double   temp_balance   =  AccountBalance();
   //Are we holding back some cash?
   if(use_holdback==true){
      //If we have enough to hold back. Hold it back.
      if( (AccountBalance()-base_extra)>1 ){temp_balance=NormalizeDouble(AccountBalance()-base_extra,2);}
      //If we don't have enough to hold back. Notify the user and disable holdbacks
      if( (AccountBalance()-base_extra)<=0){use_holdback=false; Alert("Not enough to cash hold back."); }      
   }      
   LotSize=NormalizeDouble((temp_balance/base_deposit)*min_lotsize,2); // calculate the lotsize.
   if(LotSize<min_lotsize){LotSize=min_lotsize;}   //Make sure that we don't go below the minimum lotsize
   if(LotSize>max_lotsize){LotSize=max_lotsize;}   //Make sure we don't exceed the maximum lotsize
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//+ function to display the EA trades data.
//+------------------------------------------------------------------+
void display(){
   
   string temp_display  =  "\n[ Dynamic lots? "+(string)auto_lots+" ]";
   temp_display=temp_display+" [ Minimum lotsize: "+DoubleToString(min_lotsize,2)+" ][ Maximum lotsize: "+DoubleToString(max_lotsize,2)+" ]";
   temp_display=temp_display+"\n[ Current lotsize: "+DoubleToString(LotSize,2)+" ]";
   Comment(temp_display);
}
//+------------------------------------------------------------------+
    int CloseOrders() {
         int cnt=OrdersTotal();
         for(int i=cnt-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
            if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
                 {
                 
                     OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3*Q,clrWhite);  
                      
           }
       }
   

  return(0);
} 
//*********
 

can u plz explain idea more of strategy?

add more comments, what is tp and why if tp==-1.0?

how to replicate error? 

thx and gl

 
pabloroacho:

can u plz explain idea more of strategy?

add more comments, what is tp and why if tp==-1.0?

how to replicate error? 

thx and gl

sure thing.

sadly, since i didnt code it - i have no idea what is tp==-1.0.

error is pretty easy to spot - after the EA has been reset, trades closed, we start up the EA anew, but for some reason an extra buy order is closed, just like it would have had hit a TP.


when the "flush" was implemented, we had a similar issue, which we fixed by issuing new magic numbers (which, as we see doesnt work if "hedger" is on, for some reason).

so far, as ive managed to find out - supposedly i need to improve the code for restarting :


  if(FLUSH){
  if(AccountEquity()>=(accountbalance/100*FLUSH_Percent)+accountbalance){
  Sleep((SleepXMinutes*60)*1000);
     first=true;
   nAll=nBuy=nSell=0;
   LastBuy=LastSell=0.0;
  close=CloseOrders();


  accountbalance=AccountBalance();
  MagicNumber=MagicNumber+1;
  }}
Files:
flush.set  3 kb
fush.jpg  203 kb
 
 if(nAll>0)
   {
      first=false;
      if (hedge){
      int type = Check_tp();
      // if (type>=0) OpenOrder(type,-1.0,0); // why opening extra order of same type here???
      if (type>=0) {};
     
      }else{
      if(nBuy==0) OpenOrder(OP_BUY,-1.0,0);
      if(nSell==0) OpenOrder(OP_SELL,-1.0,0);
      }
      if(nBuy>0 && (LastBuy-Ask)>=ReOpenDist*_Point*mult) OpenOrder(OP_BUY,LastBuy,0);
      if(nSell>0 &&( Bid-LastSell)>=ReOpenDist*_Point*mult) OpenOrder(OP_SELL,LastSell,0);
   }

   // call the display function.

   display();

Files:
bug2.mq4  20 kb
 
   if(nAll>0)
   {
      first=false;
      if (hedge){
      int type = Check_tp();
      if (type>=0) OpenOrder(type,-1.0,0);
      }else{
      if(nBuy==0) OpenOrder(OP_BUY,-1.0,0);
      if(nSell==0) OpenOrder(OP_SELL,-1.0,0);
      }
      if(nBuy>0 && (LastBuy-Ask)>=ReOpenDist*_Point*mult) OpenOrder(OP_BUY,LastBuy,0);
      if(nSell>0 &&( Bid-LastSell)>=ReOpenDist*_Point*mult) OpenOrder(OP_SELL,LastSell,0);
   }
because if tp triggered we have to open "hedge" trade in opposite direction as i remember right
 
goverkms:
because if tp triggered we have to open "hedge" trade in opposite direction as i remember right

hey there,


you're almost correct - we had to open a trade in the same direction, but the lot size was calculated using the difference of positions in both direction.

 
pabloroacho:
 if(nAll>0)
   {
      first=false;
      if (hedge){
      int type = Check_tp();
      // if (type>=0) OpenOrder(type,-1.0,0); // why opening extra order of same type here???
      if (type>=0) {};
     
      }else{
      if(nBuy==0) OpenOrder(OP_BUY,-1.0,0);
      if(nSell==0) OpenOrder(OP_SELL,-1.0,0);
      }
      if(nBuy>0 && (LastBuy-Ask)>=ReOpenDist*_Point*mult) OpenOrder(OP_BUY,LastBuy,0);
      if(nSell>0 &&( Bid-LastSell)>=ReOpenDist*_Point*mult) OpenOrder(OP_SELL,LastSell,0);
   }

   // call the display function.

   display();

its what goverkms said - on TP's it opens an increased lotsize trade in the same direction.

so, if that line is disabled - when TP is met, no action is initiated.



may i ask a question?

why did you re-arrange the code?

out of habit, or does it has a different purpose?

excuse me for such a silly question.

 

re arranged code so ontick is at top, which is your main program, then the place order function because problem was extra order so seemed next most likely culprit.


check_tp seems to return the type of the last closed winning trade. assuming there is no problem with check_tp function, then you need to say new extra order only when there is only one open trade (because one closed as a winner) not more than zero open trades (two is more than zero so places your unwanted extra trade). ???


if ((type>=0) && (nAll==1)) OpenOrder(type,-1.0,0);

 

but even that seems redundant, since you then check nBuy and nSell

 if(nAll>0)
   {
      first=false;
      if (hedge)
      {
       if(nBuy==0) OpenOrder(OP_BUY,-1.0,0);
       if(nSell==0) OpenOrder(OP_SELL,-1.0,0);
      }
      if(nBuy>0 && (LastBuy-Ask)>=ReOpenDist*_Point*mult) OpenOrder(OP_BUY,LastBuy,0);
      if(nSell>0 &&( Bid-LastSell)>=ReOpenDist*_Point*mult) OpenOrder(OP_SELL,LastSell,0);
   }

 

or maybe this is better...


 if(nAll==1)
   {
      first=false;
      if (hedge)
      {
       if(nBuy==0) OpenOrder(OP_BUY,-1.0,0);
       if(nSell==0) OpenOrder(OP_SELL,-1.0,0);
      }
      else
      {
       if(nBuy>0 && (LastBuy-Ask)>=ReOpenDist*_Point*mult) OpenOrder(OP_BUY,LastBuy,0);
       if(nSell>0 &&( Bid-LastSell)>=ReOpenDist*_Point*mult) OpenOrder(OP_SELL,LastSell,0);
      }

   }

 
pabloroacho:

re arranged code so ontick is at top, which is your main program, then the place order function because problem was extra order so seemed next most likely culprit.


check_tp seems to return the type of the last closed winning trade. assuming there is no problem with check_tp function, then you need to say new extra order only when there is only one open trade (because one closed as a winner) not more than zero open trades (two is more than zero so places your unwanted extra trade). ???


if ((type>=0) && (nAll==1)) OpenOrder(type,-1.0,0);

it wasnt as much as an unwanted place, it was more of a trade placed in the wrong place, at the wrong time.

im not entirely sure what does who check where, but i do know that the extra trade was mainly only a buy, since i never saw any sells.

i couldnt get this one :

if ((type>=0) && (nAll==1)) OpenOrder(type,-1.0,0);

to work, but the middle solution seems to be working, however, ill check it thoroughly.  

we kinda fixed this error with the "hedger" input off, what it previously did, was after the "flush", it continued trading the previous (increased) lot sizes.

fixed that with issuing a new magic number. 

Reason: