ImportRandomImport TimeImportuuidg.security=[]g.daycount=0stockstemp= Get_index_stocks ('000300.XBHS') Total=Len (stockstemp) g.holdstocks= [] whileLen (g.security) < 15: Stock= Stockstemp[random.randint (0, Total-1)] ifStock not inchg.security:g.security.append (stock)defInitialize (context): Context.universe=g.securitydefHandle_data (context, data):#maximum number of positions in stockMaxhold = 5totalsize=Len (context.universe)Print(totalsize)#get the current cashCash =Context.portfolio.cash G.daycount= G.daycount + 1ifLen (g.holdstocks) = = 0:#Initial StateCount =Maxhold Singlemoney= CASH/5 whileCount >0:buystock= Context.universe[random.randint (0, Totalsize-1)] ifBuyStock not inchG.holdstocks andSymbol (BuyStock)inchdata:g.holdstocks.append (BuyStock)#buy stocks with all Singlemoney Print('buystock='+BuyStock)Print('singlemoney='+str (Singlemoney)) Order_value (symbol (buystock), Singlemoney)#record this buy #log.info ("Buying%s"% (BuyStock)) Print("Buying%s"%(BuyStock)) Count= Count-1Print('count='+Str (count))elifG.daycount > 7 andG.daycount% 5 = = 1:#5 days change Print('g.daycount='+str (g.daycount))#choose the worst-performing stock sell in the last 7 daysWeakstock ="'Weakreturns= 10000.0#dfhis = history (7, unit= ' 1d ', field= ' price ', Security_list=g.holdstocks, Df=false)Dfhis = history (7,'1d', field=' Price') forStockinchg.holdstocks:ifSymbol (stock)inchData:#Suspension Ticket Skip ContinueStartPrice=Dfhis[symbol (stock)][0] Endprice= Dfhis[symbol (stock)][-1] Curreturns= (Endprice-startprice)/StartPricePrint('curreturns='+str (curreturns))ifCurreturns <Weakreturns:weakreturns=curreturns Weakstock=StockifWeakstock = ="': Weakstock=G.holdstocks[0] Sellstock=WeakstockPrint('weakstock='+weakstock) g.holdstocks.remove (weakstock)#sell all stocks so that the final holding of this stock is 0order_target (symbol (Sellstock), 0)#record this sale #Log.info ("selling%s"% (Sellstock)) Print("Selling%s"%(Sellstock)) whileTrue:buystock= Context.universe[random.randint (0, Totalsize-1)] ifBuyStock not inchG.holdstocks andBuyStock! = Sellstock andSymbol (BuyStock)inchdata:g.holdstocks.append (BuyStock)#buy stocks with all cash Print('buystock='+BuyStock)Print('cash='+str (cash)) Order_value (symbol (buystock), cash)#record this buy #log.info ("Buying%s"% (BuyStock)) Print("Buying%s"%(BuyStock)) Break
"Strategy" Mammoth