├── BB.py ├── EMA30_50.py ├── MACD.py ├── RSI.py ├── SMA200.py ├── STOCH.py └── complex.py /BB.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | Current_stock = 0 3 | # Put any initialization logic here. The context object will be passed to 4 | # the other methods in your algorithm. 5 | def initialize(context): 6 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 7 | context.long = {} 8 | context.short = {} 9 | context.stop_loss = 1 10 | context.take_profit = 3 11 | context.max_notional = 10000.1 12 | context.min_notional = -10000.0 13 | set_commission(commission.PerTrade(cost=7)) 14 | 15 | # Will be called on every trade event for the securities you specify. 16 | def handle_data(context, data): 17 | acc_middle_bb=0 18 | acc_upper_bb=0 19 | acc_lower_bb=0 20 | acc_price=0 21 | 22 | for stock in context.sids: 23 | middle_bb = data[stock].mavg(20) 24 | bb_stddev = data[stock].stddev(20) 25 | if middle_bb == None or bb_stddev == None: 26 | return 27 | 28 | bb_stddev = data[stock].stddev(20) 29 | upper_bb = middle_bb + 2*bb_stddev 30 | lower_bb = middle_bb - 2*bb_stddev 31 | 32 | acc_middle_bb += middle_bb 33 | acc_upper_bb += upper_bb 34 | acc_lower_bb += lower_bb 35 | acc_price += data[stock].price 36 | 37 | # record(MiddleBB=middle_bb, UpperBB=upper_bb, LowerBB=lower_bb) 38 | 39 | notional = context.portfolio.positions[stock].amount * data[stock].price 40 | 41 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 42 | if data[stock].price > upper_bb: 43 | order(stock, 100) 44 | log.info("Long %f, %s" %(data[stock].price, stock)) 45 | context.long[stock] = data[stock].price 46 | elif data[stock].price < lower_bb: 47 | order(stock, -100) 48 | log.info("Short %f, %s" %(data[stock].price, stock)) 49 | context.short[stock] = data[stock].price 50 | 51 | if stock in context.long: 52 | if context.long[stock] - data[stock].price > context.stop_loss: 53 | order(stock, -100) 54 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 55 | context.long.pop(stock) 56 | elif data[stock].price - context.long[stock] > context.take_profit: 57 | order(stock, -100) 58 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 59 | context.long.pop(stock) 60 | elif stock in context.short: 61 | if data[stock].price - context.short[stock] > context.stop_loss: 62 | order(stock, 100) 63 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 64 | context.short.pop(stock) 65 | elif context.short[stock] - data[stock].price > context.take_profit: 66 | order(stock, 100) 67 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 68 | context.short.pop(stock) 69 | 70 | record(MiddleBB=acc_middle_bb/10, UpperBB=acc_upper_bb/10, LowerBB=acc_lower_bb/10, Price=acc_price/10) 71 | 72 | @batch_transform(window_length=30) 73 | def get_EMA(datapanel): 74 | global Current_stock 75 | prices=datapanel['price'] 76 | EMA = pandas.stats.moments.ewma(prices, span=20) 77 | return EMA[Current_stock][29] -------------------------------------------------------------------------------- /EMA30_50.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | Current_stock = 0 3 | # Put any initialization logic here. The context object will be passed to 4 | # the other methods in your algorithm. 5 | def initialize(context): 6 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 7 | context.long = {} 8 | context.short = {} 9 | context.stop_loss = 1 10 | context.take_profit = 3 11 | context.max_notional = 10000.1 12 | context.min_notional = -10000.0 13 | set_commission(commission.PerTrade(cost=7)) 14 | 15 | # Will be called on every trade event for the securities you specify. 16 | def handle_data(context, data): 17 | acc_fast=0 18 | acc_slow=0 19 | 20 | for stock in context.sids: 21 | global Current_stock 22 | Current_stock = int(str(stock)[9:-1]) 23 | 24 | mavg_fast=get_EMA_fast(data) 25 | if mavg_fast == None: 26 | return 27 | mavg_slow=get_EMA_slow(data) 28 | if mavg_slow == None: 29 | return 30 | acc_fast += mavg_fast 31 | acc_slow += mavg_slow 32 | 33 | notional = context.portfolio.positions[stock].amount * data[stock].price 34 | 35 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 36 | if mavg_fast > mavg_slow: 37 | order(stock, 100) 38 | log.info("Long %f, %s" %(data[stock].price, stock)) 39 | context.long[stock] = data[stock].price 40 | else: 41 | order(stock, -100) 42 | log.info("Short %f, %s" %(data[stock].price, stock)) 43 | context.short[stock] = data[stock].price 44 | 45 | if stock in context.long: 46 | if context.long[stock] - data[stock].price > context.stop_loss: 47 | order(stock, -100) 48 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 49 | context.long.pop(stock) 50 | elif data[stock].price - context.long[stock] > context.take_profit: 51 | order(stock, -100) 52 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 53 | context.long.pop(stock) 54 | elif stock in context.short: 55 | if data[stock].price - context.short[stock] > context.stop_loss: 56 | order(stock, 100) 57 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 58 | context.short.pop(stock) 59 | elif context.short[stock] - data[stock].price > context.take_profit: 60 | order(stock, 100) 61 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 62 | context.short.pop(stock) 63 | 64 | record(EMA30=acc_fast/10, EMA50=acc_slow/10) 65 | 66 | @batch_transform(window_length=30) 67 | def get_EMA_fast(datapanel): 68 | global Current_stock 69 | prices=datapanel['price'] 70 | EMA = pandas.stats.moments.ewma(prices, span=20) 71 | return EMA[Current_stock][29] 72 | 73 | @batch_transform(window_length=50) 74 | def get_EMA_slow(datapanel): 75 | global Current_stock 76 | prices=datapanel['price'] 77 | EMA = pandas.stats.moments.ewma(prices, span=50) 78 | return EMA[Current_stock][49] -------------------------------------------------------------------------------- /MACD.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | 3 | Current_stock = 0 4 | # Put any initialization logic here. The context object will be passed to 5 | # the other methods in your algorithm. 6 | def initialize(context): 7 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 8 | context.long = {} 9 | context.short = {} 10 | context.stop_loss = 1 11 | context.take_profit = 3 12 | context.max_notional = 10000.1 13 | context.min_notional = -10000.0 14 | set_commission(commission.PerTrade(cost=7)) 15 | context.database = {} 16 | 17 | # Will be called on every trade event for the securities you specify. 18 | def handle_data(context, data): 19 | acc_fast_ema=0 20 | acc_slow_ema=0 21 | acc_MACD=0 22 | 23 | for stock in context.sids: 24 | global Current_stock 25 | Current_stock = int(str(stock)[9:-1]) 26 | 27 | if Current_stock not in context.database: 28 | context.database[Current_stock] = {'ema12': [], 'ema26': []} 29 | 30 | fast_ema = get_EMA12(data) 31 | slow_ema = get_EMA26(data) 32 | 33 | if slow_ema == None: 34 | return 35 | context.database[stock]['ema12'].append(fast_ema) 36 | context.database[stock]['ema26'].append(slow_ema) 37 | diffs = map(get_diff, context.database[stock]['ema26'][-20:-1], context.database[stock]['ema12'][-20:-1]) 38 | 39 | series = pandas.Series(diffs) 40 | signal = pandas.stats.moments.ewma(series, span=9, min_periods=9).values 41 | try: 42 | signal = signal[-1] 43 | except: 44 | return 45 | # signal = signal[-1] 46 | 47 | acc_fast_ema += fast_ema 48 | acc_slow_ema += slow_ema 49 | acc_MACD += signal 50 | # record(EMA12=fast_ema, EMA26=slow_ema) 51 | 52 | notional = context.portfolio.positions[stock].amount * data[stock].price 53 | 54 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 55 | if signal > 0: 56 | order(stock, 100) 57 | log.info("Long %f, %s" %(data[stock].price, stock)) 58 | context.long[stock] = data[stock].price 59 | else: 60 | order(stock, -100) 61 | log.info("Short %f, %s" %(data[stock].price, stock)) 62 | context.short[stock] = data[stock].price 63 | 64 | if stock in context.long: 65 | if context.long[stock] - data[stock].price > context.stop_loss: 66 | order(stock, -100) 67 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 68 | context.long.pop(stock) 69 | elif data[stock].price - context.long[stock] > context.take_profit: 70 | order(stock, -100) 71 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 72 | context.long.pop(stock) 73 | elif stock in context.short: 74 | if data[stock].price - context.short[stock] > context.stop_loss: 75 | order(stock, 100) 76 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 77 | context.short.pop(stock) 78 | elif context.short[stock] - data[stock].price > context.take_profit: 79 | order(stock, 100) 80 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 81 | context.short.pop(stock) 82 | 83 | 84 | record(MACD=acc_MACD/10) 85 | # record(EMA12=acc_fast_ema/10, EMA26=acc_slow_ema/10) 86 | 87 | @batch_transform(window_length=12) 88 | def get_EMA12(datapanel): 89 | global Current_stock 90 | prices=datapanel['price'] 91 | # print prices 92 | EMA = pandas.stats.moments.ewma(prices, span=12) 93 | return EMA[Current_stock][11] 94 | 95 | @batch_transform(window_length=26) 96 | def get_EMA26(datapanel): 97 | global Current_stock 98 | prices=datapanel['price'] 99 | EMA = pandas.stats.moments.ewma(prices, span=26) 100 | return EMA[Current_stock][25] 101 | 102 | def get_diff(slow, fast): 103 | if fast == None or slow == None: 104 | return None 105 | else: 106 | return fast - slow 107 | -------------------------------------------------------------------------------- /RSI.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | 3 | Current_stock = 0 4 | # Put any initialization logic here. The context object will be passed to 5 | # the other methods in your algorithm. 6 | def initialize(context): 7 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 8 | context.long = {} 9 | context.short = {} 10 | context.stop_loss = 1 11 | context.take_profit = 3 12 | context.max_notional = 10000.1 13 | context.min_notional = -10000.0 14 | set_commission(commission.PerTrade(cost=7)) 15 | context.database = {} 16 | #the RSI is calculated based on the security's returns over a set number 17 | #of preceding observations. Here we're setting up an array to collect the 18 | #preceding 6 ticks to allow us to calc the preceding 5 period returns 19 | context.tick_history = {} 20 | 21 | # Will be called on every trade event for the securities you specify. 22 | def handle_data(context, data): 23 | acc_rsi=0 24 | 25 | for stock in context.sids: 26 | global Current_stock 27 | Current_stock = int(str(stock)[9:-1]) 28 | 29 | if Current_stock not in context.database: 30 | context.database[Current_stock] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 31 | 32 | context.database[Current_stock].insert(0, data[stock].price) 33 | context.database[Current_stock].pop() 34 | preceding_prices = context.database[Current_stock] 35 | 36 | if preceding_prices[len(preceding_prices)-1] <> 0: 37 | #create and populate an array of returns from our prices 38 | returns = [(preceding_prices[i-1]-preceding_prices[i]) for i in range(1,len(preceding_prices))] 39 | up = [] 40 | down = [] 41 | for i in range(len(returns)): 42 | if returns[i] > 0: 43 | up.append(round(returns[i],2)) 44 | elif returns[i] <= 0: 45 | down.append(round(returns[i]*-1,2)) 46 | 47 | if len(up) == 0: 48 | rsi = 0 49 | elif len(down) == 0: 50 | rsi = 100 51 | 52 | else: 53 | av_up = sum(up) / len(up) 54 | av_down = sum(down) / len(down) 55 | 56 | if av_down == 0: 57 | rsi = 100 58 | elif av_up == 0: 59 | rsi = 0 60 | else: 61 | rs = av_up / av_down 62 | rsi = 100 - (100 / (1 + rs)) 63 | 64 | else: 65 | rsi = 50 66 | 67 | acc_rsi += rsi 68 | 69 | 70 | 71 | notional = context.portfolio.positions[stock].amount * data[stock].price 72 | 73 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 74 | if rsi < 30: 75 | order(stock, 100) 76 | log.info("Long %f, %s" %(data[stock].price, stock)) 77 | context.long[stock] = data[stock].price 78 | elif rsi > 70: 79 | order(stock, -100) 80 | log.info("Short %f, %s" %(data[stock].price, stock)) 81 | context.short[stock] = data[stock].price 82 | 83 | if stock in context.long: 84 | if context.long[stock] - data[stock].price > context.stop_loss: 85 | order(stock, -100) 86 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 87 | context.long.pop(stock) 88 | elif data[stock].price - context.long[stock] > context.take_profit: 89 | order(stock, -100) 90 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 91 | context.long.pop(stock) 92 | elif stock in context.short: 93 | if data[stock].price - context.short[stock] > context.stop_loss: 94 | order(stock, 100) 95 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 96 | context.short.pop(stock) 97 | elif context.short[stock] - data[stock].price > context.take_profit: 98 | order(stock, 100) 99 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 100 | context.short.pop(stock) 101 | 102 | record(RSI=rsi) -------------------------------------------------------------------------------- /SMA200.py: -------------------------------------------------------------------------------- 1 | # Put any initialization logic here. The context object will be passed to 2 | # the other methods in your algorithm. 3 | def initialize(context): 4 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 5 | context.long = {} 6 | context.short = {} 7 | context.stop_loss = 1 8 | context.take_profit = 3 9 | context.max_notional = 10000.1 10 | context.min_notional = -10000.0 11 | set_commission(commission.PerTrade(cost=7)) 12 | 13 | # Will be called on every trade event for the securities you specify. 14 | def handle_data(context, data): 15 | all_mavg = 0 16 | all_price = 0 17 | for stock in context.sids: 18 | mavg=data[stock].mavg(200) 19 | all_mavg += mavg 20 | all_price += data[stock].price 21 | 22 | notional = context.portfolio.positions[stock].amount * data[stock].price 23 | 24 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 25 | if mavg < data[stock].price: 26 | order(stock, 100) 27 | log.info("Long %f, %s" %(data[stock].price, stock)) 28 | context.long[stock] = data[stock].price 29 | else: 30 | order(stock, -100) 31 | log.info("Short %f, %s" %(data[stock].price, stock)) 32 | context.short[stock] = data[stock].price 33 | 34 | if stock in context.long: 35 | if context.long[stock] - data[stock].price > context.stop_loss: 36 | order(stock, -100) 37 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 38 | context.long.pop(stock) 39 | elif data[stock].price - context.long[stock] > context.take_profit: 40 | order(stock, -100) 41 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 42 | context.long.pop(stock) 43 | elif stock in context.short: 44 | if data[stock].price - context.short[stock] > context.stop_loss: 45 | order(stock, 100) 46 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 47 | context.short.pop(stock) 48 | elif context.short[stock] - data[stock].price > context.take_profit: 49 | order(stock, 100) 50 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 51 | context.short.pop(stock) 52 | 53 | record(MovingAverage200=(all_mavg/10), Price=(all_price/10)) -------------------------------------------------------------------------------- /STOCH.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | Current_stock = 0 3 | # Put any initialization logic here. The context object will be passed to 4 | # the other methods in your algorithm. 5 | def initialize(context): 6 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 7 | context.long = {} 8 | context.short = {} 9 | context.stop_loss = 1 10 | context.take_profit = 3 11 | context.max_notional = 10000.1 12 | context.min_notional = -10000.0 13 | set_commission(commission.PerTrade(cost=7)) 14 | context.highs = {} 15 | context.lows = {} 16 | context.K = {} 17 | context.full_K = {} 18 | 19 | # Will be called on every trade event for the securities you specify. 20 | def handle_data(context, data): 21 | # %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100 22 | # %D = 3-day SMA of %K 23 | # Lowest Low = lowest low for the look-back period 24 | # Highest High = highest high for the look-back period 25 | # %K is multiplied by 100 to move the decimal point two places 26 | # 27 | # Full %K = Fast %K smoothed with X-period SMA 28 | # Full %D = X-period SMA of Full %K 29 | # 30 | # Input 14, 3 31 | # Returns [full %K, full %D] 32 | acc_full_K=0 33 | acc_full_D=0 34 | 35 | for stock in context.sids: 36 | if stock not in context.highs: 37 | context.highs[stock] = [] 38 | if stock not in context.lows: 39 | context.lows[stock] = [] 40 | if stock not in context.K: 41 | context.K[stock] = [] 42 | if stock not in context.full_K: 43 | context.full_K[stock] = [] 44 | 45 | context.highs[stock].append(data[stock].high) 46 | context.lows[stock].append(data[stock].low) 47 | try: 48 | K = (data[stock].price - min(context.lows[stock][-14:-1])) / (max(context.highs[stock][-14:-1]) - min(context.lows[stock][-14:-1])) * 100 49 | except: 50 | K = 0 51 | context.K[stock].append(K) 52 | 53 | full_K = pandas.stats.moments.ewma(pandas.Series(context.K[stock]), span=5).values[-1] 54 | context.full_K[stock].append(full_K) 55 | full_D = pandas.stats.moments.ewma(pandas.Series(context.full_K[stock]), span=3).values[-1] 56 | 57 | acc_full_K += full_K 58 | acc_full_D += full_D 59 | 60 | notional = context.portfolio.positions[stock].amount * data[stock].price 61 | 62 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 63 | if full_K < 20 and full_D < 20: 64 | order(stock, 100) 65 | log.info("Long %f, %s" %(data[stock].price, stock)) 66 | context.long[stock] = data[stock].price 67 | elif full_K > 80 and full_D > 80: 68 | order(stock, -100) 69 | log.info("Short %f, %s" %(data[stock].price, stock)) 70 | context.short[stock] = data[stock].price 71 | 72 | if stock in context.long: 73 | if context.long[stock] - data[stock].price > context.stop_loss: 74 | order(stock, -100) 75 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 76 | context.long.pop(stock) 77 | elif data[stock].price - context.long[stock] > context.take_profit: 78 | order(stock, -100) 79 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 80 | context.long.pop(stock) 81 | elif stock in context.short: 82 | if data[stock].price - context.short[stock] > context.stop_loss: 83 | order(stock, 100) 84 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 85 | context.short.pop(stock) 86 | elif context.short[stock] - data[stock].price > context.take_profit: 87 | order(stock, 100) 88 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 89 | context.short.pop(stock) 90 | 91 | record(FullK5=acc_full_K/10, FullD3=acc_full_D/10) 92 | # acc -------------------------------------------------------------------------------- /complex.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | Current_stock = 0 3 | # Put any initialization logic here. The context object will be passed to 4 | # the other methods in your algorithm. 5 | def initialize(context): 6 | context.sids = [sid(8655), sid(5923), sid(7797), sid(8229), sid(5484), sid(7488), sid(3136), sid(438), sid(3806), sid(3499)] 7 | context.long = {} 8 | context.short = {} 9 | context.stop_loss = 1 10 | context.take_profit = 3 11 | context.max_notional = 10000.1 12 | context.min_notional = -10000.0 13 | set_commission(commission.PerTrade(cost=7)) 14 | context.database = {} 15 | #the RSI is calculated based on the security's returns over a set number 16 | #of preceding observations. Here we're setting up an array to collect the 17 | #preceding 6 ticks to allow us to calc the preceding 5 period returns 18 | context.tick_history = {} 19 | 20 | # Will be called on every trade event for the securities you specify. 21 | def handle_data(context, data): 22 | acc_fast=0 23 | acc_slow=0 24 | acc_rsi=0 25 | acc_mavg=0 26 | acc_price=0 27 | 28 | for stock in context.sids: 29 | global Current_stock 30 | Current_stock = int(str(stock)[9:-1]) 31 | 32 | if Current_stock not in context.database: 33 | context.database[Current_stock] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 34 | 35 | context.database[Current_stock].insert(0, data[stock].price) 36 | context.database[Current_stock].pop() 37 | preceding_prices = context.database[Current_stock] 38 | 39 | if preceding_prices[len(preceding_prices)-1] <> 0: 40 | #create and populate an array of returns from our prices 41 | returns = [(preceding_prices[i-1]-preceding_prices[i]) for i in range(1,len(preceding_prices))] 42 | up = [] 43 | down = [] 44 | for i in range(len(returns)): 45 | if returns[i] > 0: 46 | up.append(round(returns[i],2)) 47 | elif returns[i] <= 0: 48 | down.append(round(returns[i]*-1,2)) 49 | 50 | if len(up) == 0: 51 | rsi = 0 52 | elif len(down) == 0: 53 | rsi = 100 54 | 55 | else: 56 | av_up = sum(up) / len(up) 57 | av_down = sum(down) / len(down) 58 | 59 | if av_down == 0: 60 | rsi = 100 61 | elif av_up == 0: 62 | rsi = 0 63 | else: 64 | rs = av_up / av_down 65 | rsi = 100 - (100 / (1 + rs)) 66 | 67 | else: 68 | rsi = 50 69 | 70 | acc_rsi += rsi 71 | 72 | mavg_fast=get_EMA_fast(data) 73 | if mavg_fast == None: 74 | return 75 | mavg_slow=get_EMA_slow(data) 76 | if mavg_slow == None: 77 | return 78 | 79 | acc_fast += mavg_fast 80 | acc_slow += mavg_slow 81 | 82 | mavg=data[stock].mavg(200) 83 | acc_mavg += mavg 84 | 85 | acc_price += data[stock].price 86 | 87 | notional = context.portfolio.positions[stock].amount * data[stock].price 88 | 89 | if stock not in context.long and stock not in context.short and notional < context.max_notional and notional > context.min_notional: 90 | if mavg_fast > mavg_slow and rsi < 50 and mavg < data[stock].price: 91 | order(stock, 100) 92 | log.info("Long %f, %s" %(data[stock].price, stock)) 93 | context.long[stock] = data[stock].price 94 | elif mavg_fast < mavg_slow and rsi > 50 and mavg > data[stock].price: 95 | order(stock, -100) 96 | log.info("Short %f, %s" %(data[stock].price, stock)) 97 | context.short[stock] = data[stock].price 98 | 99 | if stock in context.long: 100 | if context.long[stock] - data[stock].price > context.stop_loss: 101 | order(stock, -100) 102 | log.info("LONG Stop loss at %f, %s" %(data[stock].price, stock)) 103 | context.long.pop(stock) 104 | elif data[stock].price - context.long[stock] > context.take_profit: 105 | order(stock, -100) 106 | log.info("LONG Take profit at %f, %s" %(data[stock].price, stock)) 107 | context.long.pop(stock) 108 | elif stock in context.short: 109 | if data[stock].price - context.short[stock] > context.stop_loss: 110 | order(stock, 100) 111 | log.info("SHORT Stop loss at %f, %s" %(data[stock].price, stock)) 112 | context.short.pop(stock) 113 | elif context.short[stock] - data[stock].price > context.take_profit: 114 | order(stock, 100) 115 | log.info("SHORT Take profit at %f, %s" %(data[stock].price, stock)) 116 | context.short.pop(stock) 117 | 118 | record(EMA40=acc_fast/10, EMA80=acc_slow/10, RSI=acc_rsi/10, SMA200=acc_mavg/10, Price=acc_price/10) 119 | 120 | @batch_transform(window_length=40) 121 | def get_EMA_fast(datapanel): 122 | global Current_stock 123 | prices=datapanel['price'] 124 | EMA = pandas.stats.moments.ewma(prices, span=40) 125 | return EMA[Current_stock][39] 126 | 127 | @batch_transform(window_length=80) 128 | def get_EMA_slow(datapanel): 129 | global Current_stock 130 | prices=datapanel['price'] 131 | EMA = pandas.stats.moments.ewma(prices, span=80) 132 | return EMA[Current_stock][79] --------------------------------------------------------------------------------