I'm currently flat copper. Made some money on it, then gave back about a third, then the system closed the position..Best to be short Copper
I'm currently flat copper. Made some money on it, then gave back about a third, then the system closed the position..Best to be short Copper
Hey wopr,
I'm doing I guess what you described first., as I took the route of implementing event-based system from the beginning, I listen to every IB order update, as well as commission updates (actually I think there's 2-3 different callbacks that the IB sample code gives, and I'm subscribing to all of them in real-time), and I record every change to my DB connecting these pieces of information by IB Order ID, marking the last order state-update as latest.
@wopr it sounds like that you need to have separate monitor software which monitors for any trade confirmation message 24/7. And place that information in your database.
My case is different: I place an order with a limit price at the midpoint between bid and ask. If it isn't filled within one minute I change the limit price to cross the spread or even moved the price further away from my side of the spread. It happens rarely that then the order isn't filled immediately. The result is that I don't need to monitor for 24/7 whether any open orders are filled.
Thanks for the explanation!
To make sure I understood, when your system places an order, whatever process placed the order stays up and running until the order is filled? Or you have another process that's constantly up and is listening for events from the broker to update the DB?
Also, how is the IBOrderID generated?
I've found that using the permId IB generates wasn't good enough because it doesn't show up in Flex Reports, and from casual observation, doesn't seem to be unique - I haven't been able to find out how they generate that.
Have you ever had a situation where you place an order, it gets filled, but for some reason you didn't update the DB (maybe a bug), is that what you then go in and fill manually?
). I want to say that I fixed at least all my bugs in that area (hopefully
), and such mess-ups only happen when e.g. I place an order and the network goes temporary down and I don't receive the fill-update., or IB just misbehaves and doesn't send me the fill-update at all for some reason.. I think the last time it happened was a couple of months ago, and on average I'd guess it happens once every 3-5 month.. In such cases my system will think that the order wasn't filled, and the next time IB sends me back my positions (I think I request them every 20 minutes or so) my system will detect position mismatch and will place that instrument into "don't trade" state and send me an angry email. (well, the email will be normal, it's me who will be angry that I have to go and update some db-tables manually and restart the system
)So basically, you're now stubbornly waiting for the fill on your side of the spread no matter what happens to the price? What happens if the price goes away from you and never comes back, do you cancel and resubmit the order after a while (next day)?Yep, sure looks like that. I was really hoping that I wouldn't have to run a service all the time to monitor this, as I don't need the information real time - I was hoping I could have a cron job that runs every few hours and scoops executions and updates my DB, but IB exposes no reliable way of doing that.
I should also mention, I've been using an execution algo similar to yours (I legit ripped off Rob's "world's simplest execution algo") for about a year. But I saw that about 60% of executions were "Aggressive" meaning I crossed the spread or even moved it further away from my side of the spread. Then I implemented this async execution, where I put the limit on my side of the spread and go away. Pros are: ~95% of the times I get filled and capture the spread.
Cons are the bookkeeping I'm mentioning above, since I no longer have a process that sits and waits for the order to fill, I gotta update it asynchronously somehow.
So far, this new execution proves to be only added complexity, I'll have to see if getting the spread is worth it.
Hey folks, an update from me.
Nothing exciting really, sitting in a 10% drawdown.
View attachment 255604
I've been trying to get accurate trade confirmations from IB, and unless your orders are filled synchronously (eg. you send an order and your system waits for it to get filled and updates your database) seems like this is not easy to get.
I'll document some of the issues here in case other find it useful.
I'm using somewhat standard setup, IB Gateway with IBC.
As we know, IB Gateway can only return trades from today (I don't know exactly when do they make the cutoff). I then went with Flex Reports as that's the solution mentioned quite a bit, but that has its own problems.
If you use Activity flex reports, you can specify a custom date range (eg. trades from the last week), but the trades can be delayed quite a bit - currently, in my Flex report I can't see any trades from last 48 hours.
If you use Trade Confirmation Flex Query, that is hardcoded to only trades for today, and you can't change that, even though the documentation helpfully suggests you can. I've spent an hour chatting with customer support to see if there's a way to do this, and there's not.
Also, what's not helpful with Flex reports is that from what I see they don't retain the Order Ref - I've double checked that I'm setting it on the orders and I can see it in the TWS.
Customer support rep suggested that I use REST API, but that's another gateway to host and run and I don't want to do that. So that leaves us with the problem, unless you use TWS as your "gateway" which can return last 7 days of trades (I can't find info on why Gateway can do only today), you're stuck.
My current solution will likely be to write a wrapper service around IB Gateway that will be always up and do all the housekeeping, which does seem like a lot of work for something as simple as "get me the trades for last 7 days".
How do you folks do this, and do you have a need to fetch trades from last X days at all?
Yep. To be honest, that (price going away) only happened twice so far. Current behavior I have is that the order is cancelled if not filled after X hours and retried on the next day (if the new forecast still says we need the order). As with a few of you here, my system isn't impacted big time if execution is delayed by a day. Longer term, I was planning on implementing some sort of price moving on open orders, but I'd like to collect more data to see what's the best way to do that. I also have to run some calculations to see if all this is worth it - if me capturing the spread in majority of executions offsets those few where a price moves away from me.So basically, you're now stubbornly waiting for the fill on your side of the spread no matter what happens to the price? What happens if the price goes away from you and never comes back, do you cancel and resubmit the order after a while (next day)?
Yes, this is the problem I'm trying to address in an automated fashion. Though, from comments here, I'm starting to think I'm shaving some yaks.Anyway by the end of the day I should have everything reconciled and so no need to know what yesterdays orders were from IB.
But there are times when it would have been handy to go back further than a single day. If the whole system dies on say Friday afternoon, then if I don't fix it quickly I end up on Monday morning with trades missed and positions out of line. Or when I've missed an expiry, and IB closes out the position for me. As it is now I have to enter these trades manually.
I'm sure you tried this and I'm missing something, but why can't you use Flex Reports for this? You can specify a full year, define fields that you want and it also supports CSV for output. It's really easy to get them as well, both via API or from IB's admin portal. To clarify, API here is not the socket-based API that IB Gateway or TWS expose, it's the Flex Report HTTP based API, IMHO even easier to use as you can just use requests to fire off a request.And I'd *really* like the ability to get say a year of trade statements through the API, rather than the current hacky XML parser that I use when I need to do my tax returns...
Thanks for the detailed explanations Kernfusion and Rob!
My background is in software engineering at big tech co's, and my general mantra is that if I can avoid running something always-on as a service, I should. Every time you have that, you have to worry about keeping it on, providing some alerting and monitoring, and most importantly handling cases when it's down. And it will be down, for any reason - bugs, unhandled exeptions, deployments, OS upgrades, hardware issues, etc. If that happens you probably missed handling some requests while it was down, so some sort of a "backfill" is needed.
Since I'm (and I think most of us here) in a really relaxed environment, where things happen every few hours and not thousands of times per second like in a typical service, I like to use that to my advantage.
I mean to me it seems more pure and "correct"., i.e. if we look at this problem in general, what is the natural trigger for us to do anything - it's a price-change!. i.e. why do we choose to do things once a day at a certain arbitrarily-chosen time when really it's the new price-tick (change in bid or ask) is the natural trigger for us to do anything, so that's what we should be reacting too. And a more natural way to address noise-overtrading is not to "only look at the price once a day" but by using position-change thresholds and forecast-smoothing (and daily trading-limits as the emergency fail-safe).
because of course the individual price-ticks don't change your target position most of the time, i.e. 99.9% of ticks I process don't trigger any trading, and it's totally fine to do things once a day with a slow system.. But well, I just wanted to write something I liked myself the way I liked, and not what some faceless "collective" of managers, analysts and others at work wants me to..
).