Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help needed to cancel stop/limit accordingly!

Hi,

I have this execution strategy which takes into account two stocks. What i want to achieve is, if the price reaches the limit then the stop order gets canceled for the relevant stock and vice versa.

What I have currently is if one position reaches the limit or stop, it cancels all orders and leaves one open position.

Any help will be much appreciated.

Thanks

2 responses

Hi All,

I updated it to cancel orders, but i get the error that the object has no attribute status.

Any help pls!

entry_level = [115.25, 7.84]
position = ['long', 'short']

def initialize(context):
"""
Called once at the start of the algorithm.
"""
context.stock = [sid(14328), sid(7244)]

schedule_function(First_Bar, date_rules.every_day(),  
                 time_rules.market_open(minutes=3))  
schedule_function(Second_Bar, date_rules.every_day(),  
                 time_rules.market_open(minutes=6))  
context.dic = {}  
context.id1=None  
context.id2 =None  
context.id3=None  

def First_Bar(context, data):

Price = data.current(context.stock, 'price')  

'''  
3min max high and low.  
'''  

max_high = data.history(context.stock, 'high', 3, '1m').max()  

min_low = data.history(context.stock, 'low', 3, '1m').min()  


'''  
difference in max high and min low will be key_price.  
'''  
if position[0] == 'long':  
   if Price[0] <= entry_level[0] and Price[0] >= min_low[0]:  
            context.id1 = order(context.stock[0], 100)  
            log.info('im buying after first bar')  
            context.id2 = order(context.stock[0], -100, limit_price = Price[0] + 0.50)  
            context.id3 = order(context.stock[0], -100, stop_price = min_low[0] - 0.20)  


if position[1] == 'short':  
   if Price[1] >= entry_level[1] and Price[1] <= max_high[1]:  
         order(context.stock[1], -100)  
         log.info('im selling after first bar')  
         order(context.stock[1], 100, limit_price = Price[1] - 0.20)  
         order(context.stock[1], 100, stop_price = max_high[1] + 0.20)  


record(leverage = context.account.leverage)  

def Second_Bar(context, data):

Price = data.current(context.stock, 'price')  

max_high = data.history(context.stock, 'high', 3, '1m').max()  

min_low = data.history(context.stock, 'low', 3, '1m').min()

current_positions1 = context.portfolio.positions[context.stock[0]].amount  
current_positions2 = context.portfolio.positions[context.stock[1]].amount  

if position[0] == 'long':  
  if current_positions1 == 0:  
      if Price[0] <= entry_level[0] and Price[0] >= min_low[0]:  
            order(context.stock[0], 100)  
            log.info('im buying after second bar')  
            order(context.stock[0], -100, limit_price = Price[0] + 0.50)  
            order(context.stock[0], -100, stop_price = min_low[0] - 0.20)  

if position[1] == 'short':  
  if current_positions2 == 0:  
      if Price[1] >= entry_level[1] and Price[1] <= max_high[1]:  
            order(context.stock[1], -100)  
            log.info('im selling after second bar')  
            order(context.stock[1], 100, limit_price = Price[1] - 0.50)  
            order(context.stock[1], 100, stop_price = max_high[1] + 0.20)  

def handle_data(context, data):
# Do nothing if there are open orders:

context.dic['ALXN-open'] = get_order(context.id1)  
context.dic['ALXN-limit'] = get_order(context.id2)  
context.dic['ALXN-stop'] = get_order(context.id3)  
print context.dic  

if context.dic['ALXN-limit'].status == 1 :  
    cancel(context.dic['ALXN-stop'])  

if context.dic['ALXN-stop'].status == 1:  
    cancel(context.dic['ALXN-limit'])