Example #1
0
def estimate(conn,t_index,p_date,l_mu,l_sigma):
    #get the range from which will trigger the sigma event
    table_name = "t_enriched_bloomberg_prices"
    cur = conn.cursor()
    querySql = "select one_day_change from {} where name='{}' and post_date <'{}' order by post_date desc limit 30 ".format(table_name,t_index,p_date)
    cur.execute(querySql)
    rows = cur.fetchall()
    moving30 = []
    for row in rows:
        moving30.append(row[0])
    
    querySql = "select one_day_change from {} where name='{}' and post_date <'{}' order by post_date desc limit 90 ".format(table_name,t_index,p_date)
    cur.execute(querySql)
    rows = cur.fetchall()
    moving90 = []
    for row in rows:
        moving90.append(row[0])

    querySql  = "select current_value from {} where name='{}' and post_date <'{}' order by post_date desc limit 1 ".format(table_name,t_index,p_date)
    cur.execute(querySql)
    rows = cur.fetchall()
    previous_value = 0.0
    for row in rows:
        previous_value = float(row[0])
        
       
    m30 = sum(moving30)/len(moving30)
    m90 = sum(moving90)/len(moving90)
    std30 = calculator.calSD(moving30)
    std90 = calculator.calSD(moving90)
    
    s4Bottom = m30 - 4*std30
    s4Upper = m30 + 4*std30
    s3Bottom = m90 - 3*std90
    s3Upper = m90 + 3*std90
    
    bottom = s4Bottom
    upper = s4Upper
    if s4Bottom >= s3Bottom:
        bottom = s3Bottom
    if s3Upper <= s4Upper:
        upper = s3Upper
    
    #get the log range
    bottom = math.log((bottom+previous_value)/previous_value)
    upper = math.log((upper+previous_value)/previous_value)
    
    n_dis = stats.norm(l_mu,l_sigma)
    #compute the probility of negative sigama event(0412)
    p_0412 = n_dis.cdf(bottom)
    #compute the probility of positive sigama event(0411)
    p_0411 = 1 - n_dis.cdf(upper)
    
        
    "Insert into the prediction model"
#    sql = "insert into t_ar_prediction (post_date,stock_index,zscore30,zscore90,change_percent,price,event_type,ord) values (?,?,?,?,?,?,?,?)"
#    cur.execute(sql,(p_date,t_index,zscore30,zscore90,p_l,p_price,event_type,99))
    sql = "insert into t_ar_garch_prediction(post_date,name,type,p_mu,p_sigma,p_0411,p_0412,ord) values (?,?,?,?,?,?,?,?)"
    cur.execute(sql,(p_date,t_index,"Stock",l_mu,l_sigma,p_0411,p_0412,99))
