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.
I don't think there's really an advantage either way in this case. I just usually default to getting the quantity first because it tells me if the position is long or short. And the function name has fewer letters making for slightly shorter lines of code.
Ok, I've done some more fiddling and now would like to store the maximum profit/loss percentage. Here's my code. I calculate a profit/loss percentage (PLPercent) and then use the variable MPL to save whatever the maximum profit/loss is.
My problem is it's not always saving the highest PLPercent value. Sometimes it does, but other times it drops back down to the current PLPercent. Is that because the previous bar has a range, and my code may be picking up something different than what I expect?
The last line accounts for long or short positions.
Code:
def PLPercent = if qty>0 then
(qty * close - qty * GetAveragePrice()) / (qty * GetAveragePrice()) else
(qty * GetAveragePrice() - qty * close) / (qty * GetAveragePrice());
AddLabel(qty<>0, "P/L: " + AsPercent(PLPercent),
if PLPercent==0 then Color.LIGHT_GRAY else if PLPercent>0 then Color.GREEN else Color.RED);
def MPL = if GetQuantity()==0 then 0 else if GetQuantity()[1]==0 then PLPercent else (if GetQuantity()>0 then Max(PLPercent, MPL[1]) else Min(PLPercent, MPL[1]));
Ok, I've done some more fiddling and now would like to store the maximum profit/loss percentage. Here's my code. I calculate a profit/loss percentage (PLPercent) and then use the variable MPL to save whatever the maximum profit/loss is.
My problem is it's not always saving the highest PLPercent value. Sometimes it does, but other times it drops back down to the current PLPercent. Is that because the previous bar has a range, and my code may be picking up something different than what I expect?
The last line accounts for long or short positions.
Code:
def PLPercent = if qty>0 then
(qty * close - qty * GetAveragePrice()) / (qty * GetAveragePrice()) else
(qty * GetAveragePrice() - qty * close) / (qty * GetAveragePrice());
AddLabel(qty<>0, "P/L: " + AsPercent(PLPercent),
if PLPercent==0 then Color.LIGHT_GRAY else if PLPercent>0 then Color.GREEN else Color.RED);
def MPL = if GetQuantity()==0 then 0 else if GetQuantity()[1]==0 then PLPercent else (if GetQuantity()>0 then Max(PLPercent, MPL[1]) else Min(PLPercent, MPL[1]));
when i have problems, i simplify things.
take your last formula and make several formulas from it. then use bubbles to display the values on each bar.
if you put multiple variables in 1 bubble, use this to force data on a new line.
+ "\n" + to
IsNan() tests to insure that a number is present, meaning that there is an active trade... In the code above entry will either be filled with the value of the previous entry, entry[1], if GetAveragePrice() doesn't return a price or the current GetAveragePrice() if it does contain a price...
Ruby:
def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice();
@BenTen Is something like this possible for TOS? The boxes mark when you entered\exited a position and it tracks how long you were in it. (How many candles you were in it and it shows how many hours). Box is green if it's positive, and red if it's negative.
If you wish, you can submit a platform feature request to TOS Support (via the Support tab on your platform) asking for additional aggregations to be added.
I was wondering if there is a label on the chart to see when the last time a ticker was traded, possibly even with the P/L of that result of the last time the ticker was traded.
I mainly want a quick reference for the wash sale rule basically.
Is there is a label on the chart to see when the last time a ticker was traded, possibly even with the P/L of that result of the last time the ticker was traded.
I mainly want a quick reference for the wash sale rule basically.
This is awesome!! I trade outside of TD as well since I'm under 25k and use multiple brokers to bypass the PDT rule and play more positions. I see this removes the manual input, which is great I love that. Thanks for this.
I would like to know when I place a position if I can have automatically add in my graph a line and a bubble with my entry price. It's this possible to be create in thinkscrip?
What I am looking to achieve is to plot a green arrow pointing up on the chart at the price I bought into the trade. No details about the number of shares or time or anything else. Just the arrow up at my entry on the chart, and when I sell I want to plot a red arrow pointing down at the price I sold. Think about it as the TOS price bubble/ pill but without any number of shares or price, just the red or green arrow.
@Jony Giant@MerryDay . . . maybe this? I believe I read once that portfolio functions are limited to one year of data (please don't quote me on that) so not certain how far back in time this code will show. It appears to work for recent trades.
Code:
def x = GetQuantity();
plot start = x[1] == 0 and x <> 0;
start.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
start.setdefaultcolor(color.cyan);
plot end = x[1] <> 0 and x == 0;
end.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
end.setdefaultcolor(color.magenta);
@Jony Giant@MerryDay . . . maybe this? I believe I read once that portfolio functions are limited to one year of data (please don't quote me on that) so not certain how far back in time this code will show. It appears to work for recent trades.
OMGOODNESS!
I was so focused on what was not there, that I never gave thought to the 'was there' and 'now is not there' state having meaning. It is so elegant! It has such potential.
This could be SUCH a game changer in our portfolio analysis capabilities. @Pensar you continually amaze!
Thank you! For sharing!
@Jony Giant@MerryDay . . . maybe this? I believe I read once that portfolio functions are limited to one year of data (please don't quote me on that) so not certain how far back in time this code will show. It appears to work for recent trades.
Code:
def x = GetQuantity();
plot start = x[1] == 0 and x <> 0;
start.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
start.setdefaultcolor(color.cyan);
plot end = x[1] <> 0 and x == 0;
end.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
end.setdefaultcolor(color.magenta);
You absolute LEGEND. This is exactly what I was looking for!! This should definitely be integrated in TOS by default. THANK YOU!
PS: It looks like it plots the arrows when I open the position and when I close it, but it doesn't plot anything if I add more shares after the trade is open. Is still a huge leap though! Thank you!
You absolute LEGEND. This is exactly what I was looking for!! This should definitely be integrated in TOS by default. THANK YOU!
PS: It looks like it plots the arrows when I open the position and when I close it, but it doesn't plot anything if I add more shares after the trade is open. Is still a huge leap though! Thank you!
@Jony Giant If you want to show the adds/profit-takings to your position (for long positions only) add this to the code -
Code:
plot add = x > 0 and x > x[1];
add.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
add.setdefaultcolor(color.white);
plot subtract = x > 0 and x < x[1];
subtract.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
subtract.setdefaultcolor(color.gray);
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.
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.