Skip to content

mxber2022/Taipei_Love

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CryptoMaestro: BitMEX Crypto Derivatives Trading Bot

Overview

This crypto derivatives trading bot is designed to implement a basic moving average crossover strategy for futures trading on the BitMEX exchange with the LTCUSDT trading pair. The algorithm uses the ProfitView library for interacting with the exchange and executing trades.

Requirements

  • Python 3.9
  • ProfitView library

Warning

Valid Market Conditions:

  1. Trending Markets:

    • Bullish crossovers (short MA > long MA) may be effective during upward trends.
    • Bearish crossovers (short MA < long MA) may be effective during downward trends.
  2. Stable Price Movements:

    • Assumes relatively stable price movements over the short and long windows used for moving average calculations.
  3. Smooth Price Trends:

    • Suited for markets with smooth and well-defined trends.
  4. Volatility within Tolerable Range:

    • Moderate volatility is often favorable.
    • Extreme volatility could lead to false signals or rapid changes in trend direction.

Challenging Market Conditions:

  1. Sideways or Range-Bound Markets:

    • Moving average crossover strategy may generate false signals in the absence of a clear trend.
  2. Whipsawing:

    • Occurs in markets with frequent, short-lived price fluctuations, leading to multiple crossovers without a clear trend direction.
  3. Abrupt Price Changes:

    • Rapid and unpredictable price changes can lead to delayed responses by the algorithm, potentially resulting in losses.

1. Initialization and Setup

The bot is a Python script that uses the ProfitView library for trading operations. It starts by importing necessary modules and defining a class called Trading that inherits from Link, a class provided by ProfitView.

from profitview import Link, http, logger
import time

2. Trading Class Initialization

The Trading class is defined, inheriting from Link. This class will contain the trading logic and strategies.

class Trading(Link):
    def on_start(self):
        self.futures_trading_strategy()

The on_start method is called when the trading script is initialized. In this case, it immediately calls the futures_trading_strategy method.

3. Futures Trading Strategy

The futures_trading_strategy method is the core of the trading algorithm. It implements a basic moving average crossover strategy.

def futures_trading_strategy(self):
    # Strategy implementation

4. Moving Average Crossover Strategy

The moving average crossover strategy involves calculating short and long moving averages and making trading decisions based on their relationship.

  • Short MA Calculation:

    short_ma = sum(closing_prices[-short_window:]) / short_window
  • Long MA Calculation:

    long_ma = sum(closing_prices[-long_window:]) / long_window

5. Trading Logic

The trading logic is implemented based on the relationship between the short and long moving averages.

  • Bullish Crossover:

    if short_ma > long_ma:
        # Bullish crossover
        self.create_market_order(Pr.VENUE, sym=Pr.SYMBOL, side="Buy", size=1000000)
  • Bearish Crossover:

    elif short_ma < long_ma:
        # Bearish crossover
        self.create_market_order(Pr.VENUE, sym=Pr.SYMBOL, side="Sell", size=1000)

6. Error Handling

The code includes error handling for cases where fetching candles fails or encounters an error. Error messages are logged for debugging.

7. Order Cancellation

The definitely_cancel_orders method is responsible for canceling orders.

def definitely_cancel_orders(self):
    # Order cancellation logic

8. HTTP Routes

The bot also includes two HTTP routes (get_route and post_route) for handling GET and POST requests, respectively.

@http.route
def get_route(self, data):
    # Handle GET request
    return data

@http.route
def post_route(self, data):
    # Handle POST request
    return data

9. Continual Execution

The entire algorithm runs in a continuous loop, fetching candle data, applying the trading strategy, handling errors, and canceling orders. It then sleeps for 60 seconds before repeating the process.

while True:
    # Trading logic
    # Error handling
    # Order cancellation
    time.sleep(60)

This loop ensures that the trading strategy is applied repeatedly over time. Adjust parameters, such as moving average windows or order sizes, to suit specific trading preferences.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages