Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bull Flag Breakout

I have some code for a thinkorswim study I've found on a forum somewhere, I can't remember where it was, but I believe the author was "TheStudentLoanRanger". Basically it looks for bull flags, signals the period to buy in (no definitive buy price, it just flags a candle on the chart showing a buy signal) and calculates a stop loss and profit target (these values adjust as the candle moves). I've manually back-tested this looking through charts that threw signals but could not get enough data to draw any conclusions. Thinkorswim will only allow me to look back x amount of candles on a given aggregation period so I'd like to set this up with Quantopian and see what sort of results I get.

The algo would need to go through every equity with an average daily volume of at least 500k over the last 10 days. At the end of every day it would check to see if a bull flag was recognized. If somebody with knowledge of both thinkscript and Quantopian algo programming could help me with this it would be much appreciated. Thanks!

1 response

The algo is already written in thinkscript fyi, I just need it ported over to python. I started with it but it's a little out of my league at the moment as python is brand new to me. The code is below:

Input and basic definitions

input pChg = 1.50; # Daily minimum percent increase during expansion phase (def=1.50)

def C = close;
def O = open;
def H = high;
def L = low;
def V = volume;

isHigh is to stop a plotting issue inertia code, don't touch it!

def isHigh = C > 20000;

Expansion range price and volume

def XRANGE = C[3] - O[5];
def XRANGEH = H[3] - O[5];
def XVOL = V[5] + V[4] + V[3];

Define average true ranges and amount above

def atr = Average(TrueRange(high, C, low), 1.0);
def EXPANDER = atr > (Average(atr) * 1.10);
def EXPNDR = if EXPANDER[5] && EXPANDER[4] && EXPANDER[3] then 1 else 0;

Percentage change characteristics

def PercentChg = Round(100 * (C / C[1] - 1), 3);
def PCCOUNT = if (PercentChg[5] > pChg && PercentChg[4] > pChg && PercentChg[3] > pChg)
or Round(100 * (C[3] / C[5] - 1), 3) > 10.0 then 1 else 0;

Higher than average V characteristics

def Volavg = if (V[5] + V[4] + V[3]) / 3 > Average(V) * 1.10 then 1 else 0;

Price action characteristics

def CandleAction = (C[5] > O[5] && C[4] > O[4] && C[3] > O[3]);
def Signal = if EXPNDR == 1 && Volavg == 1 && CandleAction && PCCOUNT then 1 else 0;

3-day consolidation settings

def CRANGE = Highest(H, 3) - Lowest(L, 3);
def UPDRIFT = Highest(H, 3) < (H[3] + (XRANGEH * 0.25));
def CONVOL = V[0] + V[1] + V[2];

Ratio calculations

def XCRATIO = XRANGE / CRANGE;
def XVRATIO = XVOL / CONVOL;

Confirmations and plot formatting

plot BULL = if XCRATIO > 2 && XVRATIO > 1.20 && UPDRIFT && Signal == 1 then 1 else 0;
BULL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BULL.SetDefaultColor(Color.LIME);
BULL.SetLineWeight(2);
AssignPriceColor(if BULL then Color.BLUE else Color.CURRENT);

plot STPPRICE = If(BULL and C > 0, (C[3] - (XRANGE * 0.40)), If(C < 0 and !BULL , (C[0] + (XRANGE * 0.95)), Double.NaN));
STPPRICE.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
STPPRICE.SetDefaultColor(Color.RED);
STPPRICE.SetLineWeight(2);

plot TGTPRICE = If(BULL and C > 0, (C[0] + (XRANGE * 0.90)), If(C < 0 and !BULL , (C[0] - (XRANGE * 0.40)), Double.NaN));
TGTPRICE.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TGTPRICE.SetDefaultColor(Color.GREEN);
TGTPRICE.SetLineWeight(2);