Keep a count of a certain trigger within the day


Category:
0
0

Hi, I would like to keep count of a “buy signal” that I have defined in a study on an intraday chart. So basically increment to 1 if certain conditions are met, then back to 0 is a “sell signal” is triggered or if a new day starts.

Ex: rec count = if bullConditionsMet then count[1] + 1 else count[1];

Since I am using a 3 min chart agg period, 20 days interval it counts from the beginning of the displayed chart. I tried using the GetYYYYMMDD() function but since my displayed chart is intraday, the returned value is erroneous (I assume). I Have a hard time approaching this problem

First question, How can I go about defining a new day on an intraday chart so that I can use that variable in my script.

Second question, How can I reset count to zero if my sell conditions are met, or if the “New day” variable arises?

Thank you so much.

 

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on September 16, 2022 9:36 am
51 views
0
Private answer

There are many examples of this already posted throughout this forum. But since you are asking for a very generic solution which can be applied to a wide range of solutions I will provide something that fits that description.

declare lower;
def newDay = GetDay() <> GetDay()[1];
def sma = Average(close, 21);
def buySignal = close[1] < sma[1] and close > sma;
def sellSignal = close[1] > sma[1] and close < sma;
rec trackBuySignal = if newDay or sellSignal then 0 else if buySignal then 1 else trackBuySignal[1];
plot signalStatus = trackBuySignal;

Apply this code to a new chart study and it will plot a line on the lower subgraph showing the status of the buy signal. When a new trading session begins, the signal is reset to zero. When a new buy signal appears, the status is changed to 1. Then at the next sell signal the status reverts back to zero.

Screenshot below shows how this looks on the chart.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 16, 2022 2:44 pm
0
Thank you!
( at September 17, 2022 7:28 am)