Joel Grunbaum
2022-01-20 441abf1e6a15a39f1ef7bf5e4ee2a4a43bff327b
protocol.cpp
@@ -175,57 +175,61 @@
                  json::DeletedMessage* message)
{
   if (message->side == book::Buy) {
        for (auto i = bs[message->product].bidSide.begin(); i != bs[message->product].bidSide.end(); i++) {
                    if (i->id == message->id) {
                        bs[message->product].bidSide.erase(i);
                        break;
                    }
                }
      for (auto i = bs[message->product].bidSide.begin();
           i != bs[message->product].bidSide.end(); i++) {
         if (i->id == message->id) {
            bs[message->product].bidSide.erase(i);
            break;
         }
      }
   } else {
        for (auto i = bs[message->product].askSide.begin(); i != bs[message->product].askSide.end(); i++) {
                    if (i->id == message->id) {
                        bs[message->product].askSide.erase(i);
                        break;
                    }
                }
      for (auto i = bs[message->product].askSide.begin();
           i != bs[message->product].askSide.end(); i++) {
         if (i->id == message->id) {
            bs[message->product].askSide.erase(i);
            break;
         }
      }
   }
}
void tradeOrder(std::unordered_map<std::string, book::Book>& bs,
                json::TradeMessage* message)
{
    if (message->tradeType == json::BUY_AGGRESSOR) {
        if (message->passiveOrderRemaining > 0) {
            for (auto& i : bs[message->product].askSide) {
                if (i.id == message->passiveOrder) {
                    i.volume = message->passiveOrderRemaining;
                    break;
                }
            }
        } else {
            for (auto i = bs[message->product].askSide.begin(); i != bs[message->product].askSide.end(); i++) {
                if (i->id == message->passiveOrder) {
                    bs[message->product].askSide.erase(i);
                    break;
                }
            }
        }
    } else if (message->tradeType == json::SELL_AGGRESSOR) {
        if (message->passiveOrderRemaining > 0) {
            for (auto& i : bs[message->product].bidSide) {
                if (i.id == message->passiveOrder) {
                    i.volume = message->passiveOrderRemaining;
                    break;
                }
            }
        } else {
            for (auto i = bs[message->product].bidSide.begin(); i != bs[message->product].bidSide.end(); i++) {
                if (i->id == message->passiveOrder) {
                    bs[message->product].bidSide.erase(i);
                    break;
                }
            }
        }
    }
   if (message->tradeType == json::BUY_AGGRESSOR) {
      if (message->passiveOrderRemaining > 0) {
         for (auto& i : bs[message->product].askSide) {
            if (i.id == message->passiveOrder) {
               i.volume = message->passiveOrderRemaining;
               break;
            }
         }
      } else {
         for (auto i = bs[message->product].askSide.begin();
              i != bs[message->product].askSide.end(); i++) {
            if (i->id == message->passiveOrder) {
               bs[message->product].askSide.erase(i);
               break;
            }
         }
      }
   } else if (message->tradeType == json::SELL_AGGRESSOR) {
      if (message->passiveOrderRemaining > 0) {
         for (auto& i : bs[message->product].bidSide) {
            if (i.id == message->passiveOrder) {
               i.volume = message->passiveOrderRemaining;
               break;
            }
         }
      } else {
         for (auto i = bs[message->product].bidSide.begin();
              i != bs[message->product].bidSide.end(); i++) {
            if (i->id == message->passiveOrder) {
               bs[message->product].bidSide.erase(i);
               break;
            }
         }
      }
   }
}
void broker(std::unordered_map<std::string, book::Book>& bs,
@@ -238,7 +242,10 @@
   httplib::MultipartFormDataItems a = {{"message", message, "", ""},
                                        {"username", USER, "", ""},
                                        {"password", PASS, "", ""}};
   auto res = cli.Post("/execution", a);
   auto res = cli.Post("/execution",
                       "message=" + message + "&username=" + USER +
                           "&password=" + PASS,
                       "application/x-www-form-urlencoded");
   std::string b = res->body;
   std::queue<json::Message*> c = json::parse(b);
   return c.front();