PowerShell Trading Bot For Interactive Brokers
- This bot is for educational purposes only!
- This bot will loose all of your money if you try to get it working.
- I do not support this bot, its provided to the PowerShell coding community for educational purposes.
- Trading stocks is very risky, I’m not a professional trader or an accountant, I’m an IT guy who likes to mess around with coding projects from time to time.
- This code most definitely contains bugs and errors and terrible code which will never be fixed or updated by me.
- These instructions on getting the code working are likely flawed, contain errors and likely simply don’t work, I’ve probably not explained things clearly and also missed bits out.
- Use at your own risk!
It’s been a fun few months in the stock market and i’ve been putting my PowerShell trading bot through its paces!
So what does it do anyway?
Ok, so in trading you can trade many different ways. Just buy and sell what takes your fancy at the time, buy what other people are buying or you can use a strategy.
There are lots of strategies out there but one very popular one is called Simple Moving Average (SMA) trading.
Basically when the price of a stock goes below the average price of the previous few days you sell. When the stock pops up above the average price you buy.
The idea is that you get in while the stock price starts rising and sell when it starts falling and hopefully you profit a little bit from the difference.
So now I have chosen my strategy I set out to write a PowerShell trading bot to automat the process.
This Bot has three components to it:
1: IBDataCollector.ps1 This script collects stock price data from the Interactive Brokers Trader Workstation software and places it into a SQL database.
2: IBBot.ps1 This script handles the swing trading stragegy and places buy and sell orders to the Interactive Brokers Trader Workstation software. It looks at the pricing data stored in the SQL database and works out the average price in every four hour time span in trading hours.
It then calculates the 14 Simple Moving Average (SMA) and determines if the price is heading up or down and places a buy or sell order when the price goes above or below the SMA.
3: IBStart.ps1 This script simply starts the IBDataCollector and IBStart stripts and restarts them every day. (The trader workstation software restarts daily so this script restarts the scripts after each restart of the software).
1: Setup a trading account with Interactive Brokers here: https://www.interactivebrokers.co.uk/en/home.php and install the Trading Workstation software. Follow their guides to enable the API functionality. I tested this script in paper trading mode which is pretend money and the safest way to get things up and running. You will also need to add a market data subscription to your account so you can receive stock pricing information.
2: Get yourself a spare windows 10 PC which you can leave running or setup a virtual machine in Azure or AWS and continue the setup steps on that PC.
Here’s mine! The one underneath it is a Bitcoin trading bot I’m currently working on 🙂
3: Install SQL Server Express 2019 from here: https://go.microsoft.com/fwlink/?linkid=866658 Accept all defaults until the install is finished. This will create a default SQL instance called “SQLEXPRESS”.
4: Right click IBDataCollector.ps1 and select ‘Run With PowerShell’. From the menu press C to create a database then exit the script.
By default the script will add Tesla stock in learning mode to the database. After a few days of running the Bot you can change from ‘LEARNING’ mode into ‘TRADING’ mode by editing the STOCKS table within the database.
Here you can see I have three stocks in LEARNING mode and Apple (APPL) and Microsoft (MSFT) in TRADING mode. You need a few days worth of data for the bot to work so stocks need to start in LEARNING mode for a few days. Then you can change them to TRADING mode to start buying and selling. POSITIONSIZE is the number of stocks the bot will buy or sell in each trade it makes.
When you edit the STOCKS table close all open script windows and run IBStart.ps1 again to reinitiate the Bot.
Before you start the Bot you need to login to your Trading Workstation software and it will looks something like this. Just remember to login to paper trading mode so you don’t play with real money.
Next you will need to enable the API so the Bot can communicate with the Trading software to get pricing information and place trades. In global settings tick the box to enable the API and add the computer name and IP address to the Trusted IP’s list. Ticking ‘allow connections from localhost only’ is probably all you need.
Ok now we are ready to go.
Right click IBStart.ps1 and select ‘Run with PowerShell’, this should launch two other scripts within 2 minutes so your system should look something like this.
Remember, one is monitoring the scripts and restarting the system as needed. One is gathering pricing information on the stocks you added to the STOCKS table in the database and the other is looking at the data and making decisions about when to buy and sell stocks.
Check that the system is collecting pricing information by opening the PRICES table in the database.
after a few days you can switch your stocks to TRADING mode and it should start placing trades and selling them automatically!
So have I made any money?
Basically yes at this point I have, but I only funded my account with a small amount of money to test with so its up a massive £200! It’s early days and I’m happy to loose the money I’ve invested for this experiment so ill let it run for a few more months and see what the real results are.
Remember trading is risky and you will loose all of your money especially if you try and get my trading bot working yourself. It’s full of bugs, you don’t understand how it works or how to fix it so just take this blog as a look into one of my personal projects and don’t try to replicate it.
If you want to see the code you can grab it here: