A news-breaking account of the global stock market’s subterranean battles, Dark Pools portrays the rise of the “bots”–artificially intelligent systems that execute trades in milliseconds and use the cover of darkness to out-maneuver the humans who’ve created them.

In the beginning was Josh Levine, an idealistic programming genius who dreamed of wresting control of the market from the big exchanges that, again and again, gave the giant institutions an advantage over the little guy. Levine created a computerized trading hub named Island where small traders swapped stocks, and over time his invention morphed into a global electronic stock market that sent trillions in capital through a vast jungle of fiber-optic cables.

By then, the market that Levine had sought to fix had turned upside down, birthing secretive exchanges called dark pools and a new species of trading machines that could think, and that seemed, ominously, to be slipping the control of their human masters.

Dark Pools is the fascinating story of how global markets have been hijacked by trading robots–many so self-directed that humans can’t predict what they’ll do next.

Courtesy of the author, we present to our readers the following excerpt from Dark Pools: High-Speed Traders, AI Bandits, and the Threat to the Global Financial System, by Scott Patterson, author of The Quants.

In early December 2009, Haim Bodek finally solved the riddle of the stock-trading problem that was killing Trading Machines, the high-frequency firm he’d help launch in 2007. The former Goldman Sachs and UBS trader was attending a party in New York City sponsored by a computer-driven trading venue. He’d been complaining for months to the venue about all the bad trades—the runaway prices, the fees—that were bleeding his firm dry. But he’d gotten little help.

At the bar, he cornered a representative of the firm and pushed for answers. The rep asked Bodek what order types he’d been using to buy and sell stocks. Bodek told him Trading Machines used limit orders.

The rep smirked and took a sip of his drink. “You can’t use those,” he told Bodek.

“Why not?”

“You have to use other orders. Those limit orders are going to get run over.”

As the rep started to explain undocumented features about how limit orders were treated inside the venue’s matching engine, Bodek started to scribble an order on a napkin, detailing how it worked. “You’re fucked in that case?” he said, shoving the napkin at the guy.

“Yeah.”

He scribbled another. “You’re fucked in that case?” “Yeah.”

“Are you telling me you’re fucked in every case?” “Yeah.”

“Why are you telling me this?”

“We want you to turn us back on again,” the rep replied. “You see, you don’t have a bug.”

Bodek’s jaw dropped. He’d suspected something was going on in- side the market that was killing his trades, that it wasn’t a bug, but it had been only a vague suspicion with little proof.

“I’ll show you how it works.”

The rep told Bodek about the kind of orders he should use— orders that wouldn’t get abused like the plain vanilla limit orders; orders that seemed to Bodek specifically designed to abuse the limit orders by exploiting complex loopholes in the market’s plumbing. The orders Bodek had been using were child’s play, simple declarative sentences sent to exchanges such as “Buy up to $20.” These new order types were compound sentences, with multiple clauses, virtually Faulknerian in their rambling complexity.

The end result, however, was simple: Everyday investors and even sophisticated firms like Trading Machines were buying stocks for a slightly higher price than they should, and selling for a slightly lower price and paying billions in “take” fees along the way.

The special order types that gave Bodek the most trouble—the kind the trading-venue rep told him about—allowed high-frequency traders to post orders that remained hidden at a specific price point at the front of the trading queue when the market was moving, while at the same time pushing other traders back. Even as the market ticked up and down, the order wouldn’t move. It was locked and hidden. It was dark. This got around the problem of reshuffling and rerouting. The sitting-duck limit orders, meanwhile, lost their priority in the queue when the market shifted, even as the special orders maintained their priority.

Why would the high-speed firms wish to do this? Maker-taker fees that generate billions in revenue for the speed Bots every year. By staying at the front of the queue and hidden as the market shifted, the firm could place orders that, time and again, were paid the fee. Other traders had no way of knowing that the orders were there. Over and over again, their orders stepped on the hidden trades, which acted effectively as an invisible trap that made other firms pay the “take” fee.

It was fiendishly complex. The order types were pinned to a specific price, such as $20.05, and were hidden from the rest of the market until the stock hit that price. As the orders shifted around in the queue, the trap was set and the orders pounced. In ways, the venue had created a dark pool inside the lit pool.

“You’re totally screwed unless you do that,” the rep at the bar said. Bodek was astonished—and outraged. He’d been complaining for months about the bad executions he’d been getting, and had been told nothing about the hidden properties of the order types until he’d punished the it by reducing the flow he send to it. He was certain they’d known the answer all along. But they couldn’t tell everyone—because if everyone started using the abusive order types, no one would use limit orders, the food the new order types fed on.

Bodek felt sick to his stomach. “How can you do that?” he said.

The rep laughed. “If we changed things, the high-frequency traders wouldn’t send us their orders,” he said.