Position Size Calculator for ThinkorSwim

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Hello,

I've been looking for a very simple indicator that can divide my buying power with the price of the stock I'm looking at so it can show me the shares I need to buy quickly. For example, If I have $100k in buying power, I'd like to set up the indicator to divide $50k (I scale into positions) with the current price of the stock let's say $20 so it would show me I need to buy 2,500.
I've been doing this calculation manually every time but I make quick trades and sometimes these seconds I use for my calculations are very costly.

Does anyone know if an indicator like this exists? I haven't been able to find something on my own so far.

Thanks a lot!
 
@bsizzle101 Hello, I downloaded the Position Sizing Calculator then tweaked it I added the ATR to it so I could set my time frame and ATR multiplier.
I'll paste the code below so you can look it over and maybe improve it.
I would like for the chart labels to show the time period used 5min,15min so I could put several on my chart and see my stops at different time periods and different ATR multipliers.
Best of luck I hope this helps someone.

Code:
#
# PositionSizingCalculator
#
# Author: Kory Gill, [USER=212]@korygill[/USER]
## thanks to Kory Gill for this
## Jon Yadon added this ATR so you can adjust your atr mutliplier
input RiskUnit = 100;

input multiplier = 2;
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.DAY;
input showlabel = yes;





def ATR = MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength);

def Today_High = Highest(high(period = BasePeriod)[0], 1);
def Today_Low = Lowest(low(period = BasePeriod)[0], 1);
def stoploss = ATR * multiplier;
AddLabel(
    yes,
    AsDollars(RiskUnit) + " risk with " + AsDollars(stoploss) + " stop = " + Floor(Round(RiskUnit / stoploss)) + " shares",
    Color.GRAY
    );
AddLabel(
    yes,
    "High: " + Today_High + " Low: " + Today_Low,
    Color.GRAY
    );
 
Last edited by a moderator:
This morning I executed a trade with a $.04 stop. Unfortunately, the trade turned against me and I was stopped out. While I was in the position, the stock wasn't volatile at all. No surges and no flash crashes. Despite that, when I was stopped out, my P/L showed a loss that was twice what it should have been based on what I entered into the Active Trader order entry window.

When I went back to review my order, it showed that a $.08 stop loss was submitted. For a moment I thought that I may have made a typo. I knew this was highly unlikely though, because I use a position sizer that automatically calculates my share size, limit offset, stop offset and entry price based on my max risk per trade. All I have to do is type what the tool calculates into the Active Trader Ladder. But even still, it's possible that I could make a typo.

After reviewing the trade execution details under the Monitor tab, I went back to my chart to review the Active Trader Ladder. And what do you know... My stop offset was indeed set to $.04. Since I hadn't taken any additional trades after this loss, it would be impossible for the stop offset to be set to $.04 if that wasn't what I submitted in my order.

After reviewing all of this I got on chat with TD Ameritrade to explain what happened, and of course... The said that there is nothing they can do to credit my account since there's no way for them to verify that an error was made on their behalf.

I'm really concerned about trading with Thinkorswim now, and I am only going to continue trading with them while video recording my screen during all trading sessions. Had I had that proof, I could have gotten them to credit my account for the $.04 difference (slippage) in the Stop loss they executed vs. what I submitted multiplied by the number of shares I purchased.

Thankfully, I was trading with a small risk/trade, so even though my loss was doubled, I didn't blow up my account or have a major setback.

Please, please, please protect yourself. There are several free screen recording programs you can use. I'm using Debut by NCH. They have a 30 day free trial, and then after 30 days it's still free, but it has a watermark on it, which doesn't really matter, since I won't be posting these videos publicly anywhere.
Opinion: Wondering why you aren't using a percentage of the trade with a trail stop,......if you identify a set up, place an anticipation entry using a stop limit, enter the trade early, place .20-.30-.50, whatever trailstop, set it and look for the next set up....
 
Last edited by a moderator:
@Thomas doesn't a stop order turn into a market order when the condition is hit? you'd have to have the price ladder at that time to see what was on the book at the time for various exchanges. TD sells your orderflow anyway, so quite possible the market makers just pulled some orders off the books, or executed ahead of you (front running). That would be my guess. I've see the bots sweep the book just to run stops, and if your order was not 1st in line, you are executing after others may have been selling/buying current liquidity. ****s for sure...
 
@Thomas doesn't a stop order turn into a market order when the condition is hit? you'd have to have the price ladder at that time to see what was on the book at the time for various exchanges. TD sells your orderflow anyway, so quite possible the market makers just pulled some orders off the books, or executed ahead of you (front running). That would be my guess. I've see the bots sweep the book just to run stops, and if your order was not 1st in line, you are executing after others may have been selling/buying current liquidity. ****s for sure...
Kory,...whatever is a better fill, I'd prefer limit, but you are right, a market order on the open as early in the trade as possible, you want in early....but it's really a visual entry if identified early with some elbow grease, monitor the candle, place the trail stop........if you're in, fine, you miss it, look for the next set up......but entry as early as possible is secret.
 
@Projos This is amazing. What would make this incredible is if it had inputs to include ATR and maximum dollar loss. Say price is 50, 2ATR puts the stop at 49. Takes that stop price/buy price to give you a 2% stop loss in calculating how many shares you could get. Possible?
 
Hey everyone, I have been playing around with the code for a Forex strategy to get the order size to automatically be the most mini lots my account size can handle (50x leverage).
I think I got it to work at one point but I can't get it working anymore. Any help is appreciated!

input investedCapital = 3000;
def positionSize = 10000 * Floor((investedCapital + FPL()) / (close * 200));
 
@korygill Hi, I added this script to my 1 min chart and it shows the high and low as the same amount, 0.00 for the stop and the number of shares is incorrect. Other than the TF there are no other inputs. What am I missing? Also what is high and low? Is high the that limit target and low the stop loss?
 
Position sizing in risk management IS key in trading! agreed, it is even more important than any strategy.
This is what I have used:
(Purchase Price) / (Risk) / (Purchase Price) X ($ amount per risk) = number of shares to purchase.
This must be computed quickly and I can get it in about 15 to 20 seconds manually.
I do miss trades sometimes on 1 minute timeframes.
 
@jdstrader Here is something I coded up, might do what you want.

Code:
#Simple_ScaleIn_Label
#Pensar, 6/10/2020

input max_size = 100000.00;
input scale_in = yes;
input times_to_scale_in = 4;

def Size = if !scale_in then (max_size / close) else
              (max_size / times_to_scale_in) / close;

AddLabel(1, (if scale_in then "Scale In: " else "") + "Buy " + RoundDown(Size, 0) + " shares" + (if scale_in then " " + times_to_scale_in + " times" else "") + "  ---  Total Trade Size: " + AsDollars(max_size), color.white);

#End code
 
Hey, does the script let you plot entry stop, target and calculate the shares based on dollar risk amount ?
Hi
Thank you for such valuable research info
I have gone through the details and yet to explore by implementing with Ondemand

I have 3 questions .

1. does it work for Options trading?
2. Do we need to pick the entry price or the tool will give the entry based on the risk -reward 1:2 . I mean do we need to pick the entry or tool will recommends ?
3.Can you share some of the trades and the results on stocks and options?

I see that the developer running the sale but it is 109.99

Does your recommended changes are going to available with the general tool or do we need to request the developer ?

thanks
Suresh
 
Hello,

Basically, I am looking to create a study or have a script written that will show a label or price bubble, which automatically adjusts to the volatility in a stock, calculate my Stock Buying Power divided by the 5 min. high price from 9:30 - 9:35 am + $0.01.

So for instance, let's say I have $2,500 in Net Liq., but $1,000 in Stock Buying Power. Then at 9:30 am, the stock opens at $1, gets to a high price of $1.10 (within the 9:30-9:35 am interval) and closes at $1.05 at 9:30 am.

I would like to see a label or price bubble that shows my $1,000 in Stock Buying Power divided by the $1.11 ($1.10+$0.01), 5 min. high price. This would come out to be 900.90 shares available to purchase.

