Beispiel #1
0
def rating_calculation(date_150,date_50,date_21,df,date_calculation,symbol):
    
    date_150_data=pricing.get_specific_date_value(df, date_150,'close')
    date_50_data=pricing.get_specific_date_value(df, date_50,'close')
    date_21_data=pricing.get_specific_date_value(df, date_21,'close')
    latest_data=pricing.get_specific_date_data(df, date_calculation)
    
    long_term_rating=30*(latest_data['close']-latest_data['sma200'])/(latest_data['sma200'])
    +            30*(latest_data['close']-date_150_data)/(date_150_data)
    
    medium_term_rating=15*(latest_data['close']-latest_data['sma100'])/(latest_data['sma100'])
    +            15*(latest_data['close']-date_50_data)/(date_50_data)
    
    short_term_rating=5*(latest_data['close']-latest_data['sma20'])/(latest_data['sma20'])
    +            5*(latest_data['close']-date_21_data)/(date_21_data)
    
    rating=long_term_rating+medium_term_rating+short_term_rating
    
    return rating
Beispiel #2
0
def rating_calculation(date_150, date_50, date_21, df, date_calculation,
                       symbol):

    date_150_data = pricing.get_specific_date_value(df, date_150, 'close')
    date_50_data = pricing.get_specific_date_value(df, date_50, 'close')
    date_21_data = pricing.get_specific_date_value(df, date_21, 'close')
    latest_data = pricing.get_specific_date_data(df, date_calculation)

    long_term_rating = 30 * (latest_data['close'] -
                             latest_data['sma200']) / (latest_data['sma200'])
    +30 * (latest_data['close'] - date_150_data) / (date_150_data)

    medium_term_rating = 15 * (latest_data['close'] -
                               latest_data['sma100']) / (latest_data['sma100'])
    +15 * (latest_data['close'] - date_50_data) / (date_50_data)

    short_term_rating = 5 * (latest_data['close'] -
                             latest_data['sma20']) / (latest_data['sma20'])
    +5 * (latest_data['close'] - date_21_data) / (date_21_data)

    rating = long_term_rating + medium_term_rating + short_term_rating

    return rating
