how to get balance in different day

Created at 17 May 2023, 18:44
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
IR

IRCtrader

Joined 17.06.2021

how to get balance in different day
17 May 2023, 18:44


i use below code to get first balance of day 

i dont know how to get first balance of week,month or first balance of account after deposit

var oldTrades = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).ToArray();
            double StartingBalance;

            if (oldTrades.Length == 0)
            {
                StartingBalance = History.Count == 0 ? Account.Balance : History.OrderBy(x => x.ClosingTime).First().Balance;
            }
            else
            {
                StartingBalance = oldTrades.Last().Balance;
            }

 


@IRCtrader
Replies

firemyst
26 May 2023, 03:41

Getting the first balance of the week or month should be relatively straight forward. A "Brute force" way of doing it would be:

1) have a timer job that runs every x-hours (need a timer in case you're watching a market on the weekend that isn't open and no tick data is coming through to trigger either the "Calculate" or "OnTick" meth event handlers)

2) In the timer event, check that Time.DayOfWeek == DayOfWeek.Sunday. If so, grab the account balance and save it for use however you need to (eg, into a file, into a global variable, or whatever)

 

Another option is to have your code running 24/7, and every so often check if Time.DayOfWeek == DayOfWeek.Sunday. If so, you know it's the first day of the week, and grab the balance.

 

To do the beginning of the month, it's the same principle, except the comparison changes a bit, to something along the lines of:

            if (Time.Day == 1)
            {

            }

 


@firemyst