pythonfinancestocksalgorithmic-trading

Python Finance: How to use macd indicator for signals strategy?


I am trying to get my head around stock data and it's implementation in python. In starting I am using MACD indicator in Python stockstats library.

Thing I want to know, if I have 100 OHLC entries of a certain stock, how can I use MACD output to produce signals whether I should Buy or Sell or Hold? In Graph one can visualize but in terms of programming how do I get the idea? Sample code given below:

import pandas as pd
from stockstats import StockDataFrame as Sdf
from pandas_datareader import data, wb

data = pd.read_csv('data.csv')

stock = Sdf.retype(data)
print(stock.get('pdi'))

It produces output as given below:

0       0.000000e+00
1      -8.951923e-08
2       1.758777e-07
3      -3.844324e-08
4      -2.217396e-07
5      -3.893329e-07
6      -2.373225e-07
7      -5.082528e-07
8      -8.260595e-07
9      -1.099751e-06
10     -1.429675e-06
11     -1.211562e-06
12     -8.230303e-07
13     -5.163039e-07
14     -4.979626e-07
15     -4.777865e-07
16     -6.217018e-07
17     -1.145459e-06
18     -1.461550e-06
19     -1.744250e-06
20     -1.677791e-06
21     -1.820319e-06
22     -2.024092e-06
23     -1.958413e-06
24     -2.450087e-06
25     -2.805521e-06
26     -3.443776e-06
27     -4.047889e-06
28     -4.839084e-06
29     -5.208106e-06
            ...     
1410    4.856951e-06
1411    6.075773e-06
1412    9.159968e-06
1413    9.985022e-06
1414    1.069234e-05
1415    1.140865e-05
1416    1.136520e-05
1417    1.156541e-05
1418    1.065633e-05
1419    9.176497e-06
1420    9.275813e-06
1421    8.254755e-06
1422    7.583274e-06
1423    7.301820e-06
1424    6.959007e-06
1425    6.292826e-06
1426    8.411427e-06
1427    8.746155e-06
1428    1.112640e-05
1429    1.299290e-05
1430    1.398810e-05
1431    1.441297e-05
1432    1.509612e-05
1433    1.462091e-05
1434    1.436198e-05
1435    1.390849e-05
1436    1.419959e-05
1437    1.554140e-05
1438    1.884861e-05
1439    2.163656e-05
Name: macd, Length: 1440, dtype: float64

Solution

  • Here you go, with explanation in comments.

    import pandas as pd
    from stockstats import StockDataFrame as Sdf
    
    data   = pd.read_csv('data.csv')
    
    stock  = Sdf.retype(data)
    
    signal = stock['macds']        # Your signal line
    macd   = stock['macd']         # The MACD that need to cross the signal line
    #                                              to give you a Buy/Sell signal
    listLongShort = ["No data"]    # Since you need at least two days in the for loop
    
    for i in range(1, len(signal)):
        #                          # If the MACD crosses the signal line upward
        if macd[i] > signal[i] and macd[i - 1] <= signal[i - 1]:
            listLongShort.append("BUY")
        #                          # The other way around
        elif macd[i] < signal[i] and macd[i - 1] >= signal[i - 1]:
            listLongShort.append("SELL")
        #                          # Do nothing if not crossed
        else:
            listLongShort.append("HOLD")
    
    stock['Advice'] = listLongShort
    
    # The advice column means "Buy/Sell/Hold" at the end of this day or
    #  at the beginning of the next day, since the market will be closed
    
    print(stock['Advice'])