Order Size Calculator For ThinkOrSwim

mark_

New member
I created this simple indicator using AIs to figure out where to put my stop losses or to see how much profit/loss they would make if I did set them in said areas. It allows multiple types of inputs percentages, ticks, dollar amount, or entry price offset amount( like trailstop entries). It lets you choose what value is displayed at top labels so you can input a dollar amount or % value if that's what you want and it will display price level on chart and prefered output at top. Live data is calculated after the formation of last candle as well as I only calculate stock and futures price correctly since I don't use any other assets yet.

https://tos.mx/!a6SrxdLb

JavaScript:
input entryPriceCan = 5749.00;
input UseLiveData = yes;
def entryPrice = if UseLiveData then Close(priceType = PriceType.MARK) else entryPriceCan;
input takeProfitPrice = 5750.00;
input stopLossPrice = 5745.00;
input trailStopValue = 5740.00;
input calculationMethod = {default "Candle Price", "Percentage", "Offset from Entry", "Tick Value"};
input displayMethod = {default "Price", "Percentage", "Tick Value", "Offset $ Value"};
input exitMethod = {default "Stop Loss", "Take Profit", "Trail Stop", "Stop Loss and Take Profit", "Stop Loss and Trail Stop", "All"};
input numberOfContracts = 1;
input instrumentType = {default "Futures", "Stocks"};
input positionType = {default long, short};

def tickSize = TickSize();
def tickVal = TickValue();

def stopLoss;
def takeProfit;
def trailStop;

switch (calculationMethod) {
case "Offset from Entry":
    stopLoss = if positionType == positionType.long then entryPrice - stopLossPrice else entryPrice + stopLossPrice;
    takeProfit = if positionType == positionType.long then entryPrice + takeProfitPrice else entryPrice - takeProfitPrice;
    trailStop = if positionType == positionType.long then entryPrice - trailStopValue else entryPrice +trailStopValue;
case "Percentage":
    stopLoss = if positionType == positionType.long then  entryPrice-(entryPrice*(stopLossPrice/100)) else  entryPrice+(entryPrice*(stopLossPrice/100));
    takeProfit = if positionType == positionType.long then entryPrice+(entryPrice*(takeProfitPrice/100)) else  entryPrice-(entryPrice*(takeProfitPrice/100));
    trailStop = if positionType == positionType.long then  entryPrice-(entryPrice*(trailStopValue/100)) else entryPrice+(entryPrice*(trailStopValue/100));
case "Tick Value":
    stopLoss = if positionType == positionType.long then entryPrice - (stopLossPrice *  tickSize) else entryPrice + (stopLossPrice * tickSize);
    takeProfit = if positionType == positionType.long then entryPrice + (takeProfitPrice * tickSize) else entryPrice - (takeProfitPrice * tickSize);
    trailStop = if positionType == positionType.long then entryPrice - (trailStopValue * tickSize) else entryPrice + (trailStopValue * tickSize);
case "Candle Price":
    stopLoss = stopLossPrice;
    takeProfit = takeProfitPrice;
    trailStop = trailStopValue;
}

def stopLossVal;
def takeProfitVal;
def trailStopVal;

switch (displayMethod) {
case "Price":
    stopLossVal = stopLoss;
    takeProfitVal = takeProfit;
    trailStopVal = trailStop;
case "Percentage":
    stopLossVal = if positionType == positionType.long then (entryPrice - stopLoss) / entryPrice * 100 else (stopLoss - entryPrice) / entryPrice * 100;
    takeProfitVal = if positionType == positionType.long then (takeProfit - entryPrice) / entryPrice * 100 else (entryPrice - takeProfit) / entryPrice * 100;
    trailStopVal = if positionType == positionType.long then (entryPrice - trailStop) / entryPrice * 100 else (trailStop - entryPrice) / entryPrice * 100;
case "Tick Value":
    stopLossVal = if positionType == positionType.long then (entryPrice - stopLoss) / tickSize else (stopLoss - entryPrice) / tickSize;
    takeProfitVal = if positionType == positionType.long then (takeProfit - entryPrice) / tickSize else (entryPrice - takeProfit) / tickSize;
    trailStopVal = if positionType == positionType.long then (entryPrice - trailStop) / tickSize else (trailStop - entryPrice) / tickSize;
case "Offset $ Value":
    stopLossVal = if positionType == positionType.long then (entryPrice - stopLoss) else (stopLoss - entryPrice);
    takeProfitVal = if positionType == positionType.long then (takeProfit - entryPrice) else (entryPrice - takeProfit);
    trailStopVal = if positionType == positionType.long then (entryPrice - trailStop) else (trailStop - entryPrice);
}

