Not logged in

Forums

Viewing 6 posts - 1 through 6 (of 6 total)

Forums>EA Wizard>Application Support>Total OPEN P/L across ALL currency Pairs.

  • #197786 |
    Participant
    20 Posts

    Hi all,

    Can someone please help me with a custome function that returns the Total in money of all OPEN orders across ALL currency pairs i.e – across all charts?

    Thanks in advance

    Kind Regards
    Steve

    #197825
    Administrator
    348 Posts

    Hello Steven,

    sure, I will post the function

    #197826
    Participant
    20 Posts

    Hi Tomas,

    I’m not to sure where the problem is in the 3 custome functions I’ve coded so I’ve pasted them all below. I suppose it could be in any of the 3.

    In the tester in Mt4 it works fine but when I run on a demo account it adds the account balance in to the total sum.

    The three custome function code is below:

    — I initialize this variable like the below
    lv_NumOrdersToday = GetAllOrdersOpenedToday(0, “false”);

    int GetAllOrdersOpenedToday(int direction, string includePending) {
    string todayTime = TimeToStr( TimeCurrent(), TIME_DATE);
    int tradesOpenedToday = 0;

    for(int i=0;i<OrdersHistoryTotal();i++) {
    if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) {

    if(direction == 1) {
    if(OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
    // skip short orders
    continue;
    }
    } else if(direction == -1) {
    if(OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP) {
    // skip long orders
    continue;
    }
    }

    if(includePending == “false”) {
    if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
    // skip pending orders
    continue;
    }
    }

    if(TimeToStr( OrderOpenTime(), TIME_DATE) == todayTime) {
    tradesOpenedToday++;
    }
    }
    }

    for (int cc = OrdersTotal() – 1; cc >= 0; cc–) {
    if (OrderSelect(cc, SELECT_BY_POS)) {

    if(direction == 1) {
    if(OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
    // skip short orders
    continue;
    }
    } else if(direction == -1) {
    if(OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP) {
    // skip long orders
    continue;
    }
    }

    if(includePending == “false”) {
    if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
    // skip pending orders
    continue;
    }
    }

    if(TimeToStr( OrderOpenTime(), TIME_DATE) == todayTime) {
    tradesOpenedToday++;
    }
    }
    }

    return(tradesOpenedToday);
    }

    — passed into this function is lv_NumOrdersToday from the above function
    double GetAllTotalClosedPLInMoney(int orderMagicNumber, int numberOfLastOrders) {
    double pl = 0;
    int count = 0;

    for(int i=OrdersHistoryTotal(); i>=0; i–) {
    if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) {
    if(orderMagicNumber == 0 || OrderMagicNumber() == orderMagicNumber) {
    count++;
    pl = pl + OrderProfit();

    if(count >= numberOfLastOrders) break;
    }
    }
    }

    return(pl);
    }

    double GetALLOpenPLInMoney(int orderMagicNumber) {
    double pl = 0;

    for(int cnt = 0; cnt < OrdersTotal(); cnt ++)
    {
    if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
    if(orderMagicNumber == 0 || OrderMagicNumber() == orderMagicNumber) continue;

    pl += OrderProfit();
    }

    return(pl);
    }

    The functionality I’m trying to build is to add the 2 functions ((GetALLOpenPLInMoney(0) + GetAllTotalClosedPLInMoney(0, lv_NumOrdersToday)) results together and see if it is above or below a predetermined capped Profit or Loss daily amount and if so then I suspend trading on all currency pairs.

    Once again when I run in MT4 tester it works fine but when I run on a demo account it includes the account balance into the sum for an unknown reason.

    i.e. 5023 instead of 23 on a 5000 account.

    Thanking you

    Kind Regards
    Steve

    #197897
    Administrator
    348 Posts

    Hello,

    please check your mailbox. I have sent you a reply on the email sent to us.

    #200400
    Participant
    20 Posts

    Hi Tomas,

    This was the problem. An undocumented feature and as I assumed the way the data is stored. See link: https://www.mql5.com/en/forum/126192

    From the link an extract below:
    OrdersHistoryTotal()’s name is a bit misleading, cause it also includes balance and credit statements. So this method might be potentially unreliable… A better solution would be to loop on orders in history and count (filtering out balance and credit statements).

     

    FYI, although not officially documented, these are the OrderType() of balance/credit statements when they are selected:

     

    ‘Balance’ statement: OrderType()==6
    ‘Credit’ statement: OrderType()==7

     

    I’ve made changes accordingly and all is working as designed.

     

    Just to let you know.

    Kind Regards
    Steve

    #200732
    Administrator
    348 Posts

    Hello,

    glad it works for you now. I am check the open P/L function now you sent on our email

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.