Way to get the current streak

zzuegg
1503
zzuegg  

Hello, seems i can't think right today.

I need to figure out a function which returns the current streak. (5 losses in a row or 3 wins in a row for example)

Since the ordering of the OrderHistors() is not reliable (as said in the doc) the function i have is kind of expensive as it requires a loop trough the whole history for each additional win/loss in a row (3 loops if i had currently 2 win/losses in a row)

Is there a cheaper way of getting the needed results?

William Roeder
25064
William Roeder  
zzuegg:
Since the ordering of the OrderHistors() is not reliable (as said in the doc) the function

I need to figure out a function which returns the current streak. (5 losses in a row or 3 wins in a row for example)

Where exactly in here do you read that.

But if you want to be paranoid, create a sorted list

int GetHistoryOrderByCloseTime(int tickets[], int asc=1){   // Descending -1
    int nTickets = 0;
    for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360
    ){
        int nextTicket = OrderTicket(); datetime nextOCT = OrderCloseTime();
        arrayResize(tickets, nTickets+1);
        for (int iTicket = nTickets; iTicket > 0; iTicket--){ // Insertion sort
            int         prevTicket  = tickets[iTicket-1];
            OrderSelect(prevTicket, SELECT_BY_TICKET);
            datetime    prevOCT     = OrderCloseTime();
            if ((prevOCT - nextOCT) * asc <= 0) break;
            tickets[iTicket] = prevTicket;
        }
        nTickets++;     tickets[iTicket] = nextTicket;            // Insert.
    }
    return(nTickets);
}
:
    int tickets[], nTickets = GetHistoryOrderByCloseTime(tickets, -1);
    if (nTickets = 0) int   nInARow = 0;
    else{                   nInARow = 0;
        OrderSelect(tickets[0], SELECT_BY_TICKET);
        double  lastProfit = OrderProfit() + OrderSwap() + OrderCommission();
        for (int iPos = 1; iPos < nTickets; iPos++){
            OrderSelect(tickets[iPos], SELECT_BY_TICKET);
            double  prevProfit = OrderProfit() + OrderSwap() + OrderCommission();
            if (prevProfit * lastProfit < 0)    break;      // different sign
            nInARow++;
        }
    }
    if (lastProfit < 0) string last="Losses";   else last="Wins";
    Print("I've had ",nInARow," ", last," in a row");
Typed, not compiled, not tested.
dabbler
1091
dabbler  
zzuegg:

Hello, seems i can't think right today.

I need to figure out a function which returns the current streak. (5 losses in a row or 3 wins in a row for example)

Since the ordering of the OrderHistors() is not reliable (as said in the doc) the function i have is kind of expensive as it requires a loop trough the whole history for each additional win/loss in a row (3 loops if i had currently 2 win/losses in a row)

Is there a cheaper way of getting the needed results?

Order History is nice in that it is there for free. It is unreliable in the sense that the order of entries is mysterious (by actual test) and it can get truncated if you happen to change the setting in the account history tab of the terminal. I wrote a script to print out trade results and just sorted it by close time (having only entered relevant trades by magic number). But I run that script manually and have to set up the account history length beforehand. Since you can't reliably keep a local RAM copy of the trades due to system re-starts, and changing timeframes re-running init(), it seems like you own text file is the way to go. This at least limits the computational work.