"Strategy" Mammoth

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.