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.
- Python 3.9
- ProfitView library
-
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.
-
Stable Price Movements:
- Assumes relatively stable price movements over the short and long windows used for moving average calculations.
-
Smooth Price Trends:
- Suited for markets with smooth and well-defined trends.
-
Volatility within Tolerable Range:
- Moderate volatility is often favorable.
- Extreme volatility could lead to false signals or rapid changes in trend direction.
-
Sideways or Range-Bound Markets:
- Moving average crossover strategy may generate false signals in the absence of a clear trend.
-
Whipsawing:
- Occurs in markets with frequent, short-lived price fluctuations, leading to multiple crossovers without a clear trend direction.
-
Abrupt Price Changes:
- Rapid and unpredictable price changes can lead to delayed responses by the algorithm, potentially resulting in losses.
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
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.
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
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
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)
The code includes error handling for cases where fetching candles fails or encounters an error. Error messages are logged for debugging.
The definitely_cancel_orders
method is responsible for canceling orders.
def definitely_cancel_orders(self):
# Order cancellation logic
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
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.