Beispiel #3
0
def calculate_technical(df_symbol, symbol, df_mkt, start_date_time,
                        end_date_time, hist_dates, days_back):

    #list_drop_cloumns = [ 'open', 'high','low','volume']
    df_symbol_close = df_symbol[['close']]

    df_mkt_close = df_mkt[['close']]
    mom = abstract.MOM(df_symbol_close, timeperiod=5)
    df_merged = abstract.MACD(df_symbol_close,
                              fastperiod=12,
                              slowperiod=26,
                              signalperiod=9)

    #df_merged=macd.apply(np.round)

    df_std = abstract.STDDEV(df_symbol_close.pct_change(), timeperiod=100)
    df_merged['stddev'] = df_std
    df_merged['volatility'] = df_std * 100 * math.sqrt(252)
    rsi = abstract.RSI(df_symbol_close).round(2)
    sma20 = abstract.SMA(df_symbol_close, timeperiod=20).round(2)

    sma100 = abstract.SMA(df_symbol_close, timeperiod=100).round(2)
    sma200 = abstract.SMA(df_symbol_close, timeperiod=200).round(2)
    sma3 = abstract.SMA(df_symbol_close, timeperiod=3).round(2)

    sma5 = abstract.SMA(df_symbol_close, timeperiod=5).round(2)

    sma9 = abstract.SMA(df_symbol_close, timeperiod=9).round(2)

    sma13 = abstract.SMA(df_symbol_close, timeperiod=13).round(2)

    sma25 = abstract.SMA(df_symbol_close, timeperiod=25).round(2)

    sma50 = abstract.SMA(df_symbol_close, timeperiod=50).round(2)

    sma90 = abstract.SMA(df_symbol_close, timeperiod=90).round(2)

    sma36 = abstract.SMA(df_symbol_close, timeperiod=36).round(2)

    sma150 = abstract.SMA(df_symbol_close, timeperiod=150).round(2)

    df_merged['mom'] = mom
    df_merged['sma20'] = sma20
    df_merged['sma50'] = sma50
    df_merged['sma100'] = sma100
    df_merged['sma200'] = sma200
    df_merged['rsi'] = rsi
    df_merged['close'] = df_symbol['close']
    df_merged['open'] = df_symbol['open']
    df_merged['low'] = df_symbol['low']
    df_merged['high'] = df_symbol['high']
    df_merged['volume'] = df_symbol['volume']
    df_merged['sma_volume_6month'] = pd.rolling_mean(df_merged['volume'],
                                                     window=120).round(2)
    df_merged = df_merged.dropna()
    df_merged['symbol'] = symbol
    df_merged['rsi_value'] = df_merged['rsi'].apply(
        rsi_manager.calculate_rsi_values)
    df_merged['sma3'] = sma3
    df_merged['sma5'] = sma5
    df_merged['sma9'] = sma9
    df_merged['sma13'] = sma13
    df_merged['sma20'] = sma20
    df_merged['sma25'] = sma25
    df_merged['sma50'] = sma50
    df_merged['sma90'] = sma90
    df_merged['sma36'] = sma36
    df_merged['sma150'] = sma150
    df_merged['Relative_strength'] = relative_strength(
        df_symbol_close, df_mkt_close, symbol)['Relative_strength']

    df_merged['stdabove'] = df_merged.apply(calculate_stdabove, axis=1)

    df_merged['date'] = df_merged.index

    df_res = df_symbol.apply(calc_res, axis=1)
    df_merged = pd.concat([df_merged, df_res], axis=1)
    df_rating = rating_manager.calc_rating_history(df_merged, days_back,
                                                   symbol)
    df_trends = df_merged.apply(
        lambda row: trend_manager.trend_calculation(row), axis=1)

    df_merged = pd.concat([df_merged, df_trends], axis=1)

    crossover_manager.TrendChangePositive(df_merged, "short_trend",
                                          alert_constants.TREND_SHORT)
    crossover_manager.TrendChangePositive(df_merged, "inter_trend",
                                          alert_constants.TREND_INTERMEDIATE)
    crossover_manager.TrendChangePositive(df_merged, "long_trend",
                                          alert_constants.TREND_LONG)

    crossover_manager.TrendChangeNegative(df_merged, "short_trend",
                                          alert_constants.TREND_SHORT)
    crossover_manager.TrendChangeNegative(df_merged, "inter_trend",
                                          alert_constants.TREND_INTERMEDIATE)
    crossover_manager.TrendChangeNegative(df_merged, "long_trend",
                                          alert_constants.TREND_LONG)

    df_merged['rating'] = df_rating['rating']

    df_merged = df_merged.replace([np.inf, -np.inf], np.nan)

    df_merged = df_merged.dropna()
    print "********************************************************************************"
    print "********************************************************************************"
    print "********************************************************************************"
    #print df_merged

    if (df_merged is None or df_merged.symbol.count() == 0):
        return

    logger.info("Saving history for Symbol " + symbol + " length = " +
                str(len(df_merged)))
    df_merged.set_index('date', inplace=True)

    alert_manager.relative_strength(df_merged)

    alert_manager.fullGapPositive(df_merged)
    alert_manager.fullGapNegative(df_merged)
    alert_manager.partialGapPositive(df_merged)
    alert_manager.partialGapNegative(df_merged)

    alert_manager.keyReversalPositive(df_merged)
    alert_manager.keyReversalNegative(df_merged)

    alert_manager.volumePositive(df_merged)
    alert_manager.volumeNegative(df_merged)

    crossover_manager.smacrossovers(df_merged)
    crossover_manager.macd_crossovers(df_merged)
    crossover_manager.obos_alerts(df_merged)

    dbdao.save_dataframe(df_merged, "df_history")

    #alert_manager.relative_strength(df_merged, df_spy, symbol)

    df_merged['stdabove_prev'] = df_merged['stdabove'].shift(1)

    #latest data calculations
    return_data = {}

    return_data.update(calculate_beta(df_symbol_close, df_mkt_close, symbol))

    return_data.update(calculate_prices_at_dates(df_symbol_close, hist_dates))

    monthly_date = hist_dates['Monthly']
    weekly_date = hist_dates['Weekly']

    df_latest = df_merged.tail(1)

    df_latest_sign = df_latest.apply(calc_signs, axis=1)

    df_latest = pd.concat([df_latest, df_latest_sign], axis=1)

    #df_latest[['sma_20day_sign']]=df_latest.apply(calc_signs,axis=1)
    df_latest['volatility_monthly'] = price_manager.get_specific_date_value(
        df_merged, monthly_date, 'volatility')
    df_latest['volatility_weekly'] = price_manager.get_specific_date_value(
        df_merged, weekly_date, 'volatility')
    df_latest['std50days'] = df_latest['stdabove']
    df_latest['date'] = df_latest.index
    df_latest.set_index('date', inplace=True)

    for key, value in return_data.iteritems():

        df_latest[key] = value

    return df_latest
