Hi I wrote a method that places an order for a stock based off a fundamental screen that looks like this:
def buy_stock(context, data):
for stock in context.fundamental_df:
if stock.sid in context.portfolio.positions:
pass
else:
order_value(stock, 1000)
context.buy_dates[stock] = get_datetime()
break
It buys one stock making sure that the stock is not already held in the portfolio and records the date the order was placed.
I decided I wanted to buy two different stocks instead of one so I scheduled this method to run twice (ten minutes apart) thinking that the if stock.sid in context.portfolio.positions: line would prevent it from buying the same stock as the first run. The result was that I bought the exact same stock on each scheduled run.
schedule_function(buy_stock,
date_rules.month_start(),
time_rules.market_open(minutes = 30))
schedule_function(buy_stock,
date_rules.month_start(),
time_rules.market_open(minutes = 40))
Does this occur because the order from the first run of the buy_stock() method has not been filled by the time the second scheduled run occurs?