Exit based on percent of profit

Roylow3000

New member
Dozens and dozens of losses after being in the money and I'm ready to throw ToS in the trash if there isn't a more a logical way to lock in profit. How can I create an order strategy to lock in a percentage of profit? For example, if profit is equal or greater than $1,000 then enter an order to lock in 80% or $800 of that. I don't care what the equity cost or the current price, and I'm not trying to do a trailing stop based on a percentage of the stock price itself, but a percentage of my profit. How in the hell is this not a common order type?
 
@Roylow3000 I feel your pain, trust me... 2020 wasn't a good year for me at all, especially back during the pandemic crash...

Let's take a step back... Could you explain your current trading style and strategy...??? Do you scalp, day trade, swing trade, or long term trade...??? Stocks, Options, Futures, Forex, combination...??? Do you actively watch every trade or more or less set-it-and-forget-it, only checking on things occasionally...??? Do you use Active Trader...??? Do you set Limit and Stoploss orders and, if so, what criteria do you use...???

You see, more questions at this point than answers but we'll get there... I don't think the logic you described in your initial post is necessarily easy to code even with Conditional Orders, for various reasons, but I'm sure we can come up with a means of locking in some profit... Give us more details and we'll go from there...
 

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

Your problem may be relatively simple to answer, but it can not be answered completely out of context. Can you post a chart example? Mark it with your entry, and where you sold, on your primary aggregation.
 
Folks, thank you for the replies. To simplify this conversation let's not focus on the type of trading but rather the actual formula or code to make this work.

This is written in PowerShell format as I haven't looked at TD's code base. From my understanding though ThinkOrSwim doesn't allow coding of automatic orders, but indicators only?

Everything is written out long hand to make it easier to understand the logic.

$whenToStartCalculating = '$500'
$percentToSave = '.8' ## 80%
$numberOfShares = '1000'
$longOrShort = 'long'
$entryPrice = '55.50'
$currentPrice = '$56.06' ## on tick Get-CurrentPrice for dynamic updating

if($longOrShort -eq 'long')
{
$todaysGain = $currentPrice - $entryPrice * $numberOfShares ## $todaysGain is equal to $560 in this scenario
}

if($todaysGain -ge $whenToStartCalculating) ## if $todaysGain ($560) is greater than or equal to $whenToStartCalculating ($500) run scriptblock
{
$gainPerTick = $numberOfShares * '0.01' ## 1000 shares x .01 = 10 or $10 for every 1 cent up or down on the current price
$amountToLock = $todaysGain * $percentToSave ## $560 x .8 = $448
$gainNeeded = $amountToLock / $gainPerTick ## $448 divided by $10 = 44.8 (round up to 49 cents needed from entry price)
$stopPrice = $entryPrice + $gainNeeded ## $55.50 + .49 = 55.99

Enter-StopLoss -price $stopPrice ## enter a stop loss at $55.99
}
 
Last edited:
Might be easier to read it color coded. Pic embedded.

TDExit-Strategy.png
 
@Roylow3000 I will revert you back to my original questions... We don't need pseudo-code to help you... We might be able to help you without writing code but you seem somewhat hesitant to give details... How long have you been trading...??? What platforms have you used...??? There, two more questions...

I guess you could learn the Thinkorswim platform and Thinkscript and go it alone... You seem to have at least some programming knowledge... It's not that I don't want to help, and I'm sure others would be willing to as well, but seeing the big picture sure does help us help you the most effectively... Help us help you... I could give you multiple methods but how well you can implement them and whether they best suit your needs is speculative at this point... And I surely wouldn't want to waste our collective times with the wrong solution... There is good reason to ask the questions I have...
 
I guess I'm not understanding what else you need. This is an order type that doesn't exist and I'm trying to fill that gap. I spoke with TD today and the guy mentioned an API call to overcome this limitation but API call to where? He didn't know more than what I just said, so the accuracy of that info is to be taken with a grain of salt.

I've used E-trade, TradeStation, and ToS. I day trade, swing trade and some long plays. I've traded north of 700m in equities since Q4 of 2015. How does any of this help answer the question though? I say that with peace and love, but I don't see the relevance. What I've just outlined is an exit strategy I'm trying to implement without me watching price action all day. I feel like this is one of those things where someone with coding skills and an understanding of the logic I just outlined should be able to answer. At least someone with more knowledge than I on writing for this platform and the coding/platform limitations.

I welcome any additional input or thought that may help further this thread. Thank you for your response.
 
