Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stucking few SCTR #102

Open
csayantan opened this issue Apr 14, 2016 · 6 comments
Open

stucking few SCTR #102

csayantan opened this issue Apr 14, 2016 · 6 comments

Comments

@csayantan
Copy link

from nsepy import get_history
from datetime import date
import pandas as pd
import requests
from io import BytesIO 
import certifi
from dateutil.relativedelta import relativedelta
#import numpy as np
#import matplotlib.pyplot as plt
import datetime
import numpy as np
import matplotlib.colors as colors
import matplotlib.finance as finance
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import talib as ta
from talib import MA_Type

#Long-Term Indicators (weighting)

#  * Percent above/below 200-day EMA (30%)
#  * 125-Day Rate-of-Change (30%)

#Medium-Term Indicators (weighting)

#  * Percent above/below 50-day EMA (15%)
#  * 20-day Rate-of-Change (15%)

# Short-Term Indicators (weighting)

#  * 3-day slope of PPO-Histogram (5%)
#  * 14-day RSI (5%)

url1 = 'https://www1.nseindia.com/content/indices/ind_nifty_Alpha_Index.csv'
def datainpy(url):

    headers = { 'Accept' : '*/*',
                'User-Agent' : 'Mozilla/5.0',
                'Refers' : 'http://www.nseindia.com',
                'Connection' : 'keep-alive'
              }

    getContents = requests.get(url,headers=headers).content
    symbol_list=pd.read_csv(BytesIO(getContents))

    print(symbol_list.head())

    for eachSymbol in symbol_list['Symbol']:
        stock = get_history(symbol = eachSymbol,
                            start = date(2000,3,20),
                            end = date(2016,3,30))
        stock.drop_duplicates(inplace=True)
        stock.drop(stock.columns[[0,1,2,7,8,10,11,12,13]], axis = 1, inplace = True)
        print (stock.head())
        print(stock.head())
        stock.index=pd.to_datetime(stock.index)
        #stock.to_csv('./HistoricalData//' + eachSymbol + '.csv' , date_format='%Y%m%d')
            return stock;



StockData=datainpy(url=url1)   
print(StockData.head())
type(StockData)
np.set_printoptions(precision=3)

ppoHist=ta.PPO(StockData['Last'].values,12,26)

RSI14=ta.RSI(StockData['Last'].values,14)

DMA200=ta.SMA(StockData['Last'].values,200)

EMA50=ta.EMA(StockData['Last'].values,14)

ROC125=ta.ROC(StockData['Last'].values,125)

ROC20=ta.ROC(StockData['Last'].values,14)

weightPctDiffOfEMA50=(((StockData['Last'].values-EMA50)*100)/StockData['Last'].values)*15

weightPctDiffOfDMA200=(((StockData['Last'].values-DMA200)*100)/StockData['Last'].values)*30

weightROC125=ROC125*30

weightROC20=ROC20*15

weightRSI14=RSI14*5

weightppoHist=ppoHist*5
print(weightppoHist)
SCTR=(weightPctDiffOfDMA200+weightPctDiffOfEMA50+weightROC125+weightROC20+weightppoHist+weightRSI14)/100

print(SCTR)

i have few specific questions here :1
1: SCTR out put is ddifferent as expected.
2.is that PPO i had considered is based on EMA.how can i get the MA_Type?
3.how can i get the slope of PPO histogram ?

i am looking for a report in html/csv file which will display all SCTR for 51 stocks daily http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:sctr

@mrjbq7
Copy link
Collaborator

mrjbq7 commented Apr 14, 2016

If you look at the help for PPO, it shows that it uses matype: 0 (Simple Moving Average):

Help on built-in function PPO in module talib.func:

PPO(...)
    PPO(real[, fastperiod=?, slowperiod=?, matype=?])

    Percentage Price Oscillator (Momentum Indicators)

    Inputs:
        real: (any ndarray)
    Parameters:
        fastperiod: 12
        slowperiod: 26
        matype: 0 (Simple Moving Average)
    Outputs:
        real

That is the default in the ta-lib C library, I believe. If you want to use a EMA or other variations of moving averages, you can choose an MA_Type:

from talib.common import MA_Type
MA_Type.SMA
MA_Type.EMA
MA_Type.WMA
MA_Type.DEMA
MA_Type.TEMA
MA_Type.TRIMA
MA_Type.KAMA
MA_Type.MAMA
MA_Type.T3

It's basically an enum of integer values from 0 (SMA) to 8 (T3).

Regarding the histogram, I'm not sure if that's exposed by ta-lib, but you could implement it yourself, something like this (not quite tested, so make sure it does what you expect):

def PPO(c, fastperiod, slowperiod, signalperiod):
    """Percentage Price Oscillator"""
    fast = EMA(c, fastperiod)
    slow = EMA(c, slowperiod)
    ppo = 100 * (fast - slow) / slow
    signal = EMA(ppo, signalperiod)
    histogram = ppo - signal
    return ppo, signal, histogram

SCTR sounds interested. I'd love to know if/when you get it working properly!

@mrjbq7
Copy link
Collaborator

mrjbq7 commented Apr 14, 2016

P.S., you can see how ta-lib C library implements PPO.

It's not quite easy to read, but:

https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_PPO.c

@csayantan
Copy link
Author

csayantan commented Apr 18, 2016

I am getting values like : but it should be something like Stock Chart suggested.

[ nan nan nan ..., -1.142 -2.484 -2.251]
[ nan nan nan ..., 216.517 -287.457 -9.956]

what i missed actually ?

@mrjbq7
Copy link
Collaborator

mrjbq7 commented Apr 18, 2016

There must be some difference between how they are calculated...

@csayantan
Copy link
Author

If the PPO-Histogram's slope is less than -45 degrees, 0 points are contributed to the score. Otherwise, 5% of ((Slope + 1) x 50) is contributed.

missing slopes of the histogram.

@TravelTrader
Copy link

Did anyone solved the calculation of SCTR already?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants