@HarryT808 Search for the built-in "Average Trade Price" watchlist column in TOS.
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
# Profit Drawdown
# Pensar
# 08.06.2021
def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice();
def newprofithighs = if entry > 0 then if high > high[1] then high else newprofithighs[1] else double.nan;
def maxprofit = HighestAll(newprofithighs);
def distancefromentry = entry - maxprofit;
def drawdown = (maxprofit - close)/distancefromentry;
addlabel(if entry > 0 then 1 else 0, aspercent(drawdown),color.red);
# end code
Just don't get your hopes up too highAmazing! Thank you SO much. I will test it out next week. Can't wait...
@epete I've been having issues also with some stocks. I'll look into it as well.Thanks Pensar. Something didn't work quite right. I got a bunch of "NaN" displays. At times I got negative values for PRF (profit risk factor) even though the profit was going up and not coming down. I'll have to look more into the code tomorrow.
# Profit Risk Factor
# epete
# 08.10.2021
def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice();
def newprofithighs = if entry > 0 then (if high > high[1] then high else newprofithighs[1]) else double.nan;
def maxprofit = HighestAll(newprofithighs);
def distancefromentry = maxprofit - entry;
def PRF = if newprofithighs<entry then AbsValue(((newprofithighs-entry)/newprofithighs)-1) else (maxprofit-newprofithighs)/(maxprofit-entry);
AddLabel(yes, "PRF: " + AsPercent(PRF),color.LIGHT_ORANGE);
# Profit Risk Factor
# epete
# 08.10.2021
def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice();
def newprofithighs = if entry > 0 then (if high > high[1] then high else newprofithighs[1]) else double.nan;
def maxprofit = HighestAll(newprofithighs);
def distancefromentry = maxprofit - entry;
def PRF = if qty>0 then
(if newprofithighs<entry then AbsValue(((newprofithighs-entry)/newprofithighs)-1) else (maxprofit-newprofithighs)/(maxprofit-entry)) else
(if newprofithighs>entry then AbsValue(((entry-newprofithighs)/entry)-1) else (newprofithighs-maxprofit)/(entry-maxprofit));
AddLabel(qty<>0, "PRF: " + AsPercent(PRF),color.LIGHT_ORANGE);
@epete did this code fix the problems?@Pensar try this one
Code:# Profit Risk Factor # epete # 08.10.2021 def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice(); def newprofithighs = if entry > 0 then (if high > high[1] then high else newprofithighs[1]) else double.nan; def maxprofit = HighestAll(newprofithighs); def distancefromentry = maxprofit - entry; def PRF = if newprofithighs<entry then AbsValue(((newprofithighs-entry)/newprofithighs)-1) else (maxprofit-newprofithighs)/(maxprofit-entry); AddLabel(yes, "PRF: " + AsPercent(PRF),color.LIGHT_ORANGE);
I would think that when you close out the trade that since the quantity == 0 at that point, nothing needs to be done.Mine worked, but yours didn't. I also had to replace "double.nan" with "high" to not get the NaN on the label. Since I want to show this label only when I'm in a position; do I have to "reset" the values (e.g., newprofithighs) for the 2nd time I'm in a trade? Say I buy one set of shares, watch the PRF change; then sell the stock; then buy shares again. Will the "def" values need to be reset? i.e., "if qty=0 then reset variable values..."
Try looking at a volatility trailing stop or the Parabolic SAR, they are similar to what you want. Coding this might be complicated.Now I'm going to attempt to create an "accelerating trailing stop". I'm willing to take a larger loss (of profits) in the early stages of the stock run-up (so if my stock goes from $10 to $20 I'm willing to take as much as a 100% loss of that profit ($10) - which would equate to a washout; but as profits get larger, I'm not as willing to give as much of the profit back. I want the accelerated trailing stop to get tighter and tighter as the profits go up. My PRF then could continue to go from 100% down to say 40% (if that were my MPF "minimum profit factor") but never would I give back anything more than a defined MPF. Does that make sense?
i'm experimenting with making a version of this.Mine worked, but yours didn't. I also had to replace "double.nan" with "high" to not get the NaN on the label. Since I want to show this label only when I'm in a position; do I have to "reset" the values (e.g., newprofithighs) for the 2nd time I'm in a trade? Say I buy one set of shares, watch the PRF change; then sell the stock; then buy shares again. Will the "def" values need to be reset? i.e., "if qty=0 then reset variable values..."
Now I'm going to attempt to create an "accelerating trailing stop". I'm willing to take a larger loss (of profits) in the early stages of the stock run-up (so if my stock goes from $10 to $20 I'm willing to take as much as a 100% loss of that profit ($10) - which would equate to a washout; but as profits get larger, I'm not as willing to give as much of the profit back. I want the accelerated trailing stop to get tighter and tighter as the profits go up. My PRF then could continue to go from 100% down to say 40% (if that were my MPF "minimum profit factor") but never would I give back anything more than a defined MPF. Does that make sense?
def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice();
def newhigh = if entry>0 then (if high > high[1] then high else newhigh[1]) else high;
def maxhigh = HighestAll(newhigh);
#def maxhigh = if IsNaN(maxhigh[1]) then newhigh else if newhigh > maxhigh[1] then newhigh else maxhigh[1];
AddLabel(yes, "entry: " + entry + " newhigh: " + newhigh + " maxhigh: " + maxhigh, color.LIGHT_GRAY);
I'm trying to save the maximum high value of a stock/option during a trade. What I get with the following code is always the overall highest value - not the highest value within the trade. Let's say my entry price is 2.6; the newhigh is 2.72; and what I'd like to capture is the highest value SINCE my entry.
It doesn't seem to matter whether I use the first "def maxhigh" or the second "def maxhigh" (commented out). They both produce the overall high value of 17.76. But I don't want that value. I want the highest value since I bought into the stock/option.
Code:def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice(); def newhigh = if entry>0 then (if high > high[1] then high else newhigh[1]) else high; def maxhigh = HighestAll(newhigh); #def maxhigh = if IsNaN(maxhigh[1]) then newhigh else if newhigh > maxhigh[1] then newhigh else maxhigh[1]; AddLabel(yes, "entry: " + entry + " newhigh: " + newhigh + " maxhigh: " + maxhigh, color.LIGHT_GRAY);
declare lower;
def maxhigh =
if GetQuantity() != 0 then
if GetQuantity()[1] == 0 then high
else Max(high, maxhigh[1])
else maxhigh[1]
;
plot test = maxhigh;
AddLabel(yes, maxhigh, Color.LIGHT_GRAY);
i haven't used getaverageprice() , so this is theoretical
maybe using getquantity is a better variable to use, instead of GetAveragePrice() , as slippage used in his code.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
Short/Long Entry Line on Chart for ThinkorSwim | Indicators | 3 | ||
Bullish Market Entry Point for ThinkorSwim | Indicators | 36 | ||
N | Entries and Target For ThinkOrSwim | Indicators | 8 | |
Darvas Box with Target Levels for ThinkorSwim (Scalping Strategy) | Indicators | 19 | ||
Take Profit Indicator for ThinkorSwim | Indicators | 1 |
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.