Here is some information on getting API authorization... Schwab has changed some of the website's interfaces so you might need to get updated information:
https://usethinkscript.com/threads/ameritrade-api-in-c-access.4212/

Not to be lazy, but I just don't have the cycles to learn the interface right now. I'm hoping others reading this thread can asses the powerShell logic and with existing knowledge tell me;

1) ToS can't do this. Not with code or api calls and here's why.
2) ToS can't do this, but Platform XYZ can. (TradeStation probably can, but I'm not paying $700/mo again for the same feeds ToS has free)
3) Here is the code and/or how to do this.

I appreciate the effort. Thanks for the resource. I'll check it out.
 
One additional thought for someone with better math skills than I and less beers tonight.. The formula to factor the percentage for a traditional trailing stop that matches the stop price that I'm ultimately trying to arrive at. For example, if I want to be at 80% of my profit, can we find that solution by figuring out a percentage of the current price? Exactly how a trailing stop works today?
 
Last edited:
@Roylow3000 Thanks for the additional trading background... It truly does help because I now know your trading experience and am less apt to potentially waste time working with someone who really doesn't know what they want even though they can lay it out in a logical format... I've wasted a lot of time on code for people only to find out they really weren't describing what they needed anywhere near as close as they, and I, felt they had... So, lets proceed...

I think part of what TOS Support was implying is that, although I've always felt it was possible, we can't access EntryPrice() from within Conditional Order code... So that means we either need to have an exit price at the time of writing the Conditional Order or we have to be able to effectively edit the order after the trade is placed in order to manually set the entry price as a point to work from... That is why, as generic pointed out while I'm writing this, people use a percentage TrailingStop or another type of Limit...

Have you experimented with the various Limit order types to see if one of them would suffice...??? If those won't work and a Conditional Order can't be coded to suit your needs, then what you want is beyond the scope of Thinkscript and the TOS platform...
 
@Roylow3000 I don't trade using ToS but isn't this what you want? Percentage trail on the contracts itself.

Hey Generic, thanks for the reply. Unfortunately the built-in or historical trailing stops put the focus on the current price of the equity or option and not your profit. I want a percentage of the current gain. Not a percentage of the underlying equities latest move.

Appreciate your effort.
 
@Roylow3000 Thanks for the additional trading background... It truly does help because I now know your trading experience and am less apt to potentially waste time working with someone who really doesn't know what they want even though they can lay it out in a logical format... I've wasted a lot of time on code for people only to find out they really weren't describing what they needed anywhere near as close as they, and I, felt they had... So, lets proceed...

I think part of what TOS Support was implying is that, although I've always felt it was possible, we can't access EntryPrice() from within Conditional Order code... So that means we either need to have an exit price at the time of writing the Conditional Order or we have to be able to effectively edit the order after the trade is placed in order to manually set the entry price as a point to work from... That is why, as generic pointed out while I'm writing this, people use a percentage TrailingStop or another type of Limit...

Have you experimented with the various Limit order types to see if one of them would suffice...??? If those won't work and a Conditional Order can't be coded to suit your needs, then what you want is beyond the scope of Thinkscript and the TOS platform...

Hey Rad, I appreciate your candor. This is why I try not to lash out at people. We all think differently, so kudos. With that said, I have tried various orders but all platforms seem to be focused on the equities price movement versus my profit movement. I want to place orders based on my profit versus the underlying equities move if that makes sense? It just seems everything is geared toward the inverse.
 
@Roylow3000 Correct me if I'm wrong since I don't use ToS to trade but I don't think the trail applies only to the underlying if you place the order on the contracts. I could be wrong though. IE, if your contract is up 1000 and you place the 20% trail on the contract then the stop for the contract would be 800.

Ok, after thinking about it, I think I understand what you mean.
 
Last edited:
@Roylow3000 Thanks for the kind words... I understand exactly what you conveyed in your first post but perhaps you are fixating too much on rock solid numbers/percentages... Rather than doing that, wouldn't it be more beneficial to focus on guaranteed profit rather than what you feel is the need for an exact amount...??? After all, you probably won't get filled at that exact price anyway...

My point is, and I'll reflect on my own trading here, I got into a bad habit of fixating on maximum profits after being on a winning streak... That caused me to lose focus on taking what profit I could and let greed cloud my judgement... I got into the bad habit of thinking "It's going to keep going up so I'll move my Limit order higher, and higher, and higher until the price tops out" and ended up getting shredded trying to catch falling knives, over and over again... Those big wins turned into losses because as the price dropped even a Flatten order wouldn't get filled because market makers and institutional traders got filled first, leaving me with little or no profit at all... So, the only real fix was to go back to taking set profits during upward momentum and accepting that it's okay to leave money on the table... By almost never bumping up a Limit order the profits came... Not as big as I might like, but more wins than losses... Small wins are far better, psychologically, than losses... In short, sometimes we have to pick our battles...