Beispiel #4
0
def calculate_technical(df_symbol,symbol,df_mkt,start_date_time,end_date_time,hist_dates,days_back): 
 
    #list_drop_cloumns = [ 'open', 'high','low','volume']
    df_symbol_close = df_symbol[['close']]
    
    df_mkt_close = df_mkt[['close']]
    mom=abstract.MOM(df_symbol_close, timeperiod=5)    
    df_merged=abstract.MACD(df_symbol_close,fastperiod=12,slowperiod=26,signalperiod=9)
   
    #df_merged=macd.apply(np.round)
    
    df_std= abstract.STDDEV(df_symbol_close.pct_change(),timeperiod=100)
    df_merged['stddev']=df_std
    df_merged['volatility']=df_std*100*math.sqrt(252)
    rsi=abstract.RSI(df_symbol_close).round(2)    
    sma20 = abstract.SMA(df_symbol_close, timeperiod=20).round(2)

    sma100 = abstract.SMA(df_symbol_close, timeperiod=100).round(2)
    sma200 = abstract.SMA(df_symbol_close, timeperiod=200).round(2)
    sma3 = abstract.SMA(df_symbol_close, timeperiod=3).round(2)
    
    sma5 = abstract.SMA(df_symbol_close, timeperiod=5).round(2)
    
    sma9 = abstract.SMA(df_symbol_close, timeperiod=9).round(2)
    
    sma13 = abstract.SMA(df_symbol_close, timeperiod=13).round(2)
    
        
    
    sma25 = abstract.SMA(df_symbol_close, timeperiod=25).round(2)
    
    sma50 = abstract.SMA(df_symbol_close, timeperiod=50).round(2)
    
    sma90 = abstract.SMA(df_symbol_close, timeperiod=90).round(2)
    
    sma36 = abstract.SMA(df_symbol_close, timeperiod=36).round(2)
    
    sma150 = abstract.SMA(df_symbol_close, timeperiod=150).round(2)
   
    
    
    
    df_merged['mom']=mom
    df_merged['sma20']=sma20
    df_merged['sma50']=sma50
    df_merged['sma100']=sma100
    df_merged['sma200']=sma200
    df_merged['rsi']=rsi    
    df_merged['close']=df_symbol['close']
    df_merged['open']=df_symbol['open']
    df_merged['low']=df_symbol['low']
    df_merged['high']=df_symbol['high']
    df_merged['volume']=df_symbol['volume']
    df_merged['sma_volume_6month']= pd.rolling_mean(df_merged['volume'], window=120).round(2)
    df_merged=df_merged.dropna()
    df_merged['symbol']=symbol
    df_merged['rsi_value'] = df_merged['rsi'].apply(rsi_manager.calculate_rsi_values )
    df_merged['sma3']=sma3
    df_merged['sma5']=sma5
    df_merged['sma9']=sma9
    df_merged['sma13']=sma13
    df_merged['sma20']=sma20
    df_merged['sma25']=sma25
    df_merged['sma50']=sma50
    df_merged['sma90']=sma90
    df_merged['sma36']=sma36
    df_merged['sma150']=sma150
    df_merged['Relative_strength']=relative_strength(df_symbol_close, df_mkt_close, symbol)['Relative_strength']
  
    df_merged['stdabove']=df_merged.apply(calculate_stdabove,axis=1)
    
    df_merged['date']=df_merged.index
    
    df_res=df_symbol.apply(calc_res,axis=1)
    df_merged=pd.concat([df_merged,df_res],axis=1)
    df_rating=rating_manager.calc_rating_history(df_merged, days_back, symbol)
    df_trends=df_merged.apply(lambda row : trend_manager.trend_calculation(row),axis=1)

    df_merged=pd.concat([df_merged,df_trends],axis=1)
 
    crossover_manager.TrendChangePositive(df_merged,"short_trend",alert_constants.TREND_SHORT)
    crossover_manager.TrendChangePositive(df_merged,"inter_trend",alert_constants.TREND_INTERMEDIATE)
    crossover_manager.TrendChangePositive(df_merged,"long_trend",alert_constants.TREND_LONG)
    
    
    crossover_manager.TrendChangeNegative(df_merged,"short_trend",alert_constants.TREND_SHORT)
    crossover_manager.TrendChangeNegative(df_merged,"inter_trend",alert_constants.TREND_INTERMEDIATE)
    crossover_manager.TrendChangeNegative(df_merged,"long_trend",alert_constants.TREND_LONG)

    
    df_merged['rating']=df_rating['rating']
    
    df_merged=df_merged.replace([np.inf, -np.inf], np.nan)
    
    df_merged=df_merged.dropna()
    print "********************************************************************************"
    print "********************************************************************************"
    print "********************************************************************************"
    #print df_merged
   
    if(df_merged is None or df_merged.symbol.count()==0):
        return
    
    logger.info("Saving history for Symbol "+symbol + " length = "+ str(len(df_merged)))
    df_merged.set_index('date',inplace=True)  
    
    
    
    alert_manager.relative_strength(df_merged)
    
    
    alert_manager.fullGapPositive(df_merged)
    alert_manager.fullGapNegative(df_merged)
    alert_manager.partialGapPositive(df_merged)
    alert_manager.partialGapNegative(df_merged)
        
    alert_manager.keyReversalPositive(df_merged)
    alert_manager.keyReversalNegative(df_merged)
    
        
    alert_manager.volumePositive(df_merged)
    alert_manager.volumeNegative(df_merged)
    
    
    crossover_manager.smacrossovers(df_merged)
    crossover_manager.macd_crossovers(df_merged)
    crossover_manager.obos_alerts(df_merged)
    
    
    
    dbdao.save_dataframe(df_merged, "df_history")
   
    
        
    #alert_manager.relative_strength(df_merged, df_spy, symbol)

    
    
    
    
    df_merged['stdabove_prev']=df_merged['stdabove'].shift(1)
    
    #latest data calculations
    return_data={}
    
    return_data.update(calculate_beta(df_symbol_close,df_mkt_close,symbol))
    
    
    return_data.update(  calculate_prices_at_dates(df_symbol_close,hist_dates))
    
    monthly_date = hist_dates['Monthly']
    weekly_date = hist_dates['Weekly']
    
    df_latest=df_merged.tail(1) 
    
    df_latest_sign=df_latest.apply(calc_signs,axis=1)
    
    df_latest=pd.concat([df_latest,df_latest_sign],axis=1)
    
    #df_latest[['sma_20day_sign']]=df_latest.apply(calc_signs,axis=1)
    df_latest['volatility_monthly']= price_manager.get_specific_date_value(df_merged,monthly_date,'volatility')
    df_latest['volatility_weekly']= price_manager.get_specific_date_value(df_merged,weekly_date,'volatility')
    df_latest['std50days']=df_latest['stdabove']
    df_latest['date']=df_latest.index
    df_latest.set_index('date',inplace=True)
    
        
    
    
    for key, value in return_data.iteritems():
        
        df_latest[key]=value
    
    
    
    return df_latest