Skip to content

Commit 83dce36

Browse files
author
thinkdevcode
committed
initial commit
0 parents  commit 83dce36

File tree

9 files changed

+354
-0
lines changed

9 files changed

+354
-0
lines changed

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#OS junk files
2+
[Tt]humbs.db
3+
*.DS_Store
4+
5+
#Visual Studio files
6+
*.[Oo]bj
7+
*.user
8+
*.aps
9+
*.pch
10+
*.vspscc
11+
*.vssscc
12+
*_i.c
13+
*_p.c
14+
*.ncb
15+
*.suo
16+
*.tlb
17+
*.tlh
18+
*.bak
19+
*.[Cc]ache
20+
*.ilk
21+
*.log
22+
*.lib
23+
*.sbr
24+
*.sdf
25+
*.opensdf
26+
*.unsuccessfulbuild
27+
ipch/
28+
obj/
29+
[Bb]in
30+
[Dd]ebug*/
31+
[Rr]elease*/
32+
Ankh.NoLoad
33+
34+
#MonoDevelop
35+
*.pidb
36+
*.userprefs
37+
38+
#Tooling
39+
_ReSharper*/
40+
*.resharper
41+
[Tt]est[Rr]esult*
42+
*.sass-cache
43+
44+
#Project files
45+
[Bb]uild/
46+
47+
#Subversion files
48+
.svn
49+
50+
# Office Temp Files
51+
~$*
52+
53+
#NuGet
54+
packages/
55+
56+
#ncrunch
57+
*ncrunch*
58+
*crunch*.local.xml
59+
60+
# visual studio database projects
61+
*.dbmdl
62+
63+
#Test files
64+
*.testsettings

