Ethereum: I am streaming binance data in python but the prices are rounded down when inserted into my mariadb db

Optimizing Ethereum prices in Maridb: Preventing rounding

Introduction

In this article, we will explore why the prices of your Ethereum are being rounded when inserted in the Mariadb database. Then we will provide a solution to avoid this and will ensure the accurate storage of the data.

The problem

Let’s take a look at some sample data for demonstration purposes:

`Json

{

“Timestamp”: “2022-02-16t14: 30: 00.000z”,

“Priceusd”: 1.23456789

}

`

Note that the price is rounded to 1.23 when inserted into the database using the insertion 'instruction in in’.

The question

This rounding occurs due to the way prices are being stored in a decimal format (for example, float64). When entering data, the database may not accurately represent the original value due to errors or limitations of rounding in accuracy. As a result, the resulting price is rounded to the nearest integer.

The solution

To avoid this rounding and ensure accurate data storage, we can use the following techniques:

Ethereum: I am streaming binance data in python but the prices are rounded down when inserted into my mariadb db

1. Cast as decimal

We can launch the price column for decimal (8.2) instead of float64. This will allow a more accurate representation of decimal numbers.

Python

import pandas like PD

Sample Data

Data = {

“Timestamp”: [“2022-02-16t14: 30: 00.000Z”, “2022-02-16t15: 01: 45.678Z”],

“Priceusd”: [1.23456789, 1.34567890]

}

Create a Dataframe

df = pd.dataframe (data)

Cast price column for decimal

DF [“Priceusd”] = DF [“Priceusd”]. Astype (“decimal”)

Enter data into the database

df.to_sql (“my_table”, con = mecan

`

2. Use decimal library

Alternatively, we can use the decimal library 'in Python to deal with decimal arithmetic and avoid rounding.

Python

decimal import

Decimal Decimal Decimal Decimal, GetContext

Defines accuracy for decimal operations

GetContext (). Prec = 20

Sample Data

Data = {

“Timestamp”: [“2022-02-16t14: 30: 00.000Z”, “2022-02-16t15: 01: 45.678Z”],

“Priceusd”: [decimal (“1.23456789”), decimal (“1.34567890”]]]]]

}

Create a Dataframe

df = pd.dataframe (data)

Converting price columns into decimal

for column in df.columns:

If Isinstance (DF [column] .dtype, float):

DF [column] = df [column] .astype (decimal)

`

3. Update column type

If the column type is already defined as Float64, we can update it to the decimal using the following syntax:alter table my_table modify decimal column (8,2);

Conclusion

By applying one of these techniques, you will be able to avoid rounding and ensure accurate data storage to your Mariadb Database. Remember to adjust the column type according to your specific requirements.

Example of use of cases

  • Streaming Ethereum Prices with Binance API: You can use this technique to display high frequency price data accurately.

  • Historical Price Analysis: Precise representation of prices is crucial for statistical analysis and visualizations.

By following these steps, you can optimize your database scheme to store Ethereum prices accurately.

Ethereum Corporation


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *