Why won't this ZigZag line plot?

The short answer is that I've goofed up :). I'm hoping for a longer answer that will help me understand what I'm doing wrong. My purpose is to simply plot a line that I calculate myself, that is not based on a price input. I suspect the problem has to do with the first bar not having a previous bar, but I can't find a clear explanation of how to deal with that situation. Here's what I'm trying:

Code:
def signal;
def dir;

if signal[1] == 100
    {dir = -1;}
else
    if signal[1] == 50
        {dir = 1;}
    else
        {dir = dir[1];}

if dir == -1 then
    {signal = signal - 1;}
else
    {signal = signal + 1;}

plot line = signal;

This is supposed to create a line that zig-zags up and down between 50 and 100, inclusive. No error in TS editor, but nothing shows on the chart...
 

rad14733

Well-known member
VIP
@MoneyMagnet Why not review several of the existing topics that cover plotting lines... You've already discovered that it isn't all that easy to plot from on bars price to another bars price... Yoy may or may not be able to accomplish whatever it is that you want to do... TOS does have some limitations in this area...
 
What I'm hoping to do is to plot a line based on data that I create, rather than on price. I've spent a good deal of time reading posts so far, and I don't recall anyone doing anything similar. Is this something that others have done?
 

BenTen

Administrative
Staff
VIP
plot a line based on data that I create, rather than on price.

What do you mean by that? Are you going to feed the script each data point manually? Like at a certain price level? It would be helpful if you could post your entire script so we can understand it better.
 
Fair enough; let me elucidate:

For research purposes, it's often useful to control the input to a process in able to learn more about the process itself. With an indicator, the input is price, but price is whatever it is, and can't (we hope!) be controlled. I would like to be able to use this technique to get a better understanding of various indicators and indicator ideas, and to do this I need to be able to supply input data of my own, rather than price.

The best case scenario would be to gin up a stream of data - say, in a spreadsheet - then import that into ToS as an equity, so I could just get the artificial data into the indicator that way. If there's any chance that ToS allows this, I've love to hear about it. I used to do such things with another charting package I used years ago (MT4, maybe?), but I doubt that ToS would accommodate me.

Failing that, I could just mathematically create the desired data input right inside an indicator code, and then use that variable as the input to the indicator itself - something like this:

Code:
def MyData = <code that creates the data>;

plot testchart = Average(MyData, 25);

I believe that something like that should work, if I can get the data created in the first place. In the code above, I've tried to create the data by defining a variable to hold it and using code to increment then decrement the value from 50 to 100 and back, ad nauseum generating a triangle wave. As a first test, I would just plot the data raw, to see if it works, and of course, it doesn't. After some more searching, I suspect that it needs something like a test for NaN, which I read about in the (remarkably sparse) thinkScript documentation, but my every attempt makes the TS Editor cough up errors. I clearly don't understand how to get it done!

One thing I'd like to do with this method is to feed a sine wave of various wavelengths into an indicator to see how strongly it is filtered out. The frequency response curve of any price analog (i.e., moving average, etc.) could be determined this way, which could prove useful. In particular, since moving averages are in essence digital filters, this method could be used to determine the impulse response of any moving average by convolving it with a pulse input (running a data stream with a single positive spike value through it), and I personally think that would be at least fascinating... if I can figure/find out how to get past the peculiarities of thinkScript.
 

rad14733

Well-known member
VIP
@MoneyMagnet While your concept does sound creative it is beyond the scope of the capabilities of the TOS platform... For starters, you can't import data other than symbol lists... On top of that, TOS doesn't support arrays so it couldn't work off of any form of data that you might want to supply manually... It's just not what TOS was designed for... True, some other platforms have some form of testing capabilities beyond what TOS offers but I'm not even sure any of them would be suitable for what you have described... I think a better option would be to do such research of numbers and plotting from within a spreadsheet for proof-of-concept stuff... We have to live within the parameters of what TOS has to offer as I highly doubt they will be adding any semblance of the capabilities you are in search of...
 
This code does exactly what I wanted with regard to the sine wave input signal. Derived from some code at tlc.thinkorswim.com.

Code:
declare lower;
input Amplitude = 10;
input WaveLength = 20;
input Period = 5;
def w = 2 * Double.Pi / WaveLength;
def x = CompoundValue(1, x[1] + 1, 0);
plot signal = Amplitude * Sin(w * x);
plot avg = Average(signal, Period);
 
And this code creates a pulse, then plots it along with its moving average, generating an impulse response curve.

Code:
declare lower;

input Wavelength = 20;
input Period = 5;

def interval = BarNumber() / Wavelength == RoundDown(BarNumber() / Wavelength, 0);
plot pulse = CompoundValue(1, interval, 0);

plot avg = Average(pulse, Period);

Thanks for your help, guys (y):cool:
 

rad14733

Well-known member
VIP
This code does exactly what I wanted with regard to the sine wave input signal. Derived from some code at tlc.thinkorswim.com.

Code:
declare lower;
input Amplitude = 10;
input WaveLength = 20;
input Period = 5;
def w = 2 * Double.Pi / WaveLength;
def x = CompoundValue(1, x[1] + 1, 0);
plot signal = Amplitude * Sin(w * x);
plot avg = Average(signal, Period);
So you just wanted an identical repeating sine wave...??? Or is this just experimental for proof-of-concept...???
 
Like I said, I wanted to be able to mathematically generate a signal, then feed it into an indicator. Sine, pulse, random walk, whatever. Once I can create my own dataset, I can use it for research purposes. Pretty cool, eh? :geek:
 
So, the answer I was looking for in the original post was "You need to use the CompoundValue() function to initialize a variable, so it will have a value for the first bar, then be able to update future bars based on past bars." Try searching here or on tlc.thinkorswim.com for "create mathematical data as input for an indicator" and see how useful the result is. Without knowing the proper terms for something, text searching can be nearly useless. I found the answer when I went to see what it might take to calculate a sine curve, and came across the code in the article on the Sin() function. I noted the obscurely named CompoundValue() function, and went to see what the heck that was all about. Bingo.

If my research exposes anything earth-shattering, I'll be sure to share it!
 

Similar threads

Top