I don't think you can do that in TWS directly, but it would be a simple app to write for the API depending on your objective. Your buy/sell criteria sound simple, but building an app that can handle problems such as loss of power, loss of Internet connectivity, broker problems, exchange...