The Bollinger Bands® Short Entry strategy generates a Short Entry signal when the price crosses below the upper band.
The Bollinger Bands® Long Entry strategy generates a Long Entry signal when the price crosses above the lower band.
#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
def sDev = stdev(data = price[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
def buy = close crosses above LowerBand;
def sell = close crosses below UpperBand;
plot bullish = buy;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.GREEN);
bullish.SetLineWeight(1);
plot bearish = sell;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.RED);
bearish.SetLineWeight(1);
# Alerts
Alert(bullish, " ", Alert.Bar, Sound.Chimes);
Alert(bearish, " ", Alert.Bar, Sound.Bell);
The AddOrder() function is used for backtesting Strategies... If you have a Study that you are interested in, just copy it over into a Strategy and insert AddOrder() commands at the bottom using the same criteria that is providing you with signals... You can then Right-Click on the results of the Strategy and Click on Report to see the results...Hello guys,
Is there some sort of Strategy template for dummies where I can plug in the indicators and test them? I usually do them by hand but I'm looking for a quicker method so it doesnt take me sooooo long to backtest. I'm not savvy with coding at all. Looking for some help if anyone has the time. Many thanks!
plot sma1 = (close[2]+close[1])/2;
def close1 = close[1];
def close2 = close[2];
def sma2 = (close[3]+close[2])/2;
AddOrder(OrderType.BUY_TO_OPEN, tradeSize = (10000/LastPrice) ,condition = ((close1 > (sma1*.9997)) and (close2<sma2)), name = "BUY");
AddOrder(OrderType.SELL_AUTO, tradeSize = (3333/entryPrice()), condition = (LastPrice crosses above (entryprice()*1.039998)), name = "TAKE PROFIT 1");
plot tp1 = if (LastPrice crosses above (entryprice()*1.039998),yes,no);
AddOrder(OrderType.SELL_AUTO, tradeSize = (3333/entryPrice()), condition = (LastPrice crosses above (entryprice()*1.069998)), name = "TAKE PROFIT 2");
plot tp2 = if (LastPrice crosses above (entryprice()*1.069998),yes,no);
AddOrder(OrderType.SELL_AUTO, tradeSize = (1667/entryPrice()), condition = (LastPrice crosses above (entryprice()*1.149998)), name = "TAKE PROFIT 3");
plot tp3 = if (LastPrice crosses above (entryprice()*1.149998),yes,no);
AddOrder(OrderType.SELL_TO_CLOSE, tradeSize = if (tp1 == no,10000/entryPrice(),if(tp1 == yes and tp2 == no,(10000*.666666667)/entryPrice(),if(tp1 == yes and tp2 == yes and tp3 == no,(10000*.3333333334)/entryPrice(),if(tp1 == yes and tp2 == yes and tp3 == yes,(10000*.1666667)/entryPrice(),10000/entryPrice())))) ,condition = (close1 < sma1), name = "CLOSE LONG POS");
def pUP = buy_condition;
def pDown = sell_condition;
AddOrder(OrderType.BUY_TO_OPEN, condition = pUP, price = open[-1],100, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Long");
AddOrder(OrderType.SELL_TO_CLOSE, condition = pDown, price = open[-1],100, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Cover");
AddOrder(OrderType.BUY_TO_OPEN, (if BuyCondition then (yes) else no), open[-1], numshares, Color.GREEN, Color.GREEN, "Buy Shares: " );
AddOrder(OrderType.SELL_TO_CLOSE, (if (SellConditionATRs) then yes else no), open[-1], numshares, Color.RED, Color.RED, "Sell Shares: " + numshares);
Actually found that you can change this in the "global settings" in the "edit studies" portion. Super useful. Someone in the ThinkScript Lounge told me about it!As I recall, as long as you have your TOS set to allow multiple trades you can have multiple Buys but only one Sell...
Actually, for backtesting it only does single Buys and Sells... Unfortunately...
Yes, that was the setting I was referring to... Pretty sure it works for Strategies but not 100%... Have you tried it yet...???Actually found that you can change this in the "global settings" in the "edit studies" portion. Super useful. Someone in the ThinkScript Lounge told me about it!
Yeah, you can have as many buy-in orders as you want and have it all sell at the same time! Works great!Yes, that was the setting I was referring to... Pretty sure it works for Strategies but not 100%... Have you tried it yet...???
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
![]() |
thinkScript IDE For ThinkOrSwim | Tutorials | 5 | |
![]() |
Thinkorswim Chart Study and Thinkscript Plot Hierarchy | Tutorials | 5 | |
![]() |
How to debug ThinkScript | Tutorials | 6 | |
![]() |
thinkScript BarNumber() Function Usage and Examples | Tutorials | 7 | |
![]() |
Robert Payne offering to help with ThinkScript | Tutorials | 46 |
Start a new thread and receive assistance from our community.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.