ATS
An algorithmic trading system completely written in C++
PositionManager.h
Go to the documentation of this file.
1 //
2 // Created by Anouar Achghaf on 15/02/2023.
3 //
12 #ifndef ATS_POSITIONMANAGER_H
13 #define ATS_POSITIONMANAGER_H
14 #include <thread>
15 #include <vector>
16 #include <unordered_map>
17 #include <mutex>
18 #include "MarketData.h"
19 
20 namespace ats {
21 
25  struct Position{
26  double quantity;
27  double price;
28 
32  Position() : quantity(0), price(0) {}
33 
39  Position(double q, double p) : quantity(q), price(p) {}
40 
45  double total() {
46  return quantity * price;
47  }
48  };
49 
54  private:
55  MarketData &mData;
56  std::thread mPositionManagerThread;
57  double mPnL;
58  std::unordered_map<std::string, Position> mOpenPositions;
59  bool mRunning;
60  std::mutex mPositionMutex;
61  public:
62 
67 
72  PositionManager(MarketData &marketData);
73 
78 
82  void start();
83 
87  void run();
88 
92  void stop();
93 
98  bool isRunning();
99 
104  double getPnL();
105 
111  double getPosition(std::string symbol);
112 
118  void updatePosition(std::string symbol, double quantity);
119 
120  private:
124  void updatePnL();
125  };
126 
127 
128 } // ats
129 
130 #endif //ATS_POSITIONMANAGER_H
Contains the declaration of the MarketData class, which handles the streaming of market data for the ...
Handles the streaming of market data for the trading system.
Definition: MarketData.h:54
Manages the positions and the PnL of a trading system.
Definition: PositionManager.h:53
void run()
Runs the PositionManager loop.
Definition: PositionManager.cpp:24
void start()
Starts the PositionManager thread.
Definition: PositionManager.cpp:19
~PositionManager()
Destructor that stops the PositionManager if it is running.
Definition: PositionManager.cpp:15
void updatePosition(std::string symbol, double quantity)
Updates the position of the given symbol.
Definition: PositionManager.cpp:49
PositionManager()
Default constructor that initializes the market data to a default instance.
double getPnL()
Returns the current PnL of the trading system.
Definition: PositionManager.cpp:39
void stop()
Stops the PositionManager thread.
Definition: PositionManager.cpp:29
double getPosition(std::string symbol)
Returns the current position of the given symbol (quantity held).
Definition: PositionManager.cpp:43
bool isRunning()
Returns whether the PositionManager is running.
Definition: PositionManager.cpp:35
Namespace for the algorithmic trading system.
Definition: BinanceExchangeManager.cpp:8
Represents a position with its quantity and price.
Definition: PositionManager.h:25
double total()
Calculates the total value of the position.
Definition: PositionManager.h:45
Position()
Default constructor that initializes the quantity and price to 0.
Definition: PositionManager.h:32
double quantity
Quantity of the position.
Definition: PositionManager.h:26
double price
Price of the position.
Definition: PositionManager.h:27
Position(double q, double p)
Constructor that initializes the quantity and price to the given values.
Definition: PositionManager.h:39