Set EA to close positions when opposite signal show up

 

I am new to ea coding but I search and learn on internet.

this is my first ea and it doesn't work as I expect.

It do open position as I want it to work but...

I want this ea to close profit positions when the opposite signal show up

example

1.signal from iCustom show arrow up and down

2. ea open position from signal, let's say 3 sell positions

3. then signal sell show up while 2 positions are in profit and another one is negative.

4. I want it close only 2 profit positions and open 1 buy.

is that to hard for beginner like me? because I watch and read on the internet, i kind of confuse and unable to ask anyone.


here is my code

//--- input parameters
input double Lots = 0.01;


double IDC_Buy=0;
double IDC_Sell=0;

void GetIndy()


{
   IDC_Buy=iCustom(Symbol(),0,"JapCandle",0,0);
   IDC_Sell=iCustom(Symbol(),0,"JapCandle",1,0);
}

int tic;
void EntryOrder()
{
   if(IDC_Buy!=2147483647) tic=OrderSend(Symbol(),OP_BUY,1,Ask,3,0,0,NULL,0,0,clrGreen);
   
   
   if(IDC_Sell!=2147483647) tic=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,0,NULL,0,0,clrRed);
}

void close()
{
   if(OrdersTotal()>0)
   {
      for(int i=0;i<OrdersTotal();i++)
      {
         OrderSelect(i,SELECT_BY_POS);
            if(OrderType()==OP_SELL)
            {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
            }
            if(OrderType()==OP_SELL)
            {
            OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue);
            }
      }
   }
}
//

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int LastBars;
void OnTick()
  {
//---
      GetIndy();
      if(LastBars!=Bars)
      {
         EntryOrder();
         LastBars=Bars;
      }
      //Comment(IDC_Buy+" - "+ IDC_Sell);
      }


  
//--

 
//---
Reason: