Preparing the data – signal

The main part of a trading strategy (or a trading algorithm) is to decide when to trade (either to buy or sell a security or other asset). The event triggering the sending of an order is called a signal. A signal can use a large variety of inputs. These inputs may be market information, news, or a social networking website. Any combination of data can be a signal.

From the section entitled Our first algorithmic trading (buy when the price is low, and sell when the price is high), for the buy low sell high example, we will calculate the difference in the adjusted close between two consecutive days. If the value of the adjusted close is negative, this means the price on the previous day was higher than the price the following day, so we can buy since the price is lower now. If this value is positive, this means that we can sell because the price is higher.

In Python, we are building a pandas data frame getting the same dimension as the data frame containing the data. This data frame will be called goog_data_signal:

goog_data_signal = pd.DataFrame(index=goog_data.index)

Following the creation of this data frame, we will copy the data we will use to build our signal to trade. In this case, we will copy the values of the Adj Close column from the goog_data data frame:

goog_data_signal['price'] = goog_data['Adj Close']

Based on our trading strategy, we need to have a column, daily_difference, to store the difference between two consecutive days. In order to create this column, we will use the diff function from the data frame object:

goog_data_signal['daily_difference'] = goog_data_signal['price'].diff()

As a sanity check, we can use the print function to display what goog_data_signal contains:

print(goog_data_signal.head())
price daily_difference
Date
2014-01-02 552.963501 NaN
2014-01-03 548.929749 -4.033752
2014-01-06 555.049927 6.120178
2014-01-07 565.750366 10.700439
2014-01-08 566.927673 1.177307

We can observe that the daily_difference column has a non-numerical value for January 2, since it is the first row in this data frame.

We will create the signal based on the values of the column, daily_difference. If the value is positive, we will give the value 1, otherwise, the value will remain 0:

goog_data_signal['signal'] = 0.0
goog_data_signal['signal'] = np.where(goog_data_signal['daily_difference'] >; 0, 1.0, 0.0)
price daily_difference signal
Date
2014-01-02 552.963501 NaN 0.0
2014-01-03 548.929749 -4.033752 0.0
2014-01-06 555.049927 6.120178 1.0
2014-01-07 565.750366 10.700439 1.0
2014-01-08 566.927673 1.177307 1.0

Reading the column signal, we have 0 when we need to buy, and we have 1 when we need to sell.

Since we don't want to constantly buy if the market keeps moving down, or constantly sell when the market is moving up, we will limit the number of orders by restricting ourselves to the number of positions on the market. The position is your inventory of stocks or assets that you have on the market. For instance, if you buy one Google share, this means you have a position of one share on the market. If you sell this share, you will not have any positions on the market.

To simplify our example and limit the position on the market, it will be impossible to buy or sell more than one time consecutively. Therefore, we will apply diff() to the column signal:

goog_data_signal['positions'] = goog_data_signal['signal'].diff()
price daily_difference signal positions
Date
2014-01-02 552.963501 NaN 0.0 NaN
2014-01-03 548.929749 -4.033752 0.0 0.0
2014-01-06 555.049927 6.120178 1.0 1.0
2014-01-07 565.750366 10.700439 1.0 0.0
2014-01-08 566.927673 1.177307 1.0 0.0
2014-01-09 561.468201 -5.459473 0.0 -1.0
2014-01-10 561.438354 -0.029846 0.0 0.0
2014-01-13 557.861633 -3.576721 0.0 0.0

We will buy a share of Google on January 6 for a price of 555.049927, and then sell this share for a price of 561.468201. The profit of this trade is 561.468201-555.049927=6.418274.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset