Reputation: 11
I'm trying to place order using a algo trading script which will place my order only once when the conditions are met in one\same timeframe and then it should only place the order in the second timeframe and it should do it repetitively until the market ends.
`
order_exit_1st = (neg_first_diff == True and brickk_roc <= -1 and first_trend < 0 and scattered == "SELL")
order_exit_2nd = (voll_roc >= 30 and brickk_roc <= -1 and seccond_trend < 0 and scattered == "SELL")
for i in range(0,len(bricks)):
if bricks[i] == -7 and red_neg == True:
if self.timeframe_check == latest_time:
if order_exit_1st == True:
self.ExitCancelBuy(order_id,parent_order_id)
self.ExitOrderBuy(tickers,"sell",quantity)
print("buy order exited {}".format(tickers))
if order_exit_2nd == True:
self.ExitCancelBuy(order_id,parent_order_id)
self.ExitOrderBuy(tickers,"sell",quantity)
print("buy order exited {}".format(tickers))
self.timeframe_check += 1
else:
pass
` Here what the code should do is for example now the timeframe is 01 and the conditions are met then it should place the order only once and wait for the timeframe to complete without placing any new orders if again the conditions are met which could be possible .
So when once order is placed in one timeframe when condition is met then it should then explicitly wait for the current timeframe to end and it should place the order again only in the next timeframe .
And this loop should continue until the market hours stops.
Upvotes: 1
Views: 176
Reputation: 393
So there are a few different concepts here that you're addressing and need to be cleared up before writing any code would help. From what I can understand from your question you're dealing with:
There are a couple of python libs that might be of use but the concepts are more important. You're going to want to store your current state somewhere safely outside of the script, you don't want to repeat orders hundreds of times if a script dies on the last step. Depending on how paranoid you feel this could be an external database (High availability PostgreSQL remote server), a simple file based DB (sqlite), or even a csv file you refresh periodically.
You want to periodically check the state of each job (job stats stored in DB) and then based on the last run time, whether an order has been placed already, and some aggregate of the current stock prices you'll make a decision on whether to place a new order, wait a little bit more, or to cancel an order. Or whatever else your code is meant to do. Periodically could either mean once per day/hour/minute or once per stock tick. What you choose would depend on your trading strategy.
Now I could whack out some code here that could show you how to combine all the above libs but it would easily get very long and wouldn't reflect your situation. Plus I wouldn't trust some stack exchange code I found when it comes to investing my hard earned clams.
Upvotes: 1