Back to Community
illegal Quantopian/IB algo?

I gather that if I were to launch this algo with real money through Quantopian/IB, it might be illegal. I'm putting in an order, which would be sent to IB immediately, and following it with a delayed cancellation. So, it would run afoul of the rule that all trades must be intended to be executed; the only valid reason for cancellation would be if information were received that justified my order being cancelled, which clearly the algo code does not support.

Just curious...if there are Q/IB lawyers sitting around twiddling their thumbs on a spring day, maybe one could render an opinion. It's a test. They should know the answer pronto. No charge.

import time

def initialize(context):  
    context.spy = symbols('SPY')[0]  
    context.cancel_delay = 0.010 # delay between order and cancel_order, in seconds  
def handle_data(context, data):  
    order_id = order(context.spy,100)  
    wait(context.cancel_delay)  
    cancel_order(order_id)  
def wait(time_lapse):  
    time_start = time.time()  
    time_end = (time_start + time_lapse)  
    while time_end > time.time():  
        pass  
Clone Algorithm
23
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import time

def initialize(context):
    
    context.spy = symbols('SPY')[0]
    
    context.cancel_delay = 0.010 # delay between order and cancel_order, in seconds
 
def handle_data(context, data):
    
    order_id = order(context.spy,100)
    
    wait(context.cancel_delay) 
    
    cancel_order(order_id)
    
def wait(time_lapse):
	time_start = time.time()
	time_end = (time_start + time_lapse)
 
	while time_end > time.time():
		pass
There was a runtime error.
18 responses

Preface: not a lawyer.

I would say this is exactly illegal, you are creating artificial buy/sell pressure. It's obvious you meant to create a spoofing algorithm. Just because you wouldn't profit from it, that wouldn't make it any more legal. As to whether there would actually be a chance of you getting caught using it? I would say chances are slim to zero (unless your name happens to be sarao). There are many firms with many times more capital than he had doing this thousands of times a day.

To give you an idea of the sheer size of the phenomenon:
http://www.zerohedge.com/news/2015-04-23/dear-cftc-presenting-todays-e-mini-market-manipulating-spoofing

Well, it would be easy enough for the FBI to get records from Quantopian/IB. Since Q runs on minute bars, any minute with an order followed by a delayed cancel would be suspect. Examination of the suspicious algos would make fishy activity obvious, especially if they are generously commented ("Tweak market by submitting order, followed by delayed cancel."). --Grant

Grant, you are one interesting dude.

Devil's advocate: if this section of code was part of a larger algo, you could argue that the immediate cancel is part of the logic. If you submit an order and it isn't immediately filled, you would just assume the market has changed in such a way that you would no longer want to make that trade, and that I believe would have some legal basis.

