Back to Community
How to execute order_target_percent every 30 minutes while closing all the positions every 30 minutes?

Essentially, every 30 minutes, a custom function that calculates and outputs a list of weights is executed. Then that list of weights would be used for the target percentage of the corresponding securities. Then "wait" for 30 minutes, then close all the positions, then repeat. So in practice would be to open the positions, wait for 30 minutes, then close all the positions, then immediately find the new optimal target percentages and repeats.

My understanding is that if I were to include "order_target_percent" in the handle_function, then it would be executed every minute, thus would be near constantly rebalancing the weights which is not what I'm looking for.

How do people approach this?

I was thinking of declaring a global variable such that for every time handle function is executed, that global variable will increment by 1 and if it's divisible by say 30, then it executes the custom functions. But how would it actually execute the custom functions tho?

as far as I know, there is no feature for that for schedule_function

5 responses
for i in range(1,300,60):  
        schedule_function(open_positions, date_rules.every_day(), time_rules.market_open(minutes=i))  
        schedule_function(close_positions, date_rules.every_day(), time_rules.market_open(minutes=(i+30)))  

Okay, this is so frustrating. I don't think I struggled this much when learning C++ for the first time.

I've tried tweaking every bit of changes to the code and literally just to see if this general approach would work and I get a syntax error .....

without even telling me what the syntax error is

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume  
from quantopian.pipeline.filters import Q1500US 


def initialize(context):  
    context.securities = [sid(24),sid(114)]  
    for i in range(1,300,60):  
        schedule_function(open_positions,data_rules.every_day(),time_rules.market_open(minutes=i))  
        schedule_function(close_positions,data_rules.every_day(),time_rules.market_open(minutes=(i+30))  

def open_positions(context, data):  
    for i in context.securities:  
        order_target_percent(i,0.2)

def close_positions(context, data):  
    for i in context.securities:  
        order_target_percent(i,0)  

You were missing a bracket on the second schedule_function.

schedule_function(close_positions,data_rules.every_day(),time_rules.market_open(minutes=(i+30))  

Should have been:

schedule_function(close_positions,data_rules.every_day(),time_rules.market_open(minutes=(i+30)))  
Clone Algorithm
7
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
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume  
from quantopian.pipeline.filters import Q1500US 


def initialize(context):  
    context.securities = [sid(24),sid(114)]  
    for i in range(1,300,60):
        schedule_function(open_positions, date_rules.every_day(), time_rules.market_open(minutes=i))
        schedule_function(close_positions, date_rules.every_day(), time_rules.market_open(minutes=(i+30)))

def open_positions(context, data):  
    for stock in context.securities:
        order_target_percent(stock, 0.2)
                          

def close_positions(context, data):  
    for stock in context.securities:  
        order_target_percent(stock,0)  
There was a runtime error.

Why would you want to close all positions, only to buy most of the same stocks back?

Just send order_target_percent with the new weights every 30 minutes, with those you want to liquidate set to 0. The engine will calculate the difference between what you want and what you have, and order accordingly.

That makes sense. So simply requesting for order_target_percent with the new weights every 30 minutes gives the same result whether I liquidate all the positions every 30 minutes or not.

But in this specific example, on backtesting, it's suppose to buy AAPL and ADBE with target percent 20% for each every 30 minutes. But for ADBE, it buys fractions of 20% for ADBE every minute for 30 minutes until roughly the target percent of 20% is met.

I understand that in practice, if you request for X number of shares, some fractions of it might go through at different times, but is there a way to circumvent this? I just want to test my strategy first before applying whether it's practical or not.