As soon trade signal created, it generate request to server about all opened positions, I filter them by pairs, so in this class we get only positionReports about EURUSD. As soon they counted here final result compared with defined limit and send for execution or block any actions.

Now problem increase, in starts count same positions without reseting, so I see some times double and even triple numbers of positions. Maybe it's better to count them by OrderID, so we sure that this positions counted only once.

Maybe it would be an option not to display it immediately when you receive it, but
just store in one variable the value which you receive in another the last time
when you recieve it. The next time you receive new data before changing these variables, you compare the dates,
if the date is too soon, just simply replace the vlues, if the date is sufficiently later, only then
actually display. Well, then you'll display only after a little delay, when you knoe that it was not changing
for some time. On the other hand as objects put it above, anyway there is no other way that you would know that
this is the value you want to display, as the only difference between all other values
is that this one sits for a longer time

Displaying is the last question, the problem is in confusing bot, because it supplies number directly to formula.
Displaying is only for my visual control, how many positions opened for each pair.
But the main problem that counting is the current process. This I agree with you and I try to do it, we need some intermediary variable which will keep maximum counted number of positions and use it for calculations and displaying.
Now this class looks like this:

The problem with proper counting, that even filtered by pairs these messages come permanently, end even I put gap interval 2L, it still count double.
Please advice how to filter messages per unique ID, so we have as result only number of messages with unique ID, and all duplicates will be ignored.

Waht is unique ID - ?
Do you mean that your values are also accomanied bu ID, and you are looking for the moment when ID changes, and want
to show last message with the previous ID-?

If so, then you can treat ID in the same way as we were talking about dates before - keep track of the previous value of ID,
and when you see that it changed and subsequent stufgfg will be getting another ID - from that moment
start displayien this previous value until you percieve the next change.

in this case - mOrderID=40734889 is the unique number, no one else PositionReport has the same number, because it's number of open position. But if server send it few times, it will be counted as new incoming report and added to sum.
The question is to count only once and ignore all duplicates which have the same mOrderID number.

That is still difficul to understand - if this is so store this orderID in the varaible,
and then check - if the enxt one has the same OrderId just done't count it, and count only if it is
different from your stored value, then when you count store new value and wait till
a new OrderID arrives again.

That depends when a hwo you want to use them - if you are going to use them within
the same program - I'd add them to the ArrayList and then use later say for report, etc.
If you want them to persiste after the program exit and till it start again, if not in the databse,
then you want to write them to the file either at every step or before you exit form the program
and then read them back at the start of the program

So it send you this line every second - and it has only open positions?
So you need to count how manty difdferent orderid's you have?
Does it send you at any time information that sertain position (orderId) is closed?

3. Than, in this class we have to find from these filtered by pair bulked messages real number of open positions. Because< as I told before it's a limitation rule for opening only defined QTY for each pair. If number of open positions even to limit, all trade orders ignored. If it's possible to solve this problem by filtering unique ID numbers, it will be easiest way.

4. If not, we have to make DB where to record all open positions and all closed positions and get result from there.
Of course it much accurate method, but will need additional time to create separate counting system, because it has to work independent from trade system, otherwise, when I restart trading app and I have open positions, I will lose all closing reports and some parts of already closed positions will remain permanently and look as open.
If we can't do it in simple way, I will do it.

If you want to maintain information when your application is closed, you cannot get away without either database or file.

Within ath application what I would do, I would mainatin the ArrayList of orderId's.
As soon as you get the message that position is opened you add the ID to this arraylist
if(!arralist.contains(orderId))arraylst.add(orderId);

If you receive a message that position is closed - you remove this ID from the list.
So at any moment you'll have the list of all ID's opened, and the arraylisrt.size()
will give you at any time the count how many ID's are opened.

That's nice, because I don't need to persist this useless information. As I understood, even I restart app, I'l create new array with open positions and as soon they close, they will be deducted from the array.
This what I need exactly.

It has problem with this API, you can't mix these two reports in one method, I tried and I have error, you can't use pretrade and posttrade massages.
So, now I create two separated methods, to extract orderID, I think we can manage these variables without above mentioned restrictions.
But I didn't understand
>>you want two arrays - just one arraylist
how we create array list if we don't have any array?

You first say ArrayList al= new ArrayList();
Then, as soon as your data arrives you add it to your ArrayList:
al.add(object)

If you store say Strings or any other objects you just say al.add(s);
If you want to store double or int you need to wrap them into Object like new Integer(int) or new Double(double) and then add wrapped object.
ArrayList can expand as you like, and you do not need to allocate space to it on creation. Ot is in general much more convenient than array - I use arrays only in very special cases.

My previoius remark about going to arraylist and maintaining one arraylist has nothing to do with your response. No one could disallow us to do it.

I'm already extract ID numbers for easy reading and sorting.
So, first we need to write somewhere these number and count them.
2. After trade, which close position, I get two messages:
- one is PositionReport with ID traded position and
- second is ClosePositionReport which show ID of closed positions, so the similar;

I think remove(String orderid) should work to remove those which are closed

To count the number of iopen is simple:
whwn you add, you first check that this order does not already exists in the array list
if(!al.contains(orderid))al.add(orderId)
thereofr you'll have each orderid only once there
then al.size() will give you the number of currently open positions at every momemnt

What is this method - and why you feed both open and close orderiD into it?
Does not seem logical.
And why do you create ArrayList insiide it?
Makes not much sense.

How I see it - you have process which sends you messages - some of them conatin infgormation about openeing
others - about closing.

So you create ArrayList in the very beginning - before you start handiling these messages.

Then you receive the message and see if it is mesage abbout opening, or mayeb about say price of the
position which you are holding - you check idf it already exists in your arrayList you just skip it.
If it is a message about closing you remove it.

But you do not have any coupling between opening ID's and closed Id's within one method, and of course you do not create your ArrayList every time

I make stupid mistake about placing ArrayList inside the method.
But when I place it at the class beginning it counts very well, only the problem with deleting closed positions. But adding very accurate.
I don't understand this
>>When you receive the message
I have to receive message into some method and I can't receive these two messages in one method, as I told before, this is the reason why I create two separated methods, get data from both methods, create third method which collect data from both messages.
If I just create ArrayList from one type of messages, I will not be able to use in this ArrayList data from another type of message. They are completely different types, like String and double. This is the reason why I created variables independent from type of messages.
Please help to delete closed positions from this ArrayList and this question will be solved

Well, don't understadn what you mean.
In my mind you should have one ArrayList - it should be class level instance variable and
at each moment it should contain currently open positions.
So at the moment when you receive the message about opening you add this ID to the list.
At the moment when you receive a message about closing - either in the msae method or in idfferent method -
as your list is instance-wide variable - it does not matter - at this moment you remove this
ID from rthe list.
Even if these mmessages arrive to you in the methods of different class it is still doable by passing the
ArrayList between the classes, but I think you have it within one class, but in different methods - so that
should be very strightforrward. You don't need even special method for any
of that - just a line of code in the beginning for creating ArrayList,
one line of code at the place where you receive message about opening and another where you receive
message about closing. And then at any place where you want to display the totla number just use size()
method of the ArrayList

You now declared ArrayList al in the rifght place as instance variable.
I would rather create it in constructor - not in the middle of nowhere like you do - don't understand how that works.

