Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //stockPortfolio.script
  2.  
  3. // This script automatically rebalances a stock portfolio to keep an equal amount
  4. // invested in every stock, and a certain percentage in cash. It requires significant
  5. // moves in order to make transactions, in order to minimize commission fees.
  6. function formatCurrency(amt){
  7.     suffixArray = ["k","m","b","t","qa","qt","se","st","o","n","d"];
  8.     i = 0;
  9.    
  10.     while(amt >= 1000 && i < suffixArray.length){
  11.         amt /= 1000;
  12.         i++;
  13.     }
  14.    
  15.     return "$" + (Math.round(100 * amt) / 100) + suffixArray[i-1];
  16. }
  17.  
  18.  
  19. symbols = ["ECP", "MGCP", "BLD", "CLRK", "OMTK", "FSIG", "KGI", "FLCM", "STM", "DCOMM", "HLS", "VITA", "ICRS", "UNV", "AERO", "OMN", "SLRS", "GPH", "NVMD", "WDS", "LXO", "RHOC", "APHE", "SYSC", "CTK", "NTLK", "OMGA", "FNS", "SGC", "JGN", "CTYS", "MDYN", "TITN"];
  20. investQuantity = 0.9;
  21. rebalanceThreshold = 0.1;
  22.  
  23. while (true)
  24. {
  25.     prices = [];
  26.     shares = [];
  27.    
  28.     print("Getting positions...");
  29.     totalCost = 0;
  30.     for (i = 0; i < symbols.length; i = i+1)
  31.     {
  32.         position = getStockPosition(symbols[i]);
  33.         shares.push(position[0]);
  34.         totalCost += position[0] * position[1];
  35.     }
  36.    
  37.     print("Getting initial prices...");
  38.     for (i = 0; i < symbols.length; i = i+1)
  39.         prices.push(getStockPrice(symbols[i]));
  40.          
  41.     cash = getServerMoneyAvailable("home");
  42.     netWorth = cash;
  43.    
  44.     for (i = 0; i < symbols.length; i = i+1)
  45.         netWorth += prices[i] * shares[i];
  46.    
  47.     // Keep a certain amount in cash, and invest the rest
  48.     targetStock = (netWorth * investQuantity) / symbols.length;
  49.    
  50.     print("Net worth: " + formatCurrency(netWorth));
  51.     print("Total cost of shares: " + formatCurrency(totalCost));
  52.    
  53.     earningsPrint = "Earnings: " + formatCurrency(netWorth - cash - totalCost);
  54.     if (totalCost > 0)
  55.         earningsPrint += " (" + (Math.round(10 * 100 * (((netWorth - cash) / totalCost) - 1)) / 10) + "%)";
  56.     print(earningsPrint);
  57.    
  58.     print("Target stock holdings: " + formatCurrency(targetStock));
  59.    
  60.     for (i = 0; i < symbols.length; i = i+1)
  61.     {
  62.         // Get the price again to avoid script delay errors
  63.         price = getStockPrice(symbols[i]);
  64.         holdings = price * shares[i];
  65.         diff = Math.floor((targetStock - holdings) / price) * price;
  66.        
  67.         // Because of the $100k commissions, we only want to trade if it's a differential of
  68.         // $10m or more, and also 10% to avoid too much jitter.
  69.         if (diff > 10000000 && (holdings === 0 || targetStock / holdings >= (1 + rebalanceThreshold)))
  70.             buyStock(symbols[i], diff / price);
  71.         else if (diff < -10000000 && targetStock / holdings <= (1 - rebalanceThreshold))
  72.             sellStock(symbols[i], -diff / price);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement