Stop Loss "Range" Orders for Pre Market and After Hours
gonzas opened this issue · comments
Hello, first and foremost thank you for sharing your project.
As you may already know, tipical stop orders only work in regular time market hours, only limit orders do.
So your project could be very usefull to implement a feature that most brokers like tradezero don't have.
I made a first approach to this idea with something like the following code:
import time
from tradezero_api import TradeZero
from tradezero_api import Order
tz = TradeZero(user_name='TzUsername', password='TzPassword')
tz.login()
symbol_name='XELA'
quantity=1
offset=1.0
high=6.0
low=5.5
while (True):
if not tz.conn():
tz.login()
ticker = tz.data(symbol_name)
if (tz.Portfolio.invested(symbol_name)):
if (str({ticker.last}) >= str({high})):
tz.limit_order(Order.COVER, symbol_name, quantity, high+offset)
if (str({ticker.last}) <= str({low})):
tz.limit_order(Order.COVER, symbol_name, quantity, low)
time.sleep(1)
I just tested and it worked.
it surelly could be made way better than this, but that would be the main idea.
Further improvements can be:
- Specify multiple symbols (even if they aren't yet in porfolio)
- Once they are in portfolio, Retrieve the quantity from there (since partials executions can occur)
- Ability to send alerts (email)
- (minor bug) Avoid put the same order again and again (while it's waiting to execute the order that has been placed)
Do you think would possible to achieve this in a way better way than this very basic and uggly code ?
If I can help in some way please let me know.
Thanks.
Hey Gonzas. I see that you want to create a RANGE order that works also in the PM and AH sessions,
to accomplish that you would need to create a long-running loop that keeps checking if the price is above X or below Y and based on that place a LIMIT order for take-profit or stop-loss like you did in your example.
The issue is that the loop would be blocking and therefore you won't be able to do anything else until you exit that position.
To counter that, I would recommend adding the symbol to the watchlist with tz.Watchlist.add(symbol)
, and this way you will get the price for that symbol streaming even while you're doing other stuff, ofc you will still have to create and schedule a function that checks the latest price every N seconds, and based on that it will submit a LIMIT order if the price reaches TP/SL.
Other than that, the goal of this project is to create an API that reflects all the functionality and features you have on the ZeroFree web platform, and not to add any more, so this is out of the scope of this project.
Hey @shner-elmo , glad to hear from you again.
Yes, Add symbols to watchlist isn't a big problem since it can be done with variables.
The real blocking problem is getting the value of price from the "order ticket" table, instead of the table "watchlist".
If somehow another funtion could be written to get the same values but from a "watchlist" then it would be non-blocking functionality for python loop I already have.
Understood that this is out of scope, but just in case someone with more knowledge than me want to add, would be helpfull to many I believe.
In case someone else asks why I can go in deep details why this is helpfull.
Hey, if by "order ticket" you mean the "Active Orders" table inside the portfolio widget, then there already is a method for getting that table: tz.Portfolio.get_active_orders()
, once you have the DataFrame you should filter it by the order-ID (ref_number
), and then you can select any property, including the limit-value.
@gonzas Hey bro, so if you just want to get the price of a symbol from the watchlist you can do smth like this:
df = tz.Watchlist.data()
price = df.loc['AAPL']['last']
@shner-elmo @gonzas +1, this would be very useful, despite being out of scope for this project.
@gonzas, did you ever continue further work on your function above and did you get around to implementing it with the watchlist to allow multiple concurrent streams? Keen to see what you've done and how we can collaborate!