Skip to content
Snippets Groups Projects
Commit 99f97107 authored by Victor Brestoiu's avatar Victor Brestoiu
Browse files

Initial upload.

parent 73ad14ad
No related branches found
Tags 7.x-1.0
No related merge requests found
Showing
with 1289 additions and 0 deletions
AGENTS
JOE: (Couch potato)
- Considers stocks only every 100 days
- Valuation is average price over last 100 days
- Prefers buying high cost over low cost
RYAN: (Aggressive)
- Considers stocks daily
- Valuation is tangent line of last two datum, instant buy or sell if above/below certain line (Comission)
JAMES: (Passive)
- Considers stocks every 5 days
- Valuation is average of 100 day min and max
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EconSim", "EconSim\EconSim.vcxproj", "{5FDAAABC-D682-4510-AF47-8209B6D759B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Debug|x64.ActiveCfg = Debug|x64
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Debug|x64.Build.0 = Debug|x64
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Debug|x86.ActiveCfg = Debug|Win32
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Debug|x86.Build.0 = Debug|Win32
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Release|x64.ActiveCfg = Release|x64
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Release|x64.Build.0 = Release|x64
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Release|x86.ActiveCfg = Release|Win32
{5FDAAABC-D682-4510-AF47-8209B6D759B5}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
#include "stdafx.h"
#include "Stock.h"
#include "Agent.h"
#include <iomanip>
#include <algorithm>
void Agent::Start(int _id)
{
id = _id;
cash = 1000;
cashEarnedByTrading = 0;
personalWindow.create(sf::VideoMode(400, 200), "Agent Info Window");
portfolioValue.setString("$0");
portfolioValue.setFont(arial);
portfolioValue.setPosition(10.f, 150.f);
portfolioValue.setCharacterSize(13);
cashValue.setString("$0");
cashValue.setFont(arial);
cashValue.setPosition(200.f, 150.f);
cashValue.setCharacterSize(13);
for (unsigned int cnt = 0; cnt < stocks.size(); cnt++)
{
sf::Text text;
text.setString("$0");
text.setFont(arial);
text.setPosition(10.f, 10.f + 35.f * cnt);
text.setCharacterSize(12);
if (cnt == 0)
{
text.setColor(sf::Color::Blue);
}
else if (cnt == 1)
{
text.setColor(sf::Color::Red);
}
else if (cnt == 2)
{
text.setColor(sf::Color::Green);
}
else
{
text.setColor(sf::Color::White);
}
portfolioTexts.push_back(text);
}
}
void Agent::Update()
{
//cash += 10;
double portValue = 0;
personalWindow.setTitle(agentName + " Info Window");
for (unsigned int cnt = 0; cnt < portfolio.size(); cnt++)
{
portfolioTexts[cnt].setString(
std::to_string(portfolio[cnt].quantity) +
" of stock " +
std::to_string(portfolio[cnt].stock->id) +
" values " +
std::to_string(portfolio[cnt].quantity * portfolio[cnt].stock->GetCurrentData().price)
);
portValue += portfolio[cnt].quantity * portfolio[cnt].stock->GetCurrentData().price;
}
portfolioValue.setString("Portfolio Values: $" + std::to_string(portValue));
cashValue.setString("Cash Holdings: $" + std::to_string(cash));
personalWindow.clear();
personalWindow.draw(portfolioValue);
personalWindow.draw(cashValue);
for (unsigned int cnt = 0; cnt < portfolioTexts.size(); cnt++)
{
personalWindow.draw(portfolioTexts[cnt]);
}
personalWindow.display();
}
bool SortFunc(PortfolioItem _a, PortfolioItem _b)
{
return _a.quantity > _b.quantity;
}
bool SortByID(PortfolioItem _a, PortfolioItem _b)
{
return _a.stock->id < _b.stock->id;
}
void Agent::Info()
{
double portfolioFinal = 0;
std::cout << "Agent " << id << " finished the rounds with a portfolio of size " << portfolio.size() << " with the following items: " << std::endl;
std::sort(portfolio.begin(), portfolio.end(), SortFunc);
for (unsigned int cnt = 0; cnt < portfolio.size(); cnt++)
{
std::cout << "\tStock #" << portfolio[cnt].stock->id << " at a quantity of " << std::setprecision(8) << portfolio[cnt].quantity << std::endl;
portfolioFinal += portfolio[cnt].quantity * portfolio[cnt].stock->GetCurrentData().price;
}
std::cout << "Final cash amount: " << cash << std::endl;
std::cout << "Final value of portfolio: " << portfolioFinal << std::endl;
std::cout << "Cash earned by trading: " << cashEarnedByTrading << std::endl;
std::cout << "Net revenue: " << portfolioFinal + cash - 1000 << std::endl;
}
void Agent::Summary()
{
}
double Agent::ValuateStock(Stock& _stock)
{
return _stock.GetCurrentData().price;
}
double Agent::BidStock(Stock& _stock, double _opinionValue)
{
const double factor = 2;
return (factor * (pow((_opinionValue - _stock.GetCurrentData().price), 3)));
}
bool Agent::TransferStock(Stock& _stock, double _quantity)
{
for (unsigned int cnt = 0; cnt < portfolio.size(); cnt++)
{
if (portfolio[cnt].stock->id == _stock.id)
{
if (_quantity < 0)
{
//Agent is selling
if (portfolio[cnt].quantity >= -_quantity)
{
portfolio[cnt].quantity += _quantity;
cash += _stock.GetCurrentData().price * -_quantity;
cashEarnedByTrading += _stock.GetCurrentData().price * -_quantity;
}
else
{
return false;
}
}
else
{
//Agent is buying
portfolio[cnt].quantity += _quantity;
cash -= _stock.GetCurrentData().price * _quantity;
}
return true;
}
}
PortfolioItem item;
item.stock = &_stock;
item.quantity = 0;
item.quantity += _quantity;
portfolio.push_back(item);
cash -= _stock.GetCurrentData().price * _quantity;
return true;
}
void Agent::SortPortfolioByID()
{
std::sort(portfolio.begin(), portfolio.end(), SortByID);
}
#pragma once
#include "Stock.h"
struct PortfolioItem
{
Stock* stock;
double quantity;
};
class Agent
{
public:
double cash;
double cashEarnedByTrading;
int id;
std::vector<PortfolioItem> portfolio;
sf::RenderWindow personalWindow;
sf::Text portfolioValue;
sf::Text cashValue;
std::vector<sf::Text> portfolioTexts;
std::string agentName;
virtual void Start(int _id);
virtual void Update();
virtual void Info();
virtual void Summary();
virtual double ValuateStock(Stock& _stock);
virtual double BidStock(Stock& _stock, double _opinionValue);
virtual bool TransferStock(Stock& _stock, double _quantity);
void SortPortfolioByID();
};
\ No newline at end of file
#include "stdafx.h"
#include "Agent.h"
#include "Agent1-VeryPassive.h"
#include "Stock.h"
double Agent1_VeryPassive::ValuateStock(Stock & _stock)
{
if (day == 0)
{
agentName = "Very Passive Agent";
}
double bid = 0;
if (day % 30 == 0 && day > 0)
{
if (cash < 0)
{
if (_stock.GetCurrentData().price < 0)
{
return -1000;
}
else
{
return _stock.GetCurrentData().price - 1;
}
}
if (_stock.history[_stock.history.size() - 30].price > 0 &&
_stock.history[_stock.history.size() - 1].price > 0 &&
_stock.history[_stock.history.size() - 16].price > 0)
{
//y2 - y1 = m(x2-x1)
//y2 - y1 = m(30)
//m = (y2 - y1) / 30
//To guess next month's value:
// m = 30 * (y2 - y1) / 30
// m = y2 - y1
//bid = y2 - y1 + y3
//where y1 = 30 days ago
// y2 = today
// y3 = 15 days ago
bid = _stock.history[_stock.history.size() - 1].price - _stock.history[_stock.history.size() - 30].price + _stock.history[_stock.history.size() - 16].price;
}
else
{
return -1000;
}
//Standard check to make sure you don't end up with negative money
if (BidStock(_stock, bid) * _stock.GetCurrentData().price > cash)
{
bid = (cash) / _stock.GetCurrentData().price;
}
}
else
{
bid = _stock.GetCurrentData().price;
}
return bid;
}
#pragma once
class Agent1_VeryPassive : public Agent
{
public:
double ValuateStock(Stock& _stock);
};
\ No newline at end of file
#include "stdafx.h"
#include "Stock.h"
#include "Agent2-SemiPassive.h"
#include "Agent.h"
double Agent2_SemiPassive::ValuateStock(Stock & _stock)
{
if (day == 0)
{
agentName = "Semi Passive Agent";
}
double bid = 0;
if (day % 7 == 0 && day > 0)
{
if (cash < 0)
{
if (_stock.GetCurrentData().price < 0)
{
return -1000;
}
else
{
return _stock.GetCurrentData().price - 1;
}
}
if (_stock.history[_stock.history.size() - 6].price > 0 &&
_stock.history[_stock.history.size() - 1].price > 0 &&
_stock.history[_stock.history.size() - 3].price > 0)
{
//y2 - y1 = m(x2-x1)
//y2 - y1 = m(30)
//m = (y2 - y1) / 30
//To guess next month's value:
// m = 30 * (y2 - y1) / 30
// m = y2 - y1
//bid = y2 - y1 + y3
//where y1 = 30 days ago
// y2 = today
// y3 = 15 days ago
bid = _stock.history[_stock.history.size() - 1].price - _stock.history[_stock.history.size() - 6].price + _stock.history[_stock.history.size() - 3].price;
}
else
{
return -1000;
}
//Standard check to make sure you don't end up with negative money
if (BidStock(_stock, bid) * _stock.GetCurrentData().price > cash)
{
bid = (cash) / _stock.GetCurrentData().price;
}
}
else
{
bid = _stock.GetCurrentData().price;
}
return bid;
}
#pragma once
class Agent2_SemiPassive : public Agent
{
public:
double ValuateStock(Stock& _stock);
};
\ No newline at end of file
#include "Agent3-Agressive.h"
#include "Agent3-Agressive.h"
#include "stdafx.h"
#include "Stock.h"
#include "Agent.h"
#include "Agent3-Agressive.h"
/*void Agent3_Aggressive::Start(int _id)
{
std::cout << "Aggressive Agent loaded." << std::endl;
id = _id;
cash = 1000;
personalWindow.create(sf::VideoMode(400, 400), "Aggressive Agent Info Window");
}*/
double Agent3_Aggressive::ValuateStock(Stock& _stock)
{
if (day == 0)
{
agentName = "Aggressive Agent";
}
double bid = 0;
if (cash < 0)
{
return _stock.GetCurrentData().price - 1;
}
if (_stock.history.size() > 6)
{
bid += _stock.history[_stock.history.size() - 1].price - _stock.history[_stock.history.size() - 2].price;
bid += _stock.history[_stock.history.size() - 2].price - _stock.history[_stock.history.size() - 3].price;
bid += _stock.history[_stock.history.size() - 3].price - _stock.history[_stock.history.size() - 4].price;
bid += _stock.history[_stock.history.size() - 4].price - _stock.history[_stock.history.size() - 5].price;
bid += _stock.history[_stock.history.size() - 5].price - _stock.history[_stock.history.size() - 6].price;
bid += _stock.history[_stock.history.size() - 6].price - _stock.history[_stock.history.size() - 7].price;
bid += _stock.GetCurrentData().price;
}
else
{
bid = _stock.history[0].price;
}
if (BidStock(_stock,bid) * _stock.GetCurrentData().price > cash / stocks.size())
{
bid = (cash / stocks.size()) / _stock.GetCurrentData().price;
}
if (_stock.GetCurrentData().price < 0)
{
bid = _stock.GetCurrentData().price;
}
return bid;
}
\ No newline at end of file
#pragma once
class Agent3_Aggressive : public Agent
{
public:
double ValuateStock(Stock& _stock);
};
\ No newline at end of file
// EconSim.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <time.h>
#include <iomanip>
std::vector<Agent*> agents;
std::vector<Stock> stocks;
unsigned int day;
sf::Font arial;
bool ContinueRunningMarketSimulations = true;
int main()
{
sf::Clock runTimer;
sf::RenderWindow window(sf::VideoMode(900, 700), "EconSim");
sf::View mainView;
sf::Clock timer;
const sf::Time dt = sf::seconds(1.f / 90.f);
sf::Time acccum = sf::Time::Zero;
if (!arial.loadFromFile("arial.ttf"))
{
std::cout << "Error loading arial font!" << std::endl;
system("pause");
return 0;
}
srand((unsigned int)time(NULL));
Stock test;
stocks.push_back(test);
Stock alsoTest;
stocks.push_back(alsoTest);
Stock third;
stocks.push_back(third);
Stock fourth;
stocks.push_back(fourth);
Agent agent;
agents.push_back(&agent);
Agent1_VeryPassive vpass;
agents.push_back(&vpass);
Agent2_SemiPassive spass;
agents.push_back(&spass);
Agent3_Aggressive aggro;
agents.push_back(&aggro);
sf::Vertex verts[2] =
{
sf::Vertex(sf::Vector2f(0.f,600.f), sf::Color::Magenta),
sf::Vertex(sf::Vector2f(900.f,600.f), sf::Color::Magenta)
};
//Create stocks and agents
for (unsigned int itr = 0; itr < stocks.size(); itr++)
{
stocks[itr].Start(itr);
}
for (unsigned int cnt = 0; cnt < agents.size(); cnt++)
{
agents[cnt]->Start(cnt);
}
//Start Simulation
day = 0;
timer.restart();
std::cout << "Startup took " << runTimer.restart().asSeconds() << " seconds." << std::endl;
//system("pause");
while (window.isOpen())
{
acccum += timer.restart();
while (acccum >= dt)
{
acccum -= dt;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (ContinueRunningMarketSimulations)
{
///HANDLE LOGIC
//Agents bid on stocks - buy and sell
for (unsigned int cnt = 0; cnt < agents.size(); cnt++)
{
for (unsigned int itr = 0; itr < stocks.size(); itr++)
{
//Pass in the stock's actual value and the agent's valuation
double bid = agents[cnt]->BidStock(stocks[itr], agents[cnt]->ValuateStock(stocks[itr]));
BuyOrder bOrder;
SellOrder sOrder;
bOrder.placer = agents[cnt];
bOrder.stock = &stocks[itr];
bOrder.price = stocks[itr].GetCurrentData().price;
sOrder.placer = agents[cnt];
sOrder.stock = &stocks[itr];
sOrder.price = stocks[itr].GetCurrentData().price;
if (bid >= 0)
{
//Buy or hold
bOrder.quantity = bid;
sOrder.quantity = 0;
}
else
{
//Sell
bOrder.quantity = 0;
if (bid > agents[cnt]->portfolio[itr].quantity)
{
sOrder.quantity = agents[cnt]->portfolio[itr].quantity;
}
else
{
sOrder.quantity = bid;
}
}
if ((int)(stocks[itr].buys.size() - 1) >= 0)
{
stocks[itr].buys[stocks[itr].buys.size() - 1].push_back(bOrder);
}
else
{
stocks[itr].buys[stocks[itr].buys.size()].push_back(bOrder);
}
if ((int)(stocks[itr].sells.size() - 1) >= 0)
{
stocks[itr].sells[stocks[itr].sells.size() - 1].push_back(sOrder);
}
else
{
stocks[itr].sells[stocks[itr].sells.size()].push_back(sOrder);
}
}
}
//Transfer stocks according to bids
for (unsigned int itr = 0; itr < stocks.size(); itr++)
{
stocks[itr].Transfer();
}
//Handle the actual buying and selling - agents gain and lose stocks here
for (unsigned int cnt = 0; cnt < stocks.size(); cnt++)
{
stocks[cnt].Update();
}
}
//Update agents to display their values
for (unsigned int cnt = 0; cnt < agents.size(); cnt++)
{
agents[cnt]->Update();
}
///DRAW
window.clear();
for (unsigned int cnt = 0; cnt < stocks.size(); cnt++)
{
window.draw(&stocks[cnt].graph[0], stocks[cnt].graph.size(), sf::Lines);
window.draw(stocks[cnt].priceLabel);
}
window.draw(verts, 2, sf::Lines);
window.display();
///Keep track of number of 'turns'
if (ContinueRunningMarketSimulations)
{
day++;
}
if (day % 900 == 0 && day !=0 && ContinueRunningMarketSimulations)
{
for (unsigned int cnt = 0; cnt < stocks.size(); cnt++)
{
stocks[cnt].Info();
}
for (unsigned int cnt = 0; cnt < agents.size(); cnt++)
{
agents[cnt]->Info();
}
std::cout << "Running " << day << " rounds took " << std::setprecision(5) << runTimer.getElapsedTime().asSeconds() << " seconds!" << std::endl;
system("pause");
if (false)
{
ContinueRunningMarketSimulations = false;
}
else
{
timer.restart();
mainView = window.getView();
mainView.setCenter(mainView.getCenter().x + 900, mainView.getCenter().y);
window.setView(mainView);
verts[0].position.x += 900;
verts[1].position.x += 900;
for (unsigned int cnt = 0; cnt < stocks.size(); cnt++)
{
stocks[cnt].priceLabel.setPosition(stocks[cnt].priceLabel.getPosition().x + 900, stocks[cnt].priceLabel.getPosition().y);
}
for (unsigned int cnt = 0;cnt < agents.size();cnt++)
{
agents[cnt]->SortPortfolioByID();
}
}
}
}
}
return 0;
}
/*
Core loop:
Stocks have init values + history
Each agent valuates each stock
Agents place buy or sell orders
Buy and sell orders executed in order they are added
Stock changes price based on what happened this 'round'
*/
/*
#include <iostream>
class A
{
public:
virtual const char* fetchClassName() { return "A"; }
};
class B : public A
{
public:
const char* fetchClassName() { return "B"; }
};
int main(void)
{
B obj_b;
A& obj_a = obj_b;
std::cout << obj_a.fetchClassName() << "\n";
system("pause");
}
*/
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{5FDAAABC-D682-4510-AF47-8209B6D759B5}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>EconSim</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>G:\EconSim\SFML\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>G:\EconSim\SFML\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-audio-d.lib;sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-network-d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>G:\EconSim\SFML\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>G:\EconSim\SFML\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-audio.lib;sfml-graphics.lib;sfml-window.lib;sfml-system.lib;sfml-network.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Agent.h" />
<ClInclude Include="Agent1-VeryPassive.h" />
<ClInclude Include="Agent2-SemiPassive.h" />
<ClInclude Include="Agent3-Agressive.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="Stock.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Agent.cpp" />
<ClCompile Include="Agent1-VeryPassive.cpp" />
<ClCompile Include="Agent2-SemiPassive.cpp" />
<ClCompile Include="Agent3-Agressive.cpp" />
<ClCompile Include="EconSim.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Stock.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\Base">
<UniqueIdentifier>{adc3b693-a8fc-46ef-8d65-37f94f2ae114}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Base">
<UniqueIdentifier>{f6e5729a-1234-49a2-af62-57ca94bacbec}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Agents">
<UniqueIdentifier>{d9eaf2c8-31d5-4f68-a18f-6e5d1c72da86}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Agents">
<UniqueIdentifier>{78fb9896-4dcd-42ba-a997-46584806365a}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="targetver.h">
<Filter>Header Files\Base</Filter>
</ClInclude>
<ClInclude Include="Agent.h">
<Filter>Header Files\Base</Filter>
</ClInclude>
<ClInclude Include="stdafx.h">
<Filter>Header Files\Base</Filter>
</ClInclude>
<ClInclude Include="Stock.h">
<Filter>Header Files\Base</Filter>
</ClInclude>
<ClInclude Include="Agent3-Agressive.h">
<Filter>Header Files\Agents</Filter>
</ClInclude>
<ClInclude Include="Agent1-VeryPassive.h">
<Filter>Header Files\Agents</Filter>
</ClInclude>
<ClInclude Include="Agent2-SemiPassive.h">
<Filter>Header Files\Agents</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Stock.cpp">
<Filter>Source Files\Base</Filter>
</ClCompile>
<ClCompile Include="Agent.cpp">
<Filter>Source Files\Base</Filter>
</ClCompile>
<ClCompile Include="EconSim.cpp">
<Filter>Source Files\Base</Filter>
</ClCompile>
<ClCompile Include="stdafx.cpp">
<Filter>Source Files\Base</Filter>
</ClCompile>
<ClCompile Include="Agent3-Agressive.cpp">
<Filter>Source Files\Agents</Filter>
</ClCompile>
<ClCompile Include="Agent1-VeryPassive.cpp">
<Filter>Source Files\Agents</Filter>
</ClCompile>
<ClCompile Include="Agent2-SemiPassive.cpp">
<Filter>Source Files\Agents</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ShowAllFiles>false</ShowAllFiles>
</PropertyGroup>
</Project>
\ No newline at end of file
#include "Stock.h"
#include "Stock.h"
#include "stdafx.h"
#include "Agent.h"
#include "Stock.h"
#include <random>
#include <time.h>
#include <iomanip>
#include <SFML\Graphics.hpp>
Stock::Stock()
{
}
void Stock::Start(int _id)
{
InvestmentMoment cur;
int rng = rand();
cur.marketVolume = 10000;
cur.price = ((double)(rng % 10000) / 10000) + 7;
history.push_back(cur);
id = _id;
marketCap = cur.marketVolume * 2;
std::vector<BuyOrder> nextBuys;
std::vector<SellOrder> nextSells;
buys.push_back(nextBuys);
sells.push_back(nextSells);
priceLabel.setFont(arial);
priceLabel.setCharacterSize(20);
priceLabel.setPosition(35.f, 10.f + _id * 35.f);
Info();
}
void Stock::Update()
{
InvestmentMoment cur = GetCurrentData();
bool growing = ((rand() % 2) != 0);
int rng = rand();
//Randomly change price
if (growing)
{
cur.price += ((double)(rng % 1000) / 1000) * (cur.marketVolume / marketCap);
}
else
{
cur.price -= ((double)(rng % 1000) / 1000) * (cur.marketVolume / marketCap);
}
history.push_back(cur);
sf::Vertex vert;
if (id == 0)
{
vert.color = sf::Color::Blue;
priceLabel.setColor(sf::Color::Blue);
}
else if (id == 1)
{
vert.color = sf::Color::Red;
priceLabel.setColor(sf::Color::Red);
}
else if (id == 2)
{
vert.color = sf::Color::Green;
priceLabel.setColor(sf::Color::Green);
}
else
{
vert.color = sf::Color::White;
priceLabel.setColor(sf::Color::White);
}
vert.position.x = (float)day;
vert.position.y = 600.f - (float)(cur.price * 5);
graph.push_back(vert);
priceLabel.setString("Price: $" + std::to_string(cur.price) + "\tMarket Volume: " + std::to_string(cur.marketVolume));
std::vector<BuyOrder> nextBuys;
std::vector<SellOrder> nextSells;
buys.push_back(nextBuys);
sells.push_back(nextSells);
}
void Stock::Info()
{
std::cout << "Stock " << id << " has a total market volume of " << std::setprecision(5) << GetCurrentData().marketVolume << " at a price of $" << std::setprecision(2) << GetCurrentData().price << std::endl;
}
void Stock::Summary()
{
}
void Stock::HandleBuysAndSells()
{
/*for (unsigned int cnt = 0; cnt < buys[buys.size() - 1].size(); cnt++)
{
for (unsigned int itr = 0; itr < buys[buys.size() - 1][cnt].placer->portfolio.size(); itr++)
{
//If the stock in the buyer's portfolio is this stock, increase how many he has
if (buys[buys.size() - 1][cnt].placer->portfolio[itr].stock == buys[buys.size() - 1][cnt].stock)
{
//buys[buys.size() - 1][cnt].placer->portfolio[itr].quantity += buys[buys.size() - 1][cnt].quantity;
buys[buys.size() - 1][cnt].placer->cash -= buys[buys.size() - 1][cnt].quantity * buys[buys.size() - 1][cnt].price;
}
}
}
for (unsigned int cnt = 0; cnt < sells[sells.size() - 1].size(); cnt++)
{
for (unsigned int itr = 0; itr < sells[sells.size() - 1][cnt].placer->portfolio.size(); itr++)
{
//If the stock in the buyer's portfolio is this stock, decrease how many he has
if (sells[sells.size() - 1][cnt].placer->portfolio[itr].stock->id == sells[sells.size() - 1][cnt].stock->id)
{
sells[sells.size() - 1][cnt].placer->portfolio[itr].quantity -= sells[sells.size() - 1][cnt].quantity;
if (sells[sells.size() - 1][cnt].placer->portfolio[itr].quantity < 0)
{
std::cout << "Agent " << buys[buys.size() - 1][cnt].placer->id << " sold more " << buys[buys.size() - 1][cnt].stock->id << " stocks than he owned!" << std::endl;
}
//sells[buys.size() - 1][cnt].placer->cash += sells[sells.size() - 1][cnt].quantity * sells[sells.size() - 1][cnt].price;
}
}
}*/
}
InvestmentMoment& Stock::GetCurrentData()
{
return history[history.size() - 1];
}
void Stock::Transfer()
{
double totalQuantityBought = 0;
//Populate total quantity transfered today
for (unsigned int cnt = 0; cnt < buys[buys.size() - 1].size(); cnt++)
{
totalQuantityBought += buys[buys.size() - 1][cnt].quantity;
}
//Nothing needed for selling, each can only sell as many as they have
for (unsigned int cnt = 0; cnt < sells[sells.size() - 1].size(); cnt++)
{
if (sells[sells.size() - 1][cnt].placer->TransferStock(*sells[sells.size() - 1][cnt].stock, sells[sells.size() - 1][cnt].quantity))
{
GetCurrentData().marketVolume -= sells[sells.size() - 1][cnt].quantity;
}
}
//Verify if number of stocks being bought outnumbered those on the market; if they do, give each buyer a proportional amount to what they requested
if (GetCurrentData().marketVolume > 0)
{
if (totalQuantityBought >= GetCurrentData().marketVolume)
{
for (unsigned int cnt = 0; cnt < buys[buys.size() - 1].size(); cnt++)
{
if (buys[buys.size() - 1][cnt].placer->TransferStock(*buys[buys.size() - 1][cnt].stock, (buys[buys.size() - 1][cnt].quantity) / (totalQuantityBought)))
{
GetCurrentData().marketVolume -= (buys[buys.size() - 1][cnt].quantity) / (totalQuantityBought);
if (GetCurrentData().marketVolume < 0)
{
std::cout << "BLAH!" << std::endl;
GetCurrentData().marketVolume = 0;
}
}
}
}
else
{
for (unsigned int cnt = 0; cnt < buys[buys.size() - 1].size(); cnt++)
{
if (buys[buys.size() - 1][cnt].placer->TransferStock(*buys[buys.size() - 1][cnt].stock, buys[buys.size() - 1][cnt].quantity))
{
GetCurrentData().marketVolume -= buys[buys.size() - 1][cnt].quantity;
}
}
}
}
}
#pragma once
#include "stdafx.h"
#include "Agent.h"
class Stock;
class Agent;
struct InvestmentMoment
{
double price;
double marketVolume;
};
struct BuyOrder
{
Agent* placer;
Stock* stock;
double quantity;
double price;
};
struct SellOrder
{
Agent* placer;
Stock* stock;
double quantity;
double price;
};
class Stock
{
public:
int id;
double marketCap;
std::vector<std::vector<BuyOrder>> buys;
std::vector<std::vector<SellOrder>> sells;
std::vector<InvestmentMoment> history;
std::vector<sf::Vertex> graph;
sf::Text priceLabel;
Stock();
virtual void Start(int _id);
virtual void Update();
virtual void Info();
virtual void Summary();
virtual void HandleBuysAndSells();
virtual InvestmentMoment& GetCurrentData();
virtual void Transfer();
};
\ No newline at end of file
File added
// stdafx.cpp : source file that includes just the standard includes
// EconSim.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <random>
#include <iostream>
#include <SFML\Graphics.hpp>
#include "Agent.h"
#include "Stock.h"
#include "Agent1-VeryPassive.h"
#include "Agent2-SemiPassive.h"
#include "Agent3-Agressive.h"
extern std::vector<Stock> stocks;
extern unsigned int day;
extern sf::Font arial;
// TODO: reference additional headers your program requires here
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment