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))