# Close the first position MQL4

56

Hello to all professors

With this code, I want the first position that went to 10 pips of profit to be closed (the first position)

Sales positions are calculated separately and buying positions separately

But here the expert comes and checks that every position that was in 10 pips of profit finds the same

I just want to count the first position and if it wins in 10 pips, it will be close

Where is my code wrong ???

Global thanks

```//---------------------------------------------------------------------
void Trail2()
{
int total = OrdersTotal();
for (int i=0; i<=OrdersTotal(); i++)
{
if(OrderSelect(i-0,SELECT_BY_POS)==true)
int type   = OrderType();

bool result = false;
if(
OrderMagicNumber()==Magic1
&&
(((Bid-OrderOpenPrice())/Point)>100)
&&
)
result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
// Sell
if(
OrderMagicNumber()==Magic2
&&
&&
OrderType()==OP_SELL
)

result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////```

Moderator
29899

I just want to count the first position and if it wins in 10 pips, it will be close

Define what you mean by first position.

56

Keith Watford:

I just want to count the first position and if it wins in 10 pips, it will be close

Define what you mean by first position.

Hi

I mean, if the first buy position was at 10 pips of profit, the position would be closed

Only this operation should be done in the first position and not look at other positions

But now any position that has 10 pips in profit closes from the end to the beginning

But I want to count from beginning to end

Global thanks

2430

ghobar:

Hello to all professors

With this code, I want the first position that went to 10 pips of profit to be closed (the first position)

Sales positions are calculated separately and buying positions separately

But here the expert comes and checks that every position that was in 10 pips of profit finds the same

I just want to count the first position and if it wins in 10 pips, it will be close

Where is my code wrong ???

Global thanks

```void Trail2()
{
int  FirsOrderClose=0;
int total = OrdersTotal();
for (int i=0; i<=OrdersTotal(); i++)
{
if(OrderSelect(i-0,SELECT_BY_POS)==true)
int type   = OrderType();

bool result = false;
if(
OrderMagicNumber()==Magic1
&&
(((Bid-OrderOpenPrice())/Point)>100)
&&
&& FirsOrderClose==0
)
{
result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
FirsOrderClose=1;
break;
}
// Sell
if(
OrderMagicNumber()==Magic2
&&
&&
OrderType()==OP_SELL
&& FirsOrderClose==0
)
{
result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
FirsOrderClose=1;
break;
}
}

}```
56

Mehmet Bastem:

Hi Dear Mehmet Bastem

I tried your code but I think it's wrong

Take a look at this photo Buy number 1 must be closed first, then Buys number 2 and ....

But in this photo The first number 4 is closed after 10 pips in profit

Global thanks

56

ghobar:

Hi Dear Mehmet Bastem

I tried your code but I think it's wrong

Take a look at this photo Buy number 1 must be closed first, then Buys number 2 and ....

But in this photo The first number 4 is closed after 10 pips in profit

Global thanks

I want Just close first sell position and first buy position after 10 pip profit .

2430

ghobar:

I want Just close first sell position and first buy position after 10 pip profit .

int  FirsOrderClose=0;

If you get this piece of code at the beginning of the program, you will get the result you want

56

Mehmet Bastem:

int  FirsOrderClose=0;

If you get this piece of code at the beginning of the program, you will get the result you want

Hi

I could not close the first position wherever I put this code

I need more guidance

Global thanks

```extern int Magic1=1;

extern int Magic2=2;

extern int period1=3;

extern int period2=8;

extern int periodrsi=7;

extern double highlevel=55;

extern double lowlevel=45;

extern double lot=0.1;

extern int slippage=6;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
if(Volume[0]<=1)
{
{

if(Orders()==10)
{
}
}

if(Statusma1()=="sell" && Statusrsi()=="sell")
{

if(Orders()==10)
{
Pendsell();
}
}
}
Trail2();
return(0);
}
//+------------------------------------------------------------------+
string Statusma1()
{
double ma1_shift1=iMA(Symbol(),0,period1,0,MODE_SMA,PRICE_CLOSE,1);
double ma1_shift2=iMA(Symbol(),0,period1,0,MODE_SMA,PRICE_CLOSE,2);

double ma2_shift1=iMA(Symbol(),0,period2,0,MODE_SMA,PRICE_CLOSE,1);
double ma2_shift2=iMA(Symbol(),0,period2,0,MODE_SMA,PRICE_CLOSE,2);

if(ma1_shift1>ma2_shift1&&ma1_shift2<ma2_shift2)

else

if(ma1_shift1<ma2_shift1&&ma1_shift2>ma2_shift2)
return("sell");
}
//+------------------------------------------------------------------+
string Statusrsi()
{
double rsi=iRSI(Symbol(),0,periodrsi,PRICE_CLOSE,1);

if(rsi<lowlevel)

else

if(rsi>highlevel)
return("sell");
}
//+------------------------------------------------------------------+
int Orders()
{
int num=10;

for(int i=OrdersTotal()-1;i>=10;i--)
{

if(OrderMagicNumber()==Magic1 || OrderMagicNumber()==Magic2)

num++;

}
return(num);
}
//---------------------------------------------------------------------
{
int ticket;
,0,"www.Ghobar.com",Magic1,0,Blue);

}
//---------------------------------------------------------------------
void Pendsell()
{
int ticket;
ticket=OrderSend(Symbol(),OP_SELL,MathPow(2,Lossnumber())*lot,Bid,slippage,0
,0,"www.Ghobar.com",Magic2,0,Red);
}
//---------------------------------------------------------------------
int Lossnumber()
{
int num=0;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{

if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;

if(OrderMagicNumber()==Magic1 || OrderMagicNumber()==Magic2)
{

if(OrderProfit()<0)num++;

if(OrderProfit()>=0)break;
}
}
return(num);
}
//--------------------------------------------------------------
void Trail2()
{
int  FirsOrderClose=0;
int total = OrdersTotal();
for (int i=0; i<=OrdersTotal(); i++)
{
if(OrderSelect(i-0,SELECT_BY_POS)==true)
int type   = OrderType();

bool result = false;
if(
OrderMagicNumber()==Magic1
&&
(((Bid-OrderOpenPrice())/Point)>100)
&&
&& FirsOrderClose==0
)
{
result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
FirsOrderClose=1;
break;
}
// Sell
if(
OrderMagicNumber()==Magic2
&&
&&
OrderType()==OP_SELL
&& FirsOrderClose==0
)
{
result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
FirsOrderClose=1;
break;
}
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////```

56

ghobar:

Hi

I could not close the first position wherever I put this code

I need more guidance

Global thanks

Where do I go wrong professors?
18

Hi @ghobar

How would you want to treat the 2nd position?

Let it continue run? Or close with 10pips profit?
18

Try:

```int FirstOrderCloseBuy = 0;
int FirstOrderCloseSell = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Trail2()
{

bool result;
for(int i=0; i<=OrdersTotal()-1; i++)
{
if(OrderSelect(i,SELECT_BY_POS)==true)

if(
OrderMagicNumber()==Magic1
&&
(((Bid-OrderOpenPrice())/Point)>100)
&&
)
{
result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
break;
}
// Sell
if(
OrderMagicNumber()==Magic2
&&