WARNING
Trading involves significant risk and may result in the loss of your investment. Read our full Disclaimer.
Overview
The Super Algorithm open source framework inlcudes a sample CCXT exchange integration which you can use as a starting point.
Specificaly we use:
- creating limit and market orders
- cancelling orders
- fetching orders and order status updates
- listening to trades
- getting account balances
Based on these APIs the strategy will place orders, maintain the internal positions and balances.
WARNING
This feature is experimental and intended for educational use only. Before using it thoroughly test the functionality. Implement your own live trading system. Verify all integrations and settings work correctly. You are responsible for ensuring the system performs as expected in a live environment.
Switching from Paper to Live Trading
Since the PaperExchange mimicks the workings of a real exchange, all that is required is to change the DataSource and Exchange instance.
You can create a new file called main.py
and place the below sample code inside.
import asyncio
from datetime import datetime, timedelta
from sma_sample_strategy import SMAStrategy
from superalgorithm.exchange import CCXTExchange
from superalgorithm.data.providers.ccxt import CCXTDataSource
from superalgorithm.utils.config import config
async def main():
preload_ts = (datetime.now() - timedelta(days=2)).timestamp() * 1000
datasource = CCXTDataSource("BTC/USDT", "5m", exchange_id="binance", preload_from_ts=preload_ts)
strategy = SMAStrategy(
[datasource],
CCXTExchange(exchange_id="binance", config={"apiKey": "", "secret": ""}),
)
await strategy.start()
if __name__ == "__main__":
asyncio.run(main())
We are now using the CCXTDataSource
which connects to the Binance exchange to load the 5-minute BTC/USDT data. We have also added the preload_ts
parameter to instruct the CCXTDataSource
to load 2 days' worth of data for playback before executing any trades. This allows us to populate our indicators and bring our strategy up to date with the current time.
We also replaced PaperExchange
with CCXTExchange
. We will use CCXTExchange
with binance as the target, and have to supply the config requirements as per the CCXT documentation.
Testing
CCXT offers a wide range of exchange integrations, but our experience has shown that thorough testing of each exchange connection is crucial. Connection issues and data inconsistencies are common and can occur at any point during operation. To ensure reliable and safe trading, consider the following precautions and potential pitfalls:
Connection stability: Intermittent disconnections or API timeouts can disrupt trading activities.
Rate limiting: Many exchanges impose strict API call limits. Ensure your code respects these limits to avoid temporary or permanent bans.
Data consistency: Verify the accuracy and timeliness of market data, as discrepancies can lead to incorrect trading decisions.
Order execution: Confirm that orders are placed, filled, and cancelled as expected. Exchanges may have different behaviors for various order types.
Account balance discrepancies: Regularly reconcile your local balance calculations with the exchange-reported balances.
Fees: Account for trading fees in your calculations, as they can significantly impact profitability, especially for high-frequency trading.
Asset precision: Be aware of the decimal precision required for different assets to avoid rounding errors.
Timezone issues: Ensure your system's timezone aligns with the exchange's to prevent scheduling and data interpretation errors.
API changes: Stay informed about any updates to the exchange's API, as these may require code modifications.
Regulatory compliance: Be aware of any regulatory requirements or restrictions for the exchanges you're using.
To mitigate risks:
- Test strategies with a small account first to limit potential losses.
- Isolate strategies by using individual trading accounts for each.
- Implement safeguards such as placing limits on order sizes and trading frequency.
- Use paper trading or backtesting features when available to validate strategies before live trading.
- Implement comprehensive logging and monitoring to quickly identify and respond to issues.
- Regularly update your CCXT library to benefit from the latest bug fixes and improvements.
By being aware of these common issues and implementing appropriate safeguards, you can create more robust and reliable trading systems.