Trade IWB on News Sentiment
Intro
This strategy creates a portfolio of IWB (iShares Russell 1000 ETF) that is bought based on recent news sentiment. The state of the news being reported has an incredible effect on the stock market, as popular reporting agencies can directly increase/decrease people’s optimism about the economy, leading to a snowball effect, of sorts. After all, a positive article can make anyone from a day trader to an amateur trader buy shares in a company. More on this here.
A major challenge to trading based on consumer optimism is the impossibility of assigning a unit of measure to people’s opinions about the economy. While optimism cannot be measured in any real units, we have a different method of measuring consumer sentiment about the economy: when a news article is published, it is given a ranking of -1, 0, or 1 corresponding directly to negative, neutral, or positive in feelings about the economy. To gauge total sentiment in a recent period, these values are averaged. In this way, positive sentiment value indicates consumer optimism whereas a negative sentiment value indicates consumer pessimism.
While we cannot guarantee any particular return on investment due to news, consider this portfolio for an easily managed recurring algorithmic trading strategy. For more information on the stock holdings within IWB, see here.
Strategy
The following image is an example of our prewritten Trade IWB on News Sentiment algorithm.
At start, we run the first flow at the top of the page which defines all of the necessary trading values and tables, to be run once each time an overnight trade algo is created. At the beginning of the flow we see the following two nodes.
The inject node titled “GO” is used to begin the flow. Within the first function node titled “Define Trading Values” we see the following block of code, where we define all of the initial parameters of the trade.
On line 6, we define the ticker we would like to trade on.
On line 10, we define the name of the algorithm, to be reported on the front end.
On line 11, we define the initial size of the portfolio, to be reported on the front end.
On line 12, we define whether we would like to use the live or paper Alpaca account.
On line 13, we define the desired number of shares to trade with.
On line 14, we define whether we are trading stocks or crypto.
On line 15, we define whether the algorithm is currently running or not, to be reported on the front end.
On line 16, we define whether the algo is interday or intraday, to be reported on the front end.
On line 17, we define the benchmark for the trade.
Changing any of these values will result in a different trade (i.e. different ticker or size) or a change in front end reporting. For consistency, ensure all values are correct before running.
These are all of the necessary values that we as algorithmic traders must specify before running a trade. The rest of the nodes in this flow (from the first delay node to the last postgres node) function to store information from the algorithm into tables in postgres in order to update the front end. These nodes need not be altered.
Moving on to the flows that executes the actual trades, we look at the following buy and sell orders.
To begin with the buy order, we see an inject node followed by a switch node, as shown below.
By default, this algorithm is set to buy IWB five minutes after the market opens, every business day. This timeframe can be altered by double clicking the inject node, and editing the inject time at the bottom of the window. To unlock the gate node (that is, switch it from the “closed” to the “open” position), the “Start News Algo” node must be injected from the home menu. It should be noted that injecting this node will also cause the algo to run; that is, the node titled “9:35 am” need not be injected once the gate is open.
Moving on, we see the following three nodes. The function node titled “trade long or short” contains all of the necessary logic to trade on the aforementioned news sentiment. If it is necessary to change this logic—for example, only execute a trade when the sentiment is above a certain threshold value—it is possible to edit the two “if” statements within the function node. For the most part, it is recommended to use the included logic: that is, trade long when the sentiment is positive and trade short when the sentiment is negative.
Next, we have the sell order flow as shown below.
Similar to the buy order, the sell order is set to run every business day at 3:55pm, just before the market closes. This timeframe can be altered by double clicking the inject node, and editing the inject time at the bottom of the window. At this point, the gate node should already be unlocked, as the “Start News Algo” button from the home page is set to unlock both the buy and sell order.
After the gate node we see a function node titled “prepare Positions query” and an Alpaca node with the purpose of attaining the current holdings of IWB. Depending on whether we are holding a long or short position during the day, the algorithm will respectively sell or buy these positions at the end of the day in order to close all positions. All of this logic is taken care of in the function node titled “close positions (single ticker),” and executed with the Alpaca node.
With this, we now have an algorithm that trades IWB intraday based on the recent news sentiments.
Other Features
The Trade IWB on News Sentiment flow contains one more feature worth mentioning. By injecting the following flow, we are able to see a summary of the trading values defined above in the node titled “Define Trading Values,” printed in the debug window.
If we are interested in seeing the current news sentiment value, we inject the following flow to display the number in the debug window.
NOTE: ALL STRATEGIES ARE PROVIDED FOR ILLUSTRATIVE PURPOSES ONLY AND SHOULD NEVER BE IMPLEMENTED IN LIVE TRADING ACCOUNTS WITHOUT CAREFUL REVIEW. MACHINETRADER IS NOT RESPONSIBLE FOR TRADING LOSES INCURRED AS A RESULT OF CODING ERRORS.