Custom-made: a story about how a line of code turned into kilotons of coal



Brad's phone burst into the familiar trill of an office call.

- Yes? He barked, picking up the phone. - What do you want?

By the standards of Brad, such a way of communicating over the phone was, in fact, considered even polite. As the Chief Trader of ecxecor, one of the largest energy trading companies in the world, Brad was not going to please anyone, and he considered any manifestation of feelings, except arrogant mockery, as a sign of weakness.

“Uh,” said the obviously nervous secretary, “here you are ... uh ... delivery.” They ...

”“ Pf, ”Brad snorted, breaking her off at a glance. - Well, sign for her! Or is it so hard? Can’t you handle it yourself?

“You see,” the secretary said, “they ask me to explain how to moor.” And we also need to pay a mooring fee. They say that you are in the know. I do not understand anything.

“Okay,” Brad grumbled. - It seems, and the truth, everything here you need to do yourself.

He threw the phone and left his corner office. Despite the fact that Æxecor's residence was located in an area adjacent to the old marina, their office was considered one of the most luxurious in the city. At one end of the spacious building, which used to be a warehouse, bosses' offices were located, at the other — Brad was going there and headed — there was a reception room, whose windows overlooked the company’s personal pier near the river itself.

- Well, here I am! He announced angrily, barely crossing the threshold of the hall. - Come on, lay it out. What should I ...

Brad stopped short before finishing the sentence. His gaze immediately shifted to the spectacle on the pier near the Æxecor office, which opened through high, floor-to-ceiling windows. There was a monstrous-sized barge — no, an entire armada of securely fastened barges filled to the brim with heaps of coal. All this armada intended to moor at the pier.

“What the fuck ...

” “And you must be Brad,” came a peppy voice. Brad turned his eyes to the shabby-looking peasant in some kind of working clothes, who was sitting on one of the chairs in the waiting room. “Well, for starters, how do we moor?” I have two stoppers, but you can’t catch it. And do you have anything ready for unloading?

For once, Brad was speechless. He had no idea who this person was, and almost did not understand a word from what he was saying. Moreover, this community, gradually approaching the building.

“Uh ...” he muttered, “hey ... did you bring ... coal?” And for us?

- Well yes! Twenty eight thousand tons of good old black gold! - Worker mockingly wrinkled his forehead and added: - Well, unless, of course, the address was not mistaken, haha. Is this Æxecor? Berth number fifty three? Are you Brad, the same guy who ordered it?

And then Brad's hand met in a split second with Brad's face. He realized that something had gone very wrong according to plan, and instead of selling twenty-eight thousand tons of coal virtually, he somehow got them in his hands

A short course in commodity futures trading


If you have ever watched the 1983 classic " Swap " movie with Eddie Murphy and Dan Ackroyd in the lead roles, you can already imagine how the raw materials are sold. In its most general terms, commodities - for example, gold, wool, soybeans - are sold by manufacturers and, ultimately, reach customers. But Billy Ray Valentine made his money and busted the Duke brothers by not just selling frozen concentrated orange juice from a tray. He managed to break into the market due to the fact that he sold and bought up futures contracts. In fact, this is the only way commodity trading is carried out.

Futures contracts are a fairly transparent thing: you agree to purchase goods in quantities of X at a price of Y per unit at a certain point in the future. Of course, it may seem strange that a person decides to buy twenty tons of brisket for $ 34,420 in April (even if he loves brisket very much), but the bottom line is that he intends to sell this delivery long before the beginning of April and for an amount significantly exceeding 34 420 $. Almost any product that you can imagine is sold and bought in this way even before the start of production. The point of such a trading scheme is that the risks (and benefits) caused by fluctuations in commodity prices are no longer given to producers (farmers, miners, etc.), but to traders.

Of course, commodity traders are not at all interested in staying with kilotons of brisket in their hands. Therefore, a whole chain of intermediaries — brokers, stock exchanges, clearing houses — that work in the sweat of their faces, is included in the process, so that the person who says: “In May I will buy three hundred tons of brisket for $ 518,000”, I can be sure that I will not buy it in May three hundred tons of brisket for $ 518,000 in the literal sense of these words.

Brokers, for example, organize circular transactions, suggesting that every futures contract purchased will be compensated by selling the corresponding contract to a person who is really interested in the product. The automated trading systems used by exchanges have built-in codes of practice to catch obvious errors (for example, the delivery of large quantities of goods to the territory of a commercial enterprise). And finally, clearing houses once again verify transactions to make sure that nothing is sent in error.

Based on the foregoing, it is almost impossible for a trader to be the actual owner of the goods that he buys. Nearly.

