Link to home
Start Free TrialLog in
Avatar of gbcbr
gbcbrFlag for Cyprus

asked on

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
Avatar of for_yan
for_yan
Flag of United States of America image



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 ?

Avatar of gbcbr

ASKER

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


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
Avatar of gbcbr

ASKER

the question what to put into while (....)?
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
Avatar of gbcbr

ASKER

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.


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.
Avatar of gbcbr

ASKER

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

Avatar of gbcbr

ASKER

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


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.
Avatar of gbcbr

ASKER

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

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.






Avatar of gbcbr

ASKER

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

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?






Avatar of gbcbr

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gbcbr

ASKER

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.
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.
 

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.
 
Avatar of gbcbr

ASKER

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