Constant Rebalance Via Trigger - Simple Question

Hi, How one goes about setting up a constant rebalace of a given sid or sids that is controlled by some other condition, Ie:
if value X is above value Y, close open position on security N and reopen the same position on the same session (harvesting any gain/loss), repeat the process the next day (given the X above Y setup is still in place)

i could do that with a schedule function (by just scheduling a recheck few minutes after the first check) but theres probably a more intelligent way

Thank you

4 responses

I am not sure I understand, you want to completely sell a position in N, and then repurchase it right away?

Thanks Simon for getting back - Correct: I attached a simple example of the rebalance logic, not sure if its executing properly though, meaning it should rebalance on the same day (within the same schedule function)

3
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 talib

def initialize(context):
context.spy = symbol('SPY')
context.switch = {}

schedule_function(orderlogic,
date_rules.every_day(),
time_rules.market_close(hours=0, minutes=3))

def orderlogic(context, data):
spypos = context.portfolio.positions[context.spy].amount

h = history(200, "1d", "close_price")
t = h[context.spy]
maveragea = talib.SMA(t, 5)[-1]
maverageb = talib.SMA(t, 10)[-1]

# Setting the ma conditions here
if maveragea > maverageb:
context.switch = True

if maveragea < maverageb:
context.switch = False

# if ma’s are set in this way, then constantly rebalance the long position
if context.switch == True:
# removing the exisitng position
order_target_percent(context.spy, 0)
# starting a new pos in the same direction
order_target_percent(context.spy, 0.5)

# if ma’s are set in this way, then constantly rebalance the short position
if context.switch == False:
order_target_percent(context.spy, 0)
order_target_percent(context.spy, -0.5)

record(Position=spypos, maverageA=maveragea, maverageB=maverageb)

def handle_data(context, data):
pass
There was a runtime error.

Hi Darell,

Your algo builds leverage up to x2.88 or so.

To rebalance you don't need to sell everything and order it back again. order_target_percent adjusts the position to your requested fraction. However, it does not taken into account open orders when calculating the quantity of shares, so two calls for the same sid messes it up completely.

See if the following code helps:

3
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 talib

def initialize(context):
context.spy = symbol('SPY')
context.switch = {}

schedule_function(orderlogic,
date_rules.every_day(),
time_rules.market_close(hours=0, minutes=3))

def orderlogic(context, data):
spypos = context.portfolio.positions[context.spy].amount

h = history(200, "1d", "close_price")
t = h[context.spy]
maveragea = talib.SMA(t, 5)[-1]
maverageb = talib.SMA(t, 10)[-1]

# Setting the ma conditions here
if maveragea > maverageb:
context.switch = True

if maveragea < maverageb:
context.switch = False

# if ma’s are set in this way, then constantly rebalance the long position
if context.switch == True:
if not get_open_orders(context.spy):
order_target_percent(context.spy, 0.5)

# if ma’s are set in this way, then constantly rebalance the short position
if context.switch == False:
if not get_open_orders(context.spy):
order_target_percent(context.spy, -0.5)

#record(Position=spypos, maverageA=maveragea, maverageB=maverageb)
record(leverage=context.account.leverage)

def handle_data(context, data):
pass
There was a runtime error.

Thanks for getting back Jack, one follow up question:
I dont see the Sell order in the code, its does it by itself via get_open_orders ?

thank you