Help with Creating Risk/Profit Study

outta_gas

New member
Hi all. I'm new to this and trying to learn as I go. I've been trying to create a script for intraday trading that adds horizontal risk and profit lines on the chart after entering into a position. I found some scripts and sort of morphed and modified them to arrive at the script below. It mostly works, but there are some issues. The main issues are (1) that it doesn't appear fast enough and (2) it doesn't seem to be able to annotate the chart when you enter and exit a position during the same bar. I think I understand why this (same bar entry/exit problem) is based on the script, but don't have a strategy to overcome the issue.

What I am really trying to achieve is already somewhat possible using active trader when the market is open using TRG w/bracket where you can set a limit and stop based on a percentage of your entry (then adjust as needed), but this doesn't work in the premarket and after hours because of the order types, so I would like a tool that shows me the same information so that I have a frame of reference to easily see and execute trade exits. If there is a way to do this already, I would rather not reinvent the wheel, so please let me know if there is something like this that you can point me to.

So some questions:

1- Is there a way to have the study appear on the chart faster?

2- Do scripts execute only when a new bar begins or can they execute upon other prompts- like when you take a position? If they can execute immediately upon entry, I don't think I would care much if the lines disappear after exiting the trade...however, it is nice to be able to go back and look at the script-generated lines on the chart when I analyze my trades at the end of the day.

3- Is there a better strategy to execute the script that would enable the data to show up on the chart and stay there, even if the position is open/closed during the same bar?

4- Do scripts like this work in OnDemand? When I was testing yesterday, I did some actual buying and could see it on the chart, but testing today with OnDemand (market closed), there seems to be an issue. So I am wondering if I broke this, or if it won't work in OnDemand, maybe because of the way the script checks for open positions...like I don't have any real open positions, only virtual ones in OnDemand.

Any suggestions on this would be very welcomed as I admittedly know very very little about this.


Code:
##RiskProfitBasedOnPosition

##Script to set Entry point, Risk, and Profit Lines on the Chart with Labels

input TakeProfitPct = 5;
input TakeProfitPct2 = 10;
input StopLossPct = 5;

def Qty = GetQuantity();
def Prc = GetAveragePrice();


##(This plots your entry and exit points)
Plot Trade = If Prc > 0 then Prc else double.Nan;
Trade.SetPaintingStrategy(PaintingStrategy.horizontal);
Trade.AssignValueColor(color.green);
trade.SetLineWeight(1);
Plot Trade1 = If Prc[1] > 0 then Prc[1] else double.Nan;
Trade1.SetPaintingStrategy(PaintingStrategy.horizontal);
Trade1.AssignValueColor(color.green);
trade1.SetLineWeight(1);

Plot TP = if Qty > 0 then Prc + (Prc * (TakeProfitPct/100)) else double.Nan;
TP.SetPaintingStrategy(PaintingStrategy.horizontal);
TP.AssignValueColor(color.light_green);
TP.SetLineWeight(1);
Plot TP1 = If QtY[1] > 0 then Prc[1] + (Prc[1] * (TakeProfitPct/100)) else double.Nan;
TP1.SetPaintingStrategy(PaintingStrategy.horizontal);
TP1.AssignValueColor(color.light_green);
TP1.SetLineWeight(1);

Plot TP2 = if Qty > 0 then Prc + (Prc * (TakeProfitPct2/100)) else double.Nan;
TP2.SetPaintingStrategy(PaintingStrategy.horizontal);
TP2.AssignValueColor(color.green);
TP2.SetLineWeight(1);
Plot TP21 = If Qty[1] > 0 then Prc[1] + (Prc[1] * (TakeProfitPct2/100)) else double.Nan;
TP21.SetPaintingStrategy(PaintingStrategy.horizontal);
TP21.AssignValueColor(color.green);
TP21.SetLineWeight(1);

Plot SL = if Qty > 0 then Prc - (Prc * (StopLossPct/100)) else double.Nan;
SL.SetPaintingStrategy(PaintingStrategy.horizontal);
SL.SetDefaultColor(GetColor(1));
Plot SP1 = If Qty[1] > 0 then Prc[1] - (Prc[1] * (StopLossPct/100)) else double.Nan;
SP1.SetPaintingStrategy(PaintingStrategy.horizontal);
SP1.SetDefaultColor(GetColor(1));
SP1.SetLineWeight(1);

##AddLabel(Trade > 0, "5% Risk: "+ asDollars(round(Qty*Prc)), color.white);
AddLabel(Trade > 0, "Shares: "+Qty, color.gray);
AddLabel(Trade > 0, "Entry: "+Trade, color.white);
AddLabel(Trade > 0, "Stop: "+SL, color.red);
AddLabel(Trade > 0, "5% Profit: "+TP, color.light_green);
AddLabel(Trade > 0, "10% Profit: "+TP2, color.green);
 
Solution
To try to answer some of your questions

ThinkScript runs most of its calculations on bar close. Oh sure it will give loads of readings during a candle (see threads about repainting indicators) as it uses the last price in place of a close. But once that bar has closed, TS only knows how to get values for OHLCV and perhaps a few other things. It doesn't know anything about what happened durring the bar. So your #3 is likely a no. I don't think #2 will have a yes either since, at bar open you had no position and at the open of the next bar you had no open position (not even at the close of this bar, actually).

Because of the limitations, you probably won't be able to get things to appear 'faster', and if you do they may be false...
To try to answer some of your questions

ThinkScript runs most of its calculations on bar close. Oh sure it will give loads of readings during a candle (see threads about repainting indicators) as it uses the last price in place of a close. But once that bar has closed, TS only knows how to get values for OHLCV and perhaps a few other things. It doesn't know anything about what happened durring the bar. So your #3 is likely a no. I don't think #2 will have a yes either since, at bar open you had no position and at the open of the next bar you had no open position (not even at the close of this bar, actually).

Because of the limitations, you probably won't be able to get things to appear 'faster', and if you do they may be false signals if the bar hasn't closed (assuming your study relies on bar closes for calculations -- whole other ball game if you use opens).

I can't answer #4 as I haven't ever used OnDemand. I tried once and found that without 'real money on the table' my emotions were so different that it didn't allow me to learn to put them aside -- they just weren't there to learn how to control and so I just learned with small positions but real ones.

good luck and have fun. And welcome to the community -- it's a good place.

-mashume
 
Solution

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
371 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