Automation Concepts


What is a automated trading strategy

Automated trading strategy is a collection of rules (on instructions) that specify which stocks to trade and when to trade them. Unlike one-time Automated Trade , automated strategy is defined to run on multiple securities and specifies both the entry condition to open a new position and exit condition to close open positions. The defined set of instructions can be based on fundamental and technical indicators, price or any other mathematical model. There are various types of automated strategies such as trend-following strategy, contrarian strategy etc.

Components of Trading Strategy

Automated trading strategy will always have following components:

trade history
Automation Scope

A huge number of stocks are traded in the market - some are penny stocks, some are large stocks, some tech stocks, and some are retail stocks. You probably don’t want to run your strategy against all stocks in the market because it's hard to come up with a winning strategy that works on all kinds of stocks. The first step is to narrow down (or limit) the set of stocks you want to use for your strategy. Investfly gives you three options to do this:

  1. Predefined Lists: You can use our predefined list of stocks such as SP500, DOW_JONES_INDUSTRIALS

  2. Custom List: You can provide your own custom list of stock symbols separate by comma ( e.g AAPL, WMT, TSLA)

  3. Dynamic Filter: You can provide a expression based on fundamental indicators (e.g MarketCap > 100B)

You can think of automation scope as an instruction to determine which stocks to trade.

Open Trade Condition (Entry Condition)

Entry conditions specify when to trade the stocks specified above. Entry condition is all about timing, and hence you must use indicators that vary with time (e.g Price, SMA, RSI). For e.g, you shouldn’t normally use fundamental metrics like Net Earnings in the entry condition because it won’t change every minute or even every day. You can however use Net Earnings to define Automation Scope as specified above. The entry conditions are simply logical expression defined using expression builder. You will use fundamental and technical indicators, price, volume, numeric values and mathematical operators to define logical expression. Please read Expression Overview to understand more details about logical expressions and how they work.

Trade signal generation

As described in expression overview section, a trade signal is generated for any security when the logical expression for that security transitions from false to true state. Since the scope may define multiple securities, trade signal will be continuously generated for many of them. There is no guarantee on the order in which the trade signal is generated for different securities even if both match the criteria at exactly the same time. It's helpful to imagine trade signals coming in as a random continuous stream in response to random continuous stream of quotes that we receive from the market. There are two main reasons for this non-deterministic (ie. random) behavior

  1. Price quotes we receive from the market comes in random security order. They are ordered by timestamp, but if we have multiple quotes for the same time, the order of securities is random.
  2. The evaluation of indicators and expression is done in highly parallel manner. Parallel computation introduce non-deterministic nature in the system. If we do it sequentially (instead of parallel), the system will be too slow to intake all quotes from the market in real time.
trade history

This non-deterministic behavior means that even if you have two copies of the exact same strategy that are deployed to trading accounts, they could trade different stocks and have completely different outcome. The only thing guaranteed is that all your rules (entry and exit conditions) are always met. If your entry and exit conditions are really working, then regardless of non deterministic behavior, your strategy should still yield positive returns. If the strategy shows favorable result for only one possible outcome, then it can be attributed to nothing more than chance or coincidence. Hence, in some sense, this non-deterministic behavior works to your advantage by providing you with additional assurance that your strategy is indeed working.

Trading Strategy is non-deterministic and the same strategy can result in different outcomes.

Portfolio Allocation

As described above, your trading account will continuously receive a randomized stream of trade signals (BUY or SELL). The only thing that is guaranteed is that all signals were generated after meeting your entry conditions with the latest price data. You portfolio will be in different states ( with different buying power, cash balance, margin requirements, different set of stocks in open positions ) as these trade signals are received. So next, you must define what to do with in your portfolio when a trade signal is received. Should you open position for every trade signal received? The answer is not straight forward. Assume you have $1000 remaining cash balance at some point in time, and you receive an open trade signal. Should you spend all $1000 on that stock, or should you only spend $500 and save the remaining $500 for the next trade signal? Or spend only $200? Generally, this will be balance between the number of unique stocks you want to have in your open positions and the amount you want to invest in each stock. If you spend too much cash on one stock, then you will have fewer open positions. If you spend little money, you can have more open positions.

To simplify this task, Investfly allows you to pick two simple approaches:

  • Invest fixed cash amount: In this approach, you will specify a fixed dollar amount (e.g $2000) to invest each time as the trade signal is received. Investfly will keep spending $2000 each time until your account runs out of cash. Then all subsequent trade signals are ignored until one of the cash is released back to the account. The downside of this approach is that as the portfolio value grows or shrinks based on profit and loss, the amount you specified originally may not be valid anymore and may require update. Assume you started with $10,000 and you specify to invest $2000 for each stock, thinking that you will have 5 open positions. What if in few months, you make profit and your account has $100,000 in cash. Then, you probably should update from $2000 to $20,000 to reflect the increase in account value.

  • Maintain fixed number of open position: In this approach, you will specify how many unique stocks you want to have in your open positions and strategy will automatically adjust the amount based on portfolio value. For example, if you started with $10,000 and you specify 5 unique stocks to maintain in your portfolio, strategy will originally invest $10,000 / 5 = $2000 for each stock. But if you make profit and the account value reaches $100,000, then strategy will invest $100,000 / 5 = $20,000 for each stock. This is the recommended choice for most users.

Exit Condition

The last thing you need is a set of exit conditions that determine when the open positions should be closed. Investfly support standard exit conditions such as target profit, target loss, trailing loss. Additionally we also support

  1. Timeout condition - the position is close after specified timeout period (e.g 3 days, 20 minutes etc)
  2. Custom close condition - Similar to entry condition, you can specify a logical expression to define a close condition.

Overall, these are the major components of the trading strategy. However, there are more details that we will cover in Create Trading Strategy