In short, we don't always get the answers we want but, instead, perhaps the answers that we need... So, is 80% of potential profit really that important, or is just making profit good enough...???

Now, you just posted while I was writing this... So, if you don't trade in TOS, do you just monitor price action in it...??? What do you use for your actual trades...??? Yes, any Limit or Stop requires an active trade... However, a Study could be written to monitor the profits of a trade by merely entering your entry price and letting it alert you when to exit...
 
@Roylow3000 Thanks for the kind words... I understand exactly what you conveyed in your first post but perhaps you are fixating too much on rock solid numbers/percentages... Rather than doing that, wouldn't it be more beneficial to focus on guaranteed profit rather than what you feel is the need for an exact amount...??? After all, you probably won't get filled at that exact price anyway...

My point is, and I'll reflect on my own trading here, I got into a bad habit of fixating on maximum profits after being on a winning streak... That caused me to lose focus on taking what profit I could and let greed cloud my judgement... I got into the bad habit of thinking "It's going to keep going up so I'll move my Limit order higher, and higher, and higher until the price tops out" and ended up getting shredded trying to catch falling knives, over and over again... Those big wins turned into losses because as the price dropped even a Flatten order wouldn't get filled because market makers and institutional traders got filled first, leaving me with little or no profit at all... So, the only real fix was to go back to taking set profits during upward momentum and accepting that it's okay to leave money on the table... By almost never bumping up a Limit order the profits came... Not as big as I might like, but more wins than losses... Small wins are far better, psychologically, than losses... In short, sometimes we have to pick our battles...

In short, we don't always get the answers we want but, instead, perhaps the answers that we need... So, is 80% of potential profit really that important, or is just making profit good enough...???

Now, you just posted while I was writing this... So, if you don't trade in TOS, do you just monitor price action in it...??? What do you use for your actual trades...??? Yes, any Limit or Stop requires an active trade... However, a Study could be written to monitor the profits of a trade by merely entering your entry price and letting it alert you when to exit...
I trade in ToS but I think we're getting caught up in trading philosophy versus solving a code problem. I find that if I'm up a thousand dollars on a day trade, I'll end up giving back 70-80% of that if not a full retrace. I'm simply looking to take the emotion out of it and since I'm fairly decent at grabbing these smaller moves from chart patterns, I'm confident if I locked in more profit by taking the emotion out, I'd be up a lot more come EOY. Unfortunately I find the standard trailing stops to be inefficient for my purposes.

With the above said, I've confirmed with ToS support if their info was accurate, that you can't code a plugin that allows ToS to make the trade automatically. However, I think it would still be useful to write an indicator that you can set the percentage of retention on, whether that's 20% to prevent a full loss or 80% to maximize the day's gains, and have that visually indicated on the chart. There is some manual intervention involved to actually submit the trade at that level, but it appears this is the best we can do on this platform at this time.

I believe the long hand powershell I wrote out solves the logic behind the indicator. I'll just need to look at the ToS code base and learn how to write for this platform. If anybody has any other ideas please do let me know. I appreciate all the input.
 
@Roylow3000 I've barely dabbled with the API's, mainly due to time constraints, but have always been interested in new programming environments... Back when I was an apartment dweller I had far more time to invest than I do since becoming a homeowner some years ago... I miss banging out code around the clock but have other priorities these days... Good luck experimenting...

As I mentioned previously, you might be able to use a Conditional Order and edit it once your order is placed to update a def with the actual entry price and let it calculate from there... Your condition would then become true once you attained a certain amount of profit and then the profit reduced back to your 80%, or a 20% reduction... Food for thought...

Feel free to look me up on the Discord channel if you'd like to hash over ideas in realtime...
 
Today is another good example of why this would be useful. I purchased 1500 shares of $GM on Friday. Got sick as a dog over the weekend and was still in the bed this morning when the price shot to 58.60 only to check at 11:00 AM EST to find it had retraced to lower than my entry. I had a stop loss in 1 cent higher than my entry so basically a null trade. Had I been able to enter an exit strategy on Friday to lock in 80% of a days given profit I would have been out where the green line is not the red X.

There has to be a way to automate this.

GM.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
387 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