Scalper Buys and Sells

Posted by on Feb 5, 2009 in Indicator, Support and Resistance | 38 comments

Here’s my incarnation of John Carter’s Scalper Buys and Sells. The methodology is a close approximation of the one described in his book Mastering the Trade. The book is highly recommended. Note the squares are not real-time but will show up once the third bar has confirmed a reversal. (Note: The first posting of this code had a line missing.)

Scalper Buys and Sells

Scalper Buys and Sells

# Last Update 04 FEB 2009

def triggerSell = if(if(close[-1] < close,1,0) and (close[-2] < close[-1] or close[-3] <close[-1]),1,0);
def triggerBuy = if(if(close[-1] > close,1,0) and (close[-2] > close[-1] or close[-3] > close[-1]),1,0);

rec buySellSwitch = if(triggerSell, 1, if(triggerBuy, 0, buySellSwitch[1]));

def thirdBarClosed = if(isNan(close[-3]),0,1);

plot SBS = if(triggerSell and thirdBarClosed and !buySellSwitch[1], high, if(triggerBuy and thirdBarClosed and buySellSwitch[1], low, double.nan));


  1. This is a very interesting indicator. Realtime would of course be excellent, but that would be impossible.

    What happens if you use 2 bars instead of 3?

    Could you post the code for that so I could do some testing?

    Thanks for all your work!

  2. Thanks for the mail. I let you know what i see.

    One question I have, which might add some clarity to this chart, is when the signal shows.

    Lets say I’m using a “day” candlestick chart. On Monday night I look at the chart and I see that the stock had been trending down, yet today, Monday, the day started normal then by mid day it was far below the lower BB. By the end of the day it was back up again.

    For the next 4 days, the stock makes “normal” gains each day.

    Which day of the week, and at what time during the day would the signal appear below the Monday candle showing a buy signal?


  3. I have an idea to make this script a little more useful from a timing perspective. Is there a way to make look at multiple time frames, for example, evaluate a 1 minute bar time, a 5 minute bar time, and 15 minute bar time to produce a combined signal?

    For example, once 3 1 minute bars have passed to create a signal, you would get a weak “buy”, or sell, depending on which way you’re going, then if after 3 5 minute bars if the same trend has continued, it would “add” to the signal by making stronger, then the same would hold true for the 15 minute bar. The goal would be to have an indicator that travels along the price bars and changes color depending on the combined 1, 5, and 15 minute bars.

    Does this make sense?

    Thanks for your work you post, btw!

    Also, can you point me into a direction to learn thinkscript? The documentation they provide is rather slim, and trying to hack scripts isn’t very educational because I really don’t understand the syntax to begin with.

    Thanks again.


  4. Greetings ThinkScripter:

    Could you post the modification to make the signal work on two bars?



  5. I got very intrigued by this, so I did setup 8 boxes to watch different time frames. I agree 100% with Greg’s idea above…it could be quite useful!! Also, I too would like to know what you find/found with less lookback (do I understand correctly that it uses 3 bars now)? THANKS A MILLION, BEST ONE I THINK.

  6. Great Job. I modified your script to add the following ..
    1) When the second bar shows the reversal, the color white is painted
    2) the third bar code is the same as what you have except color changes to green.
    Could you verify to see if the logic is right?

    def triggerSell;
    def triggerBuy;
    def BarClosed;
    rec buySellSwitch;

    def isBarThree = if(isNan(close[-3]),0,1);

    if isBarThree {

    triggerSell = if(if(close[-1] < close,1,0) and (close[-2] < close[-1] or close[-3] close,1,0) and (close[-2] > close[-1] or close[-3] > close[-1]),1,0);
    buySellSwitch = if(triggerSell, 1, if(triggerBuy, 0, buySellSwitch[1]));
    BarClosed = if(isNan(close[-3]),0,1);
    } else {
    triggerSell = if(if(close[-1] < close,1,0) and (close[-2] close,1,0) and (close[-2] > close[-1] ),1,0);
    buySellSwitch = if(triggerSell, 1, if(triggerBuy, 0, buySellSwitch[1]));
    BarClosed = if(isNan(close[-2]),0,1);

    plot SBS = if(triggerSell and BarClosed and !buySellSwitch[1], high, if(triggerBuy and BarClosed and buySellSwitch[1], low, double.nan));
    SBS.setDefaultColor(if(isBarThree,color.Green, color.White);

    • Batra

      Have you gotten this to work? I have been using the TTM scalper with great success. Do you know how to add alert???

  7. Batra,
    Well….Your syntax looks incorrect and when I pasted that code into ToS and it pitched a fit. Was this code working for you?

  8. eric:

    the last line was creating trouble..and am no expert on thinkscript..I just took your code and tried to fit the new logic..If you have a moment and can fix will be great

  9. Hmmm…well, the last line is missing a parentheses and I don’t believe you can use setDefaultColor with more than one possible value. You must use assignValueColor.

  10. Hi Eric,

    Here’s a similar study that I found from a guy named Joshua Corum several months ago… He calls it the Gatsby Indicator. It is a 3 bar study too… and throws signal dots (red and green). His website is
    ” ”

    I don’t know if this will look right below… but here it is:

    input signalOffsetFactor = 0.20;
    def signalOffset = AvgTrueRange(high, close, low, 9) * signalOffsetFactor;
    plot Data = hlc3;
    def triggerSell = if(if(close[-1] < high, 1, 0) and (hlc3[-2] < close[-1] or hlc3[-3] low, 1, 0) and (hlc3[-2] > close[-1] or hlc3[-3] > close[-1]), 1, 0);
    rec buySellSwitch = if(triggerSell, 1, if(triggerBuy, 0, buySellSwitch[1]));
    def thirdBarClosed = if(IsNaN(hlc3[-3]), 0, 1);
    plot SBS = if(triggerSell and thirdBarClosed and !buySellSwitch[1], high + signaloffset, if(triggerBuy and thirdBarClosed and buySellSwitch[1], low – signaloffset, double.nan));
    # SBS.SetDefaultColor(color.white);

    SBS.AssignValueColor(if triggerSell then
    if thirdbarclosed then
    CreateColor(255, 0, 0) else
    CreateColor(255, 0, 0)
    else if Triggerbuy then
    CreateColor(0, 255, 0) else
    CreateColor(0, 255, 0));

    • I forgot to mention that the Gatsby seems to work on tick charts too…

      • Thanks for the addition Jeff. I’m always pleased to see “Hot-Rodded” versions of my code…It would be nice if every now and then a “Based on the indicator at” was thrown in the header but I’m not holding my breath there…More scripts are better for everyone – no complaints from me on that.

  11. ….this one should be simple

    …. how about an indicator which simply indicates that the current bar is not complete…. until it closes

    …. for example…. the current bar flashes…. or is highlighted…. while all completed bars appear normal….

    …. reason for the request….. how many times have you made a trade based on the current bar….. forgetting that it is not complete….
    …. only to watch your trade appear less desirable as the current bar finishes much differently than how you expected it to finish

    • Try this. The final bar is yellow until the next bar appears (i.e. the bar has closed).

      plot null = double.nan;
      assignPriceColor(if isNan(close[-1]) then color.yellow else color.current);

  12. Here’s a little tweak I made to your code that filters out some of the extra trades. I like fewer indications with higher probability. I also made the dot smaller and magenta and print the dot further from the high/low for visibility. Note: this version prints the dot on the active bar, not 3 bars back (another preference of mine).

    # (c) 2009
    # Last Update 04 FEB 2009

    def range=high-low;
    def triggerSell = (close < close[1]) and (close< low[1]) and (close[1] < close[2]) and (close[2] close[1]) and (close > high[1]) and (close[1] > close[2])and (close[2] >= close[3]);

    rec buySellSwitch = if(triggerSell, 1, if(triggerBuy, 0, buySellSwitch[1]));

    def thirdBarClosed = if(isNan(close[-3]),0,1);

    plot SBS = if(triggerSell and thirdBarClosed and !buySellSwitch[1], high+range*0.5, if(triggerBuy and thirdBarClosed and buySellSwitch[1], low-range*0.5, double.nan));

    • Hi Eric, this is gonna sound weird, but I just noticed that the version of my code modification (that is waiting for moderation) has a line deleted….?

      Here’s the line, let’s see if it comes thru in this comment:

      def triggerBuy = (close > close[1]) and (close > high[1]) and (close[1] > close[2])and (close[2] >= close[3]);

      If there is not line of code above this line, then the gremlins have struck again… Not sure what to make of it. I noticed several other posts have the triggerBuy line of code missing as well.

      The code listed as “waiting for moderation” is not what I pasted into the submit window. Hmmm…?

      • Glenn,

        The def triggerSell statement is highlighted red when I paste it into ToS but I can’t see what the error is. Although I’m not a ThinkScript expert, there appears to be something missing in that statement. At the end of the first line, should there be an operator between close[2] and close[1]? The clause “and (close[2] close[1])” looks suspicious.

        I pasted in your missing “def triggerBuy” line and got no error on that one.

        Thanks in advance for checking this out.

        • Make sure you don’t try to manually copy and paste this. There are icons in the upper right of the code window for this. The actual code is correct.

  13. Hi Eric,

    Just recently discovered you great site and will be making a donation. A couple of comments on the Scalping script:

    According to Carter, we should only be concerned with the current bar and the next 2 bars ([-1] and [-2]). Your script seems to go out another bar [13]. Also, the 3 bars should have consecutive higher or lower closes, hence I don’t understand the “or’s” in the if statements that define the buy and sell switches.

    Finally, I have noticed that the indicator doesn’t pick up the first occuraance if it is a buy signal. I think this is because the buySellSwitch [1] may not be defined at this time. I am a newbee at this so I may be off base.

    Thanks again for the site.


  14. i pasted the script and it doesnt work, there is an issue with def the buy/sell signal or such, anyone work it out who would be willing to share?

    • Stewart,
      My original code above works fine as I just tried cutting and pasting myself. Are you referring to one of the user variations posted here?

  15. Please ensure that you use the icons in the upper right of the code window to copy/paste the code. The second icon copies the code straight to your clipboard. If you try to do this manually you will include the line numbers and the code will not work in TOS.

  16. this is really a good indicator, any idea if we can scan the stocks in TOS using this indicator using the scan tab???

    • This indicator is not real-time and cannot be used in scans. A full description of its use is available in Carter’s book.

  17. Hey Gang need some help. I have been using the John Carter TTM Scalper in TOS and it works great for longer interday plays as well as scalps. I have been trading crude. It has to be mixed with Market Profile or the Monkey Bars. When they both line up it deadly.

    I have plowed through the thread and haven’t found what I looking for. I would like to have the bar that throws signal marked as well as an alert added. I know this is possible but have no clue how to code it.

    Thanks in advance…

    • The forum is a much better place to ask questions.

    • dahlberg,

      I’m interested in the mixing of TTM Scalper and Market Profile or Monkey Bars. Can you give an example of this? Perhaps the forum is a better place for this types of interaction but I don’t know how to search by username. Unless you post something and let me know here where you put it (title name), I won’t be able to find it or even know you posted something.

      Thank you

  18. I did this the way you said to paste the code… “Please ensure that you use the icons in the upper right of the code window to copy/paste the code. The second icon copies the code straight to your clipboard.”

    But I get an error message that says “Invalid statement: rec usage is not allowed in this context.”

    Do you have any suggestions on what I’m doing wrong?

    Your stuff is awesome, by the way.

    • This is a study only. If you are trying to use the code elsewhere it will not work. The rec command itself has also been deprecated.

  19. Hi – Looks like I am about 3 years late on this chain! I just very recently have started looking at both TTM Scalper Alert and the Scalper Buy and Sells above. I loaded both in ToS, and watched them on 1 minute charts to see how they respond. What I find disturbing is the retroactive labeling of the pivot. In particular, TTM actually labels the pivot as far as 5 bars after the pivot point that is eventually labeled. I am trying to interpret the code above, but what is causing this? Shouldn’t the study be set up to label the bar that culminates the three consecutive close rule? Right now, the study looks great but it looks like that because it is sneeking a peek into the future and then going back and labeling a bar.

    Thanks for any insight and perspective!

    • If you read Carter’s book it makes more sense. Yes, the signals require 3 bars to confirm. The signals are not real-time as a result. The idea here is to mark swing points on chart not to trade off the signals.

  20. Hi,

    I see this pile is a few years old. I was wondering if the code above is the final version. I would like to create a strategy from it in order to be able to back test but the dots from the study do not correspond to the TTM_Alert arrows from the original study found in TOS right now.

    You’re awesome for sharing your code with us!


    • The code has not changed. However, if you understand how this indicator is supposed to be used (as described in Carter’s book), you will realize that it cannot be backtested and is not designed to be a trading system.

  21. Hi to whom ever made this post or if someone can help. Do you or someone have the code of the pivot point study that is in the picture chart above. Thanks so Much, really appreciate the help Thanks.

  22. You’ve got mail. -TS

  23. Greg,
    That sounds possible. I’ll toss in in the good ideas pile. Unfortunately, the pile is rather large at this point. :) Unfortunately, the only ThinkScript reference I can find is the one ToS has. I’ve asked for a better reference and never got an answer.

  24. Batra -

    Did you get this code working properly? If so, how do you like it? Will you publish it?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>