Blueshift 2.0 API
There are no examples, I've put as much as I can understand here,
We need more examples
1) Once we make a trade e.g Sell 10,000 GBPUSD…
set_account_currency("USD")
context.gbpusd = symbol("GBPUSD")
sellorder = order(context.gbpusd, -10000) # now this is supposed to return 'order ID' as a str or None
sellorder #executes a sell of 10000 units also # is a str that is an 'order id' "N01234567800"
2) …How do we retrieve OrderID and Ave Price Filled:
get_order(sellorder) # then this is supposed to go to the order protocol 'classblueshift.protocol.Order'
#so to get pricefilled :
ave_price_filled = get_order(sellorder).average_price
The above code is wrong, what am I doing wrong?
APi Doc Links:
https://blueshift.quantinsti.com/api-docs/api.html?highlight=tradingalgorithm%20get_open_positions#blueshift.algorithm.algorithm.TradingAlgorithm.get_open_positions
https://blueshift.quantinsti.com/api-docs/objects.html#ordervalidity
No sure I get the question under #1. What you describe under #2 is correct. Note if you query the average price right after placing the order (or till the order is executed), you will get 0, as the order is yet to be executed. If you see those APIs not working for you, please share an example strategy that can reproduce your error.
Thanks for the reply, yes I was getting an 0.0 for average_price filled.
order_id(order(asset,amount)) .average_price
I will try and run a while loop, until we get an order_id
Oops, no that will be a bit wrong. Your code may get stuck at the while loop! Blueshift is event driven, so best to take advantage of it. If you place an order, you can either check the fill periodically by scheduling a function, or from inside the handle data function. If it is still open, check in the next call of the scheduled function (or handle data). See below:
from blueshift.api import( symbol,
order_target_percent,
schedule_function,
date_rules,
time_rules,
get_datetime,
)
def initialize(context):
context.long_portfolio = [
symbol('AMZN'),
symbol('AAPL'),
]
schedule_function(rebalance,
date_rules.month_start(days_offset=0),
time_rules.market_close(hours=2, minutes=30))
def before_trading_start(context, data):
context.orders_to_track = set()
def rebalance(context,data):
for security in context.long_portfolio:
oid = order_target_percent(security, 1.0/2)
if oid:
context.orders_to_track.add(oid)
def handle_data(context, data):
if context.orders_to_track:
orders = context.orders
oids = list(context.orders_to_track)
for oid in oids:
if oid in orders:
order = orders[oid]
if order.is_open():
continue
print(f'{get_datetime()}:executed price for {oid} is {order.average_price}')
context.orders_to_track.remove(oid)
The above keeps track of new orders placed (reset at the start of each trading day) and print the executed price.
Yes my code got stuck in a while loop!
I hope Blueshift servers are ok now.
I will study the above code…my answers are in there.
Thanks (again) Propdita, for taking time out of your day.