diff --git a/.gitignore b/.gitignore index d163863..40b49e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -build/ \ No newline at end of file +build/ +.idea/ +.__DS_STORE__ diff --git a/CMakeLists.txt b/CMakeLists.txt index 32abacf..8c2961b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,55 @@ -#Declare the project and it's langugages cmake_minimum_required(VERSION 3.21) project(learnsomething LANGUAGES CXX) -#Make C++ 20 required. -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) -#Fetch GTest include(FetchContent) FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.11.0 + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip ) +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) #windows tings FetchContent_MakeAvailable(googletest) +enable_testing() + +FetchContent_Declare( + quill + GIT_REPOSITORY https://github.com/odygrd/quill.git + GIT_TAG master +) + +FetchContent_MakeAvailable(quill) + +add_executable( + TradeRiskTracker + ${CMAKE_SOURCE_DIR}/src/main.cpp + ${CMAKE_SOURCE_DIR}/src/trade.cpp + ${CMAKE_SOURCE_DIR}/src/risktracker.cpp +) +target_include_directories( + TradeRiskTracker PRIVATE + ${CMAKE_SOURCE_DIR}/src +) + +target_link_libraries( + TradeRiskTracker + GTest::gtest_main + quill +) + +add_executable( + TradeRiskTrackerTests + ${CMAKE_SOURCE_DIR}/tst/test_trade.cpp + ${CMAKE_SOURCE_DIR}/tst/test_traderisktracker.cpp + ${CMAKE_SOURCE_DIR}/src/trade.cpp + ${CMAKE_SOURCE_DIR}/src/risktracker.cpp +) +target_include_directories( + TradeRiskTrackerTests PRIVATE + ${CMAKE_SOURCE_DIR}/src +) +target_link_libraries( + TradeRiskTrackerTests + GTest::gtest_main +) diff --git a/README.md b/README.md index 277e7c1..2200342 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,30 @@ -# unnamed learning project +# Part 0: Debugging, Logging, and PRs. +## Scenario +It is your first day as a SWE at a trading firm that specializes in trading Oleos, a fictional currency. Your team's intern, during their final week, created this risk tracking utility to help traders keep track of their risk exposure. Unfortunately, the intern has already left for their next internship and you are left to debug and fix the code. The traders tell you that the script is not working as expected and they are losing money because of it. You have been tasked with fixing the script and ensuring that it is working as expected. Consider the following tips: +- Read the code and understand what it is doing - all of the unit tests will pass. +- Use the logging library to help you understand what is happening in the code. + +## Instructions +- [ ] Add yourself to the `progress.md` file on the main branch. +- [ ] Fork this repo for yourself +- [ ] Clone the repo locally (Ensure you are on Branch 0!) +- [ ] Navigate to the repository root and use `mkdir build && cd build` to create a build directory and navigate to it +- [ ] Use `task build` to build the project, `./TradeRiskTracker` to run the executable, and `task test` to run the tests +- [ ] Identify the bug in the script and fix it +- [ ] Update unit tests to cover the bug +- [ ] Create a PR answering the questions below and send it to your project mentor + + +## Questions +Please include the following when you are writing your PR: +General things: +1. What is the purpose of this PR? +2. What changes did you make? Why? +3. What bugs did you find while testing? + +This PR Specific: +1. What was the bug you found? +2. How did you address it? +3. What did you struggle with? +4. Is there anything you would change about this step? diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..88c3b03 --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,15 @@ +version: '3' + +tasks: + build: + dir: './build' + cmds: + - cmake .. + - cmake --build . + + test: + dir: './build' + cmds: + - cmake .. + - cmake --build . + - ./TradeRiskTrackerTests diff --git a/src/init.cpp b/src/init.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..9787ec1 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +#include "trade.h" +#include "risktracker.h" + +int main() { + //logging setup start + quill::start(); + quill::Logger *logger = quill::create_logger("RiskTrackingLogger"); + logger->set_log_level(quill::LogLevel::TraceL2); + // logging setup end + + std::vector trades; + trades.push_back(Trade(7, false, 1.4)); + RiskTracker riskTracker(0.0, trades); + QUILL_LOG_INFO(logger, "Current risk held is {}.", riskTracker.getRisk()); + riskTracker.addTrade(Trade(7, false, 1.6)); + riskTracker.updateRisk(); + QUILL_LOG_INFO(logger, "Current risk held is {}.", riskTracker.getRisk()); + riskTracker.addTrade(Trade(44, true, 2.3)); + riskTracker.updateRisk(); + QUILL_LOG_INFO(logger, "Current risk held is {}.", riskTracker.getRisk()); + riskTracker.updateRisk(); + QUILL_LOG_INFO(logger, "Current risk held is {}.", riskTracker.getRisk()); + return 0; +} diff --git a/src/risktracker.cpp b/src/risktracker.cpp new file mode 100644 index 0000000..94f0b4c --- /dev/null +++ b/src/risktracker.cpp @@ -0,0 +1,31 @@ +// +// Created by Ethan on 8/16/2023. +// + +#include "risktracker.h" + +RiskTracker::RiskTracker(float x, std::vector trades) : totalRisk(x), pendingTrades(trades) {}; + +int RiskTracker::updateRisk() { + float runningSum = 0; + for (const auto &x: this->pendingTrades) { + if (x.side) { + runningSum += (x.price * x.quantity); + } else { + runningSum -= (x.price * x.quantity); + } + } + this->totalRisk += runningSum; + this->pendingTrades.clear(); + return 0; +} + +int RiskTracker::addTrade(Trade trade) { + this->pendingTrades.push_back(trade); + return 0; +} + +float RiskTracker::getRisk() { + return this->totalRisk; +} + diff --git a/src/risktracker.h b/src/risktracker.h new file mode 100644 index 0000000..f459059 --- /dev/null +++ b/src/risktracker.h @@ -0,0 +1,24 @@ +// +// Created by Ethan on 8/16/2023. +// +#include +#include "trade.h" + +#ifndef LEARNSOMETHING_RISKTRACKER_H +#define LEARNSOMETHING_RISKTRACKER_H + +class RiskTracker { +public: + float totalRisk = 0; + std::vector pendingTrades; + + RiskTracker(float x, std::vector trades); + + int updateRisk(); + + int addTrade(Trade trade); + + float getRisk(); +}; + +#endif //LEARNSOMETHING_RISKTRACKER_H diff --git a/src/trade.cpp b/src/trade.cpp new file mode 100644 index 0000000..094b0a0 --- /dev/null +++ b/src/trade.cpp @@ -0,0 +1,4 @@ +#include "trade.h" + +//Initialize with the initializer list. +Trade::Trade(int q, bool buy, float p) : quantity(q), side(buy), price(p) {}; diff --git a/src/trade.h b/src/trade.h new file mode 100644 index 0000000..c986357 --- /dev/null +++ b/src/trade.h @@ -0,0 +1,12 @@ +#ifndef TRADE_H +#define TRADE_H + +class Trade { +public: + int quantity; + bool side; + float price; + Trade(int q, bool buy, float p); +}; + +#endif // TRADE_H diff --git a/tst/temp.cpp b/tst/temp.cpp deleted file mode 100644 index 9c595a6..0000000 --- a/tst/temp.cpp +++ /dev/null @@ -1 +0,0 @@ -temp diff --git a/tst/test_trade.cpp b/tst/test_trade.cpp new file mode 100644 index 0000000..7b876ac --- /dev/null +++ b/tst/test_trade.cpp @@ -0,0 +1,9 @@ +#include "trade.h" +#include + +TEST(TradeRiskTrackerTest, TradeInit) { + Trade testTrade(7, true, 1.2); + EXPECT_EQ(testTrade.quantity, 7); + EXPECT_NEAR(testTrade.price, 1.2, 1e-6); //fp + EXPECT_EQ(testTrade.side, true); +} \ No newline at end of file diff --git a/tst/test_traderisktracker.cpp b/tst/test_traderisktracker.cpp new file mode 100644 index 0000000..4c07479 --- /dev/null +++ b/tst/test_traderisktracker.cpp @@ -0,0 +1,40 @@ +#include "trade.h" +#include "risktracker.h" +#include +#include + +TEST(TradeRiskTrackerTest, TrackerInit) { + std::vector trackedTrades; + Trade testTrade1(7, true, 1.2); + Trade testTrade2(7, false, 1.2); + trackedTrades.push_back(testTrade1); + trackedTrades.push_back(testTrade2); + RiskTracker riskTracker(0, trackedTrades); + EXPECT_NEAR(riskTracker.getRisk(), 0, 1e-4); + riskTracker.updateRisk(); + EXPECT_NEAR(riskTracker.getRisk(), 0, 1e-4); + Trade testTrade3(14, true, 1.55); + riskTracker.addTrade(testTrade3); + riskTracker.updateRisk(); + EXPECT_NEAR(riskTracker.getRisk(), 21.7, 1e-4); +} + +TEST(TradeRiskTrackerTest, TrackerZeroTest) { + std::vector trackedTrades; + RiskTracker riskTracker(0, trackedTrades); + EXPECT_NEAR(riskTracker.getRisk(), 0, 1e-4); + riskTracker.addTrade(Trade(44, true, 1.6)); + riskTracker.addTrade(Trade(44, false, 1.6)); + riskTracker.updateRisk(); + EXPECT_NEAR(riskTracker.getRisk(), 0, 1e-4); +} + +TEST(TradeRiskTrackerTest, UpdateRiskTrackerTest) { + std::vector trackedTrades; + RiskTracker riskTracker(0, trackedTrades); + riskTracker.addTrade(Trade(44, true, 1.6)); + riskTracker.addTrade(Trade(44, false, 1.6)); + riskTracker.updateRisk(); + EXPECT_TRUE(riskTracker.pendingTrades.empty()); + +} \ No newline at end of file