How to find and calculate the last 4 row from History

Created at 18 Nov 2021, 07:25
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!
PR

progy85

Joined 07.01.2021 Blocked

How to find and calculate the last 4 row from History
18 Nov 2021, 07:25


hi,

I want to calculate the last 4 row which is lost:

NetProfit:
50
100
-30
-50
20
-50
-70
-80
-100

74
-65
How can I get the last 4 NetProfit: -50-70-80-100=-300 

 

 var netProfit = History.Where(trade => trade.SymbolName == Symbol.Name).OrderByDescending(x => x.EntryTime).Take(4).Sum(trade => trade.NetProfit);


Thank you.


Replies

amusleh
18 Nov 2021, 08:40

Hi,

To get the last four values you can use OrderByDescending and Take, but on your posted sequence the values you are looking to get aren't last four.

You can first skip the last 2 values and then use Take to get the last 4 values:

var result = History.Where(trade => trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase)).OrderByDescending(trade => trade.EntryTime).Skip(2).Take(4).Sum(trade => trade.NetProfit);

 


@amusleh

progy85
18 Nov 2021, 08:56

RE:

amusleh said:

Hi,

To get the last four values you can use OrderByDescending and Take, but on your posted sequence the values you are looking to get aren't last four.

You can first skip the last 2 values and then use Take to get the last 4 values:

var result = History.Where(trade => trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase)).OrderByDescending(trade => trade.EntryTime).Skip(2).Take(4).Sum(trade => trade.NetProfit);

 

Hi,

problem is because I need to skip winnings and find last 4 lost.

If I have
win 50
win 100
win 30
lost 15
lost 13
lost 17
win 15
lost 8
lost 12
lost 15
lost 20

win 13

My results must be: 8+12+15+20= 55

Also is ok if use Count. What I need to do Is check if last four trade in a row are lost:
win

lost
win
win
win
lost
lost
lost
win
win
lost
lost
lost
lost

win

I hope you will help me. Thanks!
 


amusleh
18 Nov 2021, 10:03

Hi,

To get last four lost trades net profit you can filter the lost trades first then order them, after that take the last four:

var result = History.Where(trade => trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) && trade.NetProfit < 0).OrderByDescending(trade => trade.EntryTime).Take(4).Sum(trade => trade.NetProfit);

 


@amusleh

progy85
18 Nov 2021, 10:06

RE:

amusleh said:

Hi,

To get last four lost trades net profit you can filter the lost trades first then order them, after that take the last four:

var result = History.Where(trade => trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) && trade.NetProfit < 0).OrderByDescending(trade => trade.EntryTime).Take(4).Sum(trade => trade.NetProfit);

 

thanks!!!