-
Notifications
You must be signed in to change notification settings - Fork 2
/
TradingInterface.cs
65 lines (54 loc) · 2.21 KB
/
TradingInterface.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using static TradeBot.Instrument;
namespace TradeBot
{
public class TradingInterface
{
public double Balance { get; set; }
public TradingInterface(double initialBalance)
{
Balance = initialBalance;
}
public void ClosePosition(Instrument instrument, double price)
{
if (price < 0) throw new ArgumentOutOfRangeException("price should be positive");
if (instrument.State == States.Empty) throw new InvalidOperationException("can't sell because state is not bought");
var diff = price * instrument.DealLots;
if (instrument.State == States.Bought)
Balance += diff;
else
Balance -= diff;
instrument.DealLots = 0;
instrument.State = States.Empty;
}
public void OpenPosition(Instrument instrument, double price, int lots, bool isShort)
{
if (instrument.State != States.Empty) throw new InvalidOperationException("can't buy because state is bought already (sell first)");
int maxLots = (int)(Balance / price);
if (lots < 0 || lots > maxLots) throw new ArgumentOutOfRangeException("lots should be > 0 and <= maxLots");
if (price < 0) throw new ArgumentOutOfRangeException("price should be positive");
instrument.DealPrice = price;
instrument.DealLots = lots;
instrument.State = isShort ? States.Sold : States.Bought;
var diff = instrument.DealPrice * instrument.DealLots;
if (instrument.State == States.Bought)
Balance -= diff;
else
Balance += diff;
}
public void OpenPosition(Instrument instrument, double price, bool isShort)
{
if (price < 0) throw new ArgumentOutOfRangeException("price should be positive");
int maxLots = (int)(Balance / price);
OpenPosition(instrument, price, maxLots, isShort);
}
public void Reset(double newBalance)
{
Balance = newBalance;
}
}
}