Shift plot one bar to the left


Category:
0
0

Hi Pete,

Is it possible to setpaintingstrategy the previous plot?  I want to be able to put a blue point on top of an RSI if an RSI peak has formed.

Thanks

 

plot RSIpeak= if (rsi < rsi [1] and rsi [1] > rsi [2])  then rsi [1] else double.nan;

RSIpeak.setpaintingStrategy(paintingStrategy.POINTS);
RSIpeak.setdefaultColor(color.green);
RSIpeak.setlineWeight(1);

Marked as spam
Posted by (Questions: 8, Answers: 2)
Asked on May 16, 2020 7:32 am
57 views
0
Private answer

The only way to move the dot to the left is to make the code read into the future. So long as you understand this, there is no problem. When you don't understand this, it lead to problems. Such as if you wanted to backtest a strategy. You NEVER want to read the future in a backtesting strategy because in a live trading environment you can never read the future.

To shift this plot one bar to the left, we have two methods to work with.

First, we can modify the signals itself as follows:

plot rsiPeak= if (rsi[-1] < rsi[0] and rsi[0] > rsi[1]) then rsi [0] else Double.NAN;

However that way is very confusing. And if you have already built other lines of code that use that original signal at it previous position, you break a bunch of stuff. So the second method here is preferred, because it preserves the original signal location and it much easier to actually see what it's doing.

def rsiPeak= if (rsi < rsi[1] and rsi[1] > rsi[2]) then rsi[1] else Double.NaN;
plot rsiPeakShifted = rsiPeak[-1];
rsiPeakShifted.SetpaintingStrategy(paintingStrategy.POINTS);
rsiPeakShifted.SetdefaultColor(Color.GREEN);
rsiPeakShifted.SetlineWeight(1);

Key points to take away from this. The index values are how you reference bars in the past AS WELL AS in the future. Positive integers [1] reference bars in the past while negative integers [-1] reference bars in the future.

Comments on the code syntax. Notice the differences between your original code statement and what I have provided. Notice the cammelCase is applied correctly for the variable named rsiPeak. Also notice I have properly capitalized the characters for Double.NaN. Another change was to remove the extra spaces between the variable names and the indexes. So this is wrong: rsi [1] and this is correct: rsi[1]. Built-in functions and methods such as SetPaintingStrategy, SetDefaultColor and SetLineWeight all begin with capital letters. This differs from variable names which begin with a lower case. This makes the code much easier to read in a plain text environments.

You can lean much of these style and syntax rules by studying the examples provided in the Thinkorswim language reference. Although even Thinkorswim developers have been known to occasionally break these rules in their own examples. Why is all of this important? Because in many programming languages the difference between RSIpeak and rsiPeak results in two completely separate variables. Likewise for the other elements of your code that I corrected.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on May 16, 2020 9:00 am
0
Thank you for added extra information. You're the best..
( at May 17, 2020 5:47 am)