Trouble Selecting Order properly when part of it closed

 

Hi guys.

A quick question, but I have tried many things.

I have been closing parts of orders, and then basing decisions of the rest on the part of the order that is still in MODE_TRADES, and unfortunately it uses the info from the orders from MODE_HISTORY at the same time (even if I give that ticket a variable, which is not supposed to be accessed from history according to dictionary last I remember).

Is there a way around this? I mainly want to check if an order is still alive, either by ticket or magic (yet to try by comment, but rather not go that way), but it is looking in history as well, after the orders are split up, even once the trade closes in MODE_TRADES.

Is there a walk around for this?

 
You'll be better off opening minimum lot orders. Or use something like here?
 
Sounds like you need to re-select the correct order freshly from the Order Pool
 
ubzen:
You'll be better off opening minimum lot orders. Or use something like here?
wow cool thanks Ubzen, if fxnet1 was stuck on this problem, then I am not so embarrassed... awesome link... I will look into that code.. yeah I wanted to use a system to close part of an order without messing up the rest of the EA, so minimum lotsize was not possible. Haha hey I meant to get back to you ubzen, I keep seeing my tiles in my shower as 5 pip grids now too, haha!!!
RaptorUK:
Sounds like you need to re-select the correct order freshly from the Order Pool

Thanks again Raptor. Yeah I was having trouble with the reselecting doing it within the one block, cause it still thought my order was in both places even when it was in only one.. but worse it considered a ticket that was in MODE_HISTORY still in MODE_TRADES.. However your reselect solution did help when I made another final check block. I had tried this second check for a while, however I was just having trouble getting it through if the order did not exist.

void CheckIfAnyOrdersClosed()
{
     if(OrderSelect(MYSELLTICKET,SELECT_BY_TICKET)==true)
     {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MYSELLMAGIC)
           {
           datetime ctm=TimeCurrent()-OrderCloseTime();
           if(TimeCurrent()-OrderCloseTime()<60) Print("Closed Last Order (or part of) - seconds ago = ", ctm);
           if(TimeCurrent()-OrderCloseTime()<60) CheckifFirstOrderAlive();
           }
      }
}

// above is a modification of the block that I found in the dictionary...
// for now I just wanted to check over the last 60 seconds.. the print is just for diagnosis atm..

void CheckifFirstOrderAlive()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MYSELLMAGIC)
        {
            exists = true;
        }
    }
    if (exists == false)
    {
        Print("All parts of the order with this magic number of this pair are now all closed!!!!");
        GotoNextBlock();
    }
}
// above seems to be a successful way to filter if that magic number was alive...

Previously I was using an }else{ block aftter the {exists=true;} however I was chasing my tail getting the data to pass through...

I am so happy progress again, I was stuck for days, and once I posted, that idea came to me..... hey thanks yet again for your time... I will look into FX1net's code too, it looked like it could be more precise...

Cheers guys, thanks ;)

 

...hey I meant to get back to you ubzen, I keep seeing my tiles in my shower as 5 pip grids now too...

Lol... Yeah price charts grow on ya... Hopefully in-time, you'll return to having more pleasant thoughts in the shower.

 
ubzen:

...hey I meant to get back to you ubzen, I keep seeing my tiles in my shower as 5 pip grids now too...

Lol... Yeah price charts grow on ya... Hopefully in-time, you'll return to having more pleasant thoughts in the shower.


Haha yeah. If I can work it out, then showertime will be pleasant thoughts of how to spend FX profits, as long as not go mad lost in maths first. I am thinking I might have to crack out a waterproof whiteboard marker and start drawing in broker slippages, haha.

And alas, my code mod above did not help me actually, in the situation where there was part of an order closed in profit. It works if it goes straight to SL, and the order is still whole, it was late when I thought I got it... Woke up this morning and back to the grind stone...

So this time I am trying FX1net's mods. I am not sure why he said it was not the safest way. I am adapting it now, will post back how I go... Thanks for that link too, I did try to search that time for other posts to save redundancy, you did well to find it.

 

Доброго времени суток уважаемые форумчане!

Меня зовут Герман, мне 23 года, я являюсь трейдером компании "Инстафорекс"

Помогите в поиске нужного скрипта! Скрипт нужен для сетки отложенных ордеров.

 
GERICH:

Доброго времени суток уважаемые форумчане!

Меня зовут Герман, мне 23 года, я являюсь трейдером компании "Инстафорекс"

Помогите в поиске нужного скрипта! Скрипт нужен для сетки отложенных ордеров.


maybe write a seperate post, as this one is for Selecting Orders after part close.
 

Ah... I am really lost on this challenge... I had no luck with FX1's peice of code. I got it so it was selecting the part of the order that was closed as I put a print log there to tell, however I couldn't get his second section to work.

My EA thinks trades that are in MODE_TRADES when they are in MODE_HISTORY... after closing part of an order, even once that order has finally closed.

I think FX1net's block counts all the orders in history and compare them with mode trades, and then see if there are any orders in MODE_TRADES that are not in MODE_HISTORY with a more recent OPENTIME and lower LOTSIZE. And then if there is no match, it is considered all closed.... Can someone confirm for me that is what his block is doing??? Thanks

