Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This script automatically rebalances a stock portfolio to keep an equal amount
  2. // invested in every stock, and a certain percentage in cash. It requires significant
  3. // moves in order to make transactions, in order to minimize commission fees.
  4. function formatCurrency(moneyDisp){
  5.     suffix = "";
  6.     if (moneyDisp >= 1000)
  7.     {
  8.         moneyDisp /= 1000;
  9.         suffix = "k";
  10.     }
  11.     if (moneyDisp >= 1000)
  12.     {
  13.         moneyDisp /= 1000;
  14.         suffix = "m";
  15.     }
  16.     if (moneyDisp >= 1000)
  17.     {
  18.         moneyDisp /= 1000;
  19.         suffix = "b";
  20.     }
  21.     if (moneyDisp >= 1000)
  22.     {
  23.         moneyDisp /= 1000;
  24.         suffix = "t";
  25.     }
  26.     if (moneyDisp >= 1000)
  27.     {
  28.         moneyDisp /= 1000;
  29.         suffix = "qa";
  30.     }
  31.     if (moneyDisp >= 1000)
  32.     {
  33.         moneyDisp /= 1000;
  34.         suffix = "qt";
  35.     }
  36.     if (moneyDisp >= 1000)
  37.     {
  38.         moneyDisp /= 1000;
  39.         suffix = "se";
  40.     }
  41.     if (moneyDisp >= 1000)
  42.     {
  43.         moneyDisp /= 1000;
  44.         suffix = "st";
  45.     }
  46.     if (moneyDisp >= 1000)
  47.     {
  48.         moneyDisp /= 1000;
  49.         suffix = "o";
  50.     }
  51.     if (moneyDisp >= 1000)
  52.     {
  53.         moneyDisp /= 1000;
  54.         suffix = "n";
  55.     }
  56.     if (moneyDisp >= 1000)
  57.     {
  58.         moneyDisp /= 1000;
  59.         suffix = "d";
  60.     }
  61.    
  62.     return "$" + (Math.round(100 * moneyDisp) / 100) + suffix;
  63. }
  64.  
  65. 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"];
  66. investQuantity = 0.9;
  67. rebalanceThreshold = 0.1;
  68.  
  69. while (true)
  70. {
  71.     prices = [];
  72.     shares = [];
  73.    
  74.     print("Getting positions...");
  75.     totalCost = 0;
  76.     for (i = 0; i < symbols.length; i = i+1)
  77.     {
  78.         position = getStockPosition(symbols[i]);
  79.         shares.push(position[0]);
  80.         totalCost += position[0] * position[1];
  81.     }
  82.    
  83.     print("Getting initial prices...");
  84.     for (i = 0; i < symbols.length; i = i+1)
  85.         prices.push(getStockPrice(symbols[i]));
  86.          
  87.     cash = getServerMoneyAvailable("home");
  88.     netWorth = cash;
  89.    
  90.     for (i = 0; i < symbols.length; i = i+1)
  91.         netWorth += prices[i] * shares[i];
  92.    
  93.     // Keep a certain amount in cash, and invest the rest
  94.     targetStock = (netWorth * investQuantity) / symbols.length;
  95.    
  96.     print("Net worth: " + formatCurrency(netWorth));
  97.     print("Total cost of shares: " + formatCurrency(totalCost));
  98.    
  99.     earningsPrint = "Earnings: " + formatCurrency(netWorth - cash - totalCost);
  100.     if (totalCost > 0)
  101.         earningsPrint += " (" + (Math.round(10 * 100 * (((netWorth - cash) / totalCost) - 1)) / 10) + "%)";
  102.     print(earningsPrint);
  103.    
  104.     print("Target stock holdings: " + formatCurrency(targetStock));
  105.    
  106.     for (i = 0; i < symbols.length; i = i+1)
  107.     {
  108.         // Get the price again to avoid script delay errors
  109.         price = getStockPrice(symbols[i]);
  110.         holdings = price * shares[i];
  111.         diff = Math.floor((targetStock - holdings) / price) * price;
  112.        
  113.         // Because of the $100k commissions, we only want to trade if it's a differential of
  114.         // $10m or more, and also 10% to avoid too much jitter.
  115.         if (diff > 10000000 && (holdings === 0 || targetStock / holdings >= (1 + rebalanceThreshold)))
  116.             buyStock(symbols[i], diff / price);
  117.         else if (diff < -10000000 && targetStock / holdings <= (1 - rebalanceThreshold))
  118.             sellStock(symbols[i], -diff / price);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement