Download MetaTrader 5

Close Order Partially by given %?

To add comments, please log in or register
Sherif Hasan
7400
Sherif Hasan  

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++)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        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?

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  

Use OrderLots()


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

Sherif Hasan
7400
Sherif Hasan  
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.

honest_knave
Moderator
2322
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.

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
Lots = Volume
honest_knave
Moderator
2322
honest_knave  
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.
SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
It is a double variable. Yeah, missing information here.
Keith Watford
Moderator
10617
Keith Watford  
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 

Sherif Hasan
7400
Sherif Hasan  
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;
honest_knave
Moderator
2322
honest_knave  

As I suggested above, print the value of LotPer.

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

whroeder1
16076
whroeder1  
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
12
To add comments, please log in or register