Question about writing code for: [last time price crossed specific price] & [high/low since then]

Questions about MultiCharts and user contributed studies.
class in dodge
Posts: 7
Joined: 24 Sep 2020
Has thanked: 4 times

Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby class in dodge » 29 Nov 2020

Hey y’all, I’m relatively new to Powerlanguage. I’ve spent a fair bit of time trying to figure out the most efficient way to write code to create two variables:

  • the last time price crossed a specific value going in a specific direction, and
  • the pivot high/low since that time.

While I’d be delighted if the code just landed in the responses here, I’d also be super grateful for any pointers, such as keywords that might be helpful in implementation.

For example, I wonder if I need to use a (repeat-until) loop, going back bar by bar until the condition of having Crossed the specific value in the specific direction becomes true? And from there, do I need to create a data set of all bar highs/lows since then, and use something like MinList/MaxList to find the high/low?

Any pointers, even if just to say I’m likely barking up the right/wrong tree(s) with the keywords I'm considering using, would be uber appreciated!

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby TJ » 30 Nov 2020

To get a specific data, look back is one method.
But if this is a regular occurance, it is easier to capture the data while it happens.

eg. on a moving average cross over,
you should evaluate that situation on every bar.
If the MA have crossed, then immediately capture the data you want.

class in dodge
Posts: 7
Joined: 24 Sep 2020
Has thanked: 4 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby class in dodge » 30 Nov 2020

Thanks TJ! I have a follow-up question:

I have an indicator that gives me an alert. The moment it gives that alert is also the moment I discover the specific price I want to then look back to know when it was last crossed. (If I'm understanding what you suggested correctly, I realize now this is a potentially important detail that I didn't include in my first message.) I'm wondering if what you suggested will work, given that I don't know what the specific data point is that I'm looking for until at least two (and probably several) bars after it happened.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby TJ » 30 Nov 2020

Yes, just capture the data at the moment. Makes future analysis so much easier.
You did not say what data you are interested in, so I can't give you any specifics.

For example, if you want the price of the fast MA when it crosses above the slow MA,
then just assign the price to a variable at that moment, and you can refer to the variable whenever you need to.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby TJ » 30 Nov 2020

Yes, just capture the data at the moment. Makes future analysis so much easier.
You did not say what data you are interested in, so I can't give you any specifics.

For example, if you want the price of the fast MA when it crosses above the slow MA,
then just assign the price to a variable at that moment, and you can refer to the variable whenever you need to.
The same thing with bar number or date/time.

class in dodge
Posts: 7
Joined: 24 Sep 2020
Has thanked: 4 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby class in dodge » 30 Nov 2020

The thing is I don't have any way to identify the price I want to capture when it first happens (for instance, I'm not looking for something like 'when the fast MA crosses the slow MA'). It's more like an alert goes off when price hits 153 while moving down, and at that moment I want to know the last time price crossed 153 while going up (because I want to identify the highest it's been since then).

Unfortunately there's no consistent rule I can find that will help me identify that moment when price crosses 153 while moving up; I only know that's the number I want once the alert goes off two or more bars later, which is why I'm wondering if I need a loop that goes back bar by bar until that crossing condition is met.

I really appreciate your help and time, TJ. I'm aware that I don't know what I don't know, and I hope that dynamic isn't causing miscommunication.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby TJ » 30 Nov 2020

First, you have to identify what you want.

You have written a lot in the above post, but I still do not know what you need.
You must make a list -- not just narrations, but a LIST.
You need to specify exactly which data you want to capture, and at what juncture.
After that, people can help you to write your logic.

See post #6
viewtopic.php?t=11713

class in dodge
Posts: 7
Joined: 24 Sep 2020
Has thanked: 4 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby class in dodge » 30 Nov 2020

Thanks so much for the post explaining how I can better present my question. I've attached two photos with screen shots, descriptions, and an outline that hopefully better details what I'm trying to do. (I also attached the info as a PDF in case that's easier to read.)

Finding Pivot High 1 of 2.png
(85.17 KiB) Not downloaded yet
Finding Pivot High 2 of 2.png
(164.57 KiB) Not downloaded yet
Determining Previous Pivot High.pdf
(172.73 KiB) Downloaded 92 times

class in dodge
Posts: 7
Joined: 24 Sep 2020
Has thanked: 4 times

Re: Question about writing code for: [last time price crossed specific price] & [high/low since then]

Postby class in dodge » 02 Dec 2020

First, you have to identify what you want.

You have written a lot in the above post, but I still do not know what you need.
You must make a list -- not just narrations, but a LIST.
You need to specify exactly which data you want to capture, and at what juncture.
After that, people can help you to write your logic.

See post #6
viewtopic.php?t=11713
My hope is that the first page of the PDF in my last post makes clear what data I'm looking to capture and when I'm looking to capture it, and that the second page is a clear enough list of the color coded screen shots on the first page. (I couldn't figure out how to get the images to show up in the post without having to click on them.)


Return to “MultiCharts”