Exactly this type of execution is at the basis of the charges pressed against this "arcade shop" trader in London (it looks kind of sketchy that a single person caused the flash crash but I don't know).

This has been an interesting subject for me for some time. Before the flash crash Paul Rotter became famous as the 'Eurex Flipper'. Below is a portion of one of the few interviews he gave.

I believe Rotter worked without benefit of software tools other than a trade client and seeing the level2 order book.

To succeed at this today you would have to trade very large size and at the millisecond speed instead of post-minute. Also you would probably signal alerts at the broker before you placed too many trades.... I believe Rotter quit this trading when he became too visible and competitors starting to wait for him to trade, his trading became predictable and lost his edge (I believe).

10/2005

Paul Rotter - aka "the Eurex Flipper"

Paul is arguably the single largest and most successful individual futures trader on planet Earth, executing trades on the Eurex exchange primarily in the Bund, but also in the Bobl and Schatz interest rate futures. He trades between 200-300,000 round turns daily using the X_Trader platform, and clearing through GNI Touch.

q: what are your strengths as a world-class trader and where are the differences between you and other traders?
a: it's the ability to get more aggressive in winning phases, taking bigger risks, and scaling back in losing times. this is against human nature. the best thing is to have somebody around who is neutral to trading, that switches the terminals off, when a certain loss level has been reached for the day.

q: you are known as a order book-scalper, could you please explaining to our readers what you are doing and what your strategies look like? what is your tactic?
a: it's some kind of market making where you place buy and sell orders simultaneously, making very short-term trading decisions b/c of certain events in the order book (level2). for example, I usually have lots of orders in different markets at the same time, pretty close to the last traded price. the resulting trades are usually a zero sum game, but I get a pretty good feeling for what is going on and then ultimately can make a decision for a larger trade.

q: how long are you usually in a position?
a: since I do trend plays very seldom and actually scalp the market, i constantly get fills in different markets on both sides which can cause constantly changing positions for hours. sometimes i change my opinion several times within a couple of minutes, which is not pretty hard for me, since I'm only looking for the next 3-5 ticks.

q: during your professional career, have you always been a scalper or did you try other strategies (momentum/swing) as well?
a: yes, I have always been a scalper, but I am adjusting my strategies to different market situations all the time. on volatile days I of course have less orders in the market and do more 'single trades', although I usually hold them only for a couple of seconds.

q: your strategies only work on electronic exchanges?
a: yes, b/c you cannot handle that much orders in a pit, looking for counterparties and so on. computer exchanges grant fast order flow and are not as easy to manipulate.

q: as a scalper, are you trying to run stops?
a: well, yes, but because of the increase of liquidity in the last couple of years, the fast spikes caused by stops are not happening that often anymore. apart from that, that stops often are not where you would suppose them to be, because the other market participants are not silly either or learned their lesson in the past.

Google Rotter or Flipper for more....

This is not illegal.
This is part of an execution strategy, a poorly created one.
Normally it goes like this:

Submit order outside spread.
Cancel after 1 second.
Submit order at far side of spread.
Cancel after 1 second.
Submit order inside far side spread.
Cancel after 1 second.
Submit order at mid.
Cancel after 1 second.
Submit order at inside near side spread.
Cancel after 1 second.
Submit order at near side spread.
Cancel after 1 second.
Submit order outside near side spread.
Cancel after 1 second.

Repeat.

You can do this all day long.

Market Tech,

I gathered that if one submits an order with the intent that the order would not be filled, it would be an illegal practice. In the example you posted above, if you were trying to make the market move until it was in your favor, then it would be considered illegal market manipulation.

Now, if you were looking at a screen and submitting/cancelling, with the cancellation justified by the fact that your trade wouldn't be favorable, it'd be a different story.

Or no? Am I missing something?

Grant

How can you tell the intent? You can't.

With your exact example, you'd be getting filled a large percent of the time. OK. So you got filled. The other percent of the time that you didn't get filled, how is anyone to know that you didn't really want to get filled?

The scenario I outlined is a common OEX technique to get in on large positions at the same time being mildly aggressive. All the major brokers have these special order type strategies you can submit your order with and the broker does the dirty work of "working your order." Inferring intent is nearly impossible in such scenarios. Now. there are conditions where it's obvious that order spoofing is taking place. But an order handing around for 10,000 microseconds is (probably) not one of them. If the market is illiquid the probably may not apply. Especially if you did 100 orders (or so) in ~10 seconds. But if you did 10 of your orders in quick succession and then got filled... who's to say you weren't just working an order?

@ Market Tech,

If you execute the strategy, you know the intent, and so you can know if you are on the wrong side of the law. And if you are caught, and accused, you have the choice of admitting your intent, or trying to hide it.

In http://www.cftc.gov/filings/orgrules/rule082814cmedcm001.pdf, it sounds pretty black and white to me:

All orders must be entered for the purpose of executing bona fide
transactions.

Pretty simple.

Isn't this what Sarao was doing? Putting in unfilled orders to move prices.

Stumbled across this and was wondering if a "fill or kill" order is something that can be done on Quantopian and if so, would placing an obscene fill or kill order basically have the same effect as the OP?

Ethan,

I think, in effect, the code I posted above will be an approximation to a "fill or kill" order. Here's the code again:

import time

def initialize(context):  
    context.spy = symbols('SPY')[0]  
    context.cancel_delay = 0.010 # delay between order and cancel_order, in seconds  
def handle_data(context, data):  
    order_id = order(context.spy,100)  
    wait(context.cancel_delay)  
    cancel_order(order_id)  
def wait(time_lapse):  
    time_start = time.time()  
    time_end = (time_start + time_lapse)  
    while time_end > time.time():  
        pass  

If the order is still open after the wait time, it'll be cancelled. One question for the Q team is what happens if there is no delay between the order and it's cancellation? At one point, I was told that information is transmitted to IB immediately, as it is encountered in the code. So, what happens if I do this:

import time

def initialize(context):  
    context.spy = symbols('SPY')[0]  
    context.cancel_delay = 0.010 # delay between order and cancel_order, in seconds  
def handle_data(context, data):  
    order_id = order(context.spy,100)  
    # wait(context.cancel_delay)  
    cancel_order(order_id)  
def wait(time_lapse):  
    time_start = time.time()  
    time_end = (time_start + time_lapse)  
    while time_end > time.time():  
        pass  

Does my order make it to IB followed by a cancellation? Will an attempt be made by IB to fill the order? Or will they see the cancellation and kill it? And what if the initial fill is partial? Would the remainder of the order be killed immediately?

Grant

Thats a good question and unfortunately im pretty new to all of this, so I couldnt answer that.

I was wondering if you knew of any way to test your line in the queue if you have placed an after market order? Figured it might be related to some technique in sending an order and cancelling it.

I'm sure this wouldnt be easy info to find (if available at all)

Spoofing was made an explicit federal crime by the Dodd Frank law of 2010. The Flash Crash, now suspected to be caused by S&P Futures spoofing, happened just before it was passed, but that's not stopping the authorities who are charging Sarao with other financial crimes for the incident.

That said, the main sentiment on this thread is correct: if you do it, there are no CFTC robots scanning the exchanges to automatically flag your activity. The truth is if they suspect an incident is spoofing, they will investigate after the fact, to the extent they can devote time and money relative to their priority in catching the perpetrator. This is obviously the case with Sarao who is suspected of causing the Flash Crash, which was an international incident.

It's also documented this happens a lot, but that isn't necessarily a license to go crazy. As fast as humans design offensive weapons, we design defensive ones as well: in our case, just one example is Hudson River Trading's new system that does exactly what you're thinking: it watches the markets and flags patterns as likely incidents of spoofing. (http://blogs.wsj.com/moneybeat/2015/05/07/did-autozone-get-spoofed/)

The only logical conclusion is though spoofing is popular and may increase in popularity, there will be exponentially more automated spoofing detection coming online in the coming years, meaning it will get more risky overall.

Spoofing is all about liquidity detection. It's not new to algo trading guys. People did it in the Chicago pits before I was born.

Liquidity providers want to detect informed traders. By submitting orders and canceling you can basically trick other traders into revealing their trading interest. Say there's a big hidden stop limit order on the limit order book that says to sell 1m shares at limit 9.96 if price hits 9.98. The current spread is 10.01/9.99. If I'm a liquidity provider I can get fucked if I buy at 9.98 (which I think is a bargain) and then all of a sudden this guys' order hits.

This is a well established point. Market makers losses come primarily from trading against informed traders. So what traders do is test the waters and try and detect stuff like that. That's where all this spoofing and pinging and such comes from. Market makers trying to predict liquidity.

@ Luis,

Interesting. And what would be Hudson River Trading's motivation in detecting spoofing? I'm guessing that they want to profit off of the illegal activity of spoofing, not by spoofing themselves, but by taking advantage of the spoofers. One would think that this would also be illegal. Kinda like following bank robbers around, hoping to pick up some of the money that flies out of their bags... It sounds like the whole situation is hopeless, and begging for engineered controls within the electronic trading system itself.

I don't think every order placed has to have intent to be filled. i.e. after opening, placing a OCO order to close with an upper profit limit and a lower stop loss is definitely not illegal, despite the fact that if every order you placed took this structure then a max of only 66% of your orders would be filled (with the knowledge that this is the case). If you consider more complex OCO orders this max fill proportion could be much lower (still legal of course). I am no lawyer nor an expert, but if you take a look at SEC.gov and just skim some of the press releases about firms and individuals who were charged with this type of securities fraud, you'll see that they involve actual spoofing. That is, not only are the orders not intended to be filled but they are also intended to artificially manipulate the bid/ask in some way to create an advantage to the trader. Placing orders that are to be cancelled if not filled in some given time period (however small) is likely commonplace among institutional algorithmic trading firms, alluded to by the cancel-to-trade ratio data provided by the SEC. Take a look at this resource:
https://www.sec.gov/marketstructure/datavis/ma_exchange_canceltotrade.html
On some exchanges this year, there were cancel-to-trade ratios of 3000/1, which means that on a given day, only 0.03% ( or 0.0003) orders were actually filled. If you are planning on trading with a significant amount of capital using this type of strategy, you should definitely get legal counsel, but if you're just playing around with a small account on Q/IB to see the efficacy of the strategy, I wouldn't be too worried (based on that SEC data source).