Exchange.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exchange", "Exchange\Exchange.csproj", "{6D9C405F-3E1C-4D85-9373-2ABE44343FD7}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{6D9C405F-3E1C-4D85-9373-2ABE44343FD7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{6D9C405F-3E1C-4D85-9373-2ABE44343FD7}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{6D9C405F-3E1C-4D85-9373-2ABE44343FD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{6D9C405F-3E1C-4D85-9373-2ABE44343FD7}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

Exchange/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

Exchange/ByLimit.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Exchange {
2+
using System.Collections.Generic;
3+
4+
public class ByLimit : IComparer<Order> {
5+
public int Compare(Order x, Order y) {
6+
if (x.id == y.id)
7+
return 0;
8+
if (x.limit > y.limit)
9+
return 1;
10+
else if (x.limit == y.limit) {
11+
if (x.ticks > y.ticks)
12+
return 1;
13+
else
14+
return -1;
15+
}
16+
else
17+
return -1;
18+
}
19+
}
20+
}

Exchange/Exchange.csproj

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{6D9C405F-3E1C-4D85-9373-2ABE44343FD7}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Exchange</RootNamespace>
11+
<AssemblyName>Exchange</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="ByLimit.cs" />
45+
<Compile Include="Order.cs" />
46+
<Compile Include="OrderBook.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

Exchange/Order.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace Exchange {
2+
using System;
3+
4+
public class Order : IComparable {
5+
private static uint _count = 0;
6+
public readonly uint id;
7+
public readonly Type type;
8+
public uint amount;
9+
public readonly decimal limit;
10+
public readonly long ticks;
11+
public readonly DateTime created;
12+
13+
public Order(Type type, uint amount, decimal limit, long ticks) {
14+
id = _count++;
15+
this.type = type;
16+
this.amount = amount;
17+
this.limit = limit;
18+
this.ticks = ticks;
19+
this.created = DateTime.Now;
20+
}
21+
22+
public bool Match(Order order) {
23+
if (type == Type.Buy)
24+
return limit >= order.limit;
25+
else
26+
return limit <= order.limit;
27+
}
28+
29+
public int CompareTo(object obj) {
30+
if (obj == null)
31+
return 1;
32+
Order ordobj = obj as Order;
33+
if (ordobj.id == this.id)
34+
return 0;
35+
else
36+
return 1;
37+
}
38+
39+
public enum Type : byte {
40+
Buy = 0,
41+
Sell = 1
42+
}
43+
}
44+
}

Exchange/OrderBook.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace Exchange {
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
6+
public class OrderBook {
7+
private SortedSet<Order> buys;
8+
private SortedSet<Order> sells;
9+
private Stopwatch watch;
10+
private static int _trades = 0;
11+
public int trades { get { return _trades; } }
12+
13+
public OrderBook() {
14+
buys = new SortedSet<Order>(new ByLimit());
15+
sells = new SortedSet<Order>(new ByLimit());
16+
watch = new Stopwatch();
17+
watch.Start();
18+
}
19+
20+
public void SubmitOrder(Order.Type type, uint amount, decimal limit) {
21+
if (type == Order.Type.Buy)
22+
buys.Add(new Order(type, amount, limit, watch.ElapsedTicks));
23+
else
24+
sells.Add(new Order(type, amount, limit, watch.ElapsedTicks));
25+
while (buys.Count > 0 && sells.Count > 0 && sells.Min.Match(buys.Max))
26+
ExecuteTrade(buys.Max, sells.Min);
27+
}
28+
29+
public void ExecuteTrade(Order buy, Order sell) {
30+
_trades++;
31+
if (buy.amount == sell.amount) {
32+
sells.Remove(sell);
33+
buys.Remove(buy);
34+
}
35+
else if (buy.amount > sell.amount) {
36+
buy.amount -= sell.amount;
37+
sells.Remove(sell);
38+
}
39+
else if (buy.amount < sell.amount) {
40+
sell.amount -= buy.amount;
41+
buys.Remove(buy);
42+
}
43+
}
44+
45+
public void PrintOrderBooks() {
46+
Console.WriteLine(String.Format("|{0,5}|{1,6}|{2,6}|{3,6}|{4,20}|", "type", "id", "limit", "amt", "ticks"));
47+
foreach (var i in buys) {
48+
Console.WriteLine(String.Format("|{0,5}|{1,6}|{2,6}|{3,6}|{4,20}|", "BUY", i.id, i.limit, i.amount, i.ticks));
49+
}
50+
foreach (var i in sells) {
51+
Console.WriteLine(String.Format("|{0,5}|{1,6}|{2,6}|{3,6}|{4,20}|", "SELL", i.id, i.limit, i.amount, i.ticks));
52+
}
53+
}
54+
}
55+
}

Exchange/Program.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Exchange {
2+
using System;
3+
4+
class Program {
5+
static void Main(string[] args) {
6+
OrderBook book = new OrderBook();
7+
book.SubmitOrder(Order.Type.Buy, 10, 11.12m);
8+
book.SubmitOrder(Order.Type.Buy, 10, 11.00m);
9+
book.SubmitOrder(Order.Type.Buy, 20, 10.50m);
10+
book.SubmitOrder(Order.Type.Buy, 1, 11.20m);
11+
book.SubmitOrder(Order.Type.Buy, 7, 11.15m);
12+
book.SubmitOrder(Order.Type.Buy, 100, 10.05m);
13+
book.SubmitOrder(Order.Type.Buy, 2, 11.10m);
14+
book.SubmitOrder(Order.Type.Sell, 10, 11.24m);
15+
book.SubmitOrder(Order.Type.Sell, 10, 11.30m);
16+
book.SubmitOrder(Order.Type.Sell, 20, 11.50m);
17+
book.SubmitOrder(Order.Type.Sell, 1, 11.74m);
18+
book.SubmitOrder(Order.Type.Sell, 7, 11.60m);
19+
book.SubmitOrder(Order.Type.Sell, 100, 12.05m);
20+
book.SubmitOrder(Order.Type.Sell, 3, 11.10m);
21+
22+
bool exit = false;
23+
Random rnd = new Random();
24+
DateTime now = DateTime.Now.AddSeconds(10);
25+
26+
while (!exit) {
27+
int rndl = rnd.Next(1120, 1125);
28+
int rnda = rnd.Next(1, 5);
29+
byte rndt = rnd.Next(0, 100) < 50 ? (byte)0 : (byte)1;
30+
31+
Order.Type t = (Order.Type)rndt;
32+
uint a = (uint)rnda;
33+
decimal l = (decimal)rndl / 100m;
34+
35+
book.SubmitOrder(t, a, l);
36+
37+
//Thread.Sleep(1);
38+
//Console.Clear();
39+
//book.PrintOrderBooks();
40+
41+
if (DateTime.Now == now)
42+
exit = true;
43+
}
44+
Console.WriteLine(book.trades);
45+
Console.ReadLine();
46+
}
47+
}
48+
}

Exchange/Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Exchange")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("Exchange")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("f16e2b95-3b7e-4875-af85-7067c4e7cf8b")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)