def highestBarIndex = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);

plot entryLine = if (BarNumber() >= highestBarIndex - 2) then entryPrice else Double.NaN;
entryLine.SetDefaultColor(Color.WHITE);
entryLine.SetLineWeight(2);

plot stopLossLine;
stopLossLine.SetDefaultColor(Color.Red);
stopLossLine.SetLineWeight(2);

plot takeProfitLine;
takeProfitLine.SetDefaultColor(Color.Green);
takeProfitLine.SetLineWeight(2);

plot trailStopLine;
trailStopLine.SetDefaultColor(Color.MAGENTA);
trailStopLine.SetLineWeight(2);

def profitPrice;
def lossPrice;
switch(exitMethod) {
case "Stop Loss":
    profitPrice = stopLoss;
    lossPrice =  stopLoss;  
    trailStopLine = Double.NaN;
    stopLossLine = if (BarNumber() >= highestBarIndex - 2) then stopLoss else Double.NaN;
    takeProfitLine = Double.NaN;
case "Take Profit":
    profitPrice = takeProfit;
    lossPrice = -10000000;  
    trailStopLine = Double.NaN;
    stopLossLine = Double.NaN;
    takeProfitLine = if (BarNumber() >= highestBarIndex - 2) then takeProfit else Double.NaN;
case "Trail Stop":
    profitPrice = trailStop;
    lossPrice = trailStop;  
    trailStopLine = if (BarNumber() >= highestBarIndex - 2) then trailStop else Double.NaN;
    stopLossLine = Double.NaN;
    takeProfitLine = Double.NaN;
case "Stop Loss and Take Profit":
    profitPrice = takeProfit;
    lossPrice =  if positionType == positionType.long then Max(stopLoss, takeProfit) else Min(stopLoss, takeProfit);
    trailStopLine = Double.NaN;
    stopLossLine = if (BarNumber() >= highestBarIndex - 2) then stopLoss else Double.NaN;
    takeProfitLine = if (BarNumber() >= highestBarIndex - 2) then takeProfit else Double.NaN;
case "Stop Loss and Trail Stop":
    profitPrice = if positionType == positionType.long then Max(stopLoss, trailStop) else Min(stopLoss, trailStop);
    lossPrice = if positionType == positionType.long then Max(stopLoss, trailStop) else Min(stopLoss, trailStop);
    trailStopLine = if (BarNumber() >= highestBarIndex - 2) then trailStop else Double.NaN;
    stopLossLine = if (BarNumber() >= highestBarIndex - 2) then stopLoss else Double.NaN;
    takeProfitLine = Double.NaN;
case "All":
    profitPrice = takeProfit;
    lossPrice = if positionType == positionType.long then Max(stopLoss, trailStop) else Min(stopLoss, trailStop);
    trailStopLine = if (BarNumber() >= highestBarIndex - 2) then trailStop else Double.NaN;
    stopLossLine = if (BarNumber() >= highestBarIndex - 2) then stopLoss else Double.NaN;
    takeProfitLine = if (BarNumber() >= highestBarIndex - 2) then takeProfit else Double.NaN;
}

AddLabel(yes, "Entry Price: " + AsDollars(entryPrice), Color.WHITE);
AddLabel(yes, "Stop Loss: " + stopLossVal, Color.RED);
AddLabel(yes, "Take Profit: " + takeProfitVal, Color.GREEN);
AddLabel(yes, "Trail Stop: " + trailStopVal, Color.MAGENTA);

def profit;
def loss;
switch(instrumentType) {
case "Futures": 
    profit = if (positionType == positionType.long) then ((tickVal * ((profitPrice - entryPrice) / tickSize)) * numberOfContracts) else ((tickVal * ((entryPrice - profitPrice) / tickSize)) * numberOfContracts);
    loss = if (positionType == positionType.long) then ((tickVal * ((lossPrice- entryPrice) / tickSize)) * numberOfContracts) else ((tickVal * ((entryPrice - lossPrice) / tickSize)) * numberOfContracts);
case "Stocks":
    profit = if (positionType == positionType.long) then (profitPrice - entryPrice) * numberOfContracts else (entryPrice - profitPrice) * numberOfContracts ;
    loss = if (positionType == positionType.long) then (lossPrice- entryPrice) * numberOfContracts else (entryPrice - lossPrice) * numberOfContracts ;
}
#Profit = only profit(for just TP) or only Loss(for just stops)
AddLabel(yes, "Profit: " + AsDollars(profit), Color.YELLOW);
AddLabel(yes, "Loss: " + asDollars(loss), Color.Red);
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
189 Online
Create Post

Similar threads

Similar threads

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