Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:17:17+00:00 2026-06-06T05:17:17+00:00

I have the data below saved as a pandas dataframe . With this data,

  • 0

I have the data below saved as a pandas dataframe. With this data, I would like to calculate the bid ask spread for a specific second. However, as you can see there are many times more asks than bids and vice versa. So my goal is to do the following: I would like to take only data such that it is a bid followed by an ask, or an ask followed by the bid for the same timestamp and then calculate the spread and how many spreads there were.

In the below data, it would look like the following, I would take row 1 and row 2, and calculate the spread which is 0. Then I would take row 3 and row 4 and have a spread of 2.

        time quote   price  volume
0   07:00:00     B  3950.5       5
1   07:00:00     B  3950.0       4
2   07:00:00     A  3950.0       7
3   07:00:00     B  3948.0      17
4   07:00:00     A  3950.0      20
5   07:00:00     A  3950.0      31
6   07:00:00     A  3950.0      44
7   07:00:00     A  3950.0      57
8   07:00:00     A  3950.0      67
9   07:00:00     A  3950.0      57
10  07:00:00     A  3950.0      67
11  07:00:00     A  3950.0      80
12  07:00:00     A  3950.0      90
13  07:00:00     A  3950.0      99
14  07:00:01     B  3948.0      15
15  07:00:01     A  3950.0      89
16  07:00:01     A  3949.5       1
17  07:00:02     A  3950.0      89
18  07:00:03     B  3948.0      12
19  07:00:03     A  3949.0       1
20  07:00:03     B  3948.0       9
21  07:00:03     B  3948.5       4
22  07:00:04     A  3949.5       5
23  07:00:04     B  3948.5       2
24  07:00:05     B  3948.5       1

This is my desired output:

       time spread num_spread
   07:00:00      2          2 
   07:00:01      2          1  
   07:00:03      1          1
   07:00:04      1          1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:17:19+00:00Added an answer on June 6, 2026 at 5:17 am
    with open('/tmp/ba.data') as dataF:
        oldk, oldsub = None, None
        for key, subi in groupby(map(str.split,dataF), lambda x: (x[1],x[2])):
            if oldk == None:
                oldk, oldsub = key, list(subi)
            else:       
                newsub = list(subi)
                print ' '.join(oldk), '->', ' '.join(key), float(oldsub[-1][3])-float(newsub[0][3])
                oldk, oldsub = None, None
    

    gets this

    07:00:00 B -> 07:00:00 A 0.0
    07:00:00 B -> 07:00:00 A -2.0
    07:00:01 B -> 07:00:01 A -2.0
    07:00:02 A -> 07:00:03 B 2.0
    07:00:03 A -> 07:00:03 B 1.0
    07:00:04 A -> 07:00:04 B 1.0
    

    if you change

    if oldk == None:
    

    to

    if oldk == None or oldk[0] != key[0]:
    

    you`ll get

    07:00:00 B -> 07:00:00 A 0.0
    07:00:00 B -> 07:00:00 A -2.0
    07:00:01 B -> 07:00:01 A -2.0
    07:00:03 B -> 07:00:03 A -1.0
    07:00:04 A -> 07:00:04 B 1.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data that looks like this . And my code below simply
I have data like below AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE Now there is a
I have a table that has data like below(sorted by date) COL_A | COL_B
I have a internal website that users log into. This data is saved as
i have data below from streaming: 'ID|20120206|080500|0000001|0|1|record1|END' 'ID|20120206|080500|0000002|0|1|record2|END' 'ID|20120206|080500|0000003|0|1|record3|END' and i want to process
How can I order by dash? For instance, I have these data below, page_id
I have data stored as below in an MS Access database: Date User 20090101
I have a data structure which is as given below: class File { public
I have my entities as below. My data model enforces below and I cannot
I have the method below to parse data from the twitter search, strange thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.