Can anyone provide a link to an algorithm for inside day?
Can anyone provide a link to an algorithm for inside day?
@Sol M., Here's a start.
This code is more than you'd expect, but it gives you a place to extend your logic from.
And in order to test for multiple entries I included a simple period stop.
Also note that it uses the new context.S[stock].DynamicProperty which should offer system restart continuity if traded live.
Notice that the inside day logic is self contained. And you can specify a single or multiple consecutive inside days (shark tooth). No doubt there are functional ways to condense this code, but for someone just starting, laying out the code in an obvious fashion aids in comprehension.
MT
def CalcInsideDay(context, data):
# A candlestick formation that occurs when the entire daily price range
# for a given security falls within the price range of the previous day.
highDeck = history(InsideDayCount, "1d", "high").dropna(axis=1)
lowDeck = history(InsideDayCount, "1d", "low").dropna(axis=1)
highDeck = highDeck[[sid for sid in highDeck if sid in data]]
lowDeck = lowDeck[[sid for sid in lowDeck if sid in data]]
for stock in context.S:
# Assume false, we'll mark true if the conditions pass the test
context.S[stock].IsInsideDay = False
isInsideDayTestFailed = False
for i in xrange(1, InsideDayCount):
if (highDeck[stock].iloc[-i] > highDeck[stock].iloc[-(i + 1)]
or
lowDeck[stock].iloc[-i] < lowDeck[stock].iloc[-(i + 1)]):
isInsideDayTestFailed = True
break
# No test failure, condition is true, inside day(s) exist
if (not isInsideDayTestFailed):
context.S[stock].IsInsideDay = True
I truly appreciate but I find this code difficult to understand for my level of programming. It is not intuitive for traders with some programming knowledge. It is more for programmers with some trading knowledge.:)
@Sol M., I suspected as much.
So, use the bits that make sense and get rid of those that don't.
The inside day uses 4 prices, 2 highs and 2 lows. The history function is how you'd fetch those.
Everything else you can throw away.
But of course, eventually, you'll end up back here as you won't be able to trade without much of this.
Try this then:
def initialize(context):
context.SPY = symbol("SPY")
def handle_data(context, data):
highDeck = history(2, "1d", "high").dropna(axis=1)[context.SPY]
lowDeck = history(2, "1d", "low").dropna(axis=1)[context.SPY]
if (highDeck.iloc[-1] < highDeck.iloc[-2] and lowDeck.iloc[-1] > lowDeck.iloc[-2]):
order_target_percent(context.SPY, 1)