Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pivot Point Algorithm

Hi. I'm new to Python and dipping my toes. Tried to port a system I run, but not winning really. It is a system that trades daily and LONG or SHORT and holds the position only for 2 days then closes out. Only one trade is also allowed at a time.

Here is what I have at the moment.

""" This is a template algorithm on Quantopian for you to adapt and fill in.
""" from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
import numpy as np

def initialize(context):
# AAPL, MSFT, and SPY
context.securities = [sid(8554)] #sid(24), sid(5061),
context
#: Set commissions and slippage to 0 to determine pure alpha
set_commission(commission.PerShare(cost=0, min_trade_cost=0))
set_slippage(slippage.FixedSlippage(spread=0))
schedule_function(
func=close_all_positions,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(hours=0, minutes=5),
half_days=True)

def handle_data(context, data):
price_history_close = data.history(context.securities, "close", bar_count=10, frequency="1d")
price_history_low = data.history(context.securities, "low", bar_count=10, frequency="1d")
price_history_high = data.history(context.securities, "high", bar_count=10, frequency="1d")

for s in context.securities:  
    current_positions = context.portfolio.positions  
    if current_positions != 0:  
        hist_bar = (price_history_close[s][-2] + price_history_low[s][-2] + price_history_high[s][-2])/3  
        prev_bar = (price_history_close[s][-3] + price_history_low[s][-3] + price_history_high[s][-3])/3  
        if hist_bar < prev_bar:  
            order(s, 1)

def close_all_positions(context, data):
for s in context.securities:
current_positions = context.portfolio.positions
if current_positions < 1:
order_target_percent(s, 0)
log.info('cover before the close of the day')