Cause if that is the case, I might try rewrite that block so it makes more sense to me.

Is there a simpler option at all? Like when I do the part close, what associated data can I remove from that ticket, so it doesn't get mixed up....

I only need to check if all orders of MYMAGIC are closed... sounds bloody simple hey... but it is looking for them in MODEHISTORY even after they have all closed... even when I freshly check from the Order pool using MODE TRADES etc just merely having used a part close...... Funnily I even tried selecting by TICKET, cause I remember having a problem selecting a ticket once closed preivously, as by definition once you Select by Ticket it is in MODE_TRADES automatically... So I even tried to work with this sideeffect, but it looked for that TICKET in MODE_HISTORY after a parital close... Wierd hey... that defies logic....

what am I doing wrong I wonder..

(nb. I think if I do this by COMMENTS, I will have the same drama... also I have considered opening multiple orders each time, and closing them that way, but this is not really possible in my EA)

 

There must be something wrong with your codes. I don't get why it would be looking in history unless you're asking for mode_history somewhere. This is the area you should focus most of your attn.

if(OrderSelect(MYSELLTICKET,SELECT_BY_TICKET)==true)....make sure to add mode_trades here.

 

Here's a possible solution I just coded. This helped explain some question I had about partial close. My solution is because generally you know where your orderSend and orderClose commands are, adding the ticket numbers to an array is the best I can think of. Other simple solutions like if Ticket# is greater than Known Ticket# then its a partial close came to me but that breaks if you open more than one order at a time.

To make the codes more reliable to shutdowns, look into saving the value of the Arrays to file because I dunno if Arrays save their value between shut-downs. To test code save as EA, as Script might work as well. This solution is new to me and I could have missed something so use with care.

bool iPrint;
int i, j, Test_Order, iMagic=777, Known_Ticket_Ray[], Partial_Ticket_Ray[];
//~~~~~~~~~~
void start(){
    //~~~~~~~~~~
    if(!iPrint){
        Test_Order=OrderSend(Symbol(),OP_BUY,1,Ask,999,
        0,0,"Magic="+DoubleToStr(iMagic,0),iMagic,0,Blue);
    }
    //~~~~~~~~~~
    for(i=ArraySize(Known_Ticket_Ray);i>=0;i--){
        if(Test_Order>Known_Ticket_Ray[i]){
            ArrayResize(Known_Ticket_Ray, i+1);
            Known_Ticket_Ray[i]=Test_Order; break;
        }
    }
    //~~~~~~~~~~
    if(!iPrint){
        OrderClose(Test_Order,0.5,Bid,999,Gold);
        //~~~~~~~~~~
        for(i=OrdersTotal()-1;i>=0;i--){
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
                if(OrderSymbol()==Symbol()
                && OrderMagicNumber()==iMagic
                ){
                    for(j=ArraySize(Known_Ticket_Ray)-1;j>=0;j--){
                        if(OrderTicket()==Known_Ticket_Ray[j]){bool Known=true; break;}
                    }
                    if(!Known){
                        for(j=ArraySize(Partial_Ticket_Ray);j>=0;j--){
                            if(OrderTicket()>Partial_Ticket_Ray[j]){
                                ArrayResize(Partial_Ticket_Ray, j+1);
                                Partial_Ticket_Ray[j]=OrderTicket(); break;
                            }
                        }
                    }
                }
            }
        }
    }
    //~~~~~~~~~~
    if(!iPrint){
        for(i=ArraySize(Known_Ticket_Ray)-1;i>=0;i--){
            Print("Known_Ticket_Ray="+Known_Ticket_Ray[i]);
        }
        for(i=ArraySize(Partial_Ticket_Ray)-1;i>=0;i--){
            Print("Partial_Ticket_Ray="+Partial_Ticket_Ray[i]);
        }iPrint=true;
    }
    //~~~~~~~~~~
    /*if(!iPrint){
        for(i=OrdersTotal()-1;i>=0;i--){
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
                if(OrderSymbol()==Symbol()
                && OrderMagicNumber()==iMagic
                ){
                    Print(
                        "   OrderTicket#="+OrderTicket()+
                        "   OrderMagic#="+OrderMagicNumber()+
                        "   OrderLotSize="+OrderLots()+
                        "   OrderCloseTime="+OrderCloseTime()+
                        "   OrderComment="+OrderComment()
                    );
                }
            }
        }
    }
    //~~~~~~~~~~
    if(!iPrint){
        for(i=OrdersHistoryTotal()-1;i>=0;i--){
            if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
                if(OrderSymbol()==Symbol()
                && OrderMagicNumber()==iMagic
                ){
                    Print(
                        "   OrderTicket#="+OrderTicket()+
                        "   OrderMagic#="+OrderMagicNumber()+
                        "   OrderLotSize="+OrderLots()+
                        "   OrderCloseTime="+OrderCloseTime()+
                        "   OrderComment="+OrderComment()
                    );  iPrint=true;
                }
            }
        }
    }*/
    //~~~~~~~~~~
}
//~~~~~~~~~~
Reason: