2019-04-28 10:23:11 +02:00
|
|
|
import { CONSTANTS } from "../src/Constants";
|
2019-05-10 11:24:50 +02:00
|
|
|
import { Player } from "../src/Player";
|
2019-06-10 06:23:48 +02:00
|
|
|
import { IMap } from "../src/types";
|
|
|
|
|
|
|
|
import { Company } from "../src/Company/Company";
|
|
|
|
import { Server } from "../src/Server/Server";
|
|
|
|
|
2019-05-10 11:24:50 +02:00
|
|
|
import {
|
|
|
|
buyStock,
|
|
|
|
sellStock,
|
|
|
|
shortStock,
|
|
|
|
sellShort,
|
|
|
|
} from "../src/StockMarket/BuyingAndSelling";
|
2019-06-10 06:23:48 +02:00
|
|
|
import { IStockMarket } from "../src/StockMarket/IStockMarket";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { Order } from "../src/StockMarket/Order";
|
2019-06-10 06:23:48 +02:00
|
|
|
import {
|
|
|
|
forecastForecastChangeFromCompanyWork,
|
|
|
|
forecastForecastChangeFromHack,
|
|
|
|
influenceStockThroughCompanyWork,
|
|
|
|
influenceStockThroughServerGrow,
|
|
|
|
influenceStockThroughServerHack,
|
|
|
|
} from "../src/StockMarket/PlayerInfluencing";
|
|
|
|
import { processOrders, IProcessOrderRefs } from "../src/StockMarket/OrderProcessing";
|
2019-06-10 00:12:33 +02:00
|
|
|
import { Stock , StockForecastInfluenceLimit } from "../src/StockMarket/Stock";
|
2019-05-05 06:03:40 +02:00
|
|
|
import {
|
2019-06-10 06:23:48 +02:00
|
|
|
cancelOrder,
|
2019-05-05 06:03:40 +02:00
|
|
|
deleteStockMarket,
|
|
|
|
initStockMarket,
|
|
|
|
initSymbolToStockMap,
|
2019-06-10 06:23:48 +02:00
|
|
|
placeOrder,
|
2019-05-10 11:24:50 +02:00
|
|
|
processStockPrices,
|
2019-05-05 06:03:40 +02:00
|
|
|
StockMarket,
|
|
|
|
SymbolToStockMap,
|
|
|
|
} from "../src/StockMarket/StockMarket";
|
2019-04-28 10:23:11 +02:00
|
|
|
import {
|
2019-05-05 06:03:40 +02:00
|
|
|
forecastChangePerPriceMovement,
|
2019-04-28 10:23:11 +02:00
|
|
|
getBuyTransactionCost,
|
|
|
|
getSellTransactionGain,
|
2019-05-23 04:12:06 +02:00
|
|
|
processTransactionForecastMovement,
|
2019-04-28 10:23:11 +02:00
|
|
|
} from "../src/StockMarket/StockMarketHelpers";
|
2019-05-07 03:01:06 +02:00
|
|
|
import { OrderTypes } from "../src/StockMarket/data/OrderTypes"
|
|
|
|
import { PositionTypes } from "../src/StockMarket/data/PositionTypes";
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-05-10 04:03:13 +02:00
|
|
|
import { expect } from "chai";
|
2019-04-28 10:23:11 +02:00
|
|
|
|
|
|
|
console.log("Beginning Stock Market Tests");
|
|
|
|
|
|
|
|
describe("Stock Market Tests", function() {
|
|
|
|
const commission = CONSTANTS.StockMarketCommission;
|
|
|
|
|
|
|
|
// Generic Stock object that can be used by each test
|
2019-06-10 06:23:48 +02:00
|
|
|
let stock: Stock;
|
2019-04-28 10:23:11 +02:00
|
|
|
const ctorParams = {
|
|
|
|
b: true,
|
|
|
|
initPrice: 10e3,
|
|
|
|
marketCap: 5e9,
|
2019-06-10 00:12:33 +02:00
|
|
|
mv: 2,
|
2019-04-28 10:23:11 +02:00
|
|
|
name: "MockStock",
|
2019-05-20 23:00:20 +02:00
|
|
|
otlkMag: 20,
|
2019-04-28 10:23:11 +02:00
|
|
|
spreadPerc: 1,
|
|
|
|
shareTxForMovement: 5e3,
|
|
|
|
symbol: "mock",
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2021-05-01 09:17:31 +02:00
|
|
|
function construct(): void {
|
2019-04-28 10:23:11 +02:00
|
|
|
stock = new Stock(ctorParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(construct).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Stock Class", function() {
|
|
|
|
describe("constructor", function() {
|
|
|
|
it("should have default parameters", function() {
|
2021-05-01 09:17:31 +02:00
|
|
|
function construct(): void {
|
|
|
|
new Stock(); // eslint-disable-line no-new
|
2019-04-28 10:23:11 +02:00
|
|
|
}
|
|
|
|
expect(construct).to.not.throw();
|
2021-05-01 09:17:31 +02:00
|
|
|
|
|
|
|
const defaultStock = new Stock();
|
|
|
|
expect(defaultStock).to.not.equal(null);
|
|
|
|
expect(defaultStock.name).to.equal("");
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly initialize props from parameters", function() {
|
|
|
|
expect(stock.name).to.equal(ctorParams.name);
|
|
|
|
expect(stock.symbol).to.equal(ctorParams.symbol);
|
|
|
|
expect(stock.price).to.equal(ctorParams.initPrice);
|
|
|
|
expect(stock.lastPrice).to.equal(ctorParams.initPrice);
|
|
|
|
expect(stock.b).to.equal(ctorParams.b);
|
|
|
|
expect(stock.mv).to.equal(ctorParams.mv);
|
|
|
|
expect(stock.shareTxForMovement).to.equal(ctorParams.shareTxForMovement);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(ctorParams.shareTxForMovement);
|
2019-04-28 10:23:11 +02:00
|
|
|
expect(stock.maxShares).to.be.below(stock.totalShares);
|
|
|
|
expect(stock.spreadPerc).to.equal(ctorParams.spreadPerc);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.otlkMag).to.be.a("number");
|
|
|
|
expect(stock.otlkMag).to.equal(ctorParams.otlkMag);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(ctorParams.b ? 50 + ctorParams.otlkMag : 50 - ctorParams.otlkMag);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it ("should properly initialize props from range-values", function() {
|
|
|
|
const params = {
|
|
|
|
b: true,
|
|
|
|
initPrice: {
|
|
|
|
max: 10e3,
|
|
|
|
min: 1e3,
|
|
|
|
},
|
|
|
|
marketCap: 5e9,
|
|
|
|
mv: {
|
|
|
|
divisor: 100,
|
|
|
|
max: 150,
|
|
|
|
min: 50,
|
|
|
|
},
|
|
|
|
name: "MockStock",
|
|
|
|
otlkMag: 10,
|
|
|
|
spreadPerc: {
|
|
|
|
divisor: 10,
|
|
|
|
max: 10,
|
|
|
|
min: 1,
|
|
|
|
},
|
|
|
|
shareTxForMovement: {
|
|
|
|
max: 10e3,
|
|
|
|
min: 5e3,
|
|
|
|
},
|
|
|
|
symbol: "mock",
|
|
|
|
};
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function construct(): void {
|
|
|
|
new Stock(params); // eslint-disable-line no-new
|
2019-04-28 10:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(construct).to.not.throw();
|
2021-05-01 09:17:31 +02:00
|
|
|
|
|
|
|
const stock = new Stock(params);
|
|
|
|
expect(stock).not.equal(null);
|
|
|
|
expect(stock.price).to.be.within(params.initPrice.min, params.initPrice.max);
|
|
|
|
expect(stock.mv).to.be.within(params.mv.min / params.mv.divisor, params.mv.max / params.mv.divisor);
|
|
|
|
expect(stock.spreadPerc).to.be.within(params.spreadPerc.min / params.spreadPerc.divisor, params.spreadPerc.max / params.spreadPerc.divisor);
|
|
|
|
expect(stock.shareTxForMovement).to.be.within(params.shareTxForMovement.min, params.shareTxForMovement.max);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should round the 'totalShare' prop to the nearest 100k", function() {
|
|
|
|
expect(stock.totalShares % 100e3).to.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-04 07:21:36 +02:00
|
|
|
describe("#changeForecastForecast()", function() {
|
|
|
|
it("should get the stock's second-order forecast property", function() {
|
|
|
|
stock.changeForecastForecast(99);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(99);
|
|
|
|
stock.changeForecastForecast(1);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should prevent values outside of 0-100", function() {
|
|
|
|
stock.changeForecastForecast(101);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(100);
|
|
|
|
stock.changeForecastForecast(-1);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-28 10:23:11 +02:00
|
|
|
describe("#changePrice()", function() {
|
|
|
|
it("should set both the last price and current price properties", function() {
|
|
|
|
const newPrice = 20e3;
|
|
|
|
stock.changePrice(newPrice);
|
|
|
|
expect(stock.lastPrice).to.equal(ctorParams.initPrice);
|
|
|
|
expect(stock.price).to.equal(newPrice);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
describe("#cycleForecast()", function() {
|
|
|
|
it("should appropriately change the otlkMag by the given amount when b=true", function() {
|
2019-06-04 07:21:36 +02:00
|
|
|
stock.getForecastIncreaseChance = () => { return 1; }
|
|
|
|
stock.cycleForecast(5);
|
|
|
|
expect(stock.otlkMag).to.equal(ctorParams.otlkMag + 5);
|
|
|
|
|
|
|
|
stock.getForecastIncreaseChance = () => { return 0; }
|
|
|
|
stock.cycleForecast(10);
|
|
|
|
expect(stock.otlkMag).to.equal(ctorParams.otlkMag - 5);
|
|
|
|
});
|
2019-05-23 04:12:06 +02:00
|
|
|
|
2019-06-04 07:21:36 +02:00
|
|
|
it("should NOT(!) the stock's 'b' property if it causes 'otlkMag' to go below 0", function() {
|
|
|
|
stock.getForecastIncreaseChance = () => { return 0; }
|
|
|
|
stock.cycleForecast(25);
|
|
|
|
expect(stock.otlkMag).to.equal(5);
|
|
|
|
expect(stock.b).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#cycleForecastForecast()", function() {
|
|
|
|
it("should increase the stock's second-order forecast by a given amount", function() {
|
|
|
|
const expected = [65, 75];
|
|
|
|
stock.cycleForecastForecast(5);
|
|
|
|
expect(stock.otlkMagForecast).to.be.oneOf(expected);
|
2019-05-23 04:12:06 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#flipForecastForecast()", function() {
|
|
|
|
it("should flip the 'otlkMagForecast' property around 50", function() {
|
|
|
|
stock.otlkMagForecast = 50;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(50);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 60;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(40);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 90;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(10);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 100;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(0);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 40;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(60);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 0;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(100);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 25;
|
|
|
|
stock.flipForecastForecast();
|
|
|
|
expect(stock.otlkMagForecast).to.equal(75);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#getAbsoluteForecast()", function() {
|
|
|
|
it("should return the absolute forecast on a 1-100 scale", function() {
|
|
|
|
stock.b = true;
|
|
|
|
stock.otlkMag = 10;
|
|
|
|
expect(stock.getAbsoluteForecast()).to.equal(60);
|
|
|
|
|
|
|
|
stock.b = false;
|
|
|
|
expect(stock.getAbsoluteForecast()).to.equal(40);
|
|
|
|
|
|
|
|
stock.otlkMag = 30;
|
|
|
|
expect(stock.getAbsoluteForecast()).to.equal(20);
|
|
|
|
|
|
|
|
stock.b = true;
|
|
|
|
expect(stock.getAbsoluteForecast()).to.equal(80);
|
|
|
|
|
|
|
|
stock.otlkMag = 0;
|
|
|
|
expect(stock.getAbsoluteForecast()).to.equal(50);
|
|
|
|
|
|
|
|
stock.b = false;
|
|
|
|
expect(stock.getAbsoluteForecast()).to.equal(50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-28 10:23:11 +02:00
|
|
|
describe("#getAskPrice()", function() {
|
|
|
|
it("should return the price increased by spread percentage", function() {
|
|
|
|
const perc = stock.spreadPerc / 100;
|
|
|
|
expect(perc).to.be.at.most(1);
|
|
|
|
expect(perc).to.be.at.least(0);
|
|
|
|
|
|
|
|
const expected = stock.price * (1 + perc);
|
|
|
|
expect(stock.getAskPrice()).to.equal(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#getBidPrice()", function() {
|
|
|
|
it("should return the price decreased by spread percentage", function() {
|
|
|
|
const perc = stock.spreadPerc / 100;
|
|
|
|
expect(perc).to.be.at.most(1);
|
|
|
|
expect(perc).to.be.at.least(0);
|
|
|
|
|
|
|
|
const expected = stock.price * (1 - perc);
|
|
|
|
expect(stock.getBidPrice()).to.equal(expected);
|
|
|
|
});
|
|
|
|
});
|
2019-05-23 04:12:06 +02:00
|
|
|
|
|
|
|
describe("#getForecastIncreaseChance()", function() {
|
|
|
|
it("should return the chance that the stock has of increasing in decimal form", function() {
|
|
|
|
stock.b = true;
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 90;
|
|
|
|
stock.otlkMag = 20; // Absolute forecast of 70
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.7);
|
|
|
|
|
|
|
|
stock.otlkMag = 25; // Absolute forecast of 75
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.65);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 100;
|
|
|
|
stock.otlkMag = 0; // Absolute forecast of 50
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.95);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 60;
|
|
|
|
stock.otlkMag = 25; // Absolute forecast of 75
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.35);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 10;
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.05);
|
|
|
|
|
|
|
|
stock.b = false;
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 90;
|
|
|
|
stock.otlkMag = 20; // Absolute forecast of 30
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.95);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 50;
|
|
|
|
stock.otlkMag = 25; // Absolute forecast of 25
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.75);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 100;
|
|
|
|
stock.otlkMag = 0; // Absolute forecast of 50
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.95);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 5;
|
|
|
|
stock.otlkMag = 25; // Absolute forecast of 25
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.3);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 10;
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.35);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 50;
|
|
|
|
stock.otlkMag = 0;
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.5);
|
2019-06-04 07:21:36 +02:00
|
|
|
|
|
|
|
stock.otlkMagForecast = 25;
|
|
|
|
stock.otlkMag = 5; // Asolute forecast of 45
|
|
|
|
expect(stock.getForecastIncreaseChance()).to.equal(0.3);
|
2019-05-23 04:12:06 +02:00
|
|
|
});
|
|
|
|
});
|
2019-06-10 00:12:33 +02:00
|
|
|
|
|
|
|
describe("#influenceForecast()", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
stock.otlkMag = 10;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should change the forecast's value towards 50", function() {
|
|
|
|
stock.influenceForecast(2);
|
|
|
|
expect(stock.otlkMag).to.equal(8);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not care about whether the stock is in bull or bear mode", function() {
|
|
|
|
stock.b = true;
|
|
|
|
stock.influenceForecast(1);
|
|
|
|
expect(stock.otlkMag).to.equal(9);
|
|
|
|
|
|
|
|
stock.b = false;
|
|
|
|
stock.influenceForecast(2);
|
|
|
|
expect(stock.otlkMag).to.equal(7);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not influence the forecast beyond the limit", function() {
|
|
|
|
stock.influenceForecast(10);
|
|
|
|
expect(stock.otlkMag).to.equal(StockForecastInfluenceLimit);
|
|
|
|
|
|
|
|
stock.influenceForecast(10);
|
|
|
|
expect(stock.otlkMag).to.equal(StockForecastInfluenceLimit);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#influenceForecastForecast()", function() {
|
|
|
|
it("should change the second-order forecast's value towards 50", function() {
|
|
|
|
stock.otlkMagForecast = 75;
|
|
|
|
stock.influenceForecastForecast(15);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(60);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 25;
|
|
|
|
stock.influenceForecastForecast(15);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(40);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not change the second-order forecast past 50", function() {
|
|
|
|
stock.otlkMagForecast = 40;
|
|
|
|
stock.influenceForecastForecast(20);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(50);
|
|
|
|
|
|
|
|
stock.otlkMagForecast = 60;
|
|
|
|
stock.influenceForecastForecast(20);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(50);
|
|
|
|
});
|
|
|
|
});
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
describe("StockMarket object", function() {
|
|
|
|
describe("Initialization", function() {
|
|
|
|
// Keeps track of initialized stocks. Contains their symbols
|
2019-06-10 06:23:48 +02:00
|
|
|
const stocks: string[]= [];
|
2019-05-05 06:03:40 +02:00
|
|
|
|
|
|
|
before(function() {
|
|
|
|
expect(initStockMarket).to.not.throw();
|
|
|
|
expect(initSymbolToStockMap).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have Stock objects", function() {
|
|
|
|
for (const prop in StockMarket) {
|
|
|
|
const stock = StockMarket[prop];
|
|
|
|
if (stock instanceof Stock) {
|
|
|
|
stocks.push(stock.symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll just check that there are some stocks
|
|
|
|
expect(stocks.length).to.be.at.least(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have an order book in the 'Orders' property", function() {
|
|
|
|
expect(StockMarket).to.have.property("Orders");
|
|
|
|
|
|
|
|
const orderbook = StockMarket["Orders"];
|
|
|
|
for (const symbol of stocks) {
|
|
|
|
const ordersForStock = orderbook[symbol];
|
|
|
|
expect(ordersForStock).to.be.an("array");
|
|
|
|
expect(ordersForStock.length).to.equal(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have properties for managing game cycles", function() {
|
|
|
|
expect(StockMarket).to.have.property("storedCycles");
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(StockMarket["storedCycles"]).to.equal(0);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(StockMarket).to.have.property("lastUpdate");
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(StockMarket["lastUpdate"]).to.equal(0);
|
2019-06-03 08:29:56 +02:00
|
|
|
expect(StockMarket).to.have.property("ticksUntilCycle");
|
|
|
|
expect(StockMarket["ticksUntilCycle"]).to.be.a("number");
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Because 'StockMarket' is a global object, the effects of initialization from
|
|
|
|
// the block above should still stand
|
|
|
|
describe("Deletion", function() {
|
|
|
|
it("should set StockMarket to be an empty object", function() {
|
|
|
|
expect(StockMarket).to.be.an("object").that.is.not.empty;
|
|
|
|
deleteStockMarket();
|
|
|
|
expect(StockMarket).to.be.an("object").that.is.empty;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-10 11:24:50 +02:00
|
|
|
describe("processStockPrices()", function() {
|
|
|
|
before(function() {
|
|
|
|
deleteStockMarket();
|
|
|
|
initStockMarket();
|
|
|
|
initSymbolToStockMap();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should store cycles until it actually processes", function() {
|
|
|
|
expect(StockMarket["storedCycles"]).to.equal(0);
|
|
|
|
processStockPrices(10);
|
|
|
|
expect(StockMarket["storedCycles"]).to.equal(10);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should trigger a price update when it has enough cycles", function() {
|
|
|
|
// Get the initial prices
|
2019-06-10 06:23:48 +02:00
|
|
|
const initialValues: IMap<any> = {};
|
2019-05-10 11:24:50 +02:00
|
|
|
for (const stockName in StockMarket) {
|
|
|
|
const stock = StockMarket[stockName];
|
|
|
|
if (!(stock instanceof Stock)) { continue; }
|
|
|
|
initialValues[stock.symbol] = {
|
2019-06-04 07:21:36 +02:00
|
|
|
b: stock.b,
|
2019-05-10 11:24:50 +02:00
|
|
|
otlkMag: stock.otlkMag,
|
2019-06-04 07:21:36 +02:00
|
|
|
price: stock.price,
|
2019-05-10 11:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't know or care how many exact cycles are required
|
2019-06-04 07:21:36 +02:00
|
|
|
StockMarket.lastUpdate = new Date().getTime() - 5e3;
|
2019-05-10 11:24:50 +02:00
|
|
|
processStockPrices(1e9);
|
|
|
|
|
|
|
|
// Both price and 'otlkMag' should be different
|
|
|
|
for (const stockName in StockMarket) {
|
|
|
|
const stock = StockMarket[stockName];
|
|
|
|
if (!(stock instanceof Stock)) { continue; }
|
|
|
|
expect(initialValues[stock.symbol].price).to.not.equal(stock.price);
|
2019-06-04 07:21:36 +02:00
|
|
|
// expect(initialValues[stock.symbol].otlkMag).to.not.equal(stock.otlkMag);
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(initialValues[stock.symbol]).to.satisfy(function(initValue: any) {
|
2019-06-04 07:21:36 +02:00
|
|
|
if ((initValue.otlkMag !== stock.otlkMag) || (initValue.b !== stock.b)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2019-05-10 11:24:50 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("StockToSymbolMap", function() {
|
|
|
|
before(function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
deleteStockMarket();
|
|
|
|
initStockMarket();
|
|
|
|
initSymbolToStockMap();
|
|
|
|
});
|
|
|
|
|
2019-05-10 11:24:50 +02:00
|
|
|
it("should map stock symbols to their corresponding Stock Objects", function() {
|
|
|
|
for (const stockName in StockMarket) {
|
|
|
|
const stock = StockMarket[stockName];
|
|
|
|
if (!(stock instanceof Stock)) { continue; }
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(SymbolToStockMap[stock.symbol]).to.equal(stock);
|
|
|
|
}
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-28 10:23:11 +02:00
|
|
|
describe("Transaction Cost Calculator Functions", function() {
|
|
|
|
describe("getBuyTransactionCost()", function() {
|
|
|
|
it("should fail on invalid 'stock' argument", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
const res = getBuyTransactionCost({} as Stock, 10, PositionTypes.Long);
|
2019-04-28 10:23:11 +02:00
|
|
|
expect(res).to.equal(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail on invalid 'shares' arg", function() {
|
|
|
|
let res = getBuyTransactionCost(stock, NaN, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(null);
|
|
|
|
|
|
|
|
res = getBuyTransactionCost(stock, -1, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(null);
|
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate LONG transactions", function() {
|
2019-04-28 10:23:11 +02:00
|
|
|
const shares = ctorParams.shareTxForMovement / 2;
|
|
|
|
const res = getBuyTransactionCost(stock, shares, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(shares * stock.getAskPrice() + commission);
|
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate SHORT transactions", function() {
|
2019-04-28 10:23:11 +02:00
|
|
|
const shares = ctorParams.shareTxForMovement / 2;
|
|
|
|
const res = getBuyTransactionCost(stock, shares, PositionTypes.Short);
|
|
|
|
expect(res).to.equal(shares * stock.getBidPrice() + commission);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should cap the 'shares' argument at the stock's maximum number of shares", function() {
|
|
|
|
const maxRes = getBuyTransactionCost(stock, stock.maxShares, PositionTypes.Long);
|
|
|
|
const exceedRes = getBuyTransactionCost(stock, stock.maxShares * 10, PositionTypes.Long);
|
|
|
|
expect(maxRes).to.equal(exceedRes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getSellTransactionGain()", function() {
|
|
|
|
it("should fail on invalid 'stock' argument", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
const res = getSellTransactionGain({} as Stock, 10, PositionTypes.Long);
|
2019-04-28 10:23:11 +02:00
|
|
|
expect(res).to.equal(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail on invalid 'shares' arg", function() {
|
|
|
|
let res = getSellTransactionGain(stock, NaN, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(null);
|
|
|
|
|
|
|
|
res = getSellTransactionGain(stock, -1, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(null);
|
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate LONG transactionst", function() {
|
2019-04-28 10:23:11 +02:00
|
|
|
const shares = ctorParams.shareTxForMovement / 2;
|
|
|
|
const res = getSellTransactionGain(stock, shares, PositionTypes.Long);
|
|
|
|
const expected = shares * stock.getBidPrice() - commission;
|
|
|
|
expect(res).to.equal(expected);
|
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate SHORT transactions", function() {
|
2019-04-28 10:23:11 +02:00
|
|
|
// We need to set this property in order to calculate gains from short position
|
|
|
|
stock.playerAvgShortPx = stock.price * 2;
|
|
|
|
|
|
|
|
const shares = ctorParams.shareTxForMovement / 2;
|
|
|
|
const res = getSellTransactionGain(stock, shares, PositionTypes.Short);
|
|
|
|
const expected = (shares * stock.playerAvgShortPx) + (shares * (stock.playerAvgShortPx - stock.getAskPrice())) - commission;
|
|
|
|
expect(res).to.equal(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should cap the 'shares' argument at the stock's maximum number of shares", function() {
|
|
|
|
const maxRes = getSellTransactionGain(stock, stock.maxShares, PositionTypes.Long);
|
|
|
|
const exceedRes = getSellTransactionGain(stock, stock.maxShares * 10, PositionTypes.Long);
|
|
|
|
expect(maxRes).to.equal(exceedRes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
describe("Forecast Movement Processor Function", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
// N = 1 is the original forecast
|
2021-05-01 09:17:31 +02:00
|
|
|
function getNthForecast(origForecast: number, n: number): number {
|
2019-05-05 06:03:40 +02:00
|
|
|
return origForecast - forecastChangePerPriceMovement * (n - 1);
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function getNthForecastForecast(origForecastForecast: number, n: number): number {
|
2019-06-10 00:12:33 +02:00
|
|
|
if (stock.otlkMagForecast > 50) {
|
|
|
|
const expected = origForecastForecast - (forecastChangePerPriceMovement * (n - 1) * (stock.mv / 100));
|
|
|
|
return expected < 50 ? 50 : expected;
|
|
|
|
} else if (stock.otlkMagForecast < 50) {
|
|
|
|
const expected = origForecastForecast + (forecastChangePerPriceMovement * (n - 1) * (stock.mv / 100));
|
|
|
|
return expected > 50 ? 50 : expected;
|
2021-05-01 09:17:31 +02:00
|
|
|
} else {
|
|
|
|
return 50;
|
2019-06-10 00:12:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
describe("processTransactionForecastMovement() for buy transactions", function() {
|
2019-04-28 10:23:11 +02:00
|
|
|
const noMvmtShares = Math.round(ctorParams.shareTxForMovement / 2.2);
|
|
|
|
const mvmtShares = ctorParams.shareTxForMovement * 3 + noMvmtShares;
|
|
|
|
|
|
|
|
it("should do nothing on invalid 'stock' argument", function() {
|
2019-05-23 04:12:06 +02:00
|
|
|
const oldTracker = stock.shareTxUntilMovement;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement({} as Stock, mvmtShares);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(oldTracker);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should do nothing on invalid 'shares' arg", function() {
|
2019-05-23 04:12:06 +02:00
|
|
|
const oldTracker = stock.shareTxUntilMovement;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, NaN);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(oldTracker);
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, -1);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(oldTracker);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate a LONG transaction that doesn't trigger a forecast movement", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
const oldForecast = stock.otlkMag;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, noMvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(oldForecast);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate a SHORT transaction that doesn't trigger a forecast movement", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
const oldForecast = stock.otlkMag;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, noMvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(oldForecast);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate LONG transactions that triggers forecast movements", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, mvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
it("should properly evaluate SHORT transactions that triggers forecast movements", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, mvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
2019-05-05 06:03:40 +02:00
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions of exactly 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions that total to 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, Math.round(stock.shareTxForMovement / 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.be.below(stock.shareTxForMovement);
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxUntilMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions that are a multiple of 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, 3 * stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions of exactly 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions that total to 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, Math.round(stock.shareTxForMovement / 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.be.below(stock.shareTxForMovement);
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxUntilMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions that are a multiple of 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-06-10 00:12:33 +02:00
|
|
|
const oldForecastForecast = stock.otlkMagForecast;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, 3 * stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(getNthForecastForecast(oldForecastForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-23 04:12:06 +02:00
|
|
|
describe("processTransactionForecastMovement() for sell transactions", function() {
|
2019-04-28 10:23:11 +02:00
|
|
|
const noMvmtShares = Math.round(ctorParams.shareTxForMovement / 2.2);
|
|
|
|
const mvmtShares = ctorParams.shareTxForMovement * 3 + noMvmtShares;
|
|
|
|
|
|
|
|
it("should do nothing on invalid 'stock' argument", function() {
|
2019-05-23 04:12:06 +02:00
|
|
|
const oldTracker = stock.shareTxUntilMovement;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement({} as Stock, mvmtShares);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(oldTracker);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should do nothing on invalid 'shares' arg", function() {
|
2019-05-23 04:12:06 +02:00
|
|
|
const oldTracker = stock.shareTxUntilMovement;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, NaN);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(oldTracker);
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, -1);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(oldTracker);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
it("should properly evaluate a LONG transaction that doesn't trigger a price movement", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, noMvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(oldForecast);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
it("should properly evaluate a SHORT transaction that doesn't trigger a price movement", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, noMvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(oldForecast);
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions that trigger price movements", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
const oldForecast = stock.otlkMag;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, mvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions that trigger price movements", function() {
|
2019-05-05 06:03:40 +02:00
|
|
|
const oldForecast = stock.otlkMag;
|
2019-04-28 10:23:11 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, mvmtShares);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement - noMvmtShares);
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
2019-05-05 06:03:40 +02:00
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions of exactly 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions that total to 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, Math.round(stock.shareTxForMovement / 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.be.below(stock.shareTxForMovement);
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxUntilMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate LONG transactions that are a multiple of 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, 3 * stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions of exactly 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions that total to 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, Math.round(stock.shareTxForMovement / 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.be.below(stock.shareTxForMovement);
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, stock.shareTxUntilMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 2));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly evaluate SHORT transactions that are a multiple of 'shareTxForMovement' shares", function() {
|
|
|
|
const oldForecast = stock.otlkMag;
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
processTransactionForecastMovement(stock, 3 * stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
expect(stock.otlkMag).to.equal(getNthForecast(oldForecast, 4));
|
2019-05-23 04:12:06 +02:00
|
|
|
expect(stock.shareTxUntilMovement).to.equal(stock.shareTxForMovement);
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|
|
|
|
});
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-05-10 11:24:50 +02:00
|
|
|
describe("Transaction (Buy/Sell) Functions", function() {
|
|
|
|
const suppressDialogOpt = { suppressDialog: true };
|
|
|
|
|
|
|
|
describe("buyStock()", function() {
|
|
|
|
it("should fail for invalid arguments", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(buyStock({} as Stock, 1, null, suppressDialogOpt)).to.equal(false);
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(buyStock(stock, 0, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(buyStock(stock, -1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(buyStock(stock, NaN, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if player doesn't have enough money", function() {
|
|
|
|
Player.setMoney(0);
|
|
|
|
expect(buyStock(stock, 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not allow for transactions that exceed the maximum shares", function() {
|
|
|
|
const maxShares = stock.maxShares;
|
|
|
|
expect(buyStock(stock, maxShares + 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true and properly update stock properties for successful transactions", function() {
|
|
|
|
const shares = 1e3;
|
|
|
|
const cost = getBuyTransactionCost(stock, shares, PositionTypes.Long);
|
2019-06-10 06:23:48 +02:00
|
|
|
if (cost == null) {
|
|
|
|
expect.fail();
|
2021-05-01 09:17:31 +02:00
|
|
|
return;
|
2019-06-10 06:23:48 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
Player.setMoney(cost);
|
2019-05-10 11:24:50 +02:00
|
|
|
|
|
|
|
expect(buyStock(stock, shares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShares).to.equal(shares);
|
|
|
|
expect(stock.playerAvgPx).to.be.above(0);
|
|
|
|
expect(Player.money.toNumber()).to.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("sellStock()", function() {
|
|
|
|
it("should fail for invalid arguments", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(sellStock({} as Stock, 1, null, suppressDialogOpt)).to.equal(false);
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(sellStock(stock, 0, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(sellStock(stock, -1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(sellStock(stock, NaN, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if player doesn't have any shares", function() {
|
|
|
|
Player.setMoney(0);
|
|
|
|
expect(sellStock(stock, 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not allow for transactions that exceed the maximum shares", function() {
|
|
|
|
const maxShares = stock.maxShares;
|
|
|
|
expect(sellStock(stock, maxShares + 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true and properly update stock properties for successful transactions", function() {
|
|
|
|
const shares = 1e3;
|
|
|
|
stock.playerShares = shares;
|
|
|
|
stock.playerAvgPx = stock.price;
|
|
|
|
const gain = getSellTransactionGain(stock, shares, PositionTypes.Long);
|
|
|
|
Player.setMoney(0);
|
|
|
|
|
|
|
|
expect(sellStock(stock, shares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShares).to.equal(0);
|
|
|
|
expect(stock.playerAvgPx).to.equal(0);
|
|
|
|
expect(Player.money.toNumber()).to.equal(gain);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should cap the number of sharse sold to however many the player owns", function() {
|
|
|
|
const attemptedShares = 2e3;
|
|
|
|
const actualShares = 1e3;
|
|
|
|
stock.playerShares = actualShares;
|
|
|
|
stock.playerAvgPx = stock.price;
|
|
|
|
const gain = getSellTransactionGain(stock, actualShares, PositionTypes.Long);
|
|
|
|
Player.setMoney(0);
|
|
|
|
|
|
|
|
expect(sellStock(stock, attemptedShares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShares).to.equal(0);
|
|
|
|
expect(stock.playerAvgPx).to.equal(0);
|
|
|
|
expect(Player.money.toNumber()).to.equal(gain);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly update stock properties for partial transactions", function() {
|
|
|
|
const shares = 1e3;
|
|
|
|
const origPrice = stock.price;
|
|
|
|
stock.playerShares = 2 * shares;
|
|
|
|
stock.playerAvgPx = origPrice;
|
|
|
|
const gain = getSellTransactionGain(stock, shares, PositionTypes.Long);
|
|
|
|
Player.setMoney(0);
|
|
|
|
|
|
|
|
expect(sellStock(stock, shares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShares).to.equal(shares);
|
|
|
|
expect(stock.playerAvgPx).to.equal(origPrice);
|
|
|
|
expect(Player.money.toNumber()).to.equal(gain);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("shortStock()", function() {
|
|
|
|
it("should fail for invalid arguments", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(shortStock({} as Stock, 1, null, suppressDialogOpt)).to.equal(false);
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(shortStock(stock, 0, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(shortStock(stock, -1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(shortStock(stock, NaN, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if player doesn't have enough money", function() {
|
|
|
|
Player.setMoney(0);
|
|
|
|
expect(shortStock(stock, 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not allow for transactions that exceed the maximum shares", function() {
|
|
|
|
const maxShares = stock.maxShares;
|
|
|
|
expect(shortStock(stock, maxShares + 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true and properly update stock properties for successful transactions", function() {
|
|
|
|
const shares = 1e3;
|
|
|
|
const cost = getBuyTransactionCost(stock, shares, PositionTypes.Short);
|
2019-06-10 06:23:48 +02:00
|
|
|
if (cost == null) {
|
|
|
|
expect.fail();
|
2021-05-01 09:17:31 +02:00
|
|
|
return
|
2019-06-10 06:23:48 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
Player.setMoney(cost);
|
2019-05-10 11:24:50 +02:00
|
|
|
|
|
|
|
expect(shortStock(stock, shares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShortShares).to.equal(shares);
|
|
|
|
expect(stock.playerAvgShortPx).to.be.above(0);
|
|
|
|
expect(Player.money.toNumber()).to.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("sellShort()", function() {
|
|
|
|
it("should fail for invalid arguments", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(sellShort({} as Stock, 1, null, suppressDialogOpt)).to.equal(false);
|
2019-05-10 11:24:50 +02:00
|
|
|
expect(sellShort(stock, 0, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(sellShort(stock, -1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
expect(sellShort(stock, NaN, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if player doesn't have any shares", function() {
|
|
|
|
Player.setMoney(0);
|
|
|
|
expect(sellShort(stock, 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not allow for transactions that exceed the maximum shares", function() {
|
|
|
|
const maxShares = stock.maxShares;
|
|
|
|
expect(sellShort(stock, maxShares + 1, null, suppressDialogOpt)).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true and properly update stock properties for successful transactions", function() {
|
|
|
|
const shares = 1e3;
|
|
|
|
stock.playerShortShares = shares;
|
|
|
|
stock.playerAvgShortPx = stock.price;
|
|
|
|
const gain = getSellTransactionGain(stock, shares, PositionTypes.Short);
|
|
|
|
Player.setMoney(0);
|
|
|
|
|
|
|
|
expect(sellShort(stock, shares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShortShares).to.equal(0);
|
|
|
|
expect(stock.playerAvgShortPx).to.equal(0);
|
|
|
|
expect(Player.money.toNumber()).to.equal(gain);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should cap the number of sharse sold to however many the player owns", function() {
|
|
|
|
const attemptedShares = 2e3;
|
|
|
|
const actualShares = 1e3;
|
|
|
|
stock.playerShortShares = actualShares;
|
|
|
|
stock.playerAvgShortPx = stock.price;
|
|
|
|
const gain = getSellTransactionGain(stock, actualShares, PositionTypes.Short);
|
|
|
|
Player.setMoney(0);
|
|
|
|
|
|
|
|
expect(sellShort(stock, attemptedShares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShortShares).to.equal(0);
|
|
|
|
expect(stock.playerAvgShortPx).to.equal(0);
|
|
|
|
expect(Player.money.toNumber()).to.equal(gain);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should properly update stock properties for partial transactions", function() {
|
|
|
|
const shares = 1e3;
|
|
|
|
const origPrice = stock.price;
|
|
|
|
stock.playerShortShares = 2 * shares;
|
|
|
|
stock.playerAvgShortPx = origPrice;
|
|
|
|
const gain = getSellTransactionGain(stock, shares, PositionTypes.Short);
|
|
|
|
Player.setMoney(0);
|
|
|
|
|
|
|
|
expect(sellShort(stock, shares, null, suppressDialogOpt)).to.equal(true);
|
|
|
|
expect(stock.playerShortShares).to.equal(shares);
|
|
|
|
expect(stock.playerAvgShortPx).to.equal(origPrice);
|
|
|
|
expect(Player.money.toNumber()).to.equal(gain);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
describe("Order Class", function() {
|
|
|
|
it("should throw on invalid arguments", function() {
|
2021-05-01 09:17:31 +02:00
|
|
|
function invalid1(): Order {
|
2019-06-10 06:23:48 +02:00
|
|
|
return new Order({} as string, 1, 1, OrderTypes.LimitBuy, PositionTypes.Long);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
function invalid2(): Order {
|
2019-06-10 06:23:48 +02:00
|
|
|
return new Order("FOO", "z" as any as number, 0, OrderTypes.LimitBuy, PositionTypes.Short);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
function invalid3(): Order {
|
2019-06-10 06:23:48 +02:00
|
|
|
return new Order("FOO", 1, {} as number, OrderTypes.LimitBuy, PositionTypes.Short);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
function invalid4(): Order {
|
2019-05-07 03:01:06 +02:00
|
|
|
return new Order("FOO", 1, NaN, OrderTypes.LimitBuy, PositionTypes.Short);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
function invalid5(): Order {
|
2019-05-07 03:01:06 +02:00
|
|
|
return new Order("FOO", NaN, 0, OrderTypes.LimitBuy, PositionTypes.Short);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(invalid1).to.throw();
|
|
|
|
expect(invalid2).to.throw();
|
|
|
|
expect(invalid3).to.throw();
|
|
|
|
expect(invalid4).to.throw();
|
|
|
|
expect(invalid5).to.throw();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
describe("Order Placing & Processing", function() {
|
|
|
|
beforeEach(function() {
|
2019-06-10 00:12:33 +02:00
|
|
|
expect(initStockMarket).to.not.throw();
|
|
|
|
expect(initSymbolToStockMap).to.not.throw();
|
2019-06-10 06:23:48 +02:00
|
|
|
|
|
|
|
// Create an order book for our mock stock
|
|
|
|
StockMarket["Orders"][stock.symbol] = [];
|
2019-06-10 00:12:33 +02:00
|
|
|
});
|
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
describe("placeOrder()", function() {
|
|
|
|
it("should return false when it's called with invalid arguments", function() {
|
|
|
|
const invalid1 = placeOrder({} as Stock, 1, 1, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
const invalid2 = placeOrder(stock, "foo" as any as number, 2, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
const invalid3 = placeOrder(stock, 1, "foo" as any as number, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
|
|
|
|
expect(invalid1).to.equal(false);
|
|
|
|
expect(invalid2).to.equal(false);
|
|
|
|
expect(invalid3).to.equal(false);
|
|
|
|
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true and update the order book for valid arguments", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 9e3, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(true);
|
|
|
|
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(1);
|
|
|
|
const order = StockMarket["Orders"][stock.symbol][0];
|
|
|
|
expect(order).to.be.an.instanceof(Order);
|
|
|
|
expect(order.stockSymbol).to.equal(ctorParams.symbol);
|
|
|
|
expect(order.shares).to.equal(1e3);
|
|
|
|
expect(order.price).to.equal(9e3);
|
|
|
|
expect(order.type).to.equal(OrderTypes.LimitBuy);
|
|
|
|
expect(order.pos).to.equal(PositionTypes.Long);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("cancelOrder()", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
StockMarket["Orders"][stock.symbol] = [];
|
|
|
|
|
|
|
|
const res = placeOrder(stock, 1e3, 9e3, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
expect(res).to.equal(true);
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns true & removes an Order from the order book", function() {
|
|
|
|
const order = StockMarket["Orders"][stock.symbol][0];
|
|
|
|
const res = cancelOrder({ order });
|
|
|
|
expect(res).to.equal(true);
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should also work when passing in order parameters separately", function() {
|
|
|
|
const res = cancelOrder({
|
|
|
|
stock,
|
|
|
|
shares: 1e3,
|
|
|
|
price: 9e3,
|
|
|
|
type: OrderTypes.LimitBuy,
|
2021-04-30 05:52:56 +02:00
|
|
|
pos: PositionTypes.Long,
|
2019-06-10 06:23:48 +02:00
|
|
|
});
|
|
|
|
expect(res).to.equal(true);
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false and do nothing when the specified order doesn't exist", function() {
|
|
|
|
// Same parameters, but its a different object
|
|
|
|
const order = new Order(stock.symbol, 1e3, 9e3, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
const res = cancelOrder({ order });
|
|
|
|
expect(res).to.equal(false);
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(1);
|
|
|
|
|
|
|
|
const res2 = cancelOrder({
|
|
|
|
stock,
|
|
|
|
shares: 999,
|
|
|
|
price: 9e3,
|
|
|
|
type: OrderTypes.LimitBuy,
|
2021-04-30 05:52:56 +02:00
|
|
|
pos: PositionTypes.Long,
|
2019-06-10 06:23:48 +02:00
|
|
|
});
|
|
|
|
expect(res2).to.equal(false);
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("processOrders()", function() {
|
|
|
|
let processOrdersRefs: IProcessOrderRefs;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
expect(initStockMarket).to.not.throw();
|
|
|
|
expect(initSymbolToStockMap).to.not.throw();
|
|
|
|
|
|
|
|
StockMarket[stock.name] = stock;
|
|
|
|
SymbolToStockMap[stock.symbol] = stock;
|
|
|
|
StockMarket["Orders"][stock.symbol] = [];
|
|
|
|
|
|
|
|
stock.playerShares = 1e3;
|
|
|
|
stock.playerShortShares = 1e3;
|
|
|
|
Player.setMoney(100e9);
|
|
|
|
|
|
|
|
processOrdersRefs = {
|
2021-05-01 09:17:31 +02:00
|
|
|
rerenderFn: () => undefined,
|
2019-06-10 06:23:48 +02:00
|
|
|
stockMarket: StockMarket as IStockMarket,
|
|
|
|
symbolToStockMap: SymbolToStockMap,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function checkThatOrderExists(placeOrderRes?: boolean): void {
|
2019-06-10 06:23:48 +02:00
|
|
|
if (typeof placeOrderRes === "boolean") {
|
|
|
|
expect(placeOrderRes).to.equal(true);
|
|
|
|
}
|
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(1);
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function checkThatOrderExecuted(): void {
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(StockMarket["Orders"][stock.symbol]).to.have.lengthOf(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should execute LONG Limit Buy orders when price <= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 9e3, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(9e3);
|
|
|
|
processOrders(stock, OrderTypes.LimitBuy, PositionTypes.Long, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShares).to.equal(2e3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute SHORT Limit Buy Orders when price >= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 11e3, OrderTypes.LimitBuy, PositionTypes.Short);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(11e3);
|
|
|
|
processOrders(stock, OrderTypes.LimitBuy, PositionTypes.Short, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShortShares).to.equal(2e3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute LONG Limit Sell Orders when price >= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 11e3, OrderTypes.LimitSell, PositionTypes.Long);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(11e3);
|
|
|
|
processOrders(stock, OrderTypes.LimitSell, PositionTypes.Long, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShares).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute SHORT Limit Sell Orders when price <= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 9e3, OrderTypes.LimitSell, PositionTypes.Short);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(9e3);
|
|
|
|
processOrders(stock, OrderTypes.LimitSell, PositionTypes.Short, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShortShares).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute LONG Stop Buy Orders when price >= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 11e3, OrderTypes.StopBuy, PositionTypes.Long);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(11e3);
|
|
|
|
processOrders(stock, OrderTypes.StopBuy, PositionTypes.Long, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShares).to.equal(2e3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute SHORT Stop Buy Orders when price <= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 9e3, OrderTypes.StopBuy, PositionTypes.Short);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(9e3);
|
|
|
|
processOrders(stock, OrderTypes.StopBuy, PositionTypes.Short, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShortShares).to.equal(2e3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute LONG Stop Sell Orders when price <= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 9e3, OrderTypes.StopSell, PositionTypes.Long);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(9e3);
|
|
|
|
processOrders(stock, OrderTypes.StopSell, PositionTypes.Long, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShares).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute SHORT Stop Sell Orders when price >= order price", function() {
|
|
|
|
const res = placeOrder(stock, 1e3, 11e3, OrderTypes.StopSell, PositionTypes.Short);
|
|
|
|
checkThatOrderExists(res);
|
|
|
|
|
|
|
|
stock.changePrice(11e3);
|
|
|
|
processOrders(stock, OrderTypes.StopSell, PositionTypes.Short, processOrdersRefs);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShortShares).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should execute immediately if their conditions are satisfied", function() {
|
|
|
|
placeOrder(stock, 1e3, 11e3, OrderTypes.LimitBuy, PositionTypes.Long);
|
|
|
|
checkThatOrderExecuted();
|
|
|
|
expect(stock.playerShares).to.equal(2e3);
|
|
|
|
});
|
2019-06-10 00:12:33 +02:00
|
|
|
});
|
2019-06-04 07:21:36 +02:00
|
|
|
});
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-06-10 06:23:48 +02:00
|
|
|
// TODO
|
2019-06-04 07:21:36 +02:00
|
|
|
describe("Player Influencing", function() {
|
2019-06-10 06:23:48 +02:00
|
|
|
const server = new Server({
|
|
|
|
hostname: "mockserver",
|
|
|
|
moneyAvailable: 1e6,
|
|
|
|
organizationName: "MockStock",
|
|
|
|
});
|
|
|
|
|
|
|
|
const company = new Company({
|
|
|
|
name: "MockStock",
|
|
|
|
info: "",
|
|
|
|
companyPositions: {},
|
|
|
|
expMultiplier: 1,
|
|
|
|
salaryMultiplier: 1,
|
|
|
|
jobStatReqOffset: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
expect(initStockMarket).to.not.throw();
|
|
|
|
expect(initSymbolToStockMap).to.not.throw();
|
|
|
|
|
|
|
|
StockMarket[stock.name] = stock;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("influenceStockThroughServerHack()", function() {
|
|
|
|
it("should decrease a stock's second-order forecast when all of its money is hacked", function() {
|
|
|
|
const oldSecondOrderForecast = stock.otlkMagForecast;
|
|
|
|
influenceStockThroughServerHack(server, server.moneyMax);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(oldSecondOrderForecast - forecastForecastChangeFromHack);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not decrease the stock's second-order forecast when no money is stolen", function() {
|
|
|
|
const oldSecondOrderForecast = stock.otlkMagForecast;
|
|
|
|
influenceStockThroughServerHack(server, 0);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(oldSecondOrderForecast);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("influenceStockThroughServerGrow()", function() {
|
|
|
|
it("should increase a stock's second-order forecast when all of its money is grown", function() {
|
|
|
|
const oldSecondOrderForecast = stock.otlkMagForecast;
|
|
|
|
influenceStockThroughServerGrow(server, server.moneyMax);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(oldSecondOrderForecast + forecastForecastChangeFromHack);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not increase the stock's second-order forecast when no money is grown", function() {
|
|
|
|
const oldSecondOrderForecast = stock.otlkMagForecast;
|
|
|
|
influenceStockThroughServerGrow(server, 0);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(oldSecondOrderForecast);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("influenceStockThroughCompanyWork()", function() {
|
|
|
|
it("should increase the server's second order forecast", function() {
|
|
|
|
const oldSecondOrderForecast = stock.otlkMagForecast;
|
|
|
|
|
|
|
|
// Use 1e3 for numCycles to force a change
|
|
|
|
// (This may break later if numbers are rebalanced);
|
2019-06-11 09:18:14 +02:00
|
|
|
influenceStockThroughCompanyWork(company, 1, 500);
|
2019-06-10 06:23:48 +02:00
|
|
|
expect(stock.otlkMagForecast).to.equal(oldSecondOrderForecast + forecastForecastChangeFromCompanyWork);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be affected by performanceMult", function() {
|
|
|
|
const oldSecondOrderForecast = stock.otlkMagForecast;
|
|
|
|
|
|
|
|
// Use 1e3 for numCycles to force a change
|
|
|
|
// (This may break later if numbers are rebalanced);
|
|
|
|
influenceStockThroughCompanyWork(company, 4, 1e3);
|
|
|
|
expect(stock.otlkMagForecast).to.equal(oldSecondOrderForecast + 4 * forecastForecastChangeFromCompanyWork);
|
|
|
|
});
|
|
|
|
});
|
2019-05-05 06:03:40 +02:00
|
|
|
});
|
2019-04-28 10:23:11 +02:00
|
|
|
});
|