Then answer the following questions (I don't know the flow of your application - I cannot do it myself):
Is there a place where you receive the message that the position Order_ID is opened (forget about counting it)?
(say, where you receive some string or parse some received string, which contains this information)
as soon as you receive such notification and Order_ID beciomes known to you add line
al.add(Order_ID);

Is there a place where you receive the mssage that the position Order_ID is closed (forget about counting it)?
at this pioint add line:

al.remove(Order_ID);

Is there a place where you want to display the total number of opend Orders?
At this point add line:

System.out.println("Number of opened positions: " + al.size());
or setText on the text box or label or whatever

>>Is there a place where you receive the message that the position Order_ID
yes, but to see it I have to extract it, this I'm doing by
open_ordID = aPositionReport.getOrderID();
because message full of different data.
So, it has to be like this:

Well, but how does it contradict with what I wrote?
You pick up hundred kilos of stuff - underneath you undig your jewell - no matter
how you got to it - there it is now - so after you got hold of it - so add it to your ArrayList

I can't insert into your code - I don't understand what your code is doing.

Is your code only dedicated to counting the number of open positions?
Or it is supposed to do some other stuff? Or as I had assumed, it was showing something
on the screen, implementing trades, etc. ?
In any case I don't see the reason of having so many counter methods.

This is the general scheme:

You maintain the ArrayList, the elements of which at any given moment will be the OrderId's of open positions.

You declare this ArrayList as instabnce variable.

At the moment when your class is instantiated (usually in constructor)
you also create this ArrayList - it will be empty at this moment.

My assumption that somewhere in your class there is a method
which is called with some argument which in some way (perhaps encapsulated in some text)
has the orderId of the opened position.

I also believe that somewhere in your class (perhaps in the same method or in another method)
you receive another another string(text) containing information of the closing position and this
information refers to the order_id of the opening of this position.
If this assumption is not true, then we need some other way to know
which of the opened positions is closed, but for now this is my assumpltion.

With these assumtions when you receieve and extract information about openieng
position you add element order_id to your array list.
When you receive information about closing of the position (which contains reference to the
openeing orderid for this position) then you remove the string order_id from your arraylist.

With these two condditions your ArrayList at any moment of its life will contain
only ID's of those positions which are opened.
If you count the size of the ArrayList (just use its size() method), you'll
always have the mnumer of open positions.

Don't write - I am confused - this statement contains zero information - rather
explain what in particular you do not understand.

>>I can't insert into your code
I write that I'm confused because in my imagination I did exactly what you say above.
>>you also create this ArrayList - it will be empty at this moment.
1. create new ArrayList in constructor;

I don't ask you to write some specific code for my app.
Logically this code has to remove records.
a. it gets close message;
b. ID identified;
c. we can see in outprint that all necessary values arrive to this method for deleting record, but it doesn't.

I ask only to correct the part responsible for removing old data only.

I don't understand what you mean by "abstract" - you
should know if it is opening position message or closing position message - and this is
a crucial part of information - how can you count anything without it?

Let's go from the beginning:
1. It's two types of messages (Open) PositionReport which I get after opening position and it comes periodically for some calls until this position will be closed and I can't get them in one method:

So, this is crazy stream which has information about all closed positions and if we just use any ClosePositionReport for deleting records it will be big mess. This is why I try to define which exactly number of position has to be deleted.

Well, that still makes not much sense - why they should come in pairs - close and open togrther - and who gurantees that they come
simultaneously, and what that another message about open has to do with the closing.
And if the opening message didn't arrive, but only the closing message - should not you still
remove it?

I would look only for waht contains critical information.

Whatever is opened should trigger adding, wherever you receive this message.
If you happen to receive more than one message about
openeing the same order_id
then, as we discussed below, check if arrayList already containes this order_id and add only if
it does not contain such order_id - no matter how many times you'll receive opening messge
- your ArrayList in this case will contain this order_id only once.

About removing - even that precaution is unnecessary - once you receive closing position message - remove
corresponding order_id; even if you receve two closing messages for one number -
second removal of the same order_id will not hurt - there is no reason to check - just remove.
Why to wait for another opening message when you need to remove is not understandable to me.

Once again, no sense to explain it to me - just add printouts and see
what is happening and why it does not remove.

Introduction
This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…