Example #2
0
def warning_check(conn,surObj,regeFlag=False):
#   surObj = {'embersId': 'f0c030a20e28a12134d9ad0e98fd0861fae7438b', 'confidence': 0.13429584033181682, 'strength': '4', 'derivedFrom': [u'5df18f77723885a12fa6943421c819c90c6a2a02', u'be031c4dcf3eb9bba2d86870683897dfc4ec4051', u'3c6571a4d89b17ed01f1345c80cf2802a8a02b7b'], 'shiftDate': '2011-08-08', 'shiftType': 'Trend', 'location': u'Colombia', 'date': '2012-10-03', 'model': 'Finance Stock Model', 'valueSpectrum': 'changePercent', 'confidenceIsProbability': True, 'population': 'COLCAP'}
    stock_index = surObj["population"]
    trend_type = surObj["strength"]
    date = surObj["shiftDate"]
    cur = conn.cursor()
    
    try:
        pClusster = trend_type
        table_name = "t_enriched_bloomberg_prices"  
        
        sql = "select current_value from {} where name='{}' and post_date < '{}' order by post_date desc limit 1".format(table_name,stock_index,date)
        cur.execute(sql)
        result = cur.fetchone()
        current_val = float(result[0])
        
        querySql = "select one_day_change from {} where name='{}' and post_date <'{}' order by post_date desc limit 30 ".format(table_name,stock_index,date)
        cur.execute(querySql)
        rows = cur.fetchall()
        moving30 = []
        for row in rows:
            moving30.append(row[0])
        
        querySql = "select one_day_change from {} where name='{}' and post_date <'{}' order by post_date desc limit 90 ".format(table_name,stock_index,date)
        cur.execute(querySql)
        rows = cur.fetchall()
        moving90 = []
        for row in rows:
            moving90.append(row[0])
        
        m30 = sum(moving30)/len(moving30)
        m90 = sum(moving90)/len(moving90)
        std30 = calculator.calSD(moving30)
        std90 = calculator.calSD(moving90)
        
        eventType,cButtom,cUpper = dailySigmaTrends(stock_index,str(pClusster),m30,m90,std30,std90,current_val)

        dailyRecord = {}
        dailyRecord["date"] = date
        dailyRecord["cBottom"] = cButtom
        dailyRecord["cUpper"] = cUpper
        dailyRecord["currentValue"] = current_val
        
        "Construct the warning message"
        warningMessage ={}
        derivedFrom = surObj["embersId"]
        model = surObj["model"]
        event = eventType
        confidence = surObj["confidence"]
        confidenceIsProbability = surObj["confidenceIsProbability"]
        eventDate= surObj["shiftDate"]
        population = surObj["population"]
        location = surObj["location"]
        comments = surObj["comments"]
        comObj = json.loads(comments)
        
        warningMessage["derivedFrom"] = derivedFrom
        warningMessage["model"] = model
        warningMessage["eventType"] = event
        warningMessage["confidence"] = confidence
        warningMessage["confidenceIsProbability"] = confidenceIsProbability
        warningMessage["eventDate"] = eventDate
        warningMessage["population"] = population
        warningMessage["location"] = location
        warningMessage["version"] = __version__
        operateTime = datetime.now().isoformat()
        warningMessage["dateProduced"] = operateTime
        comObj["trendVersion"] = CONFIG["trendRange"]["version"]
        warningMessage["comments"] = json.dumps(comObj)
        warningMessage["description"] = "Use Bayesian to predict stock sigma events"
        
        embersId = hashlib.sha1(json.dumps(warningMessage)).hexdigest()
        warningMessage["embersId"] = embersId
        
        if not regeFlag:
            insert_warningmessage(conn,warningMessage)
        
        if eventType != "0000":
            return warningMessage
        else:
            return None
        
    except lite.Error, e:
        log.exception( "Error: %s" % e.args[0])
Example #3
0
def warningCheck(surObj):
    #    surObj = {'embersId': 'f0c030a20e28a12134d9ad0e98fd0861fae7438b', 'confidence': 0.13429584033181682, 'strength': '4', 'derivedFrom': [u'5df18f77723885a12fa6943421c819c90c6a2a02', u'be031c4dcf3eb9bba2d86870683897dfc4ec4051', u'3c6571a4d89b17ed01f1345c80cf2802a8a02b7b'], 'shiftDate': '2011-08-08', 'shiftType': 'Trend', 'location': u'Colombia', 'date': '2012-10-03', 'model': 'Finance Stock Model', 'valueSpectrum': 'changePercent', 'confidenceIsProbability': True, 'population': 'COLCAP'}
    stockIndex = surObj["population"]
    trendType = surObj["strength"]
    date = surObj["shiftDate"]

    try:
        con = common.getDBConnection()
        cur = con.cursor()
        pClusster = trendType

        sql = "select sub_sequence,last_price from t_daily_stockindex where stock_index=? and date<? order by date desc limit 1"
        cur.execute(sql, (stockIndex, date))
        row = cur.fetchone()
        subSequence = row[0]
        currentVal = row[1]

        querySql = "select one_day_change from t_daily_stockindex where stock_index=? and sub_sequence>=? and sub_sequence<=?"
        cur.execute(querySql, (stockIndex, subSequence - 29, subSequence))
        rows = cur.fetchall()
        moving30 = []
        for row in rows:
            moving30.append(row[0])

        querySql = "select one_day_change from t_daily_stockindex where stock_index=? and sub_sequence>=? and sub_sequence<=?"
        cur.execute(querySql, (stockIndex, subSequence - 89, subSequence))
        rows = cur.fetchall()
        moving90 = []
        for row in rows:
            moving90.append(row[0])

        m30 = sum(moving30) / len(moving30)
        m90 = sum(moving90) / len(moving90)
        std30 = calculator.calSD(moving30)
        std90 = calculator.calSD(moving90)

        eventType, cButtom, cUpper = dailySigmaTrends(stockIndex,
                                                      str(pClusster), m30, m90,
                                                      std30, std90, currentVal)

        dailyRecord = {}
        dailyRecord["date"] = date
        dailyRecord["cBottom"] = cButtom
        dailyRecord["cUpper"] = cUpper
        dailyRecord["currentValue"] = currentVal

        "Construct the warning message"
        warningMessage = {}
        date = surObj["date"]
        derivedFrom = surObj["embersId"]
        model = surObj["model"]
        event = eventType
        confidence = surObj["confidence"]
        confidenceIsProbability = surObj["confidenceIsProbability"]
        eventDate = surObj["shiftDate"]
        population = surObj["population"]
        location = surObj["location"]

        warningMessage["date"] = date
        warningMessage["derivedFrom"] = derivedFrom
        warningMessage["model"] = model
        warningMessage["eventType"] = event
        warningMessage["confidence"] = confidence
        warningMessage["confidenceIsProbability"] = confidenceIsProbability
        warningMessage["eventDate"] = eventDate
        warningMessage["population"] = population
        warningMessage["location"] = location

        embersId = hashlib.sha1(json.dumps(warningMessage)).hexdigest()
        warningMessage["embersId"] = embersId

        if eventType != "0000":
            insert_warningmessage(warningMessage)
            return warningMessage
        else:
            return None

    except lite.Error, e:
        print "Error: %s" % e.args[0]
