1. ## Close Specific Trade Positions

Hi,
How would you close all the open positions in a specific pair when the sum profit had reached a certain level?

Without closing all other open positions

Example

Profit Target = 50

Sum all EURUSD positions
If sum of EURUSD positions >= Profit Target
Close all EURUSD positions

2. Originally Posted by jimhook_uk1
Hi,
How would you close all the open positions in a specific pair when the sum profit had reached a certain level?

Without closing all other open positions

Example

Profit Target = 50

Sum all EURUSD positions
If sum of EURUSD positions >= Profit Target
Close all EURUSD positions

Hi jimhook_uk,

Parameters

Code:
```extern double ProfitTarget=50;
extern string SpecificPairs="EURUSD";```
Start

Code:
```if(AccountEquity()>=(AccountBalance()+ProfitTarget))
{
CloseSellOrders(Magic);
return(0);
}```

Functions

Code:
```//|---------close buy orders

{
int result,total=OrdersTotal();

for (int cnt=total-1;cnt>=0;cnt--)
{
if(OrderMagicNumber()==Magic&&OrderSymbol()==SpecificPairs)
{
{
OrderClose(OrderTicket(),OrderLots(),Bid,3);
switch(OrderType())
{
result=OrderDelete(OrderTicket());
}
}
}
}
return(0);
}

//|---------close sell orders

int CloseSellOrders(int Magic)
{
int result,total=OrdersTotal();

for(int cnt=total-1;cnt>=0;cnt--)
{
if(OrderMagicNumber()==Magic&&OrderSymbol()==SpecificPairs)
{
if(OrderType()==OP_SELL)
{
switch(OrderType())
{
case OP_SELLLIMIT:
case OP_SELLSTOP:
result=OrderDelete(OrderTicket());
}
}
}
}
return(0);
}```

3. Surely that will close the open fiber positions when the overall account profit hits 50. As I had read the question, jimhook wanted to close the fiber positions when the open profit from just the fiber positions had hit 50 regardless of any other profit or loss on the account.

Or have I misunderstood?

If I've not misunderstood, you'd need to add the profits together as you're counting the open orders.

4. Hi jezzer,
You are correct; I want this code to only close positions in a specific pair if the total profit of that pair hits the target. I want all other open positions to remain open until they are closed by some other process.

5. Jim,

I thought this was what you wanted. The logic is quite simple.

Firstly you need to define 2 doubles. One double we'll call TotalOpenProfit. It will sit before the OrderSelect() function and will = 0 so that it will reset as the code returns.

The second double will be called OpenProfit and will sit within the OrderSelect() function before any conditons.

OpenProfit = OrderProfit()+OrderSwap()+OrderCommission();

Then, when the OrderSelect() function starts counting the open trades, TotalOpenProfit will equal TotalOpenProfit+OpenProfit and will either increase or decrease with each order in the pool that the OrderSelect() function counts.

Once the TotalOpenProfit>=50 then you can start closing open positions and deleting pending orders.

I hope that helps. I'd rather post the idea behind the logic in the hope that you understand, than a load of code which is probably meaningless to many.

6. Originally Posted by jimhook_uk1
Hi jezzer,
You are correct; I want this code to only close positions in a specific pair if the total profit of that pair hits the target. I want all other open positions to remain open until they are closed by some other process.
Ok good question, you should create first a loop, something like this :

Code:
```double TotalProfits=0;

int i;
int Limit=500;
for(i=0;i<Limit;i++)
{
if(OrderSymbol()==SpecificPairs&&OrderMagicNumber()==Magic)
{
TotalProfits+=(OrderProfit());

}
}```
And then to replace this :

Code:
`if(AccountEquity()>=(AccountBalance()+ProfitTarget))`
by :

Code:
`if(TotalProfits>=ProfitTarget)`