Hi - can someone please take a look at this and let me know why its not trading anything? Seriously spent over 4 hrs trying a whole bunch of different things before resorting to asking Q Crowd to assist this newbie. Took code from a few different places and somehow lost track along the way (wish some version control functionalities can be introduced at some point ... or I should just be a bit more careful)...

Summary of what I am trying:

- take predefined universe

- get 2 /30 EMA

- get delta between EMA's

- sort to get the top 5 delta's

- calculate a weight for the top 5

- purchase those 5 positions based on the calculated weight

Currently just want to try it long only, but tried to build framework around in case wanted to start testing shorts also ... any assistance would be much appreciated

Returns | 1 Month | 3 Month | 6 Month | 12 Month |

Alpha | 1 Month | 3 Month | 6 Month | 12 Month |

Beta | 1 Month | 3 Month | 6 Month | 12 Month |

Sharpe | 1 Month | 3 Month | 6 Month | 12 Month |

Sortino | 1 Month | 3 Month | 6 Month | 12 Month |

Volatility | 1 Month | 3 Month | 6 Month | 12 Month |

Max Drawdown | 1 Month | 3 Month | 6 Month | 12 Month |

#https://www.quantopian.com/posts/meb-fabers-3-way-system # Meb Faber 3 ways system #TO DOOO!!! #consider stock screens on medium volatility stocks/indexs to use #still far too correlated to SPY ... figure out why #check out research portfolio that allow different backstests based on chaning a parameter and see how results look # order the measures to limit to only buy top 6-8 stocks # Three asset classes: Stocks, bonds, gold. # Invest equally in whatever is going up (defined as 3 month SMA > 10 month SMA). # - See more at: http://mebfaber.com/2015/06/16/three-way-model/#sthash.nUERh7tS.dpuf import pandas as pd import talib as ta import numpy as np from numpy import array def initialize(context): set_symbol_lookup_date('2015-04-11') context.stocks = symbols( 'IVV','IWF','IWM','IJH','IWD','IJR','IBB','IYR','ICF','IYW','IYH','ITB','IYF','IHE','IYC','EFA','EEM','EWJ','EZU','IEMG','EWG','IEFA','FXI','INDA','IDV','IGE','IXJ','IGF','IXN','KXI','FLWS','SRCE','TWOU','SIXD','EGHT','AVHI','ATEN','AAC','AAON','AIR','ABAX','ABY','ABEO','ANF','ABMD','ABM','AXAS','ACTG','ACAD','AKR','AXDX','XLRN','ANCX','ACCO','ARAY','ACW','ACET','ACHN','ACIW','ACOR','ACTA','ATU','ACXM','ADMS','AE','ADUS','ADPT','ADTN','WMS','AEIS','AMD','ADXS','ADVS','ABCO','AEGR','AEGN','AEPI','AERI','HIVE','AJRD','AVAV','AFMD','AFFX','MITT','AGEN','AGRX','AGYS','ADC','AIRM','ATSG','AYR','AKS','AKBA','ALG','AIN','AMRI','ALDR','ALEX','ALX','ALCO','ALIM','ALGT','ALE','AFOP','AIQ','AMOT','AFAM','ALJ','AOSL','AAMC','ASPS','RESI','AIMC','AMAG','AMBC','AMBA','AMBR','AMC','AMED','APEI','AMRC','AAT','AXL','MTGE','AEO','AEL','AMNB','ARII','ARPI','ASEI','AMSW_A','AWR','AVD','AMWD','CRMT','ABCB','AMSF','ATLO','FOLD','AMKR','AHS','AMPH','AMSG','ANAC','ALOG','ABCW','ANDE','ANGI','ANGO','ANIP','ANIK','AXE','ANN','ATRS','ANTH','ANH','APOG','ARI','APOL','AMTG','AGTC','AIT','AMCC','AAOI','AREX','PETX','ARC','ARCB','ACAT','ARDX','ASC','ARNA','ACRE','AGX','AGII','ARIA','AI','AHH','ARR','ARRY','AROW','ARWR','ARTN_A','ABG','ASNA','ASCM_A','AHP','AHT','AINC','AZPN','ASMB','AEC','ASTE','AST','AF','ATRO','ATRA','AT','ATNI','AAWW','AFH','ATRC','ATRI','ATW','LIFE','AAVL','AVG','AVID','AVA','AVX','ACLS','AXLL','AZZ','BGS','BMI','BCPC','BWIN_B','BANC','BANF','BLX','TBBK','BXS','BKMU','BMRC','OZRK','BFIN','RATE','BANR','BHB','BKS','B','CUDA','BBSI','BAS','BSET','BV','BBCN') context.stocks1 = symbols( 'BBX','BECN','BSF','BZH','BEBE','BELF_B','BDC','BLCM','BEL','BHE','BNCL','BNFT','BHLB','BERY','BGCP','BGFV','BIG','BH','BBG','BCRX','BDSI','BRLI','BIOS','BSTC','BEAT','BTX','BJRI','BBOX','BDE','BKH','BLKB','HAWK','BLMN','BLT','BCOR','BLBD','BHBK','NILE','BRG','BNCN','BOBE','BOFI','WIFI','BCC','BCEI','BOOT','SAM','BPFH','EPAY','BDBD','BOX','BYD','BRC','BBRG','BDGE','BBNK','BPI','BGG','BFAM','BCOV','BCO','BRS','BSFT','BRKL','BRKS','BMTC','BLMT','BKE','BWLD','BBW','BLDR','BURL','CJES','BNK','CCMP','CACI','CACQ','CZR','CALM','CLMS','CAMP','CVGW','CAL','CCC','CWT','CALA','CALX','ELY','CALD','CPE','ABCD','CBM','CAC','CCG','CMN','CPLA','CBF','CCBG','CSU','CFFN','CMO','CARA','CRR','CARB','CBYL','CFNL','CSII','CATM','CRCM','CECO','CTRE','CKEC','CRS','CSV','CRZO','TAST','CACB','CSCD','CWST','CASY','CSH','CASS','ROX','CSLT','CTLT','CPRX','CTT','CATY','CATO','CVCO','CAVM','CBZ','CDI','CEB','CECE','CDR','CGI','CLDX','CBMG','CEMP','CSFL','CETV','CENT_A','CPF','CENX','CNBK_A','CCS','CPHD','CERS','CEVA','CSG','ECOM','GTLS','CHFN','CCF','CLDT','CKP','CAKE','CHEF','CHGG','CHE','CHFC','CCXI','CHMT','CHKE','CHSP','CPK','CHS','PLCE','CMRX','CBK','CHDN','CHUY','CBR','CIEN','CIFC','CMPR','CBB','CIR','CRUS','CTRN','CZNC','CIA','CHCO','CVEO','CIVI','CLC','CLNE','CLFD','CLW','CNL','CLF','CSBK','CLD','CLVS','MYCC','CCNE','CNO','COBZ','COKE','CDE','CUZ','CVTI','COWN','CRAI','CBRL','BREW','CRD_B','CRAY','CROX','CCRN','CRWN','CRY','CSGS','CSS','CTIC','CTS','CUNB','CUBE','CUB','CFI','CMLS','CRIS','CW','CUBI','CUTR','CVBF','CVT','TLT', 'agg', 'LQD', 'TIP', 'hyg', 'shy', 'emb','lEmb','igov', 'GOVT', 'CRED','GLD', 'uso','fxe','slv','IAU','GSG','COMT','PICK','VEGI') # from 2005 context.longterm = 30 context.shortterm = 2 context.N = (context.longterm)*20 context.bet_amount = context.portfolio.cash / 5 context.count = 5 #schedule_function(handle_data_daily, # date_rule=date_rules.every_day(), # time_rule=time_rules.market_open(minutes=30)) # Will be called on every trade event for the securities you specify. def handle_data(context, data): prices = history(context.N, frequency="1d", field='price') ranking = sort_returns(prices) if ranking is not None: column_name = ranking.columns[0] # bottom quantile to go long bottom = ranking[-1*context.count:] longs = bottom[bottom[column_name] < 0] # top quantile to go short top = ranking[:context.count] shorts = top[top[column_name] > 0] for stock in data.keys(): if stock in longs.index: amount = calculate_order_amount(context, stock, 1, data[stock].price) elif stock in shorts.index: amount = calculate_order_amount(context, stock, -1, data[stock].price) else: amount = calculate_order_amount(context, stock, 0, data[stock].price) order(stock, amount) # This method is purely for order managment. It calculates and returns an # order amount to place binary bets. # If signal_val is -1, get to a short position of -1 * context.bet_size # If signal_val is 1, get to a long position of context.bet_size def calculate_order_amount(context, stock, signal_val, cur_price): current_amount = context.portfolio.positions[stock].amount prices = history(context.N, frequency="1d", field='price') prices = prices.dropna(axis=1) prices = prices.resample('BM', how='last',closed ='left', label='left') pricesshort = prices.apply(ta.EMA, timeperiod=context.shortterm) priceslong = prices.apply(ta.EMA, timeperiod=context.longterm) updown = pricesshort.iloc[-1] > priceslong.iloc[-1] weight = 1.0/len(updown[updown==True]) if len(updown[updown==True])!=0 else 0.0 abs_order_amount = int(context.bet_amount / weight) if signal_val == -1: return (1 * abs_order_amount) - current_amount elif signal_val == 1: return 0 * current_amount elif signal_val == 0: return 0 * current_amount else: return 0 def sort_returns(prices): longterm = 30 shortterm = 2 N = (longterm)*20 prices = prices.dropna(axis=1) prices = prices.resample('BM', how='last',closed ='left', label='left') pricesshort = prices.apply(ta.EMA, timeperiod=shortterm) priceslong = prices.apply(ta.EMA, timeperiod=longterm) updown = pricesshort.iloc[-1] > priceslong.iloc[-1] updowndelta = pricesshort.iloc[-1] - priceslong.iloc[-1] # use a slice operator to get the most recent returns last_delta = updowndelta[-1:] last_date = last_delta.index[-1] sorted_delta = last_delta.T.sort() log.info (sorted_delta) log.info (last_delta) return sorted_delta