Like clockwork


Æxecor only traded coal on one exchange (WTFSE), and coal transactions did not take place very often. Accordingly, when WTFSE introduced a new API that turned to the user and was based on web services, the internal trading system was unable to interact with it. A couple of coal deals loomed on the horizon, so the company had a small problem looming.

Fortunately, there were a couple of self-taught programmers in Æxecor and they managed to put together a solution that got along well with the new WTFSE API. In essence, the programmer added some XML code to the trading requests. In particular, there was such a fragment:

<AdditionalProperties>
   <PhysicallyDeliver>
      <value>False</value>
   </PhysicallyDeliver>
</AdditionalProperties>

Find nothing strange in this XML code? If you answered: "The value should not be False, but 0", you can pat yourself on the shoulder. As it turned out, the WTFSE system perceived only 1 and 0 to denote True and False, respectively; any other value was simply automatically equated to one. Oops!

Of course, in normal circumstances, a big disaster would not have come out of this. To make sure that everything is accurate in the transaction, WTFSE (and any other exchange) sends a transaction confirmation to the client, where all the source information is written in XML. Thus, both parties have access to data. On the Æxecor side, everything looked completely safe, primarily due to the following lines in the code:


bool physicallyDeliver = 
    (getNodeVal("PhysicallyDeliver").toLower() == "true");

Well done, the developer did verification ... only a line can give much more values ​​than true or false. Let's say 1 or 0. Oops.

But even the fact that the wrong transaction by mistake passed verification, in theory, is not yet a disaster, because the clearing house would have noticed that something was fundamentally wrong with it. You can’t just call the FedEx delivery service and ask them to take several thousand tons of raw materials to some office building in the business center. The list of points where you can deliver raw materials is very limited - usually these are warehouses near depots or ports. However, the Æxecor residence was located near the fifty-three pier in the coastal area with storage facilities, which has recently undergone renovation - at first glance, it is a reasonable place to deliver a whole pile of coal, especially from the point of view of the machine. Oops

Fortunately, the commodity futures trading market does not rely entirely on software. On both sides of the transaction (and also in a pair of intermediate points), the data passes through the employees of the operational departments who check to see if the trader has done any stupid things: for example, accidentally ticked the box “physical delivery”, entered the group transaction, which immediately will give a loss, and so on.

Since so many people are viewing transactions, it would be logical to assume that at least someone noticed that the trading giant Æxecor asks physically to deliver them a load of coal for one and a half million dollars. Yes, someone probably noticed, but Brad was engaged in the deal, which means it was impossible to conceive that there was some kind of misunderstanding.

As the Chief Trader of xecor, Brad conveyed to everyone with utmost clarity: no one, “even His Holiness the Pope of Rome” dares to question his actions. In the end, Brad has to make the most difficult bidding decisions that no one else can comprehend. Sometimes he buys expensive, and sells cheaply. Sometimes it holds the goods, although the price falls. Sometimes he refuses to sell it at all regardless of the price. Brad’s paths are mysterious, and if he says: “Do it!”, Then it’s better to do it.

Early christmas


“Just don’t tell me that it’s not your coal,” sensing that the matter was unclean, the worker took a defensive position. - Do you need it, do not need it - understand it yourself. But your coal, Mr. Brad.

Brad would be glad to object, but he himself understood: his coal. Even worse, he over and over again confidently confirmed this to the Æxecor operations department. He simply assumed that it was typical for him that these stupid paper-shifters could not really read. He mentally went back to his last conversation with a coal purchase department employee (“I told you to get a fucking deal, what word do you not understand?”) And lost in thought about what to do with fifty-six million pounds of coal in its physical embodiment.

Try to imagine for a moment how you yourself would get away with a pile of coal worth one and a half million dollars. Even Craigslist features still have limits.

As it turned out, selling real coal was even more difficult than Brad could have imagined. The commodity market, in fact, trades with futures alone - everyone who needs twenty-eight thousand tons of coal at the moment has already bought it in advance. Anyway, who will buy coal from some obscure type named Brad? In the end, spending sky-high sums on berthage, delivery, environmental tax, docking, unloading, loading and other duties, Brad heartbroken in half sold coal at twenty cents to the dollar.

After this “grandiose purchase”, Brad’s mountain of coal was never forgotten. Each time, passing by colleagues in the corridor, he knew that they remember about coal, and they knew that he understood this. No one particularly mocked or made fun of him, but it did not matter. Brad was no longer perceived as the ecxecor Chief Trader, he became the guy who mistakenly bought a breakthrough of coal.

All Articles