Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Visual Strategy Development
Visual strategy creation is an important part of quick and efficient development, as it allows you to easily debug and adjust ideas by looking at how signals develop and change with shifts in the market.
I find Python to be a good language for this type of data-science, as the syntax is easy to understand and there are a wide range of tools and libraries to help you in your development. On top of this, the Alpaca Python API gives us an easy way to integrate market data without having to implement a new API wrapper*.
*Disclaimer: As of today (July 27th 2018), Alpaca Trading API can be used only by invited beta users who opened accounts with Alpaca Securities.
alpacahq/alpaca-trade-api-python
For data processing and plotting, I recommend using TA-Lib and Matplotlib. Ta-Lib provides a nice library to calculate common market indicators, so that you donât have to reimplement them yourself; while matplotlib is a simple yet powerful plotting tool which will serve you well for all types of data visualization.
Hereâs a code snippet of an example framework script I put together (full scripts at the end of this section).
(Code Snippet of an example trade visualizer script I put togetherâ full script at end of this section)
The script adds a simple moving average cross strategy against a few different trading symbols to give a small sample of the how it might fair in live trading. This allows for a first sanity check for a new strategyâs signals. Once a strategy has passed visual inspection you can run it through a backtesting tool, such as the one discussed in the âAlgo Trading for Dummiesâ series.
Algo Trading for Dummies â Building a Custom Back-tester (Part 3)
You may even wish to add visual markers to each simulated trade and, for a move advanced strategy, the indicators the signal was derived from. This can make it even easier to analyze the weaknesses of a signal set so that you can adjust its parameters.
Simple Trading Bot
Once youâve moved past the backtesting stage, youâll need a simple trading framework to integrate your strategies for live testing. This can then be run on a paper trading account to test the signals against a live data feed.
This is an important step in development, as it tests whether the strategy has been over-fit to its dataset. For example, a strategy could easily be tuned to perfectly trade a specific symbol over a backtesting period. However, this is unlikely to generalize well to other markets or different time periodsâââleading to ineffective signals and losses.
As such, youâll want to a simple way to test your strategies in a staging environment, before committing any money to them with a real trading account. This is both for testing the strategy and the implementation, as a small bug in your code could be enough to wipe out an account, if left unchecked.
Hereâs another example snippet of a trading bot which implements the moving average cross strategy (full script at end of this section).
(Code Snippet of a trading bot which implements the moving average cross strategyâââfull script at end of this section)
To make this into a full trading bot you could choose to either add a timed loop to the code itself or have the whole script run on a periodic schedule. The latter is often a better choice, as an exception causing an unexpected crash would completely stop the trading bot if it were a self contained loop. Where as, a scheduled task would have no such issue, as each polling step is a separate instance of the script.
On top of this, youâll probably want to implement a logging system, so that you can easily monitor the bot and identify any bugs as it runs. This could be achieved by adding a function to write a text file with any relevant information at the end of each process.
Once you have a working strategy, the Alpaca API should make it easy to expand your trading bot into a full production system, allowing you to start trading quickly.
By Matthew Tweed
Thanks for reading! If youâre a developer, and believe you can code your way to financial freedom, please support our project âa commission-free brokerage for developersâ by joining the waitlist!
Follow Alpaca on Medium, and @AlpacaHQ on twitter. For exclusive content, keep an eye on the Alpaca Blog.
Easily Build a Stock Trading Bot Using Broker API was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.