Step-by-Step Setup of Your Automated Home Trading System

Step-by-Step Setup of Your Automated Home Trading SystemThis article walks you through the step-by-step setup of your automated home trading system, built in Python.

Luke PoseyBlockedUnblockFollowFollowingMay 29Disclaimer: Nothing herein is financial advice, and NOT a recommendation to trade real money.

Many platforms exist for simulated trading (paper trading) which can be used for building and developing the methods discussed.

Please use common sense and always first consult a professional before trading or investing.

This article builds upon a recent article I wrote called “Building an Automated Trading System From the Comfort of Your Own Home.

” I recommend reading that article first before following the more technical steps showcased in this article.

That article gives you a high level view, whereas this one walks through some actual code snippets, step-by-step.

Data SourceThe essential ingredient of any automated trading system is reliable, accurate data.

I am a massive fan of IEX and use it for almost all of my trading projects.

There are tons of valuable IEX endpoints.

Here’s just a few I use consistently:Price DataFundamental to any trading project is reliable price data.

You may be inclined to gather this data from your trading platform rather than from IEX.

Most likely, IEX will have much richer data with more features and better documentation.

You can also access real-time quotes.

We will use these quotes for our trading system later in the article.

chartDF OutputVarious Asset StatisticsFinancials DataframeBalance Sheet DataframeAnd many many more endpoints available.

Link to IEX documentation.

Step-by-Step Order IntegrationAlpaca is an API built for algorithmic traders, trading bots, and building applications; it allows for simple order execution.

You will first need to acquire your secret key to continue with the setup.

The examples shown here are for paper trading.

You can check your account status with the following (should return ‘ACTIVE’ if everything is working correctly):I add the environment variable for setting this to paper trading.

I highly recommend getting experience with this API through paper trading before EVER trying to use real money.

Your paper trading account will include $100,000 fake dollars that you can use as you see fit.

Your account is limited to 200 trades/min which is more than enough for most low to medium frequency trading applications.

You can reset the account from your dashboard at any time.

Submitting an order can be simply executed.

You can follow the Python API documentation here.

REST.

submit_order(symbol, qty, side, type, time_in_force, limit_price=None, stop_price=None, client_orer_id=None)symbol is the asset you want to buy or sell, qty is the quantity of the asset, side determines whether you wish to buy or sell, type is the type of order which can be market, limit, or stop, limit_price is the price limit you are setting if you choose a limit order (market order will execute at market price)You can cancel an order at any time using:Note: the Alpaca documentation says to use “order id” but “id” is the correct usage for cancelling the order.

View your orders:View your positions:Integrating Strategy With Order ExecutionWe have covered data gathering and order execution.

Now how can we integrate our trading strategy with the order API?The following is an oversimplified example that you SHOULD NOT use for trading.

This is a pretend strategy that you SHOULD NOT copy paste for real use; it merely serves as an example for learning purposes.

This example very simply will execute a market buy of 25 shares of AMD stock if the AMD price falls below a certain threshold and the AMD position in your account is greater than 1 share and less than 10.

There are many different ways to get this script to continually check if the conditional is true.

Your mind may jump to a cron job or daemon.

More simply, for very simple scripts we can do something like the following.

This script will execute the desired function every five seconds.

Many Alternatives: LINKExtra: Linking to Brokerage APIsLinking to your everyday brokerage is another option.

You will need authentication, just like in the above examples.

You will use your token to access your account and perform GET, POST, DELETE, etc…Here is example documentation from a popular brokerage.

You can find similar documentation for REST APIs and other services from most brokerages.

TradeStation is probably the most popular service for this part.

Example Order DocumentationIf this interests you, I highly recommend creating a paper trading account (most brokerages offer this) and testing execution of simulated trades on your preferred brokerage before you EVER try to execute real trades.

//Order:{ "session": "'NORMAL' or 'AM' or 'PM' or 'SEAMLESS'", "duration": "'DAY' or 'GOOD_TILL_CANCEL' or 'FILL_OR_KILL'", "orderType": "'MARKET' or 'LIMIT' or 'STOP' or 'STOP_LIMIT' or 'TRAILING_STOP' or 'MARKET_ON_CLOSE' or 'EXERCISE' or 'TRAILING_STOP_LIMIT' or 'NET_DEBIT' or 'NET_CREDIT' or 'NET_ZERO'", "cancelTime": { "date": "string", "shortFormat": false }, "complexOrderStrategyType": "'NONE' or 'COVERED' or 'VERTICAL' or 'BACK_RATIO' or 'CALENDAR' or 'DIAGONAL' or 'STRADDLE' or 'STRANGLE' or 'COLLAR_SYNTHETIC' or 'BUTTERFLY' or 'CONDOR' or 'IRON_CONDOR' or 'VERTICAL_ROLL' or 'COLLAR_WITH_STOCK' or 'DOUBLE_DIAGONAL' or 'UNBALANCED_BUTTERFLY' or 'UNBALANCED_CONDOR' or 'UNBALANCED_IRON_CONDOR' or 'UNBALANCED_VERTICAL_ROLL' or 'CUSTOM'", "quantity": 0, "filledQuantity": 0, "remainingQuantity": 0, "requestedDestination": "'INET' or 'ECN_ARCA' or 'CBOE' or 'AMEX' or 'PHLX' or 'ISE' or 'BOX' or 'NYSE' or 'NASDAQ' or 'BATS' or 'C2' or 'AUTO'",}This single request will return a JSON full of rich information.

You can use this to keep track of your orders, view specifics of an order, etc.

Another great endpoint is a GET for quotes.

While IEX will provide more data, this endpoint will get you the latest available price data from the brokerage, necessary for checking before executing a trade.

You can get a JSON full of data from all sorts of assets like stocks, mutual funds, ETFs, options, futures, and others.

//Option:{ "symbol": "string", "description": "string", "bidPrice": 0, "bidSize": 0, "askPrice": 0, "askSize": 0, "lastPrice": 0, "lastSize": 0, "openPrice": 0, "highPrice": 0, "lowPrice": 0, "closePrice": 0, "netChange": 0, "totalVolume": 0, "quoteTimeInLong": 0, "tradeTimeInLong": 0, "mark": 0, "openInterest": 0, "volatility": 0, "moneyIntrinsicValue": 0, "multiplier": 0, "strikePrice": 0, "contractType": "string", "underlying": "string", "timeValue": 0, "deliverables": "string", "delta": 0, "gamma": 0, "theta": 0, "vega": 0, "rho": 0, "securityStatus": "string", "theoreticalOptionValue": 0, "underlyingPrice": 0, "uvExpirationType": "string", "exchange": "string", "exchangeName": "string", "settlementType": "string"}Final ThoughtsWe walked through the very simple process of setting up your automated trading system, step-by-step.

If you have any questions on the process feel free to drop a comment below and I will try to explain the process or clarify any missing details.

Happy trading!.

. More details

Leave a Reply