Working on a Trade Calculator for ThinkorSwim

netarchitech

Well-known member
@RobertPayne I'm sure that there will be a number of uTS scripters looking to take advantage of your very generous offer. Thank you :)

On that note, I was hoping to ask you to take a look at a script I've been working on, when you have a moment. I'm trying to put together a simple trade calculator...

Rich (BB code):
# filename: _TradeCalculator_
# author: netarchitech
# version: 20181105

def priceC = close;
input averageType = AverageType.WILDERS;
input ATRlength = 10; # default length = 14 - can range anywhere from 6 to 21
input ATRmultiplier = 1.50; # default multiplier = 2.0 - can range anywhere from 1.0 to 5.0
def ATR = MovingAverage(averageType, TrueRange(high, close, low), ATRlength);
def avgDailyMovement = (ATR / priceC) * 100;
input profitMultiplier = 3.00;

input AccountEquity = 100000;
input PercentRiskPerTrade = 0.005;
def EquityRiskPerTrade = AccountEquity * PercentRiskPerTrade;

def SharesToBuy = (EquityRiskPerTrade / (avgDailyMovement * ATRmultiplier));
def currentPositionSize = SharesToBuy * priceC;

def stopAmount = avgDailyMovement * ATRmultiplier;
def longSellStop = priceC - stopAmount;
def shortBuyStop = priceC + stopAmount;

def profitTarget = avgDailyMovement * profitMultiplier;
def longSellLimit = priceC + profitTarget;
def shortBuyLimit = priceC - profitTarget;

AddLabel(yes, "Shares to Buy: " + Round(SharesToBuy, 0), CreateColor (255, 255, 255));
AddLabel(yes, "Position Size: " + AsText(currentPositionSize, NumberFormat.TWO_DECIMAL_PLACES), CreateColor (255, 255, 255));

AddLabel(yes, "LONG Sell STOP: " + AsText(longSellStop, NumberFormat.TWO_DECIMAL_PLACES), CreateColor (255, 255, 255));
AddLabel(yes, "LONG Sell LIMIT: " + AsText(longSellLimit, NumberFormat.TWO_DECIMAL_PLACES), CreateColor (255, 255, 255));

AddLabel(yes, "SHORT Buy STOP: " + AsText(shortBuyStop, NumberFormat.TWO_DECIMAL_PLACES), CreateColor (255, 255, 255));
AddLabel(yes, "SHORT Buy LIMIT: " + AsText(shortBuyLimit, NumberFormat.TWO_DECIMAL_PLACES), CreateColor (255, 255, 255));


Any pointers/tips would be much appreciated :)
 
@RobertPayne Thank you for the swift reply. I apologize for the lack of specificity. Essentially, I guess I was hoping to have a second set of eyes desk-check the logic of the script.

Admittedly, when it comes to thinkscripting, I'm more of an editor than a coder. This is one of a few scripts I've written from the ground up. I guess I was thinking this was appropriate to send your way because you mentioned helping those who are "coding their own indicators".

Thanks again :)
 
@RobertPayne Essentially, I guess I was hoping to have a second set of eyes desk-check the logic of the script.

Admittedly, when it comes to thinkscripting, I'm more of an editor than a coder. This is one of a few scripts I've written from the ground up. I guess I was thinking this was appropriate to send your way because you mentioned helping those who are "coding their own indicators".

Thanks again :)

No. I am not interested in proofing scripts or providing my opinion on them. What I said was, "If you are stuck on something, perhaps I can help you figure it out." I loaded your script and it seems to function.

This is the "Tutorial" section of the forum. I am looking to provide assistance that can be beneficial to the community at large by showing how something may be accomplished. Then someone may use that technique in their own scripts.

Here are a couple of examples of what I have in mind.

https://funwiththinkscript.com/count-the-number-of-bars-between-successive-highs/
https://funwiththinkscript.com/adding-space-between-candles/
 
@rlohmeyer : Thank you for the detailed annotated charts and offering your ATR Risk Control Indicator to the uTS community...You have a very unique setup/style...I always find it interesting/educational to see how other people trade, especially those whom have done so consistently/successfully for an extended period of time...


With that said, I stumbled across this thread and it re-ignited my interest in reviving an old project of mine...namely a Risk Manager/Position Sizer/Trade Calculator...I've tried off and on for quite some time and yet success in this particular endeavor still eludes me...

Do you think you might be able to review the code I have and potentially provide a tip or some insight into how I might go about successfully implementing a solution? There was quite a bit of dust on it, but here's the code I have so far:

