Back to Community
I don't know how to exit a position after my target has been hit?

Hello, once again I am stuck and I am hoping you can help me out. I am building algo's on drops at the moment. When a particular symbol drops 2.1% I want to get long. Then when the symbol mean reverts and reaches a percent change of 0, I want to exit the trade. I am able to get in the position fine, the trouble is I can't get out of the position. I do not know how to identify when I am in the trade, to tell the IDE when to get out of the trade. So then the algo just keeps buying and never selling. I have played around with get_open_orders and get_order, but I do not know how to use them apparently. Any help is greatly appreciated,

Clone Algorithm
3
Loading...
Backtest from to with initial capital
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):
    
    #Change context . secruties to any name you want. Sid controls the symbols. so delete everything back to sid then add ( this will bring up a type box and you can put any symbol you want in there. It will then run the algo for that symbol. 
    context.securities = sid(47740)
    
    schedule_function(
        rebalance,
        date_rules.every_day(),
        time_rules.market_open(hours=1),
    )

def rebalance(context,data):
    Range = data.history(context.securities,fields='price',
                                bar_count=500,
                                frequency='1d')
    
    percent_change = Range.pct_change()
    
    print(percent_change)
    
    if percent_change[-1] < -0.021:
        order(context.securities,199)
        record(change = percent_change[-1])
There was a runtime error.
3 responses

Hello Josh,

Attached.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
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):
    
    #Change context . secruties to any name you want. Sid controls the symbols. so delete everything back to sid then add ( this will bring up a type box and you can put any symbol you want in there. It will then run the algo for that symbol. 
    context.securities = sid(47740)
    
    schedule_function(
        rebalance,
        date_rules.every_day(),
        time_rules.market_open(hours=1),
    )

def rebalance(context,data):
    Range = data.history(context.securities,fields='price',
                                bar_count=500,
                                frequency='1d')
    
    percent_change = Range.pct_change()
    
    print(percent_change)
    
    if percent_change[-1] < -0.021 and context.securities not in context.portfolio.positions:
        order(context.securities,199)
    elif percent_change[-1] >= 0. and context.securities in context.portfolio.positions:
        order(context.securities,0)
        
    record(change = percent_change[-1], leverage = context.account.leverage)
There was a runtime error.

Thank you so much!!! I knew I was over thinking it. I tried for 3 hours reading documentation yesterday and trying different things. Thank you very much!

Part of that is ok but the way to exit a position is order_target(security_object, 0) because order(security_object, 0) would make no change.
Often people will use order_target_percent(security_object, 0). For the purist (that would be me), that's an extra unnecessary step for the code to decide how many shares 0% of the portfolio represent.

https://www.quantopian.com/help#api-order-methods

order_optimal_portfolio  
order  
order_percent  
order_target  
order_target_percent  
order_target_value  
order_value