Also, while the stock price moves up and down, my label/price bubble would adjust its calculations based on the volatility of the stock. So if at 9:31 a.m. the stock was at $1.08, the label or bubble would show my SBP divided by $1.09 ($1.08+$0.01). If at 9:34 am the stock's high was at $1.10, it would adjust and show my SBP divided by $1.11 ($1.10+$0.01). The label/price bubble would adjust as the stock moves up and this would continue until 9:35 am, where it would just show me my SBP divided by the highest price within the first 5 min. candle + $0.01, 9:30-9:35 am.

This would save me a lot of time from calculating the amount of shares I can buy by manually inputting the numbers. Also, I know there's a way to purchase shares based on my dollar amount available, but this type of order takes even more time than if I were to just compute my SBP by the 5 min. high + $0.01 on my calculator.

This study would help me be a faster trader and earn more profits. If you could help, I would really appreciate it. It would be used for ORB and GapnGo's, usually within the first 5-15 minutes. It would be helpful because at those times, during market open, the securities are very volatile and I don't want to miss my entry point due to me having to waste 3-5 seconds with my calculator.
 
Last edited:
This is so elegant. I have used an Excel file I keep floating on my desktop for years. Since I trade 5 min, it worked OK when I didn't have access to NinjaTrader and the like. This is so much nicer.
 
I've got a better one. There's three scenarios:
Scenario 1: You can choose your size and your risk, but not your stop
Scenario 2: You can choose your size and your stop, but not your risk
Scenario 3: You can choose your risk and your stop, but not your size

Why not make it an indicator that lets you see the result of those decisions? I decided to make one. The only issue I'm uncertain of is whether I am making price what it actually is. I decided to go with Mark

Here's the code:

Code:
#
# PositionSizingCalculator
#
# Author: RamonDV. AKA Pelonsax
#
#

input Risk_Amount = 100;
input Shares = 1000;
input Stop_Price = 100.00;
input Choose_Size_and_Risk = yes; #can't choose stop
input Choose_Size_and_Stop = yes; #can't choose risk
input Choose_Risk_and_Stop = yes; #can't choose size

def Mark = close(PriceType = PriceType.MARK);
def Size  = Risk_Amount / (Mark - Stop_Price);
def Stop = ((Mark * Shares) - Risk_Amount) / Shares;
def Risk = (mark - Stop_Price) * Shares;

#Display current price
AddLabel(yes, "Current Price: " + AsDollars(Mark), color.gray);

#No defined stop
AddLabel(Choose_Size_and_Risk, "No defined stop: " + Shares + " Shares with " + AsDollars(Risk_Amount) + " risk with Stop Loss at " + AsDollars(stop) + "  .", color.gray);

#No defined risk
AddLabel(Choose_Size_and_Stop, "No Defined risk: " + Shares + " Shares with " + AsDollars(risk) + " risk with Stop Loss at " + AsDollars(Stop_Price) + "  .", color.gray);

#No defined size
AddLabel(Choose_Risk_and_Stop, "No defined size: " + Floor(Round(size)) + " Shares with " + AsDollars(Risk_Amount) + " risk with Stop Loss at " + AsDollars(Stop_Price) + "  .", color.gray);

Here's the link:

https://tos.mx/l2H5IBg
 
This one I wrote to auto calculate the number of shares to buy based on an input risk % and capital size. You will need to update the stop level based on whatever signal you are using.

Code:
########################################
input capital_size = 25000;
input risk_percent = 1;
def risk_price = close;

def risk_percent_calc = risk_percent / 100;
def entry_stop_bull = (risk_price - bullstop);
def entry_stop_bear = (bearstop - risk_price);
def units_to_buy_bull = (risk_percent_calc * capital_size) / (entry_stop_bull) / risk_price;
def units_to_buy_bull1 = RoundDown(units_to_buy_bull,0);
def total_cost_bull = units_to_buy_bull1 * risk_price;
def units_to_buy_bear = (risk_percent_calc * capital_size) / (entry_stop_bear) / risk_price;
def units_to_buy_bear1 = RoundDown(units_to_buy_bear,0);
########################################
 
Last edited:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
422 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

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.

How do I get started?

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.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top