Skip to content

Section 3 - Functions

3.1 - initialize

def initialize(context):

Required function that is run on algorithm start up.

Parameters

  • context: global context class

Example

1
2
def initialize(context):
   context.SPY = 'SPY'

3.2 - schedule_function

schedule_function(function_name, daterule, date_offset, timerule, time_offset)

Schedules a function to run at the specified time. This should only be used inside the initialize function!

Parameters

  • function_name : string
  • daterule : str
  • date_offset : int
  • timerule : str
  • timeoffset : int

Date Rules

  • every_day: Runs every day
  • week_start: Runs on first tradeable day of the week
  • week_end: Runs on last tradeable day of the week
  • month_start: Runs on first tradeable day of the month
  • month_end: Runs on last tradeable day of the month

Time Rules

  • every_minute: Runs every minute
  • market_open: Runs on first tradeable minute of the day (9:31 PM EST)
  • market_close: Runs on last tradeable minute of the day (3:59 PM EST)

Example

1
2
3
4
5
6
def initialize(context):
    schedule_function('trade', daterule='every_day', 
                      timerule='market_close', time_offset=5)

def trade(context):
    print('Hello World')

3.3 - set_bechnmark

set_bechnmark(symbol)

Sets algorithm's benchmark for comparison. This should only be used inside the initialize function!

Parameters

  • symbol: str

Example

1
2
def initialize(context):
    set_benchmark('TLT')

3.4 - set_commission

set_commission(amount)

Sets the per share commission in dollars. This should only be used inside the initialize function!

Parameters

  • amount: float

Example

1
2
def initialize(context):
    set_commission(0.003)

3.5 - data.history

data.history(symbol, OHLCV, lookback)

Returns a pandas.Series of the OHLCV of symbol with the given lookback

Parameters

  • symbol: str
  • OHLCV: str
  • lookback: int

OHLCV

  • Open
  • High
  • Low
  • Close
  • Volume

Example

1
2
3
4
5
6
7
8
# Get close of current day (2019-01-02)
print(data.history('SPY', 'Close', 1))

output:

2019-01-02 PRINT - _id
2019-01-02    245.534673
Name: Close, dtype: float64

3.6 - order

order(symbol, amount)

Places order to buy/sell amount of symbol

Parameters

  • symbol: str
  • amount: int float

Example

1
2
# Places a buy order of 10 shares of SPY
order('SPY', 10)
1
2
# Places a sell order of 10 shares of SPY
order('SPY', -10)

3.7 - order_target

order(symbol, amount)

Places order on the given symbol to match amount

Parameters

  • symbol: str
  • amount: int float

Example

1
2
# Places order to make net quantity of SPY equal to 20 shares
order_target('SPY', 20)
1
2
# Places order to make net quantity of SPY equal to -20 shares, net short
order_target('SPY', 20)

3.8 - order_target_percent

order(symbol, amount)

Places order on the given symbol to match amount percent of account value

Parameters

  • symbol: str
  • amount: float

Example

1
2
# Places order to make net quantity of SPY equal to 50% of account value
order_target_percent('SPY', 0.5)
1
2
# Places order to make net quantity of SPY equal to -50% of account value
order_target_percent('SPY', -0.5)

3.9 - print

print(input)

Prints input, limited to 1024 characters

Parameters

  • input: anything

Example

1
2
3
4
5
print('Hello World')

output:

2020-01-02 PRINT - 'Hello World'

3.10 - clock.get_date

clock.get_date()

Returns datetime object of the algorithms current date

Example

1
2
3
4
5
print(clock.get_date())

output:

2020-01-02 PRINT - 2020-01-02 00:00:00