• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Ignoring repeating results

When I get data from last raw of table every second and new records in this table create in irregular interval, I need to ignore repeated value or make it null.
 
String s;
                Timestamp ts;
                String bot_buy;

                s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx();
                bb = Double.parseDouble(bot_buy);

                if (s.equals("EUR/USD"))
                {

                    symbol[0] = s;
                    buy_px[0] = bb;
                    ts_buy[0] = ts;

                    System.out.println("   Symbol  " + symbol[0] +
                                       "    ts_buy[0]    " + ts_buy[0] +
                                       "   buy_px[0]  " + buy_px[0]);
...................

   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145
   Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:24:37.147   buy_px[0]  1.35145

Open in new window

I'll use this data in dataset to build bar chart on the top of the line chart.
Thank you
0
gbcbr
Asked:
gbcbr
  • 10
  • 10
1 Solution
 
for_yanCommented:


So when you receive new string of data why can't you compare it to the latest string and if it equals, hjut igniore it and not process it ?

0
 
gbcbrAuthor Commented:
Hi for_yan, long time don't hear you.
First of all I don't have idea about dataset for chart creation, is it possible don't have any data at the moment of chart creation, because line chart will be created with one second interval and always will be feeds with data.
So, it should be two datasets, one of them most the time will be empty, I fear that I'll get NPE.
And second, I don't know how to compare last value with previous and take ant decision: ignore, make it zero etc.
Please advice


0
 
for_yanCommented:
lets think you are recieveing theses string and processing them in the loop

so you do it this way:

String s1 = null;

while(.....) {

String s0 = getString(0;
if(s0.equals(s1))continue;
s1 = s0;

//go aheadr and process and store...



}

Dion't understadn about the chart - that's next issue
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gbcbrAuthor Commented:
the question what to put into while (....)?
0
 
for_yanCommented:
While or not while you should get these strings from somehwere and see them arriving in a lop or somehwere - how otherwise you arev oroicessing thgem
0
 
gbcbrAuthor Commented:
Sorry, but I do not understand this construction.

I have:
String bot_buy;
bot_buy = data.getBuyPx();

so, I get String value from the table.
Now I have to compare it with previous value of bot_buy = data.getBuyPx() and only after decide, what to do with it.

I don't understand how to separate values of this variable to previous and actual.
0
 
for_yanCommented:


Is there a statement in you program where this new string first arrives:

Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349

Say you should have a method where you get this String
something like  String s = getNewData()
or something where this information first
comes to your program.
Please post this line and a few lines around it.
0
 
gbcbrAuthor Commented:
String s;
                Timestamp ts;
                String bot_buy;
               
                s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx(); 
                bb = Double.parseDouble(bot_buy);

                if (s.equals("EUR/USD"))
                {

                    symbol[0] = s;
                    buy_px[0] = bb;
                    ts_buy[0] = ts;

                    System.out.println("   Symbol   " + symbol[0] +
                                       "   ts_buy[0]    " + ts_buy[0] +
                                       "   buy_px[0]   " + buy_px[0]);

Open in new window

0
 
gbcbrAuthor Commented:
Sorry
 
public void run() {

        try {


            List<DataBuy> dataList =
                conn.selectBuy("SELECT * FROM EURUSD_BOT_BUY where ts = ( select max( ts ) from EURUSD_BOT_BUY ) and rownum = 1 union all " +
                               "SELECT * FROM EURCHF_BOT_BUY where ts = ( select max( ts ) from EURCHF_BOT_BUY ) and rownum = 1 "); //union all " +


                String s;
                Timestamp ts;
                String bot_buy;
               
                s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx(); 
                bb = Double.parseDouble(bot_buy);

                if (s.equals("EUR/USD"))
                {

                    symbol[0] = s;
                    buy_px[0] = bb;
                    ts_buy[0] = ts;

                    System.out.println("   Symbol   " + symbol[0] +
                                       "   ts_buy[0]    " + ts_buy[0] +
                                       "   buy_px[0]   " + buy_px[0]);

Open in new window

0
 
for_yanCommented:

So, as I understand from some method you get back the List called dataList

Don't see that you use this list anywhere  below in your snippet.

What is this list - is it the list of strings like this:
  Symbol  EUR/USD    ts_buy[0]    2011-10-07 17:00:27.22   buy_px[0]  1.349

and you subsequently parse these lines?

None of that is seen in the snippet above - what is dataList and how you use it?
Please, elaborate.
0
 
gbcbrAuthor Commented:
I get data from tables and put it into arrays

 
public class DataBuy {

    private String symbol;
    private Timestamp ts;
    private String bot_buy;


    public DataBuy(String symbol, Timestamp ts, String bot_buy) {

        this.symbol = symbol;
        this.ts = ts;
        this.bot_buy = bot_buy;
    }

    public String getSymbol() {
        return symbol;
    }

    public Timestamp getTs() {
        return ts;
    }

    public String getBuyPx() {
        return bot_buy;
    }

Open in new window

System.out.println("   Symbol  >>>>" + s + "<<<<   buy_px  " +
                                   bot_buy + "   bb   " + bb);
............

Symbol  >>>>EUR/USD<<<<   buy_px  1.34745   bb   1.34745

Open in new window

public SelectData() throws SQLException, ClassNotFoundException,
                               IllegalAccessException, InstantiationException,
                               Exception {

        conn = new ConnectDB();
    }

    String[] symbol = new String[25];
    Timestamp[] ts_buy = new Timestamp[25];
    double[] buy_px = new double[25];
    double bb;

public void run() {

        try {..............

Open in new window

0
 
for_yanCommented:
I am sorry, I cannot understand, please, look at your code attentively and post
the place where you get new portion of data.

You posted this:

 List<DataBuy> dataList =
                conn.selectBuy("SELECT * FROM EURUSD_BOT_BUY where ts = ( select max( ts ) from EURUSD_BOT_BUY ) and rownum = 1 union all " +
                               "SELECT * FROM EURCHF_BOT_BUY where ts = ( select max( ts ) from EURCHF_BOT_BUY ) and rownum = 1 "); //union all " +

seems to be reasonable place where you get new data,
but your subsequent code does not show that you process this data which you got, because
dataList is not mentioned there.

What are the elements of this dataList which you received through this method?
Do you iterate through those elements? If so, show the code where you are doing it?

The relevance of your most recent post is not quite clear, as I'm asking at what point you are receiving new data -
that is the point where we should be able to analyze if the new data is identical to the previously
received data and ignore it if it is.
I still don't see this place.






0
 
gbcbrAuthor Commented:
This data called by Timer from the Main class
 
public static void callSelect() throws Exception {
        try {
            timer = new Timer();
            timer.schedule(new SelectData(), 10000, 1000);

Open in new window

after, from ConnectDB, class DataBuy with all elements you see above
 
public List<DataBuy> selectBuy(String sql) {
        List<DataBuy> dataBuy = new ArrayList<DataBuy>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = pool.checkout();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {

                String s = rs.getString("SYMBOL");
                Timestamp ts = rs.getTimestamp("TS");
                String bot_buy = rs.getString("BUY_PX");

                dataBuy.add(new DataBuy(s, ts, bot_buy));
            }

Open in new window

and finally execute this query every second
public SelectData() throws SQLException, ClassNotFoundException,
                               IllegalAccessException, InstantiationException,
                               Exception {

        conn = new ConnectDB();
    }

    String[] symbol = new String[25];
    Timestamp[] ts_buy = new Timestamp[25];
    double[] buy_px = new double[25];
    double bb;

public void run() {

        try {

            List<DataBuy> dataList =
                conn.selectBuy("SELECT * FROM EURUSD_BOT_BUY where ts = ( select max( ts ) from EURUSD_BOT_BUY ) and rownum = 1 union all " +
                               "SELECT * FROM EURCHF_BOT_BUY where ts = ( select max( ts ) from EURCHF_BOT_BUY ) and rownum = 1 "); //union all " +


                String s;
                Timestamp ts;
                String bot_buy;
               
                s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx(); 
                bb = Double.parseDouble(bot_buy);

                if (s.equals("EUR/USD"))
                {

                    symbol[0] = s;
                    buy_px[0] = bb;
                    ts_buy[0] = ts;

                    System.out.println("   Symbol   " + symbol[0] +
                                       "   ts_buy[0]    " + ts_buy[0] +
                                       "   buy_px[0]   " + buy_px[0]);

Open in new window

0
 
for_yanCommented:
You are still missing the piece here

  List<DataBuy> dataList =
                conn.selectBuy(...);




s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx();
                bb = Double.parseDouble(bot_buy);




what is "data", when we just retireved "dataList" ?

How do we get data form dataList ?

You should iterate through dataList to get to each "data"


Additional question - are all of these pieces in one class?






0
 
gbcbrAuthor Commented:
Sorry, this happened because of my selective copying from the big method run, hope this is complete snippet:

 
try {

            List<DataBuy> dataList =
                conn.selectBuy("SELECT * FROM EURUSD_BOT_BUY where ts = ( select max( ts ) from EURUSD_BOT_BUY ) and rownum = 1 union all " +
                               "SELECT * FROM EURCHF_BOT_BUY where ts = ( select max( ts ) from EURCHF_BOT_BUY ) and rownum = 1 "); //union all " +

for (DataBuy data : dataList) {

 String s;
                Timestamp ts;
                String bot_buy;
               
                s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx(); 
                bb = Double.parseDouble(bot_buy);

                if (s.equals("EUR/USD"))
                {

                    symbol[0] = s;
                    buy_px[0] = bb;
                    ts_buy[0] = ts;

                    System.out.println("   Symbol   " + symbol[0] +
                                       "   ts_buy[0]    " + ts_buy[0] +
                                       "   buy_px[0]   " + buy_px[0]);

Open in new window

0
 
for_yanCommented:

Taking into account that you run your query with one second interval I think it cannot happen that the data
in the next query will be identical with the data in the previous query - then you can do it like below:

If there is such possibility, then you want to declare s0, ts0, bot_buy0 as instance variables rather than
inside the method as I did. Other than that it would be the same.

One other alternative is to define method equals() the definition of the DataBuy method, then you'll not need to compare
all three of them as I made below - but in fact it does not matter much where you caompare them.

try {
    
String s0 = null;
Timestamp ts0 = null;
String bot_buy0 = null;

            List<DataBuy> dataList =
                conn.selectBuy("SELECT * FROM EURUSD_BOT_BUY where ts = ( select max( ts ) from EURUSD_BOT_BUY ) and rownum = 1 union all " +
                               "SELECT * FROM EURCHF_BOT_BUY where ts = ( select max( ts ) from EURCHF_BOT_BUY ) and rownum = 1 "); //union all " +

for (DataBuy data : dataList) {

 String s;
                Timestamp ts;
                String bot_buy;
               
                s = data.getSymbol();
                ts = data.getTs();
                bot_buy = data.getBuyPx();
      if(s.equals(s0) && ts.equals(ts0) && bot_buy.equlas(bot_buy0))continue;
         s0 = s;
        t0 = ts;
        bot_buy0 = bot_buy;

 
                bb = Double.parseDouble(bot_buy);

                if (s.equals("EUR/USD"))
                {

                    symbol[0] = s;
                    buy_px[0] = bb;
                    ts_buy[0] = ts;

                    System.out.println("   Symbol   " + symbol[0] +
                                       "   ts_buy[0]    " + ts_buy[0] +
                                       "   buy_px[0]   " + buy_px[0]);

Open in new window

0
 
gbcbrAuthor Commented:
This data most time is identical, because it's table which collect ONLY trading signals from the bot and bot generates this signal normally once at 1 or more minutes.
I'll try your code tomorrow when it will start new week data session and let you know.
Anyway, thank you for your attention for this question.
0
 
for_yanCommented:
So, if you want to compare across different queries, that initialize these variables as instance variables
immediately under the class declaration - that's why I was asking if all of this is happening within the same class - so I assume that it does.
 
0
 
for_yanCommented:

correction:
So, if you want to compare across different queries, then initialize these variables as instance variables
immediately under the class declaration - that's why I was asking if all of this is happening within the same class - so I assume that it does.
 
0
 
gbcbrAuthor Commented:
Excellent and clear solution!
Just small remark, we don't need to check symbol "s", otherwise it will never get result. I left it as:
                    if (ts.equals(ts0) &&
                        bot_buy.equals(bot_buy0))
                    continue;
Thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now