Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //stockPortfolio.script
- // This script automatically rebalances a stock portfolio to keep an equal amount
- // invested in every stock, and a certain percentage in cash. It requires significant
- // moves in order to make transactions, in order to minimize commission fees.
- function formatCurrency(amt){
- suffixArray = ["k","m","b","t","qa","qt","se","st","o","n","d"];
- i = 0;
- while(amt >= 1000 && i < suffixArray.length){
- amt /= 1000;
- i++;
- }
- return "$" + (Math.round(100 * amt) / 100) + suffixArray[i-1];
- }
- 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"];
- investQuantity = 0.9;
- rebalanceThreshold = 0.1;
- while (true)
- {
- prices = [];
- shares = [];
- print("Getting positions...");
- totalCost = 0;
- for (i = 0; i < symbols.length; i = i+1)
- {
- position = getStockPosition(symbols[i]);
- shares.push(position[0]);
- totalCost += position[0] * position[1];
- }
- print("Getting initial prices...");
- for (i = 0; i < symbols.length; i = i+1)
- prices.push(getStockPrice(symbols[i]));
- cash = getServerMoneyAvailable("home");
- netWorth = cash;
- for (i = 0; i < symbols.length; i = i+1)
- netWorth += prices[i] * shares[i];
- // Keep a certain amount in cash, and invest the rest
- targetStock = (netWorth * investQuantity) / symbols.length;
- print("Net worth: " + formatCurrency(netWorth));
- print("Total cost of shares: " + formatCurrency(totalCost));
- earningsPrint = "Earnings: " + formatCurrency(netWorth - cash - totalCost);
- if (totalCost > 0)
- earningsPrint += " (" + (Math.round(10 * 100 * (((netWorth - cash) / totalCost) - 1)) / 10) + "%)";
- print(earningsPrint);
- print("Target stock holdings: " + formatCurrency(targetStock));
- for (i = 0; i < symbols.length; i = i+1)
- {
- // Get the price again to avoid script delay errors
- price = getStockPrice(symbols[i]);
- holdings = price * shares[i];
- diff = Math.floor((targetStock - holdings) / price) * price;
- // Because of the $100k commissions, we only want to trade if it's a differential of
- // $10m or more, and also 10% to avoid too much jitter.
- if (diff > 10000000 && (holdings === 0 || targetStock / holdings >= (1 + rebalanceThreshold)))
- buyStock(symbols[i], diff / price);
- else if (diff < -10000000 && targetStock / holdings <= (1 - rebalanceThreshold))
- sellStock(symbols[i], -diff / price);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement