martingale on next bar

 

Hey guys 

can some one help me on this code as it does do martingale but on the next signal (arrow) and i want it to trade on the next bar.

so if a signal(arrow) comes up and  opens a trade  which it losses it then must do martingale on the next bar till it wins.

please help

extern string note1 = "Martingle Amounts";
input double Amount1=1,Amount2=3,Amount3=6,Amount4=14,Amount5=31,Amount6=70,Amount7=158,Amount8=250,Amount9=200,Amount10=300,Amount11=400,Amount12=500;

extern string note2 = "How Many steps do you want to Martigle?";
input int Martingle_Step = 8;

extern string note3 = "Expiry Time";
input int ExpireSeconds=60;


extern string note5 = "How Long The EA must Run For? (Brokers Time)";
input int HourBegin=0;
input int HourEnd=24;

int MagicNumber=100;

double Lot[];
bool sel,mod,cl;
int tic;

double gd_amount[12];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   gd_amount[0]= Amount1;
   gd_amount[1]= Amount2;
   gd_amount[2]= Amount3;
   gd_amount[3]= Amount4;
   gd_amount[4]= Amount5;
   gd_amount[5]= Amount6;
   gd_amount[6]= Amount7;
   gd_amount[7]= Amount8;
   gd_amount[8]= Amount9;
   gd_amount[9]= Amount10;
   gd_amount[10]= Amount11;
   gd_amount[11]= Amount12;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function  Risk management                                           |
//+------------------------------------------------------------------+
void OnTick()
  {
   int total=0;
   bool newb=NewBar();
   int tip=-1;
   for(int z=OrdersTotal()-1;z>=0;z--)
      {
       sel=OrderSelect(z,SELECT_BY_POS);
       if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
         {
         total++;
         }
      }
      
   Comment("\n\n Trade Time...\n"+" Amount: "+ DoubleToString(GetLot(),0)+ "    Time Second:"+ IntegerToString(TimeSeconds(TimeCurrent())) );
   datetime lastcl=0;   
   for(int z=OrdersHistoryTotal()-1;z>=0;z--)
      {sel=OrderSelect(z,SELECT_BY_POS,MODE_HISTORY);
       if((OrderType()==OP_SELL||OrderType()==OP_BUY)&&OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
            {
             if(OrderCloseTime()>lastcl)
               {
               lastcl=OrderOpenTime();
               }
            }
         
      }   
 if(TimeHour(TimeCurrent())>=HourBegin&&TimeHour(TimeCurrent())<HourEnd&&iBarShift(Symbol(),0,lastcl)!=0&&total==0)
   {
      OpenOrder(tip);
   }  
//---
   
  }
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar = 0;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else return(false);
}

void BuyOrder()
{RefreshRates();
//GetLot();
double ordersize = GetLot();
//for(int z=0;z<ArraySize(Lot);z++)
 //  { 
   //tic=OrderSend (Symbol(),OP_SELL,Lot[z],Close[0],0,0,0,"BO exp:"+string(ExpireSeconds),MagicNumber,0,Red);
   tic=OrderSend (Symbol(),OP_BUY,ordersize,Close[0],0,0,0,"BO exp:"+string(ExpireSeconds),MagicNumber,0,Green);
     if(tic==-1){Print("eror=",GetLastError());return;}
 //  }

}

void SellOrder()
{RefreshRates();
//GetLot();
double ordersize = GetLot();

//for(int z=0;z<ArraySize(Lot);z++)
  // { 
   // tic=OrderSend (Symbol(),OP_BUY,Lot[z],Close[0],0,0,0,"BO exp:"+string(ExpireSeconds),MagicNumber,0,Green);
   tic=OrderSend (Symbol(),OP_SELL,ordersize,Close[0],0,0,0,"BO exp:"+string(ExpireSeconds),MagicNumber,0,Red);
    if(tic==-1){Print("eror=",GetLastError());return;}
 //  }
}



void OpenOrder(int tip)
{
 double arrowup=iCustom(NULL,0,"BBarrow",0,0);
 double arrowdown=iCustom(NULL,0,"BBarrow",1,0);
 

  if(arrowup!=EMPTY_VALUE)

  {
       BuyOrder();
       return;
      
  } 

  if(arrowdown!=EMPTY_VALUE)

 { 
       SellOrder();
       return;
      
 } 
   
     
}
bool ExpireCandle()
{if(TimeCurrent()<SecondsForOpening+Time[0])
return(true);
return(false);
}

double GetLot() 
{
   // calculate lot size
   int cnt_loss = 0;
   if(Martingle_Step>1) 
   {
   cnt_loss = CountLoss();
   while (cnt_loss>=Martingle_Step) cnt_loss = cnt_loss-Martingle_Step;
   }
   if(cnt_loss ==0 ) return(Amount1);
   else return(gd_amount[cnt_loss]);
}

int CountLoss() 
{
   int value =0;
   for (int counter = OrdersHistoryTotal()-1; counter >=0 ; counter--) {
      if (OrderSelect(counter, SELECT_BY_POS,MODE_HISTORY)) {
      if(OrderSymbol()!=Symbol())continue;
      if(OrderMagicNumber()!=MagicNumber)continue;
      if(OrderType()>1) continue;
      if (OrderType()==OP_BUY && OrderClosePrice()>OrderOpenPrice()) break;
      else if (OrderType()==OP_SELL && OrderClosePrice()<OrderOpenPrice()) break;
      if (OrderType()==OP_BUY && OrderClosePrice()<OrderOpenPrice()) value++;
      else if (OrderType()==OP_SELL && OrderClosePrice()>OrderOpenPrice()) value++;
      }
   }   
   return(value);
}
 

look, it's not a nice code to solve

I mean that it's hard to a lot of people to understand samebody else code that writen in one page and without comments


so please write same comments

best wishes

 
nice code thanks mate
 
byron88:

Hey guys 

can some one help me on this code as it does do martingale but on the next signal (arrow) and i want it to trade on the next bar.

so if a signal(arrow) comes up and  opens a trade  which it losses it then must do martingale on the next bar till it wins.

please help

Reason: