MT4 calculate all position asymbol, buy position,Sell posiion, Calculate today profit and loss, Calculate weekly profit and loss

 // all position

int countmaxpos()

{

int max=0;

for(int i=OrdersTotal()-1;i>0;i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

string currencypair=Symbol();

if(OrderSymbol()==currencypair)

{

max=max+1;

}

}

return max;

}



///buy position


int countbuypos()

{

int nobuy=0;

for(int i=OrdersTotal()-1;i>0;i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

string currencypair=Symbol();


if(OrderSymbol()==currencypair&&OrderType()==OP_BUY)

{

nobuy=nobuy+1;

}

}

return nobuy;

}

///Sell posiion

int countsellpos()

{

int nosell=0;

for(int i=OrdersTotal()-1;i>0;i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

string currencypair=Symbol();


if(OrderSymbol()==currencypair&&OrderType()==OP_SELL)

{

nosell=nosell+1;

}

}

return nosell;

}


//today pnl 



double CalculateTodayPnL()

{

    double todayPnL = 0.0;

    datetime todayStart = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));


    int totalOrders = OrdersHistoryTotal();

    for (int i = totalOrders - 1; i >= 0; i--)

    {

        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

        {

            if (OrderCloseTime() >= todayStart)

            {

                double orderPnL = OrderProfit();

                todayPnL += orderPnL;

            }

        }

        else

        {

            Print("Error selecting historical order. Error code: ", GetLastError());

        }

    }


    return todayPnL;

}

//Calculate weekly profit and loss


double CalculateWeeklyPnL()

{

    double weeklyPnL = 0.0;

    datetime nearestMonday = TimeCurrent() - ((TimeDayOfWeek(TimeCurrent()) - 1) * PeriodSeconds(PERIOD_D1));


    // If it's Monday, return 0

    if (TimeDayOfWeek(TimeCurrent()) == 1)

    {

        return 0.0;

    }


    datetime todayStart = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));

    datetime previousDayEnd = todayStart - PeriodSeconds(1);


    int totalOrders = OrdersHistoryTotal();

    for (int i = totalOrders - 1; i >= 0; i--)

    {

        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

        {

            if (OrderCloseTime() >= nearestMonday && OrderCloseTime() < todayStart)

            {

                double orderPnL = OrderProfit();

                weeklyPnL += orderPnL;

            }

        }

        else

        {

            Print("Error selecting historical order. Error code: ", GetLastError());

        }

    }


    return weeklyPnL;

}









Comments

Popular posts from this blog

MQL5 : Add time to current time in mins

MQL5: Closed order detail