Back to Community
Using schedule_function every day BUT mondays. Possible?

I know that I can launch a function every day once the market has started and 1 minute has passed (having a full 1 minuto candlestick):

schedule_function(my_function,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open(hours=0, minutes=1))  

But what if I wanted to launch the function every day but mondays? Is it possible to combine two conditions somehow?

My target here is to launch the function every day but mondays, first tradeable day of the month and monthly expiration friday (3rd of the month, usually).

Any easy way to achieve this?

EDIT: I asume I can always check current simulation datetime in my_function, but would be lovely to achieve it in an elegant, pythonic way at the schedule_function call.

7 responses

Would something like this work?

Clone Algorithm
40
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    context.SPY=sid(8554)
    
          
    
def handle_data(context,data):
    pass


def rebalance(context, data):
    if get_datetime().dayofweek == 0: # 0 is Monday in Pandas
        print("----------- It is Monday!")
        return

    print("Not Monday!")

There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Sure, I used get_datetime().date().dayofweek, but of course it works. Just saying it would be cleaner and more elegant if we could do this with a complex schedule_function call

For every day BUT mondays you can combine four schedule_function calls

Clone Algorithm
3
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    schedule_function(my_function,  
                      date_rule=date_rules.week_start(days_offset=1),  
                      time_rule=time_rules.market_open(minutes=1))  
    schedule_function(my_function,  
                      date_rule=date_rules.week_start(days_offset=2),  
                      time_rule=time_rules.market_open(minutes=1))  
    schedule_function(my_function,  
                      date_rule=date_rules.week_start(days_offset=3),  
                      time_rule=time_rules.market_open(minutes=1))  
    schedule_function(my_function,  
                      date_rule=date_rules.week_start(days_offset=4),  
                      time_rule=time_rules.market_open(minutes=1))  

    
    context.SPY=sid(8554)
    
def my_function(context,data):
    d=get_datetime().dayofweek 
    log.info(str(d))

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    pass
There was a runtime error.

Well, this is an option too. But I am also looking for a way of avoiding first tradeableday of the mont, which would mean 30-31 rules if done this way.

Bring it on Roman.

Clone Algorithm
1
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np

down_months = [9]
down_days = [4,5,6,7,14,16]
down_weekday = [0]

_U = 1.0
_D = 0

def initialize(context):
    context.SPY=sid(8554)
    #for trade_day in np.arange(1,4,1):
    schedule_function(myfunc,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open(minutes=1))
        
def myfunc(context,data):
    dayofweek = get_datetime().dayofweek 
    today = get_datetime().day
    tomonth = get_datetime().month
    weight = _U
    if today in down_days or dayofweek in down_weekday or tomonth in down_months:
        weight = _D
    order_target_percent(context.SPY,weight)
    log.info('m %s d %s, %s ' % (tomonth,today,dayofweek))

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    pass












There was a runtime error.

Ok, this is a harder one:

Avoid monthly expiration day. I have achieved skipping it if it is the third friday of the month in a similar way to your last example, but we all know this is not always the case due to holidays. So, any idea to get the correct monthly expiration day?