- Solve real problems with our hands-on interface
- Progress from basic puts and calls to advanced strategies
Posted September 26, 2022 at 11:58 am
The article “Live Algo Trading on the Cloud – AWS” first appeared on AlgoTrading101 Blog.
Excerpt
Live algorithmic trading on the Cloud means that your trading bots can use the cloud provider’s resources to run 24/7 while being easily maintainable.
A Cloud Service offers cloud computing as a service with an intent to provide affordable, easy and efficient access to various resources without the need to have your own hardware or infrastructure.
Cloud services have many purposes for which they could be used for and here are some of the most common ones:
There are many cloud services and picking a quality one is important. In order to make your decisions process easier I will mention a few that are on top of their cloud game:
Amazon Web Services are cloud services that offer various features like computing power, database storage, content delivery, financial services, machine learning model training, and more.
In this article, we will want to explore the AWS Lightsail cloud service that offers affordable servers on which we can run our trading strategies. The simple trading strategy that we will use will feature the Kraken exchange.
Before we set up a server and let the bot go crazy with trading, we need to explain how this strategy works and where you can learn how to code one.
The main idea behind the strategy will be to buy ETH when BTC moves 5% in the last 5 minutes. For this to work, we will pull BTC prices in 5 minute intervals and calculate the percentage difference.
If this difference is more or equal to 5% we will buy ETH, if the requirement isn’t reached we will continue to loop around. The strategy also features error management, logging, and environmental variables.
Error management is used to catch errors that might occur, logging is used so we can obtain data about our bot and what it’s been up to while running on the server and environmental variables allow us to interact with the bot.
The code below is how our trading strategy looks like and here you can find an article about Kraken and how to code a similar strategy by yourself.
# Import the libraries and load the API Keys
import time, logging, os
import pandas as pd
import krakenex
from pykrakenapi import KrakenAPI
api = krakenex.API()
kraken = KrakenAPI(api)
api.load_key('KrakenPass.txt')
# Create environment variables
track_ticker = os.environ.get('ticker_to_track')
trade_ticker = os.environ.get('ticker_to_trade')
logname = os.environ.get('my_log_name') # Name of the saved log file
# Set up the logging
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(level=logging.INFO, format='%(asctime)s: %(levelname)s: %(message)s',
filename=logname, filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s: %(levelname)s: %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
# Create the main script logic
while True:
logging.info('--------- Start of current 5 minute period ---------')
logging.info(pd.Timestamp.now())
try:
BTC_old = float((kraken.get_ticker_information(track_ticker))['b'][0][0])
except Exception as e:
logging.warning('Error obtaining data')
time.sleep(300)
try:
BTC_new = float((kraken.get_ticker_information(track_ticker))['b'][0][0])
except Exception as e:
logging.warning('Error obtaining data')
percent = ((BTC_new - BTC_old)*100) / BTC_old
logging.info(f'BTC moved {percent:.2f}% over the last 5 minutes')
if percent >= 5:
try:
ETH = float((kraken.get_ticker_information(trade_ticker))['a'][0][0]) + 2
except Exception as e:
logging.warning('Error obtaining data')
try:
response = kraken.add_standard_order(pair=trade_ticker, type='buy', ordertype='limit',
volume='0.007', price=ETH, validate=False)
except Exception as e:
logging.warning('Error placing order')
logging.info(f'Order details:\n{response}')
time.sleep(2)
try:
check_order = kraken.query_orders_info(response['txid'][0])
except Exception as e:
logging.warning('Error checking order')
if check_order['status'][0] == 'open' or 'closed':
logging.info('Order completed sucessfully')
break
else:
logging.info('Order rejected')
break
else:
logging.info('--------- End of current 5 minute period ---------')
Notice: The code is stored in a private GitHub repository.
Visit AlgoTrading101 for instructions on how to sign up for an AWS server: https://algotrading101.com/learn/live-algo-trading-on-the-cloud-aws/.
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from AlgoTrading101 and is being posted with its permission. The views expressed in this material are solely those of the author and/or AlgoTrading101 and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Join The Conversation
For specific platform feedback and suggestions, please submit it directly to our team using these instructions.
If you have an account-specific question or concern, please reach out to Client Services.
We encourage you to look through our FAQs before posting. Your question may already be covered!