Skip to content

pmutua/tradingbot

Repository files navigation

How the Trading Bot Determines Buy or Sell Actions

  1. Relative Strength Index (RSI) Calculation:

    • RSI is a technical indicator used to analyze the strength and direction of price movements.
    • It's calculated based on historical price data, specifically the closing prices of the last RSI_PERIOD periods.
    • RSI values range from 0 to 100, where:
      • Values above 70 typically indicate overbought conditions, suggesting that the asset may be overvalued and due for a price decrease.
      • Values below 30 typically indicate oversold conditions, suggesting that the asset may be undervalued and due for a price increase.
    • By comparing the current RSI value to predefined thresholds (RSI_OVERBOUGHT and RSI_OVERSOLD), the bot determines whether the market is potentially overbought or oversold.
  2. Buy/Sell Decision:

    • Sell Signal: If the current RSI value exceeds RSI_OVERBOUGHT, the bot generates a "Sell" signal, indicating that the market may be overvalued and it might be a good time to sell.
    • Buy Signal: If the current RSI value falls below RSI_OVERSOLD, the bot generates a "Buy" signal, indicating that the market may be undervalued and it might be a good time to buy.
  3. Trade Execution:

    • Upon receiving a buy or sell signal, the bot evaluates the available trading balance (trading_limit) and the amount of cryptocurrency it intends to trade (trade_amount).
    • If the calculated trade amount multiplied by the current price of the asset is within the trading limit, the bot proceeds to execute the trade.
    • The bot places either a buy or sell order on the Binance exchange, depending on the generated signal.
  4. Continuous Monitoring:

    • The bot continuously monitors the market data, including the current price and RSI values, in real-time.
    • It repeats the RSI calculation and trade decision-making process at regular intervals, typically every few seconds or minutes.
    • This continuous monitoring allows the bot to adapt to changing market conditions and execute trades accordingly.

Overall, the bot's actions are driven by the interpretation of RSI indicators and predefined thresholds, aiming to capitalize on potential price movements in the market.

Determining Buy or Sell Actions with RSI and 1-Minute K-Line Data

Frequency of RSI Calculation

  • With RSI_PERIOD = 14, the bot calculates the Relative Strength Index (RSI) based on the closing prices of the last 14 one-minute k-line data points.
  • RSI is updated approximately every 14 minutes.

Possibility of Buy or Sell Signals

  • Depending on market conditions, the bot may generate buy or sell signals multiple times within an hour.
  • If RSI exceeds RSI_OVERBOUGHT (e.g., 70), indicating overbought conditions, the bot might generate a sell signal.
  • Conversely, if RSI falls below RSI_OVERSOLD (e.g., 30), indicating oversold conditions, the bot might generate a buy signal.

Timing of Buy or Sell Actions

  • Buy or sell actions depend on when the RSI thresholds (RSI_OVERBOUGHT and RSI_OVERSOLD) are crossed.
  • For instance, the bot might sell when RSI becomes overbought and buy when it becomes oversold.
  • The exact timing of these actions varies with market fluctuations and RSI values.

Considerations

  • RSI signals should be considered alongside other factors like market trends, volume, and fundamental analysis.
  • Risk management mechanisms such as stop-loss orders and position sizing strategies are essential to mitigate potential losses.

Overall, the bot's buying or selling actions are influenced by RSI signals based on 1-minute k-line data, but the exact timing and frequency depend on market dynamics and specific RSI thresholds.

Summary: Understanding Relative Strength Index (RSI) for Trading

The Relative Strength Index (RSI) is a popular technical indicator used by traders to assess the strength and momentum of price movements in financial markets. It provides insights into whether an asset is overbought or oversold, helping traders make informed decisions about buying or selling.

1. Introduction to RSI

  • RSI is a momentum oscillator that measures the speed and change of price movements.
  • It oscillates between 0 and 100, with high values indicating overbought conditions and low values indicating oversold conditions.

2. Interpretation of RSI Values

  • Overbought: RSI values above a certain threshold (commonly 70) suggest that the asset may be overvalued, and a reversal or correction in price could occur.
  • Oversold: RSI values below a certain threshold (commonly 30) indicate that the asset may be undervalued, and there could be a potential upward price movement.

3. Using RSI in Trading

  • Buy Signals: Traders may consider buying opportunities when the RSI falls below the oversold threshold (e.g., RSI < 30), indicating potential upward price movement.
  • Sell Signals: Conversely, selling opportunities may arise when the RSI rises above the overbought threshold (e.g., RSI > 70), signaling potential downward price movement.
  • Confirmation and Context: RSI signals should be confirmed with other technical indicators or market analysis techniques.
  • Adaptability and Experimentation: Traders may adjust RSI thresholds based on the characteristics of the asset being traded and their trading strategies. Experimentation and backtesting can help determine optimal thresholds for different market conditions.

4. Calculation of RSI

  • RSI is calculated using the formula: RSI = 100 - (100 / (1 + RS))
  • RS (Relative Strength) is the average of x days' up closes divided by the average of x days' down closes.

5. Market Indicators

  • Overbought Conditions: When RSI values exceed the overbought threshold (e.g., RSI > 70), it suggests that the market may be overvalued, and a correction or reversal may occur.
  • Oversold Conditions: Conversely, when RSI values fall below the oversold threshold (e.g., RSI < 30), it indicates that the market may be undervalued, and a potential rebound or upward movement may occur.

By understanding and effectively utilizing RSI, traders can enhance their trading strategies, identify potential entry and exit points, and manage risk more effectively in various financial markets.

Frequency of WebSocket Updates: The frequency at which the WebSocket receives updates affects how often the bot receives new data. If updates are frequent (e.g., every minute for 1-minute k-line data), the bot will append new data and perform analysis more frequently.

Size of Data Window: The size of the data window used for analysis (e.g., RSI_PERIOD) determines the number of data points considered for calculating indicators like RSI. For example, if RSI_PERIOD is set to 14, the bot will consider the last 14 data points for RSI calculation.

Trading Strategy: The trading strategy implemented by the bot also influences its behavior. For example, if the bot waits for specific conditions (e.g., RSI crossing certain thresholds) before making trading decisions, it may take longer to trigger buy or sell signals.

Processing Time: The time taken by the bot to process incoming data, perform analysis, and make trading decisions depends on factors such as hardware performance, efficiency of code implementation, and network latency.

Market Conditions: Market conditions, including volatility, liquidity, and trading volume, can impact the frequency and timing of trading signals. In highly volatile markets, the bot may encounter more frequent trading opportunities.

Event Time (E)

  • Type: Integer (Unix timestamp)
  • Description: The event time indicates the time when the message was received or generated.

Kline Data (k)

This section contains details about the specific k-line (candlestick) data.

  • t (Kline Start Time)

    • Type: Integer (Unix timestamp)
    • Description: The starting time of the k-line interval.
  • T (Kline Close Time)

    • Type: Integer (Unix timestamp)
    • Description: The closing time of the k-line interval.
  • s (Symbol)

    • Type: String
    • Description: The trading pair symbol, such as "BTCUSDT".
  • i (Interval)

    • Type: String
    • Description: The interval for the k-line data, such as "1m" for 1-minute intervals.
  • f (First Trade ID)

    • Type: Integer
    • Description: The ID of the first trade during this k-line interval.
  • L (Last Trade ID)

    • Type: Integer
    • Description: The ID of the last trade during this k-line interval.
  • o (Open Price)

    • Type: String
    • Description: The opening price of the asset at the beginning of the k-line interval.
  • c (Close Price)

    • Type: String
    • Description: The closing price of the asset at the end of the k-line interval.
  • h (High Price)

    • Type: String
    • Description: The highest price of the asset reached during the k-line interval.
  • l (Low Price)

    • Type: String
    • Description: The lowest price of the asset reached during the k-line interval.
  • v (Volume)

    • Type: String
    • Description: The trading volume, representing the total amount of the asset traded during the k-line interval.
  • n (Number of Trades)

    • Type: Integer
    • Description: The number of trades that occurred during the k-line interval.
  • x (Is Closed)

    • Type: Boolean
    • Description: Indicates whether the k-line is closed. If true, the k-line interval has ended, and the data is complete.
  • q (Quote Asset Volume)

    • Type: String
    • Description: The volume of the quote asset traded during the k-line interval.
  • V (Taker Buy Base Asset Volume)

    • Type: String
    • Description: The volume of the base asset bought by takers during the k-line interval.
  • Q (Taker Buy Quote Asset Volume)

    • Type: String
    • Description: The volume of the quote asset bought by takers during the k-line interval.
  • B (Ignore)

    • Type: String
    • Description: This field is typically ignored and can be disregarded.

