# Close Order Partially by given %?

7400

Hi,

I want to close when order runs with +20 pips i would like to close running order % of order partially with some profit.

eg:

lot size: 1 lot

partial close parameter is = 50%

so when order is running above 20+ profit pips i would like to close the order partially 50% of lot size. according to settings it should close 50% of lot size 0.50 order partially, remains running with 0.50 lot.

i have tried coding. but it shows error message.

error message:

```invalid lots number for OrderClose function
OrderClose error 4051```

Code i tried:

```    int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[30][2];
for (int i = 0; i < orderstotal; i++)
{
if (OrderType() != OP_SELL || OrderSymbol() != Symbol() || OrderMagicNumber() != 1)
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
double LotPer = MathRound(OrderLots()*Lots/100);
bool ret = OrderClose(OrderTicket(), LotPer, OrderClosePrice(), 4, Red);
if (ret == false)
Print("OrderClose() error - ", ErrorDescription(GetLastError()));
}
}```

what wrong with this above code?

1399

Use OrderLots()

Something like that. No need to recalculate lots when you are closing orders.

7400

deysmacro:

Use OrderLots()

Something like that. No need to recalculate lots when you are closing orders.

I used OrderLots()

but says error message as given above.

Moderator
2322

Print the value of LotPer after you have calculated it.

I don't know what 'Lots' is, but I'm guessing LotPer won't be what you expect.

1399

Lots = Volume
Moderator
2322

deysmacro:
Lots = Volume
It's a variable or a constant.. but I don't see it declared or given a value in the code snippet.
1399

It is a double variable. Yeah, missing information here.
Moderator
10617

honest_knave:

Print the value of LotPer after you have calculated it.

I don't know what 'Lots' is, but I'm guessing LotPer won't be what you expect.

I agree

as MathRound returns an integer it can never be 0.5 as Sherrif expects

7400

GumRai:

I agree

as MathRound returns an integer it can never be 0.5 as Sherrif expects

I removed Mathround() function. still error.

can any one help me to get exact way!

`double LotPer = OrderLots()*Lots/100;`
Moderator
2322

As I suggested above, print the value of LotPer.

And show us where you declare/set the value of 'Lots'

16076

sheriffonline:
`double LotPer = OrderLots()*Lots/100;`
You must NormalizeLots your lots, and verify that the close amount and remaining amount are both at least minlot. See my close function