how do i stop my ea from opening order right after the stoploss it hit

 

ok, here's the problem. i got my ea work but there is just one problem; it opens order right after the stoploss is hit. the thing is it should wait for the next signal to open an order.

so here what im asking how do i get my ea the wait for the next signal after the stoploss is hit

also it would be nice if you could show me how to porperly put the code into the ea

extern double LotSize=0.1;
extern int SL=50,
           TP=30;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits==3 || Digits==5)
    {
     SL*=10;
     TP*=10;
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//double mfi=iMFI( NULL, 0, 20,0); 
//double ma=iMA(NULL,0, 14, 0, 0, 0, 0); 
double macd11=iCustom(Symbol(),0,"macd6",10,26,1,0,2);
double macd21=iCustom(Symbol(),0,"macd6",10,26,1,1,2);


static int trade_bar;
int nobuytrade = 0;
int noselltrade = 0;

int last_trade=HistoryTotal();
if(last_trade>0)
  {
   if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
     {
      if(OrderType() == OP_BUY) nobuytrade = 1;
      if(OrderType() == OP_SELL) noselltrade =1;
     }
  }


if((macd21==0   ) && TOOC()==0  && nobuytrade == 0){OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,NULL,0,0,Green);}
if((macd11==0  ) && TOOC()==0  && noselltrade ==0){OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,NULL,0,0,Red);}
//----

   SetSLTP();
//----
   return(0);
  }
//+------------------------------------------------------------------+

int TOOC()
{
  int tooc;
  for(int a=0;a<OrdersTotal();a++) 
   if(OrderSelect(a,0,0))
    if(OrderSymbol()==Symbol())
      tooc++;
  return(tooc);
}

void SetSLTP()
{
  for(int a=0;a<OrdersTotal();a++)
   if(OrderSelect(a,0,0))
    if(OrderSymbol()==Symbol())
     {
      if(OrderType()==OP_BUY)
       {
        RefreshRates();
        if(OrderStopLoss()==0)OrderModify(OrderTicket(),OrderOpenPrice(),Bid-SL*Point,OrderTakeProfit(),0,Yellow);
        RefreshRates();
        if(OrderTakeProfit()==0)OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Bid+TP*Point,0,Yellow);
       }
      if(OrderType()==OP_SELL)
       {
        RefreshRates();
        if(OrderStopLoss()==0)OrderModify(OrderTicket(),OrderOpenPrice(),Ask+SL*Point,OrderTakeProfit(),0,Yellow);
        RefreshRates();
        if(OrderTakeProfit()==0)OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask-TP*Point,0,Yellow);
       }
     }
  return;
}
 

1) int last_trade=HistoryTotal();

supposed to be int last_trade=OrdersHistoryTotal(); (i think)

2) if(OrderSelect(a,0,0))
???????

 

made the change it's still doing the same thing

 

both ?

 

o

i dont understand how to change this

2.) if(OrderSelect(a,0,0)

do i delete it or what

 
this line:
int last_trade=HistoryTotal();

with
int last_trade=OrdersHistoryTotal();


& this line:
if(OrderSelect(a,0,0)

with
if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES)==true)
 

i made the change and it did no different i just dont understand... its a simple ea... but for some reason i cant get it to work properly

 

i can't read your all code

(double macd11=iCustom(Symbol(),0,"macd6",10,26,1,0,2);
double macd21=iCustom(Symbol(),0,"macd6",10,26,1,1,2);)

maybe there is a signal now

check if the code above is giving a signal now (use Print() or Alert())

 

oppsss

i don't c in the code where u updating the var. last_trade

because last_trade is always greater then 0 after closing a position

 

use this indicator

//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

//
//    ?????????? ?? ???????????? ???D 
//    1. ?????????? ? ????? ??/??
//    2. ???????? ?? ??????????? ???????? ????
//
//    Difference from ???D of standart
//    1. color of style of AC/AO
//    2. unvisible null bar
#property  copyright "Aleksandr Pak,Almaty, 2006"
#property  link  "ekr-ap@mail.ru" 
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Green
#property  indicator_color2  Red
#property  indicator_color3  Silver

#property  indicator_width1  2
#property  indicator_width2  2
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double     MacdBuffer[],   MacdBufferUp[],   MacdBufferDown[];
double     SignalBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
  IndicatorBuffers(4);
   
   SetIndexBuffer (0,MacdBufferUp);
   SetIndexStyle  (0,DRAW_HISTOGRAM);
   SetIndexBuffer (1,MacdBufferDown); 
   SetIndexStyle  (1,DRAW_HISTOGRAM);
   SetIndexBuffer (2,SignalBuffer);
   SetIndexStyle  (2,DRAW_LINE);
   SetIndexDrawBegin(2,SignalSMA);
   SetIndexBuffer (3,MacdBuffer);
   SetIndexStyle  (3,DRAW_NONE);   
   
   SetIndexLabel  (0,"Buffer 0");
   SetIndexLabel  (1,"Buffer 1");
   SetIndexLabel  (2,"Signal");
   IndicatorDigits(Digits+3);
   IndicatorShortName("MACD_color("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit,i;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   for(i=0; i<limit; i++)
         {
         MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
         }
   for(i=0; i<limit; i++)
   {
      MacdBufferDown[i]=0.0; MacdBufferUp[i]=0.0;
     
         if(i>=1) //??? ????? ?? ??????????? ???????? ???? //break the null bar  
            {
               if(MacdBuffer[i]-MacdBuffer[i]>=0)MacdBufferDown[i]=MacdBuffer[i]; //??????? ???????? //condition of color
                  else MacdBufferUp[i]=MacdBuffer[i];}
            }
   for(i=0; i<limit; i++) SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
   return(0);
  }
//+------------------------------------------------------------------+
 

can u explain what is the purpose of this code:

int last_trade=HistoryTotal();
if(last_trade>0)
  {
   if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
     {
      if(OrderType() == OP_BUY) nobuytrade = 1;
      if(OrderType() == OP_SELL) noselltrade =1;
     }
  }

Reason: