Volume based decision to skip codes


Category:
0
0

Is there a way to skip multiple definitions and calculations if a particular condition is met? For example, I have been having problems making volume-based calculations on SPX. Is there a way to skip entire lines of codes, where there are multiple definitions and calculations if there is no Volume or predefined Symbols? I tried “switch and case”, but that is for plotting or defining one variable. I tried the if-then statement, but I couldn’t make definitions inside then {}.

For example:

If the subject Symbol is SPX or subject Symbol doesn’t have volume skip {

def x = …;

def y = …;

plot myDecisions = x + y;

etc.

}

 

 

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on June 3, 2022 8:00 am
49 views
0
Private answer

When I was researching a potential solution to your previous post...

https://www.hahn-tech.com/ans/script-which-adapts-when-volume-not-available/

...I also looked for any available methods for isolating code based on a volume condition.

In Thinkorswim, it is not possible to place a "def" statement within an if/then/else structure. Each "def" statement must be declared globally. One way you can get around this is by using a script structure. Details here:

https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script

But for your previous post I also tested a simple method of hiding the volume component within a script structure and it still failed. Not saying it's impossible. But I only have 15 minutes to provide each free solution in the Q&A Forum and I don't have time to fully research all potential avenues. As it is I already spent 40 minutes researching yesterday's solution. I broke my rules, but I felt the topic was an important one to document.

Custom Functions

The next thing I suggest you test is this. For the sections of code you want to isolate, create two different custom chart studies. Then within your main chart study you test for volume, as described in the previous solution linked above. Based on that test, use conditional statements to reference one of those other two custom chart studies. In this way you are using the other two chart studies sort of like a "method" or "function" which you might employ in C++ or C#.

These "external" chart studies can be constructed so that they can receive a variety of inputs and outputs.

Here is a brief example using one of the built-in studies included with Thinkorswim:

def highMa = SimpleMovingAvg(hlc3, 8).SMA;
def lowMa = SimpleMovingAvg(close, 21).SMA;
def checkPrice = close > 100;
plot switchMa = if checkPrice then highMa else lowMa;

The first two lines are referencing external studies named "SimpleMovingAvg". Each one is supplying different input parameters. There is only one output available "SMA". But if that chart study included multiple plot statements (BollingerBands?) you could reference each of them directly. The third line of code checks if the current price is greater than 100. Just some arbitrary number I picked for testing. Plot this on a stock that is greater than 100 and this test study will plot the SMA from the first line of code. For stocks less than 100 this test study will plot the SMA from the second line of code. And if the stock plotted on the chart includes some bars above 100 and some below, then the first SMA would plot for those bars with close greater than 100 and the second SMA would plot for those bars with close less than 100.

But I suspect you will still run into that block wall described in the previous post. Only way to know for sure is to test it. And I am out of time.

For those viewers new to writing code. Please understand the concepts described here are super advanced. Don't be concerned if you read this post and feel completely lost.

Marked as spam
Posted by (Questions: 37, Answers: 4091)
Answered on June 3, 2022 8:44 am