I need to automate a trading strategy that will enter on Stock A and short on Stock B simultaneously near market close (~3:50 pm EST). The candidates A and B are chosen among a predefined list of stocks according to an indicator that I will provide. The indicators will be used to determine A and B near the market close around (4:45pm EST). The strategy exit at the next day close via MOC (Market on Close).
The complication is in the entry, and less so on the exit since MOC is pretty simple. This is not the conventional pair trading as there is no monitoring of spread.
- An indicator function that I have written will rank all the stocks (about 30-100) and decide which one to long and short, say A and B.
The refresh timer for Amibroker automatic analysis should be 5-10s, but let me know if you could achieve better by lower/increase it.
- Once A and B are determined, check for the less liquid stock (the one with wider bid/ask or lower Volume), say A
- Submit a REL (relative) order for A
- If A is filled fully, perform a MKT (market) order for B immediately
(If A is only filled for X% only, perform a MKT (market) order for B with similar percentage X%)
- During the waiting for REL order, my indicator might signify that the current pair A/B is no longer worth trading. New candidate pair could be C and D. In this case, cancel the A, B trades and repeat the above steps for C, D.
- By the end of the trading session at 4:00PM EST, it is paramount that the I hold positions of equal dollar amount of stocks A and stocks B. Situation like $1000 long in A but $10 short in B is very detrimental to my performance, Either 1000/1000, 10/10 or 0/0 on A/B is good, 1000/10 is deadly.
- If 4:00PM EST is reached, print the results and go sleep.
- Suppose we have enter long/short for A/B by 4:00PM EST
- By ~7:00AM EST, submit a MOC order for both A/B to close the positions. Timing is not so crucial since next close is 4PM next day.
The Program needs to have following safety check
- A routine must be in place to prevent the program to keep buying/selling stocks non-stop !!
- A routine to monitor the exposure/holding of all stocks in list, once the exposure reach certain threshold, prevent opening of any new position. This is a general red alert check, such situation should not happen if other parts of the codes are written/functioning correctly, but who knows?
- A routine to check that the entry price is not too far from recent price level. If the current price is somehow in an extreme level, do not trade!
- The program must be able handle the following situation if the InteractiveBrokers API throws error such as
-- Order rejected for whatever reason
-- Disconnected to broker
-- Disconnected to datasource
-- Other errors thrown by the IB API
- All operations must be recorded/printed into DebugView or a log file so that we know what the Program had done, not done.
*I could provide more information if you have any questions.
- My indicator is a simple function that provide a 'score' range from 0-1.
The highest score stock is to be short, and the lowest score is to be long. Ranking can be done inside your automation script