How to store the last order type (Buy/Sell)? - page 2

 
c0d3:


one time doing the loop is enough and make a habit to count down checking your trades

the last order type you check with OrderOpenTime()

and it can be that one is still open....

 
deVries:


one time doing the loop is enough and make a habit to count down checking your trades

the last order type you check with OrderOpenTime()

and it can be that one is still open....

i'm checking history, why would any open trades show up??

The loop is only for closed trades, via OrdersTotalHistory() method

 
c0d3:

i'm checking history, why would any open trades show up??

The loop is only for closed trades, via OrdersTotalHistory() method


they could be still open, I know you do a loop at OrdersTotalHuistory() but this topic was beginning with

OmegaFX 2012.07.17 21:51

How to store the last order type buy or sell in to order to be used as controller for other operators/expressions?

and which type of declaration should be used? Global or Local variable?

So you need also to check if a trade is still open..... or if there was no trade before

And all you did wanna check inside OrdersTotalHuistory() can be done with one time going through the loop

While you run through the loop, you run again through the loop that is not needed here....

 

i see, well you gave clear direction as to what needs to be done to achieve his desired result.

Scan history order pool
Scan live order pool
Set global variable to the last trade from history, if none are currently open, else store the last opened order type.

 

This is what I had in mind. Limited Tested. Note:

1) Instead of using the maximum ticket# in the top function, one could select both orders again and compare their open-times, that is if you don't trust the brokers ticket numbering system.

2) If you're going to modify the custom period for history or not always use all history then the static variables within the function could become a problem as history_total could be <than static history total.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    int Last_Type=Last_Sent_Active_Order_Type();
    Print("Last_Type="+Last_Type);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Last_Sent_Active_Order_Type(){
/*Function Returns 0=OP_BUY, 1=OP_SELL, -1=None*/
    int Last_Active_Ticket=Ticket_Of_Last_Active_OpenTime();
    int Last_History_Ticket=Ticket_Of_Last_History_OpenTime();
    int Ticket=MathMax(Last_Active_Ticket,Last_History_Ticket);
    if(!OrderSelect(Ticket, SELECT_BY_TICKET)){return(-1);}
    return(OrderType());
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Ticket_Of_Last_Active_OpenTime(){
    static int Last_Total;  static int Last_Ticket;
    if(Last_Total==OrdersTotal()){return(Last_Ticket);}
    datetime Last_OpTime;
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
        && OrderOpenTime()>Last_OpTime
        && OrderType()<=OP_SELL
        //&& OrderMagicNumber()==Magic
        //&& OrderSymbol()==Symb
        ){
            Last_OpTime=OrderOpenTime();
            Last_Ticket=OrderTicket();
    }   }
    Last_Total=OrdersTotal();
    return(Last_Ticket);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Ticket_Of_Last_History_OpenTime(){
    static int Last_Total;  static int Last_Ticket;
    if(Last_Total==OrdersHistoryTotal()){return(Last_Ticket);}
    datetime Last_OpTime;
    for(int i=OrdersHistoryTotal()-1; i>=Last_Total-1; i--){
        if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
        && OrderOpenTime()>Last_OpTime
        && OrderType()<=OP_SELL
        //&& OrderMagicNumber()==Magic
        //&& OrderSymbol()==Symb
        ){
            Last_OpTime=OrderOpenTime();
            Last_Ticket=OrderTicket();
    }   }
    Last_Total=OrdersHistoryTotal();
    return(Last_Ticket);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
ubzen:

This is what I had in mind. Limited Tested. Note:

1) Instead of using the maximum ticket# in the top function, one could select both orders again and compare their open-times, that is if you don't trust the brokers ticket numbering system.

2)If you're going to modify the custom period for history or not always use all history then the static variables within the function could become a problem as history_total could be <than static history total.


This code is looking nice ....

About 1) Ticketnumber ticket numbering system " using the maximum ticket# "

It is true a newer trade will get a higher ticketnumber.

But..... if you have several trades open and you partial close trades the remaining part will also get a higher ticketnumber

In that case the last opened trade is not always the trade with highest ticketnumber...

 
deVries:


This code is looking nice ....

About 1) Ticketnumber ticket numbering system " using the maximum ticket# "

It is true a newer trade will get a higher ticketnumber.

But..... if you have several trades open and you partial close trades the remaining part will also get a higher ticketnumber

In that case the last opened trade is not always the trade with highest ticketnumber...

You could be correct. I was getting lazy there and used a short-cut which could indicate that one could use the ticket# because new trades gets a higher ticket#. <--But you never know what brokers would do.

But..... if you have several trades open and you partial close trades the remaining part will also get a higher ticketnumber. I failed to consider that. Do you know if it'll keep the OrderOpenTime the same?

Update: it keeps the original open time when it splits according to the tester. But in any case you'll want to select the orders which are returned and check their open-times, then return the type of the highest open time.

 

Hi Everybody.


I find a liitle problem in your's proposition, and this was a question that I'll going to ask around here, so it is the moment :


I agree with Globavariableset() wich I use since longtime and often forgotten in Ea. Never problem with them, and if I don't need to optimizing parameters, I prefer put the Users parameters in GV. As they are displayed in alphabetical order

with the F3 key, we can sort the firsts one GV that we want Users see directly on up page without having to scroll., they are cancelled only if they hasn't been only read one time during 4 week.

More : The users can change its parameters without disconnect the EA ..

I think that as the Arrays witch are static Variable, but volatil, the GV has direct memory adress and that this run fast. I think too that necesserly the GV are stored in metatrader terminal file somewhere in our console when we close our PC, as template

or the other users' configs.


Well, about OrderHistoryTotal();, the problem is for me that with that we only get the TotalHistory that are staying at the moment loaded into our terminal.

I mean that, if using terminal ( CTRL-T) ; History, select period TODAY . : you'll only get your today's history order.

SO that's means that we have to choose in CTRL-T ; history, select period " ALL History " to get the history from the beginning of the account concerned. ( or the period we'ld want, could be a month).



My question was : is there a way to send to the server an order from the EA to select the loading period we want in our terminal. ?



Thanks,


Daniel

 

My question was : is there a way to send to the server an order from the EA to select the loading period we want in our terminal. ? Not with mql4. Perhaps with WINAPI you could modify the custom period for history.

GlobalVariableSet are nice for all the reasons you described. It certainly have some advantages like changing values on the fly. However, one of its dis-advantage is not being able to continue from a computer/terminal crash. The global variables I'm mostly discouraging are the global_variables defined above the start() and not the GlobalVariableSet.

 
ubzen:

This is what I had in mind. Limited Tested. Note:

1) Instead of using the maximum ticket# in the top function, one could select both orders again and compare their open-times, that is if you don't trust the brokers ticket numbering system.

2) If you're going to modify the custom period for history or not always use all history then the static variables within the function could become a problem as history_total could be <than static history total.T

Thanx Ubzen.... that was just the code I was looking for....

Cheers 

Reason: