3

Adjust Your Market Risk Wisely With This Awesome Python and Google Sheets Rotati...

 3 years ago
source link: https://hackernoon.com/adjust-your-market-risk-wisely-with-this-awesome-python-and-google-sheets-rotation-dashboard-3n1e34l1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Methodology (in code):

(data is a DataFrame with ohlc prices.)

0 reactions
def get_sign(data):

    # Get sign of each bar (-1, 0, 1)
    data['sign'] = np.sign(data['close'] - data['open'])
    # Group together (including 0's at end of group)
    prev = data['sign'][0]
    g = 0
    for i, s in zip(data.index, data['sign']):
        if s == 1:
            if prev == 1:
                # continue up group
                data.loc[i, 'g'] = g
            else:
                # new up group
                g += 1
                data.loc[i, 'g'] = g
        if s == -1:
            if prev == -1:
                # continue down group
                data.loc[i, 'g'] = g
            else:
                # new down group
                g += 1
                data.loc[i, 'g'] = g
        if s == 0:
            # add to last group
            data.loc[i, 'g'] = g
        prev = s

    return data

def get_rotations(data):

    r = []
    for x in range(int(max(data['g']))):
        # if g == x or x+1, find min/max
        # if g sign is +, find the +/- rot (high)
        # if g sign is -, find -/+ rot (low)
        if data.loc[data['g'] == x, 'sign'].iloc[0] > 0:
            h = data.loc[(data['g'] == x) | (data['g'] == x+1), 'high']
            i = h.idxmax()
            h = h.max()
            r.append([i, h])
        if data.loc[data['g'] == x, 'sign'].iloc[0] < 0:
            l = data.loc[(data['g'] == x) | (data['g'] == x+1), 'low']
            i = l.idxmin()
            l = l.min()
            r.append([i, l])

    return pd.DataFrame(r)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK