Start Date for an Indicator

I wish to code a stop loss indicator commencing with a given date ( The date on which I purchased the stock)

Would someone be able to advise me how to code the start date into my indicator. I cannot find any reference
as to how to do this
Michael Mulcahy

There are multiple interpretations I get while reading your post, can you elaborate more with a specific example and real values.

My initial post on rereading was very poor. Thanks for pointing this out. Please accept my apologies.

My aim

To set up a stop loss indicator based on the HHV (h ,looking back to the date from which I purchased the stock.)

The value of this would be reduced by 10% and plotted on the screen to give my stop loss.

To this end I only want the indicator to be displayed on the screen from this date.

My current indicator formula is

Plot ((HHV(h,200)-(HHV(h,200)*0.1)),"HHV (200)",colorBrightGreen);

200 chosen for development only. Could be any number.

This function will plot for all the data on the screen but will not suite my aim as it refers to 200 days and may well give a HHV very different from my entry point.

My wish is to

Make the look back period a variable based on the number of trading days between the current date and the buy date.

Variable = No. of trading days between current date and buy date = A

So the plot would become

Plot ((HHV(h, A )-(HHV(h, A )*0.1)),"HHV ( A )",colorBrightGreen);

So I am really looking for a mechanism to enter the initial date and then have the system use the current date to calculate the value of “A” the look back period .

Hopefully the indicator will only be displayed commencing at the buy date.

1 Like

First storing a permanent Value

// In a separate part of your code, store the buy Date
// Use some unique naming methodology for the variable name
setBuyDT = DateTime();        // or suitable code
StaticVarSetText( "Symb1Buy1", DateTimeToStr( setBuyDT ), True );

Do read about Persist=True in Static Variables.

Retrieving and using it.

// In another part of your code, where you want to retrieve the value
// I will skip NZ and other validation checks, bad get variable will be erroneous.
getBuyDt = StrToDateTime( StaticVarGetText( "Symb1Buy1" ) );

barsSinceBuy = DateTimeDiff( LastValue( DateTime() ), getBuyDt )  / Interval();

// Now you have the Bars since you bought

The above computes bars, if your chart is Daily, then you can infer it as Trading Days :slight_smile:

Is this good enough a direction?

Edit: barsSinceBuy would be the value referring to A in your code.

@MMulcahy, depending on your needs a simpler solution might be to use HighestSince() instead of HHV():

Thanks for you help.
I decided not to proceed with the indicator.

Thanks for your help
I decided not to proceed with the indicator