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.
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);