BlueShift: can_trade() function, how to use it?

Hello, 



I am doing an online Udemy Course, Python for Financial Analysis and Algorithmic Trading by Pierian Training. The course was initially supposed to be for Quantopian (*zipline) but now the course instructor has recommended BlueShift. Pierian Training are not supporting the algo trading questions on Blueshift.



Some of the Quantopian code works verbatim in Blueshift, others I have successfully edited by looking at the BlueShift API documentation.



I am stuck on can_trade()



This code does not work in Blueshift, however it did in Quantopian.

 

from blueshift.api import(    symbol,
                            order_target_percent,
                            schedule_function,
                            date_rules,
                            time_rules,
                            can_trade,
                       )

def initialize(context):
    # Reference to Tech Stocks
 
    context.amzn = symbol("AMZN")

def handle_data(context,data):
    # This insures we don't hit an exception!
    if data.can_trade(symbol("AMZN")):
        order_target_percent(context.amzn, 1.0)

API Functions — Blueshift 2.0.0 documentation (quantinsti.com) 

  • scroll down 70% the page to see can-trade() function docs
  • TradingAlgorithm.can_trade(assets)
  • Can someone give me a working example?


(If anyone else is on the same Udemy course maybe we can start a Thread here to get us through the last 2 Sections 12 & 13. I have been posting answers on there, because I don't think it is fair that we pay for their new QuantConnect Course)
 

Thanks for reporting this. You should call can_trade directly (the one you are importing from blueshift.api, not via data.can_trade). But It seems there is a bug in the implementation of can_trade. We will release a fix soon. In the mean time, you can define the following function, and use this version in your strategy code.

 

def can_trade(asset):
    if asset.auto_close_date:
        if asset.auto_close_date >= get_datetime().date():
            return True
    return False

Do not foget to import get_datetime from blueshift.api for this to work. If you run into any issues, feel free to give us a shout.

wow, 



Thank you very much Propdita, I will also let others know, that are on the same course as me. I'll give this link URL too.



Philip