Skip to content

Instantly share code, notes, and snippets.

@seanhess
Created January 23, 2015 21:35
Show Gist options
  • Save seanhess/6f0ba82547929cfe0873 to your computer and use it in GitHub Desktop.
Save seanhess/6f0ba82547929cfe0873 to your computer and use it in GitHub Desktop.
Stuff we did Allan
function averagePricePaid(array, inv) {
var totalCost = 0;
var totalShares = 0;
for(var i = 0; i < array.length; i++) {
totalCost += array[i].cost;
totalShares += array[i].shares;
}
inv.costBasis = totalCost/totalShares;
return(inv.costBasis);
};
function simpleMovingAverage(sArray) {
var smaTotal = 0;
// console.log(sArray);
for (s=0; s < sArray.length; s++) { // gives the sum of the trades over the smaPeriods
smaTotal += sArray[s];
}
var sma = smaTotal/sArray.length; // divides the sum by the number of smaPeriods
return(sma);
}
// exports
// module.exports
exports.averagePricePaid = averagePricePaid
exports.simpleMovingAverage = simpleMovingAverage
exports.hello = "hello"
exports.version = 3
// dont' worry about this for now
//module.exports = {
//hello: "hello"
//}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Trading Simulation Test</title>
<script language="javascript" type="text/javascript" src="p5/p5.js"></script>
<script language="javascript" type="text/javascript" src="p5/addons/p5.dom.js"></script>
<script type='text/javascript' src='build/bundle.js'></script>
</head>
<body>
<h1>Trading Simulation</h1>
<h2>with a random stock price</h2>
<form id="form1">
Buy Rule:<br>
If the stock price is below the <input id="smaPeriods1"> day Simple Moving Average<br>
Then trade <input id="cashAmount"> % of cash account.<br> <br>
Sell Rule:<br>
If stock price is <div id="stockDrop"> </div> above the average cost basis<br>
Then trade <div id="percentSell" value =5> </div>% the number of shares owned.<br>
<button onclick="startSimulation()">Try it</button>
</form>
<div>
<p id="demo"></p>
</div>
</body>
</html>
var x=0;
var investorA;
var stockA;
var smaInput;
var buyPercInput;
var stockDropPercInput;
var percentSellInput;
var volatilitySlider;
var averages = require('./averages')
// document.getElementById("whatever").value will give you the value
// WEBPACK STUFF
// npm install -g webpack
// cd into your directory
// webpack -w and leave it open. It will create build/bundle.js every time you save
// <script src="build/bundle.js"/> in your main file
function setup() {
frameRate(100);
smaInput = createInput(15).attribute( "type", "number").attribute("min", 2).attribute("max", 100).size(40).parent("smaPeriods1");
console.log("smaInput: " + smaInput);
buyPercInput = createInput().attribute("type", "number").attribute("min",1).attribute("max", 100).size(40).parent("cashAmount");
console.log("buyPercInput: " + buyPercInput);
//stockDropPercInput = createInput(5).attribute("type", "number").attribute("min",1).attribute("max", 100).size(40).parent("stockDrop").value();
percentSellInput = createInput().attribute("type", "number").attribute("min",1).attribute("max", 100).size(40).parent("percentSell");
console.log("percentSellInput: " + percentSellInput);
volatilitySlider = createSlider(0,30,15);
console.log("volatility: " + volatilitySlider.value());
createCanvas(800, 400).position(350,100);
background(244, 244, 255);
translate(0, height);
stockA = new Stock(100, smaInput.value(), volatilitySlider.value());
investorA = new Investor(1000000);
stockA.tradeSim(investorA);
}
function draw() {
stroke(152, 152, 152, 70);
text(stockA.allPrices[x].net,100,-200);
line(0, stockA.allPrices[0].price*(-1), width, stockA.allPrices[0].price*(-1));
stroke(0);
line(x, stockA.allPrices[x].price*(-1), x+1, stockA.allPrices[x+1].price*(-1));
stroke(250, 41, 236);
line(x, stockA.allPrices[x].sma*(-1), x+1, stockA.allPrices[x+1].sma*(-1));
stroke(185, 120, 16);
// line(x, stockA.allPrices[x].costBasis*(-1), x+1, stockA.allPrices[x+1].costBasis*(-1));
// console.log("costBasis: " + costBasis);
stroke(53, 199, 0);
line(x, stockA.allPrices[x].profit*(-1), x+1, stockA.allPrices[x+1].profit*(-1));
stroke(0, 76, 31);
text(stockA.allPrices[x].net,100,-140);
while(x < width-2) {
// console.log(stockA.allPrices[x].net);
x++;
return;
}
}
var total = 0
function addToTotal(num) {
total += num
}
function add(a, b) {
return a + b
}
total = add(total, 3)
function startSimulation() {
stockA.tradeSim(investorA);
}
function Investor(cash) {
// var cash = cash;
this.sharesOwned = 0;
var pricesPaid = [];
this.costBasis = 0;
var startDollars = cash;
this.profit = function (currentPrice) {
// console.log("Profit: " + this.netWorth(currentPrice)/startDollars*100);
return this.netWorth(currentPrice)/startDollars*100;
};
this.netWorth = function (currentPrice) {
return(this.sharesOwned * currentPrice + cash);
};
this.getHistory = function () {
return pricesPaid;
};
this.buy = function (currentPrice, percentage) {
var sharesToBuy = Math.floor((cash * percentage) / currentPrice);
var cashToWithdraw = sharesToBuy * currentPrice;
cash -= cashToWithdraw;
this.sharesOwned += sharesToBuy;
pricesPaid.push({cost: cashToWithdraw, shares: sharesToBuy});
// add more steps here
};
this.sell = function (currentPrice, percentage) {
var sharesToSell = Math.floor(this.sharesOwned * percentage);
var cashToDeposit = sharesToSell * currentPrice;
cash += cashToDeposit;
this.sharesOwned -= sharesToSell;
pricesPaid.push({cost: -cashToDeposit, shares: -sharesToSell});
};
}
//class Investor {
//constructor() {
//}
//profit(currentPrice) {
//return 234
//}
//}
//var investor = new Investor()
//investor.profit(1234)
function Stock (price, smaPeriods, volatility) {
this.price = price;
this.smaPeriods = smaPeriods; //*** This will change with user input
this.volatility = volatility;
this.allPrices = [];
this.dailyChange = 0;
// random numbers, loops, does calculations, updates state
this.tradeSim = function(invest) {
// console.log("stockPrice: " + this.price);
var smaArray = [];
for (var p = 0; p < this.smaPeriods; p++) { // Populates the smaArray with the initial stock price to make the SMA calculation easier.
smaArray.push(this.price);
}
invest.buy(this.price, 0.5); //Initial purchase of 50% of cash
for (var i = 0; i < width; i++) {
// console.log("Net worth: " + invest.netWorth(this.price) + " Profit: " + invest.profit(this.price));
this.woot()
}
};
this.woot = function() {
var posOrNeg = Math.round(Math.random()) * 2 - 1; // creates a random +/-1
this.dailyChange = Math.pow(Math.random(), 3) * this.volatility * posOrNeg; // creates a random number that is more likely to be close to 0
this.price += this.price * this.dailyChange / 100; // changes the stock price by the dailyChange percentage.
this.price = Math.round(this.price * 100) / 100; // rgb(255,0,0)uces Price to 2 decimal places
//This keeps an array of the last last (smaPeriods) stock prices
smaArray.shift();
smaArray.push(this.price);
averages.simpleMovingAverage(smaArray);
averagePricePaid(invest.getHistory(), invest);
// Buying decision
if(this.price < averages.simpleMovingAverage(smaArray)) {
console.log("buyPercInput: " + buyPercInput.value());
invest.buy(this.price, buyPercInput.value()/100);
}
// Selling Decision
else if(this.price > invest.costBasis) {
invest.sell(this.price, percentSellInput.value()/100);
}
this.allPrices.push({
price: this.price,
change: this.dailyChange,
sma: averages.simpleMovingAverage(smaArray),
costBasis: invest.costBasis,
net: invest.netWorth(this.price),
profit: invest.profit(this.price),
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment