OKEx Cryptocurrency Automated Trading with Python: API Trading Methods (Part 3)

ยท

Simplest Example

Monitoring Real-Time Prices

Building on the previous section, we've already created a function to print real-time market data. Now, let's implement the most basic automated feature: monitoring live cryptocurrency prices.

# coding: utf-8
import time
from client import OkexClient

client = OkexClient(None, None)
symbol = 'eos_usd'
res = client.ticker(symbol, 'this_week')
print(res['ticker']['sell'])

The print function displays the current market price. By adding an if condition, you can effectively create a price alert system.

๐Ÿ‘‰ Discover advanced trading strategies

How to Run Programs Automatically in Background

Using Crontab

Crontab allows you to execute system commands or shell scripts at fixed intervals. You can schedule tasks by minutes, hours, days, months, weeks, or any combination thereof. This makes it ideal for periodic tasks like log analysis or data backups.

Let's modify our previous program to demonstrate:

Logging Market Data to Files

# coding: utf-8
import time
from client import OkexClient

client = OkexClient(None, None)
symbol = 'eos_usd'
res = client.ticker(symbol, 'this_week')
print(res['ticker']['sell'])
# Log to eos_ticker.txt
f = open("/root/okex/AutoTrade/client/eos_ticker.txt", "a")

This modified version saves the price data to a text file, which can be processed later or used for analysis.

๐Ÿ‘‰ Learn professional crypto trading techniques

Frequently Asked Questions

How often should I check prices using API calls?

For most trading strategies, checking prices every 1-5 minutes provides sufficient data without hitting API rate limits. High-frequency trading may require more frequent checks.

What's the advantage of logging to files?

File logging creates a historical record of price movements, allowing for backtesting strategies and identifying market patterns over time.

Can I run multiple trading bots simultaneously?

Yes, but you'll need to manage API rate limits carefully and ensure each bot has distinct trading logic to avoid conflicts.

Is Python the best language for crypto trading bots?

Python excels for algorithmic trading due to its extensive libraries (like Pandas for data analysis) and relatively gentle learning curve, though other languages like C++ may offer better performance for high-frequency trading.

How secure are API keys in Python scripts?

Never hardcode API keys in scripts. Use environment variables or encrypted configuration files, and always restrict API key permissions to only what's necessary.