Tag Archives: technology

Building a Better Trading Bot: Lessons from FastTrade to VOLT

The bot that’s running on the VPS, which is testing my ability to stomach losses, I’ve called FastTrade (now at version 1.3). This is the bare-bones design, and “works” perfectly. I was happy with where I’d got it to but didn’t want to be slowed by years old coding decisions. It’s mad how you can have code that works well, but then when you spend a bit of time actually thinking about it, you come up with another solution. Then you think some more, and change it again.

One example of this was my logging code. In OSCAR (a retired bot) I’d written code for each logging event – this wrote a line to my logging sheet individually as they were called, including an xlUp line (obviously this brings into question how serious I am about efficiency). It did work, though writing to the sheet on each log event is not in line with my quest for speed. So, one goal I had was to store log events to an array and paste the array at market change, where trading isn’t happening so speed is less important. I moved all the logging code to it’s own module, added an array, and put the repeated code in it’s own sub. Each logging event, still having its own sub, added a string element such as “Trading Period Started” and called the sub that added all the other detail at that exact moment. I then refined this further by adding a string argument to the sub for the name of the logging event. My logging code has gone from multiple subs to just 2 – the log-event-with-argument sub, and the log-log-to-log-sheet sub. Splendid.

I’ve been building my new bot from the ground up, including the log stuff. I’m calling this one VOLT (VOLume Trader). I’d tested manually and got to a point where live testing was possible. An important element here – always have a boolean argument that allows trading. Set to false and the entire code can run but the bit that writes the trigger/order is bypassed. Once you’re happy to trade, change it to true. Although this isn’t an emergency stop, it does allow for testing and quick interventions if things aren’t quite right but you want monitoring to continue.

Well I’d tested and tested, debugged and tested, debugged etc. Literally as soon as I went live it failed – subscript out of range. I actually like this fault finding part, it’s solving the puzzle that I enjoy. Fortunately for me it was one puzzle after another. Not checking for previous data entries, so writing the same thing over and over, exceeding the array size. Then not incrementing after adding data, so missing loads of log events. Checking for the wrong cased words (Closing vs CLOSING). The list is long but my time is short (a bit dramatic).

I’ve written this post whilst watching VOLT run through Sunday evening US horse markets on another screen and all is now well. Logging is working, as is market navigation, where I appear to have got rid of the double quick-pick-list refresh that caused no problem but I found mildly irritating.. The trigger code is there but not active yet. Next step is to add some temporary what-if logging, see how it fares. Happy trading, botters.

Some changes

First a small clarification. I said in the last post that when the currently active bot is triggered, it takes the Back offer. However, it doesn’t do that. It places a back bet at 1 tick lower than the best Lay price. In a tight market that is most likely going to take the Back offer as there won’t be any gap in the spread, but I thought I should be clear. It also explains why I don’t always get matched and the fill or kill element is in the order.

Admin news – I’ve stumped up for a personal WordPress plan to get rid of the banner and ads that came with viewing my blog which should make your experience more pleasurable, or less painful. It also came with a new domain name which looks neat, and my view count has jumped, bonus. There may be some decorative changes to come too, if I get on to it.

Admin news extra – Twitter/X has descended to such a low that WordPress no longer connects to it. But it does connect to Bluesky, so I’ve joined that small village scale community…

To the code – it’s always worth going back to basics. Thinking about it, there are elements of my code that I’ve reused without thinking, as they just work. To that end, in all my time botting and with all the different bots I’ve tried, I’ve always used the same worksheet change event code trigger in Sheet1, which is basically this –

Public Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

Application.EnableEvents = False

With Workbooks("MyBot.xlsm").Sheets(Target.Worksheet.Name)

MyCode 'this is in module1

End With

Application.EnableEvents = True

End Sub

Well it turns out that the With reference part – “.Sheets(Target.Worksheets.Name)” is a general reference and uses a look-up to get to the specific sheet from within the Sheets collection. However, for my bot it’s always going to be the same sheet so the reference can be changed to “.Worksheets(“Sheet1″)”. This is apparently better form as specific references should be used where possible. By not looking-up, there should also be a speed advantage (possibly multiple microseconds if I’m lucky). Remember, small wins are still wins. Anyhoo, it now looks like this –

Public Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

Application.EnableEvents = False

With Workbooks("MyBot.xlsm").Worksheets("Sheet1")

MyCode 'this is in module1

End With

Application.EnableEvents = True

End Sub

Finally – a shout out to Toby at Punter2Pro who featured my blog in an article last year. I’ve had a few click-throughs, so thanks.