Simple trading algorithm TWS API (Python)
Budget: $10 – $30 USD
I need a relatively simple script for TWS API written in Python.
The script must be able to pull recent prices of 2 instruments, calculate ratio between their opening prices, calculate differences between their closing and opening prices, and then make decision of buying or selling one or both of the instruments.
Variables that must be editable:
- Time interval
- Instrument tickers
- Position size
- Stop loss / take profit levels expressed in USD or % (by default stop orders are turned off)
- option to turn off one or both legs of algorithm at pre-defined periods (i.e. between 3:55 and and 4pm)
- log function dumping messages to .txt file.
Here is the algorithm with examples as SPY and QQQ etfs:
For each 5 min interval calculate:
i) Conversion ratio at opening of last interval: Ratioopen_t-1 = [SPYprice_open_t-1] / [QQQprice_open_t-1]
ii) Price differential at closing of last interval: diffclose_t-1 = [SPYprice_close_t-1] – [Ratioopen_t-1 x QQQprice_close_t-1]
When the diffclose_t-1 < 0, in the current interval t0 sell 1’000 shares of SPY at the opening at MID price
Then at the end of the interval t0
if diffclose_t0 < 0, hold the short position
if diffclose_t0 > 0, close position (buy 1’000 of SPY) and open long position
When the diffclose_t-1 > 0, in the current interval t0 buy 1’000 shares of SPY at the opening at MID price
Then at the end of the interval t0
if diffclose_t0 > 0, hold the long position
if diffclose_t0 < 0, close position (sell 1’000 of SPY) and open short position
The script must be able to pull recent prices of 2 instruments, calculate ratio between their opening prices, calculate differences between their closing and opening prices, and then make decision of buying or selling one or both of the instruments.
Variables that must be editable:
- Time interval
- Instrument tickers
- Position size
- Stop loss / take profit levels expressed in USD or % (by default stop orders are turned off)
- option to turn off one or both legs of algorithm at pre-defined periods (i.e. between 3:55 and and 4pm)
- log function dumping messages to .txt file.
Here is the algorithm with examples as SPY and QQQ etfs:
For each 5 min interval calculate:
i) Conversion ratio at opening of last interval: Ratioopen_t-1 = [SPYprice_open_t-1] / [QQQprice_open_t-1]
ii) Price differential at closing of last interval: diffclose_t-1 = [SPYprice_close_t-1] – [Ratioopen_t-1 x QQQprice_close_t-1]
When the diffclose_t-1 < 0, in the current interval t0 sell 1’000 shares of SPY at the opening at MID price
Then at the end of the interval t0
if diffclose_t0 < 0, hold the short position
if diffclose_t0 > 0, close position (buy 1’000 of SPY) and open long position
When the diffclose_t-1 > 0, in the current interval t0 buy 1’000 shares of SPY at the opening at MID price
Then at the end of the interval t0
if diffclose_t0 > 0, hold the long position
if diffclose_t0 < 0, close position (sell 1’000 of SPY) and open short position