Hi,
Needs some help to achieve something. Here is a part of code that I did simplify to make sure I get an answer on what I'm looking for. It do not make any logic as for the order criterias but you will get what I want to achieve.
Here I want to make some decision when it is 9h32AM, for a universe of stock, with some conditions to decide if I buy or if I sell. Everything goes great until I try to close my orders. In the ''close my position'' section, my problem is that I want to create a decision using the exact same data ''low_1m'' that I use in the ''order section'' . This data is being created at 9h32 and it is the low of a stock at the first minute candlestick of the day. It seems like when I get out of the first ''for'' loop, and create an other one for the stock in context.portfolio.position, it won't reconize the ''low_1m''. And if I recreate this low_1m using history, it won't use the history of the first two minute of the day....it will use the last two minutes of any moment in the day....
Any help would be appreciated....
def initialize(context):
set_universe(universe.DollarVolumeUniverse(94.0, 99.0))
def handle_data(context, data):
exchange_time = get_datetime().astimezone(pytz.timezone('US/Eastern'))
close_ = history(2, '1m', 'close_price')
low_ = history(2, '1m', 'low')
high_ = history(2, '1m', 'high')
open_ = history(2, '1m', 'open_price')
price_ = history(2, '1m', 'price')
dayprice = history(2, '1d', 'price')
volume_history = history(10, '1d', 'volume')
trade_ = 0
Order section ######
if exchange_time.hour == 9 and exchange_time.minute == 32:
for stock in data:
open_1m = open_[stock][-2]
low_1m = low_[stock][-2]
high_1m = high_[stock][-2]
close_1m = close_[stock][-2]
open_2m = open_[stock][-1]
close_2m = close_[stock][-1]
if open_2m < open_1m:
order_value(stock, 20000)
elif open_2m < high_1m:
order_value(stock, -20000)
Close my position section ######
open_orders = get_open_orders()
elif:
for stock in context.portfolio.positions:
current_ratioreturn = data[stock].price / cost_
amount_ = context.portfolio.positions[stock].amount
if stock not in open_orders and amount_ > 0 and data[stock].price <= low_1m or current_ratioreturn >= 1.04:
order_target(stock, 0)
elif stock not in open_orders and amount_ < 0 and data[stock].price >= high_1m or current_ratioreturn <= 0.96: