Quantitative trading indicator function sort (continually updated)


# Depending on the time and fees provided slippage DEF set_slip_fee (context): # point to the slide 0 set_slippage (FixedSlippage (0)) # set according to different time periods charged dt = context.current_dt IF dt> datetime .datetime (2013,1,. 1): set_commission (PerTrade (buy_cost = 0.0003, sell_cost = 0.0013, (min_cost) =. 5)) elif dt> A datetime.datetime (2011,1,. 1): set_commission (PerTrade (buy_cost = from 0.001, sell_cost 0.002 =, = (min_cost). 5)) elif dt> A datetime.datetime (2009, 1,. 1): set_commission (PerTrade (buy_cost = 0.002, sell_cost = 0.003, (min_cost) =. 5)) the else: set_commission (PerTrade (buy_cost = 0.003, sell_cost = 0.004, min_cost = 5))

  

Calculating EMA
# Calculated exponential moving average data 
# Input: ticker - string, moving EMA days - integer, Data 
# outputs: yesterday, today and exponential moving average - float 
DEF get_EMA (security_code, Days, Data): 
    # If then only one day, the previous day's closing price is the moving average 
    IF Days ==. 1: 
    # obtained two days before the closing price data, as a moving average of the last one, as a current period after the moving average 
        t = attribute_history (security_code, 2, '1D', ( 'Close')) 
        return T [ 'Close'] [- 2], T [ 'Close'] [-. 1] 
    the else: 
    # g.EMAs If the global variable does not exist, create a dictionary of variable type, used to record already calculated EMA value 
        IF 'EMAs' Not in the dir (G): 
            g.EMAs} = { 
        # keyword dictionary determined uniquely connected with the stock and encoding the number of days, so different stocks or different days exponential moving average get together 
        Key = "% D% S"% (security_code, days) 
        # If the keyword is present, calculated EMA been described before, and directly to the iteration 
        if key in g.EMAs :
            Calculated alpha value #
            = Alpha (Days-1.0) / (1.0 + Days) 
            # obtained the day before EMA (this is the preserved) 
            EMA_pre = g.EMAs [Key] 
            # EMA iteration 
            EMA_now = EMA_pre * alpha + data [ security_code]. * use Close (1.0-Alpha) 
            # write the new EMA value 
            g.EMAs [Key] = EMA_now 
            # returned to the user of yesterday and today two EMA value 
            return (EMA_pre, EMA_now) 
        not before # If the keyword is not present, explain calculated that this EMA, so to initialize 
        the else: 
            # obtain days-day moving average 
            mA = get_MA (security_code, days) 
            # If the moving average exists (does not return NaN), then we have enough data can be on the EMA initialized 
            if not (isNaN (mA)): 
                g.EMAs [Key] = mA 
                # just because initialization, so the first one of the EMA does not exist
                return (float ( "NaN"), mA) 
            the else: 
                # insufficient data days moving average day, had to return NaN values 
                return (float ( "nan") , float ( "nan"))

  

 

  • Calculating a moving average
  • Calculating a moving average data # 
    # Input: ticker - string, moving average number of days - Integer 
    # Output: arithmetic mean - float 
    DEF get_MA (security_code, days): 
    # DAYS days before obtaining the data, see the API 
    A attribute_history = (security_code, days, '1D', ( 'Close')) 
    SUM # defines a local variable, for summation 
    SUM = 0 
    # dAYS days before the closing price for summing 
    for i in range (1, days + 1'd): 
    SUM = a + [ 'Close'] [- I] 
    # sum divided by the number of days after it is possible to obtain the arithmetic mean it 
    return sum / days

Guess you like

Origin www.cnblogs.com/medik/p/11108650.html