Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Scalping Algorithm - Help needed for a rookie

Gents / Ladygents,

I'm trying to create a scalping algorithm, but I'm stabbing in the dark (not much of a coding background)

I'm trying to do the following:
- Rebalance daily at open+1h
- Calculate the stddev of the last 60 minutes prices (previous days
- from this, create 2 variables
- ProfitTake = Open + stddev
- Stop = Open - stddev
- test positions
- if not held order target percent = 0.9 (stop loss = stop variable)
- and then simultaneously enter the sell order (profit take variable)

I try to do a quick back test to test the syntax, but it just spools and then I need to cancel the
What am I doing wrong?

for sure I'm missing something obvious, but any tips would be great

def initialize(context):

context.SPY=symbol('SPY')

# Rebalance every Monday (or the first trading day if it's a holiday)  
# at market open.  
schedule_function(rebalance,date_rules.every_day(), time_rules.market_open(minutes=60))

def rebalance(context, data):

# Get the 30-day price history for each security in our list.  
SPYHist=data.history(context.SPY,"price",60,"1m")  

# standard deviation  
std_SPY = SPYHist.std()  

SPYStop=data.history(context.SPY, "open",1,"1d")-std_SPY  
SPYProfitTake=data.history(context.SPY, "open",1,"1d")+std_SPY  

# check no open positions  
MyPositions = (context.portfolio.positions[context.SPY].amount  
if data.can_trade(context.SPY):  
    if MyPositions == 0:  
        order_target_percent(context.SPY, 0.9, style=StopOrder(SPYStop))  
    elif MyPositions > 0:  
        order_target_percent(context.SPY, 0, style=LimitOrder(SPYProfitTake))  
2 responses

The 'history' method in this case returns a Pandas series. So, SPYStop and SPYProfitTake in the two lines of code below will be pandas series objects and not, as I believe you intended, a scalar value.

# SPYStop and SPYProfitTake will be pandas series objects  
SPYStop = data.history(context.SPY,  "open", 1, "1d") - std_SPY  
SPYProfitTake = data.history(context.SPY,  "open", 1, "1d") + std_SPY  

There are a number of ways to get a specific value in a series but the 'iat' method provides a fast integer based lookup (http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.iat.html). So something like this will work.

# Get todays open as a scalar value  
todays_open = data.history(context.SPY, "open",1,"1d").iat[0]  


# Calc order prices  
SPYStop = todays_open - std_SPY  
SPYProfitTake = todays_open + std_SPY  

Attached is a backtest with this change.

@dan thanks, ill have a play around

ever played with this sort of strategy?