Script when price goes above previous months High


Category:
0
0

Here is what i have so far

Some on my WL registered properly, others not

Thanks for any assistance

 

input builtInPeriod = aggregationPeriod.DAY;

input periodsBack = 1;
Def high = close(period = aggregationPeriod.MONTH)[periodsBack];

def CrossAboveMoHigh = close crosses above high(period = builtInPeriod)[periodsBack]within 1 bar;

plot value = if crossAboveMoHigh then 1 else 0;

Marked as spam
Posted by (Questions: 11, Answers: 16)
Asked on June 20, 2020 1:14 pm
161 views
0
Private answer

You should not even be able to apply that code to a watchlist. Secondary aggregation periods are not permitted in custom watchlist columns. There are a lot of problems with that code. None of it should work.

If you are here to learn something I will take the time to give you the analysis of your code so you can see where you went wrong.

  1. input builtInPeriod = aggregationPeriod.DAY;
    1. Secondary aggregation periods are not supported
  2. Def high = close(period = aggregationPeriod.MONTH)[periodsBack];
    1. You created a variable name that is already used as a reserved word. You should not ever use the word "high" as a variable name.
  3. def CrossAboveMoHigh = close crosses above high(period = builtInPeriod)[periodsBack]within 1 bar;
    1. Here you are checking if the close of the current time frame is crossing above the previous high of the Daily time frame "builtInPeriod". Nothing in this code even attempts to find the monthly high, because....
    2. .... secondary aggregation periods are not supported

Now, how do we actually do this. Well we cannot use secondary aggregation periods. So we need a statement that checks for a new month and begins tracking the highest high of each month. This requires recursion, which is an advanced coding technique. At the beginning of each new month, we need to grab the previous bar's value of that highest high from the previous month and carry it through until the next month begins. Confused? Wait until you try to read the code for this solution.

def newMonth = GetMonth() <> GetMonth()[1];
rec trackMonthlyHigh = if newMonth then high else if high > trackMonthlyHigh[1] then high else trackMonthlyHigh[1];
rec previousMonthlyHigh = if newMonth then trackMonthlyHigh[1] else previousMonthlyHigh[1];
plot data = close > previousMonthlyHigh;

This code gets applied to a custom watchlist column that is set to the daily time frame.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 20, 2020 2:11 pm
0
Thanks for the correction and information. When loaded into watchlist I get the following error: Error:Trying to self assign a non initialized rec:trackMonthlyHigh Thanks for any further input
( at June 20, 2020 4:07 pm)
0
Sorry about that. I caught that error when I tested it on the watchlist column but failed to copy that over to my answer. I have updated the code in my answer to correct this error.
( at June 20, 2020 5:33 pm)
0
Thanks for the help...I'm learning
( at June 20, 2020 6:17 pm)