Automating buy/sell operations

is it possible to automate buy/sell operations based on some criteria available on a google sheet or on the broker itself (Interactive Brokers in my case)
More specifically, using google sheet I monitor my two ETFs:
IWDA (stocks) 40%
DBZB (obligations) 60%

When the IWDA drops 7% (in a time frame of 6 months) I get notified by google sheets and move 16% of DBZB into IWDA.
When the market recovers I get notified by google sheets and I move it back.

Is there a way to automate this?
I realised that I might not be available to sell/buy when the threshold is crossed (despite google sheet will send me emails when the 7% threshold is breached) . For that reason, an automation could come handy.

Depends on your level of programming skills everything is possible. What might fit your needs would be TradingView they have integrations with many brokers however I’m not sure if they support complex buy/sells based on your rules.

1 Like

IBKR offers API’s, see here. I would probably look into the Client Portal API and use Python to use it. Word of caution tough - as a software engineer myself, I would be VERY CAUTIOUS with automating trading. Develop one serious bug in your code, and your capital is at stake. Think a bug in a loop, where you order hundrets of sell orders in a couple of seconds, or stuff like that. Hedge funds with a lot of smart people have done this in the past. So build in saftey mechanisms :wink:


I have only a basic knowledge about programming.
Thanks for underlining the risks involved.
Do you know if the automation (checks and executions) can be implemented within the broker portal itself without the need of an external computer which logs in every 10 minutes (for example) checks something (on Google?) and eventually executes the order?
I really have no idea about how this could work (high level).

Then I would simply not do it.

There are many things that can do wrong and you need to consider and there is extensive testing needed with a paper trading account and also you need to make sure your credentials do not get leaked somewhere (so don’t store it on Google or somewhere :)).

Hi Patron,
thank for your feedback.
The risks has been already raised by the previous user and I have the impression that you are over complicating thins, considering that you don’t know the details about what I am trying to do.and what is my approach to do it.
I would like to know how this could work high level to understand is it is worth or not.

Beside that the credential is quite a hot topic. That’s why I am asking if the automation can run directly on the broker systems.

If you want to do this with the broker systems, then IB provides a rebalancing tool where you can specify target allocations and then TWS creates sell/buy orders accordingly. However, this is not automation it just helps you rebalancing so you don’t have to calculate deviations on your own and manually create buy/sell orders. You have to be logged in for this and it’s not an ongoing process.

Several people have already posted some sites that could potentially be used for something like that to automate tasks but my view on this is, that I personally would never ever provide any of my credentials to a third party site. You don’t know what those sites are doing with those credentials and how their security works etc. so for me it would be an absolute no go.

If I wanted to create something like this on my own I would probably want it to run not on my computer but somewhere in a safe environment. So probably I would use a cloud provider like AWS or so for it and create some functions in Python and run them as Lambda functions in the cloud, that analyse the deviation between target and effective allocation and issues corresponding buy/sell orders if the deviation is outside certain parameters.

Credentials would have to be stored in a proper key management system (basically a multi-tenant hardware security module) but even then, I would never ever store my regular credentials with full access to my account there. So one of the prerequisites I would check before, would be if the IB API allows you to create separate credentials just to be used for the API with limited permissions, e.g. cannot read personal data, cannot transfer money, etc. and can only buy/sell products you specifically whitelisted, all other account actions should be denied. I also wouldn’t want to disable 2FA for that on my main account. I haven’t used the IB API yet but if I’m going to use it one day this would be a prerequisite. If I would have to use my real credentials that provide full access to my account I would not do it.

Of course, there are several ways on how you could do this differently. But reading your initial question, I’m not sure if it’s worth the effort. A 60/40 portfolio with only 2 positions and an event trigger on a 7% drop on one position that initiates an allocation change is something I would probably do manually as it’s not expected to happen very frequently and it’s also not overly complex. So, once you get the Google sheet alert you could use the TWS rebalancing tool to change your target allocation and issue corresponding buy/sell order. Not sure if that’s what you want or you deem sufficient.


thanks again for the information.
I am also not happy to leave my credentials on those websites…so it seems there are not many option left, until the broker decides to allow some kind of automation internally. I will ask them as it seems quite a basic function. Perhaps one day they will add it if many users demands it.

They will refer you to the APIs that’s what they’re there for.

They will refer you to the APIs that’s what they’re there for.

The problem is that the thing running the API request is something external to their system domain.At least based on my understanding. They could just allow people to automate locally, within their account…that’s what I meant.