I guess you have to wrap the order_target around a function that checks how many positions you currently have.Something like...
def initialize(context):
context.max_position = 100
def my_order_target(context, security, amount):
current_position = 0
for sec in context.portfolio.positions:
sec_amount = context.portfolio.positions[sec].amount
current_position += sec_amount if sec_amount >= 0 else -sec_amount
if current_position+amount >= context.max_position:
amout = context.max_position - current_position
if amout >= 0:
order_target(security, amount)
You might also want to avoid calling the function if there are open orders pending.