go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Object Data
 
Subject: Object Data
Author: WebSpider
In response to: The Basic Objects and Functions
Posted on: 05/02/2019 10:24:53 PM

data.current(assets, fields)

This method returns the current value of the given assets for the given fields at the current algorithm time.

Parameter fields:

  • 'price' -- returns the last known close price, NaN if not founded
  • 'last_traded' -- returns the date of the last trade event
  • 'open' -- return the relevant information for the current trade bar
  • 'high' -- return the relevant information for the current trade bar
  • 'low' -- return the relevant information for the current trade bar
  • 'close' -- return the relevant information for the current trade bar
  • 'volume' -- returns the trade volume, 0 if there is no trade this minute
  • 'contract' -- returns the current active contract

    Example:
            price = data.current(symbol('AAPL'), 'price');
    



    data.history(assets, fields, bar_count, frequency)
    This method returns a window of data for the given assets and fields.

    Parameter fields:
  • 'price' -- returns the last known close price, NaN if not founded
  • 'open' -- return the relevant information for the current trade bar
  • 'high' -- return the relevant information for the current trade bar
  • 'low' -- return the relevant information for the current trade bar
  • 'close' -- return the relevant information for the current trade bar
  • 'volume' -- returns the trade volume, 0 if there is no trade this minute

    Parameter frequency:
  • '1m' -- for minutely data
  • '1d' -- for daily data

    Examples (DAILY):
        # returns the current price
        price_history = data.history(assets, "price", 1, "1d"); 
    
        # returns yesterday's close price and the current price
        price_history = data.history(assets, "price", 2, "1d");
    
        # returns the prices for the previous 5 days and the current price
        price_history = data.history(assets, "price", 6, "1d");
    
        # returns the volume since the current day's open, even if it is partial
        price_history = data.history(assets, "volume", 1, "1d"); 
    


    Examples (MINUTELY):
        # returns the current price
        price_history = data.history(assets, "price", 1, "1m"); 
    
        # returns the previous minute's close price and the current price
        price_history = data.history(assets, "price", 2, "1m"); 
    
        # returns the prices for the previous 5 minutes and the current price
        price_history = data.history(assets, "price", 6, "1m");
    
        # returns the volume for the previous 60 minutes
        price = data.history(assets, "volume", 60, "1m");
    


    Example -- Up or down compared to previouse day's close price?
    def initialize(context):
        # AAPL, MSFT, and SPY
        context.securities = [sid(24), sid(5061), sid(8554)]
    
    def handle_data(context, data):
        price_history = data.history(context.securities, fields="price", bar_count=2, frequency="1d")
        for s in context.securities:
            prev_close = price_history[s][-2];
            curr_price = price_history[s][-1];
            if curr_price > prev_close:
                print(s, prev_close, curr_price, ' --> UP');
            else:
                print(s, prev_close, curr_price, ' --> DOWN');
    


    Example -- VWAP (Volume Weighted Average Price)
    def initialize(context):
        # AAPL, MSFT, and SPY
        context.securities = [sid(24), sid(5061), sid(8554)]
    
    def vwap(prices, volumes):
        return (prices * volumes).sum() / volumes.sum()
    
    
    def handle_data(context, data):
        hist = data.history(context.securities, fields=["price", "volume"], bar_count=30, frequency="1d")
    
        vwap_10 = vwap(hist["price"][-10:], hist["volume"][-10:])
        vwap_30 = vwap(hist["price"], hist["volume"])
    
        for s in context.securities:
            if vwap_10[s] > vwap_30[s] :
                order(s, 100)
            elif vwap_10[s] < vwap_30[s] :
                order(s, -10)
    


    Other data's methods:
  • data.can_trade(assets) -- For the given asset or iterable of assets, returns true if the security has a known last price
  • data.is_stale(assets) -- For the given asset or iterable of assets, returns true if the asset has ever traded and there is no trade data for the current simulation time
     

    > On 05/02/2019 12:28:10 AM WebSpider wrote:

    Object context:

    context is a Python dictionary used for maintaining state by attaching any property:
        context.some_property = some_value
    



    Object assets:

    assets is a Asset or a list of Assets which represents security.

    Find security asset by name:
          assets = symbol('AAPL')
          assets = [symbol('AAPL'), symbol('MSFT')]
          assets = symbols('AAPL', 'MSFT')
    


    Find security asset by id:
          # AAPL
          assets = sid(24)  
          # AAPL, MSFT, and SPY
          assets = [sid(24), sid(5061), sid(8554)]  
    






    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.