In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use cross-index algorithm to quantify cryptocurrency transactions in Python, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In this tutorial, we will learn how to use cross-indicators to predict buy / sell signals in the cryptocurrency market, and provide a complete Python code at the end of the tutorial, using this algorithm to achieve a triple return on bitcoin revenue on market historical data.
1. Basic knowledge: Golden Cross vs. Death crossing
If you have a financial background, you know that the golden crossover (golden Cross) and death crossover (Death Cross) are derived from the moving average algorithm, also known as cross indicator. Both cross-indicators use the following formula to calculate the average market closing price over a specific period of time:
The concept combines two moving averages (short-term and long-term) to obtain cryptocurrency trends. When the short-term moving average exceeds or reviews the long-term moving average, a buy or sell signal will appear.
Mathematically, if you choose a short-term moving average of 5 cycles and a long-term moving average of 20 cycles, we will get the buy signal through the following conditions:
MA (5) is calculated as follows:
MA (20) is calculated as follows:
CPrice corresponds to the closing value. For example, the current price of bitcoin is $49670, and t represents the definition of a time period (more on this in the video at the end of this article). These cross-indicators are part of the equation and can help detect global trends in the cryptocurrency under study. These CI (crossover indicators) are widely used by multiple traders and funds around the world to define support forces, resistance levels, stops and targets and to understand potential trends.
Now that we have covered some background, let's start testing how they can help predict the cryptocurrency market.
2. Software stack used
Before continuing with the following tutorials, make sure you have installed Python3 and the following packages:
Pandas:
NumPy:
Yfinance:
Plotly: not required, but useful when drawing
You can install the above packages using pip, for example:
Pip install yfinancepip install plotly3, data pipelines and modeling
Now we can define the data processing flow, which consists of three different steps:
Use Yahoo Finance API to query data of real-time cryptocurrency
Define a time period, create new columns for the data we want to calculate, and then update these values every second.
Draw in real time and check whether our signal is accurate.
In this article, I won't go into too much detail about the code and API, you can learn how to use Python to get real-time cryptocurrency market data in the following article. Now we can start coding!
4. Import the software package
The first step will include importing the necessary software packages. Import the previously installed package using the following line of code:
# Raw Packageimport numpy as npimport pandas as pd#Data Sourceimport yfinance as yf#Data vizimport plotly.graph_objs as go
Now that the library has been imported, we can import our cryptocurrency data.
5. Obtain real-time market data
Now the different packages you need have been uploaded. We will take the BTC-USD transaction pair as an example and import it through the Yahoo Finance API setting.
You can expand the required legal currency and cryptocurrency options. For example, if you are from India or Canada, you can use INR or CAD, or you can set whether Ripple or Ethereum data is required.
Let's go back to the API structure.
When calling Yahoo Finance API, you need to pass in three parameters in order:
Transaction pair Code (1)
Start date + end date or period (2)
Interval (3)
In our example, the transaction pair code (parameter 1) will be a BTC-USD pair. In addition, in this example, we will select the last 7 days as the time period (parameter 2). And set an interval of 90 minutes (parameter 3).
To invoke data, you must use the following structure:
Before going any further, I'll introduce some details about the third parameter (interval).
6. Available time interval
Here I want to take a quick look at the different intervals that can be set using yahoo finance API.
The possible intervals are listed in detail below:
Now that we have defined three parameters, let's execute the query and check the output:
Data = yf.download (tickers='BTC-USD',period = '8dink, interval =' 90m')
Here is the output:
Now that we have downloaded and stored the data, we can continue and define the East China average and buy and sell signals.
7. Implementation of the algorithm
Our real-time data is now downloaded and stored in a variable called data. The next step involves calculating our moving average and setting buy and sell signals.
We will need to create the following calculated fields:
MA (5)
MA (20)
To do this, we will use the scrolling function included in Python to get the average of the n latest cycles. With regard to MA (5), we will apply our strategy in the last five 90-minute cycles. This means that we will calculate the average closing price for the last 7 hours and 30 minutes (5 times 90 minutes).
Similarly for MA (20), the average of 20 rather than five 90-minute periods is calculated. The Python code is as follows:
# Moving average using Python Rolling functiondata ['MA5'] = data [' Close'] .cake (5). Mean () data ['MA20'] = data [' Close']. Mean (20)
After the above code is executed, two new columns are created for the data box, as follows:
All right, now we can test the strategy.
8. Real-time drawing
The final step of our plan is to draw the data and check whether the market can be predicted. In the following figure, I mark green as a good prediction and black as a wrong prediction:
9. Quantitative transaction Python code
The complete Python code is as follows:
# Raw Packageimport numpy as npimport pandas as pd#Data Sourceimport yfinance as yf#Data vizimport plotly.graph_objs as go#Importing market datadata = yf.download (tickers='BTC-USD',period = '8dwells, interval =' 90m') # Adding Moving average calculated fielddata ['MA5'] = data [' Close'] .mean () data ['MA20'] = data [' Close'] .mean (20). Mean () # declare figurefig = go.Figure () # Candlestickfig.add_trace (go.Candlestick (x=data.index) Open=data ['Open'], high=data [' High'], low=data ['Low'], close=data [' Close'], name = 'market data') # Add Moving average on the graphfig.add_trace (go.Scatter (x=data.index, y = data [' MA20'], line=dict (color='blue', width=1.5), name = 'Long Term MA')) fig.add_trace (go.Scatter (x=data.index) Y = data ['MA5'], line=dict (color='orange', width=1.5), name =' Short Term MA') # Updating X axis and graph# X-Axesfig.update_xaxes (rangeslider_visible=True, rangeselector=dict ([dict (count=3, label= "3D", step= "days", stepmode= "backward"), dict (count=5, label= "5d", step= "days", stepmode= "backward"), dict (count=7, label= "WTD" Step= "days", stepmode= "todate"), dict (step= "all")])) # Showfig.show ()
All transactions are not perfect, and sometimes we tend to lag behind entering or leaving the market, but in a period of Bitcoin stability, the golden crossover strategy has become a useful strategy to boost our profits. After a simple calculation using the existing historical data, our algorithm can get a return of 7.1% in a week, while the return on bitcoin transactions over the same period is stable at around 1.7%.
This is the answer to the question about how to use the cross-index algorithm to quantify the cryptocurrency transaction in Python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 273
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.