https://usethinkscript.com/threads/working-on-a-trade-calculator-for-thinkorswim.917/post-5592

I should mention that I'm trying to do this with Futures in mind...I primarily focus on /NQ, but I also trade /GC and /CL when the opportunity arises...I tried using your ATR Risk Control Indicator, but had some difficulty extracting trading information from it...Bottom line, I'm pretty sure it was me, as I admittedly require a user manual for most things these days...

I understand that your time and efforts are finite, so I look forward to your reply if you have a chance to do so...Thanks in advance, whether you reply or not...If not, I look forward to further developments with the ATR Risk Control Indicator, as they arise...

Good Luck and Good Trading :cool:
 
Last edited by a moderator:
@netarchitech

Not sure I am the person to ask and not sure of your question about it. In the thread you are referring to Payne said the code was working.

I learned my coding with the help of others on this forum and in this thread the Risk Control Indicator I coded is for use with a day frame chart, not intraday, and is based on an average true range which is seen on the charts as the yellow dotted lines across the chart as indicated in Post 13 above. This tells me what the dollar risk is for 1 share, or if I change the dollar risk, how many shares I could hold either long or short to equate to the dollar risk I am willing to assume on the trade. The ATR lines help with determining where to set stops as I trail a position.

Regards,
Bob
 
@rlohmeyer : Thank you for the detailed annotated charts and offering your ATR Risk Control Indicator to the uTS community...You have a very unique setup/style...I always find it interesting/educational to see how other people trade, especially those whom have done so consistently/successfully for an extended period of time...



With that said, I stumbled across this thread and it re-ignited my interest in reviving an old project of mine...namely a Risk Manager/Position Sizer/Trade Calculator...I've tried off and on for quite some time and yet success in this particular endeavor still eludes me...

Do you think you might be able to review the code I have and potentially provide a tip or some insight into how I might go about successfully implementing a solution? There was quite a bit of dust on it, but here's the code I have so far:

https://usethinkscript.com/threads/working-on-a-trade-calculator-for-thinkorswim.917/post-5592

I should mention that I'm trying to do this with Futures in mind...I primarily focus on /NQ, but I also trade /GC and /CL when the opportunity arises...I tried using your ATR Risk Control Indicator, but had some difficulty extracting trading information from it...Bottom line, I'm pretty sure it was me, as I admittedly require a user manual for most things these days...

I understand that your time and efforts are finite, so I look forward to your reply if you have a chance to do so...Thanks in advance, whether you reply or not...If not, I look forward to further developments with the ATR Risk Control Indicator, as they arise...

Good Luck and Good Trading :cool:

it is unclear what you want. you reference some old code and some other links.
can you describe what you want to see? list out what changes to do to your post1 code.
 
Last edited by a moderator:
anyone, if you talk about another post from here, please include a link.
click on the post number. then copy the web address

i added a link to your post #6.

@halcyonguy : Thank you for your time and the moderation...I would've included the link, but I was replying in the other thread at the time and it seems that it would have been redundant to link to that thread while replying in the same thread...I got a little discombobulated when my post was moved from the other thread to this one...In any event, I will make sure to enter the appropriate links as needed from this point forward...

it is unclear what you want. you reference some old code and some other links.
can you describe what you want to see? list out what changes to do to your post1 code.

I did some searching and came across a Risk/Reward Calculator you created that has a structure/flow/framework I would like to utilize, with your permission, to hopefully get closer to accomplishing my goal...a functional Risk Manager/Position Sizer/Trade Calculator to use and share with those whom might be interested in the uTS community...

As for my list of requirements/changes to my initial code:
  1. It needs to work with Futures
  2. It needs to handle/calculate/display amounts for the following, so I can easily transcribe the information into OCO orders:
    • Long Buy Limits
    • Long Sell Stops
    • Short Sell Limits
    • Short Buy Stops
  3. If possible, it needs to have a basic interface accessible via switch/case statements for the following selections:
    • Futures vs. Stocks
    • Long vs. Short
    • Dollar vs. ATR vs. Percentage
I've prepared a flowchart to illustrate what I would hope to achieve...I understand that there are limitations to the possibilities...

202408232321_flowchart_.png


Unfortunately, the thinkscript switch/case statement is limited...With that said, I'm trying to devise a way to accomplish what I envision, but I don't see a way forward at this time...I would welcome any thoughts/suggestions you might have...

Thanks again for your time, knowledge and potential assistance in this endeavor...It is greatly appreciated...

Note: I forgot to mention/add Position Sizing...arghh! I'll have to work on updating the requirements and the flowchart over the weekend...
 
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
293 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