Example #4
0
def warningCheck(surObj):
#    surObj = {'embersId': 'f0c030a20e28a12134d9ad0e98fd0861fae7438b', 'confidence': 0.13429584033181682, 'strength': '4', 'derivedFrom': [u'5df18f77723885a12fa6943421c819c90c6a2a02', u'be031c4dcf3eb9bba2d86870683897dfc4ec4051', u'3c6571a4d89b17ed01f1345c80cf2802a8a02b7b'], 'shiftDate': '2011-08-08', 'shiftType': 'Trend', 'location': u'Colombia', 'date': '2012-10-03', 'model': 'Finance Stock Model', 'valueSpectrum': 'changePercent', 'confidenceIsProbability': True, 'population': 'COLCAP'}
    stockIndex = surObj["population"]
    trendType = surObj["strength"]
    date = surObj["shiftDate"]
    
    try:
        con = common.getDBConnection()
        cur = con.cursor()
        pClusster = trendType
            
    
        sql = "select sub_sequence,last_price from t_daily_stockindex where stock_index=? and date<? order by date desc limit 1"
        cur.execute(sql,(stockIndex,date))
        row = cur.fetchone()
        subSequence = row[0]
        currentVal = row[1]
        
        querySql = "select one_day_change from t_daily_stockindex where stock_index=? and sub_sequence>=? and sub_sequence<=?"
        cur.execute(querySql,(stockIndex,subSequence-29,subSequence))
        rows = cur.fetchall()
        moving30 = []
        for row in rows:
            moving30.append(row[0])
        
        querySql = "select one_day_change from t_daily_stockindex where stock_index=? and sub_sequence>=? and sub_sequence<=?"
        cur.execute(querySql,(stockIndex,subSequence-89,subSequence))
        rows = cur.fetchall()
        moving90 = []
        for row in rows:
            moving90.append(row[0])
        
        m30 = sum(moving30)/len(moving30)
        m90 = sum(moving90)/len(moving90)
        std30 = calculator.calSD(moving30)
        std90 = calculator.calSD(moving90)
        
        eventType,cButtom,cUpper = dailySigmaTrends(stockIndex,str(pClusster),m30,m90,std30,std90,currentVal)

        dailyRecord = {}
        dailyRecord["date"] = date
        dailyRecord["cBottom"] = cButtom
        dailyRecord["cUpper"] = cUpper
        dailyRecord["currentValue"] = currentVal
        
        "Construct the warning message"
        warningMessage ={}
        date = surObj["date"]
        derivedFrom = surObj["embersId"]
        model = surObj["model"]
        event = eventType
        confidence = surObj["confidence"]
        confidenceIsProbability = surObj["confidenceIsProbability"]
        eventDate= surObj["shiftDate"]
        population = surObj["population"]
        location = surObj["location"]
        
        warningMessage["date"] = date
        warningMessage["derivedFrom"] = derivedFrom
        warningMessage["model"] = model
        warningMessage["eventType"] = event
        warningMessage["confidence"] = confidence
        warningMessage["confidenceIsProbability"] = confidenceIsProbability
        warningMessage["eventDate"] = eventDate
        warningMessage["population"] = population
        warningMessage["location"] = location
        
        embersId = hashlib.sha1(json.dumps(warningMessage)).hexdigest()
        warningMessage["embersId"] = embersId
        
        if eventType != "0000":
            insert_warningmessage(warningMessage)
            return warningMessage
        else:
            return None
        
    except lite.Error, e:
        print "Error: %s" % e.args[0]