Yes, it's advisable to save the streamed data in real-time to perform your calculations effectively. By saving the streamed data, you can maintain a historical record of price movements, volumes, and other relevant metrics over time. This historical data is crucial for conducting technical analysis, such as calculating indicators like the Relative Strength Index (RSI), Moving Averages (MA), and others.

Here are some reasons why saving the streamed data is beneficial:

Historical Analysis: Saving the data allows you to analyze past market behavior, identify patterns, and develop trading strategies based on historical trends.

Backtesting: You can backtest your trading strategies using historical data to evaluate their performance under different market conditions.

Real-time Decisions: Having access to historical data enables you to make more informed decisions in real-time by comparing current market conditions to past patterns.

Risk Management: Historical data can help you assess the risk associated with certain trading decisions and adjust your strategies accordingly.

Data Integrity: By saving the data locally, you ensure that you have a reliable source of information, even if there are interruptions in the WebSocket connection or issues with external APIs.

Therefore, it's recommended to implement a mechanism to save the streamed data in real-time, such as storing it in a database or a structured file format, to support your trading activities effectively.

A trading pair is a set of two assets that are being traded against each other on a financial exchange platform. In cryptocurrency trading, a trading pair typically consists of two different cryptocurrencies, or a cryptocurrency and a fiat currency, such as Bitcoin (BTC) and US Dollar (USD).

For example, in the trading pair BTC/USD:

BTC (Bitcoin) is the base currency, which means it is the asset being bought or sold. USD (US Dollar) is the quote currency, which means it is the currency used to determine the price of BTC. When you trade in a specific trading pair, you are essentially exchanging one asset for another at the current market price. In the BTC/USD pair, if you buy BTC, you are using USD to purchase BTC, and if you sell BTC, you are exchanging BTC for USD. Trading pairs allow traders to speculate on the value of one asset relative to another and profit from price fluctuations.

https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md

https://docs.python-telegram-bot.org/en/v21.2/examples.conversationbot2.html https://www.mindk.com/blog/how-to-develop-a-chat-bot/

{
    "ok": true,
    "result": [
        {
            "update_id": 352110434,
            "message": {
                "message_id": 59,
                "from": {
                    "id": 699510250,
                    "is_bot": false,
                    "first_name": "Somacode",
                    "username": "somacode",
                    "language_code": "en"
                },
                "chat": {
                    "id": 699510250,
                    "first_name": "Somacode",
                    "username": "somacode",
                    "type": "private"
                },
                "date": 1716709683,
                "text": "/starttrading",
                "entities": [
                    {
                        "offset": 0,
                        "length": 13,
                        "type": "bot_command"
                    }
                ]
            }
        }
    ]
}

https://docs.python-telegram-bot.org/en/v21.2/examples.customwebhookbot.html

docker exec -it trading_bot-telegram-bot-1 python /app/check-data.py

Then, use docker cp to copy the streaming-markets.db file from the container to your host machine.

docker cp trading_bot-telegram-bot-1:/app/streaming-markets.db .

This command will copy the streaming-markets.db file from the container's /app directory to the current directory on your host machine.

docker exec my_container python -m unittest tests.py

OR

docker exec <container_name_or_id> python -m unittest tests.py

https://stackoverflow.com/questions/57006437/calculate-rsi-indicator-from-pandas-dataframe

https://github.com/peerchemist/finta/blob/master/finta/finta.py

https://itnext.io/using-the-rsi-indicator-to-generate-trading-signals-in-python-with-ta-lib-3328d4566c6e#:~:text=All%20you%20have%20to%20do,the%20default%2014%2Dday%20window.

TA-Lib/ta-lib-python#479

https://wire.insiderfinance.io/calculate-rsi-with-python-and-yahoo-finance-c8fb78b1c199

https://medium.com/@deepml1818/mastering-financial-analysis-with-python-and-ta-lib-an-intermediate-guide-40735bb61865

https://hexdocs.pm/talib/TAlib.Indicators.RSI.html#rsi/2

https://pypi.org/project/TA-Lib/