Script which adapts when volume not available


Category:
0
0

Hi Pete,

I have a Thinkscript-specific question.
I’m writing a script that uses volume as input and other price data and its upper study. It uses AssignPriceColor and different painting strategies.
However, as you know, some symbol doesn’t provide volume.
I found out that when there is volume, the script doesn’t calculate the majority of the script, even when the volume isn’t needed to finish some of the script. Is there a way to have the script run one way if there is volume and another way if there isn’t volume?

Also, I have a script to call other symbol’s volume as lower study, as in “declare lower;”. It runs fine with symbols with volume, but the symbol like “SPX” it doesn’t show up at all even when there is required input from “SPX”. What is a way to fix this?

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on June 1, 2022 10:54 pm
143 views
0
Private answer

I updated the title of your question to something I hope will be easier for others to locate when searching for this specific question. It should be a common request but this is the first time it's been posted in this forum.

I was intrigued by your request and I was certain there would be a solution. Or at least I was 100% sure I could devise a section of code to put this to the test. So the fact that you did not include your own code as an example to work with does not prevent me from offering my response.

During testing I found that any use of the keyword "volume" within a plot statement causes Thinkorswim to completely disable the chart study when SPX or some other non-volume ticker symbol is loaded into the chart. This happens even when the reference to "volume" is applied indirectly. And also occurs even if you try to trick the code by using "volume" in a mathematical formula. Shockingly, even if we test for !IsNaN(volume) it does not isolate that from the code at all. Yes, I really did push this as far as I could imagine.

For those who want to play around with this (or see the proof for themselves) I offer the following script:

def maOne = Average(close, 50);
def checkVolume = !IsNaN(vwap);
def checkThis = volume;
#plot thisFails = if !checkVolume then maOne else checkThis;
plot thisWorks = if !checkVolume then maOne else close;

The first line is a simple moving average. The next line checks if vwap != NaN. Why vwap? Because if I tried to use the "volume" keyword in that line Thinkorswim would remove the study from the chart even if I avoided using it directly in a plot statement. So vwap works as a proxy for volume data. At least we can write a line of code that is able to check if volume data is available. But that's as far as we can go. It's pretty clear from this testing that if "volume" somehow leaks into any plot statement Thinkorswim automatically removes the study from the chart.

The third line is the variable which contain the volume data.

The last two lines are interchangeable. Move the "#" symbol to select which of those two plot statements to apply. When you turn on "thisWorks" and load SPX onto the chart it will plot the close on the chart. When you switch to a ticker symbol that includes volume it will plot the simple moving average.

If you try the same experiment when you turn on "thisFails", you will find it behaves the same when the ticker symbol includes volume but when you switch to SPX the study is completely removed from the chart.

Curiously, if you change "thisWorks" to the following:

plot thisWorks = if !checkVolume then maOne else vwap;

You will find the study does NOT get removed from the chart when SPX is loaded. The study remains, but nothing plots and the title bar of the study indicates that all plot values are "N/A".

The smart folks in the room are now asking, is there a way to reverse the vwap data to reconstitute the volume data? and do so without using "volume" in that process? I don't have time to get into that level of detail in the Q&A Forum. I'm already way beyond my self imposed 15 minute limit for free solutions I provide in this forum.

Well, that's not a solution but it will certainly help anyone who has been spending a lot of time and effort on this. If you want it to work, you need to contact support at TD Ameritrade, present them that section of code, and kindly as them to create a workaround so the platform is able to handle the lack of volume data more gracefully.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 2, 2022 8:28 am
0
Thanks, Pete! It's good to know that I'm not the only one that would have a problem trying to use "volume" with SPX in Thinkscript. I have some experience using C++, but I've fooled around with Thinkscript for only about a couple of months now. I contacted TD Ameritrade at the same time as I wrote on this forum. I'll let you know what I find out and if it is a bug, hopefully, one day have them fix it.
( at June 2, 2022 11:46 am)