Need HELP on MARTINANGLE CODE

 

Hi All,

I have write a code for MARTINAGLE EA based on Bollinger Bands.

I'm currently stuck at continues OP until reach Upper BB or Lower BB.

Can someone advice me.

Very appreciate your help

 int OP=0;
   int OP1=0;
   double price=0;
   double price1=0;
   double sll=0;
   double sll1=0;
   double tpp=0;
   double tpp1=0;
   double lot=0;
   double lot1=0;
   double lota=0;
   double lot1a=0;
   double profit=0;
   int total=0;
   int typ=7;
   int tik=0,tik1=0;
   for(int x=0;x<OrdersTotal();x++){
   get=OrderSelect(x,SELECT_BY_POS,MODE_TRADES);
   if(OrderSymbol()!=Symbol()){continue;}
   if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==Magic){
   OP++;price=OrderOpenPrice();sll=OrderStopLoss();tpp=OrderTakeProfit();
   lot=OrderLots();lota+=OrderLots();
   tik=OrderTicket();
   }
   if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==Magic){
   OP1++;price1=OrderOpenPrice();sll1=OrderStopLoss();tpp1=OrderTakeProfit();
   lot1=OrderLots();lot1a+=OrderLots();
   tik1=OrderTicket();
   }
   //
   if(OrderSymbol()==Symbol() && OrderType()<2 && OrderMagicNumber()==Magic){
   typ=OrderType();
   total++;profit+=OrderProfit()+OrderSwap()+OrderCommission();
   }
   }
//---------------------------------- 
double bbup=iBands(_Symbol,0,BB,Deviations,0,PRICE_WEIGHTED,MODE_UPPER,1);
double bbdn=iBands(_Symbol,0,BB,Deviations,0,PRICE_WEIGHTED,MODE_LOWER,1);

bool buy=Close[1]<=bbdn && OP==0;
bool sell=Close[1]>=bbup && OP1==0;

if(buy){
tiket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,sl,tp,Comment_,Magic,0,Blue);}
if(sell){
tiket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,sl1,tp1,Comment_,Magic,0,Red);}

if(OP>0 && price-Ask>=MartingaleStep*SetPoint && LOT_MULTI(Multiplier,0)<MaxLot){
tiket=OrderSend(Symbol(),OP_BUY,LOT_MULTI(Multiplier,0),Ask,slippage,sl,tp,Comment_,Magic,0,Blue);
ModTP(0);
}

if (OP>=bbup) 
   {
          buy=False;
   } else {
          buy=True;
   }
if(OP1>0 && Bid-price1>=MartingaleStep*SetPoint && LOT_MULTI(Multiplier,1)<MaxLot){
tiket=OrderSend(Symbol(),OP_SELL,LOT_MULTI(Multiplier,1),Bid,slippage,sl1,tp1,Comment_,Magic,0,Red);
ModTP(1);
}
if (OP1<=bbdn) 
   {
          sell=False;
   } else {
          sell=True;
   }


//----------------------------------   
   
//INFO
if(AccountProfit()>=0){colorz=Lime;}else colorz=White;
 T("1",NamaEA, colorz,0, 10, 60);
 T("2","Profit Percentage = "+DoubleToStr((profit/AccountBalance())*100,2)+"%", colorz,0, 10, 20);
 T("3","Total Profit = "+DoubleToStr(profit,2)+" "+AccountCurrency(), colorz,0, 10, 40);
}
//+------------------------------------------------------------------+
 
void SetBroker(){
if(MODE==PIPS){
 if(Digits==3||Digits==5){SetPoint=10*Point;}else
 SetPoint=Point;}else
 SetPoint=Point;
 }
//
//=================================


void T(string n, string tx, color col, int cor, int ax, int ay)
{

 int font  =10;
 string typ="Arial";

   ObjectCreate(n, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(n, tx, font, typ, col);
   ObjectSet(n, OBJPROP_CORNER, 1);
   ObjectSet(n, OBJPROP_XDISTANCE, ax);
   ObjectSet(n, OBJPROP_YDISTANCE, ay);
   } 
   
 //+------------------------ 
//+-----------------------------------

 
void ModTP(int typ)
{
   int      i=0;
   double   TP=0;
         for(i=0;i<OrdersTotal();i++)
         {
           
           get=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=Magic||OrderType()!=typ){continue;}
           if(typ==0){
           if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
               if(TP==0) {TP=OrderTakeProfit();}
               if(TP>OrderTakeProfit()) {TP=OrderTakeProfit();}
               
           }
           }else if(typ==1){
           if(OrderType()==OP_SELL && OrderSymbol()==Symbol()  && OrderMagicNumber()==Magic)
           {
               if(TP==0) {TP=OrderTakeProfit();}
               if(TP<OrderTakeProfit()) {TP=OrderTakeProfit();}         
           }
              }       
         }
         for(i=0;i<OrdersTotal()-1;i++)
         {
           
           get=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=Magic||OrderType()!=typ){continue;}
           if(OrderType()==typ && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
               get=OrderModify(OrderTicket(), OrderLots(), OrderStopLoss(), TP, 0);
           }
                     
         
         }        
} 
//------------------ 
double LOT_MULTI(double multi, int tipe_ord){
double lot=0;
int buycount=0,sellcount=0; 
  for (int y = 0; y < OrdersTotal(); y++)
   {
      get=OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType()==0) { 
      buycount++;  }
       if (OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType()==1) { 
      sellcount++;  }
   }

if(tipe_ord==0){
lot = Lots*MathPow(multi,MathFloor(buycount));}else
if(tipe_ord==1){
lot = Lots*MathPow(multi,MathFloor(sellcount));}
 
return(lot); 
}
//+-- 
 
Pak Wan:

Hi All,

I have write a code for MARTINAGLE EA based on Bollinger Bands.

I'm currently stuck at continues OP until reach Upper BB or Lower BB.

Can someone advice me.

Very appreciate your help

you don't have OnTick() function to execute.

 
Pak Wan:

Hi All,

I have write a code for MARTINAGLE EA based on Bollinger Bands.

I'm currently stuck at continues OP until reach Upper BB or Lower BB.

Can someone advice me.

Very appreciate your help

Replace Close[1] by : 

iClose(_Symbol,PERIOD_CURRENT,1)

And don't forget to change your Aktivasi code since you shared the source ... 

 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2.    get=OrderSelect(x,SELECT_BY_POS,MODE_TRADES);
    Check your return codes for errors, report them. Don't just silence the compile, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  3.          for(i=0;i<OrdersTotal()-1;i++){
               :
               get=OrderModify ..
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

  4. Martingale, guaranteed to blow your account eventually.
Reason: