PPO column based on conditional values


Category:
0
0

Hi I’m trying to create a ppo column so I can sort by it in the mobile phone app. Currently I have a column that says if all 3 are true then give me a 1 or if any are false then 0.

I’m trying to get it so that if the shortest time frame is true then assign a value of 900 else 0, if the second time frame is true assign a value of 90 else 0, and the 3rd condition is true assign a value of 9 else 0.

So if all 3 conditions are true 999 would be the plot
If only the first and 2nd then 990
909 first and third ppo true, second is false
99
90
9
0

This is the conditional value I have now but can’t get it to be the above. Thanks for any help you can provide!
ppoenhanced(“fast period” = 1, “slow period” = 5, “signal period” = 1).”PPO” is greater than 0 and ppoenhanced(“fast period” = 5, “slow period” = 13, “signal period” = 1).”PPO” is greater than 0 and ppoenhanced(“fast period” = 21, “slow period” = 34, “signal period” = 1).”PPO” is greater than 0

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on January 10, 2022 1:35 am
190 views
0
Private answer

Whenever you are working with custom chart studies it's important that you NOT try to reference the custom study in your code. Meaning that its best if you avoid using the Condition Wizard to build your solution. Why? It has been observed that referencing a custom study in the Condition Wizard has been shown to produce unreliable results in the past. It may work in most cases, but not all. So the safest thing is to always use the entire code in your solutions when a custom study is involved.

For this solution I have embedded your code within a script structure. This allows us to reference the same set of computations multiple times while using a different set of inputs in each instance. This saves a lot of space and makes the code much easier to read and maintain. If you have not worked with script structures in Thinkorswim before I suggest you read up on them here:

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

Here is the code that you can drop into a custom watchlist column to produce the output you specified:

script myPPO {
input fastPeriod = 8;
input slowPeriod = 17;
input signalPeriod = 9;
input price = close;
def fastEma = ExpAverage(price, fastPeriod);
def slowEma = ExpAverage(price, slowPeriod);
def periodOK = fastPeriod < slowPeriod;
plot ppo = if periodOK then (((fastEma – slowEma) / slowEma) * 100) else 0;
}
# ppoenhanced(“fast period” = 1, “slow period” = 5, “signal period” = 1).”PPO” is greater than 0;
def conditionOne = if myPPO(1, 5, 1) > 0 then 900 else 0;
# ppoenhanced(“fast period” = 5, “slow period” = 13, “signal period” = 1).”PPO” is greater than 0;
def conditionTwo = if myPPO(5, 13, 1) > 0 then 90 else 0;
# ppoenhanced(“fast period” = 21, “slow period” = 34, “signal period” = 1).”PPO” is greater than 0;
def conditionThree = if myPPO(21, 34, 1) > 0 then 9 else 0;
plot data = conditionOne + conditionTwo + conditionThree;

One final note about this solution. I have included your original conditions as comment lines in this code. This will help you match your original conditions to the new code structure I created to plot the values as you specified.

Edit: I thought it would be helpful to convert this code to plot as a lower study with a chart label displaying the same value as the watchlist column. The following code displays three plots on the lower sub-graph. I have not included any styles for the plot statements so you can adjust them however you like. I personally found that histogram style makes things pretty clear.

declare lower;
script myPPO {
input fastPeriod = 8;
input slowPeriod = 17;
input signalPeriod = 9;
input price = close;
def fastEma = ExpAverage(price, fastPeriod);
def slowEma = ExpAverage(price, slowPeriod);
def periodOK = fastPeriod < slowPeriod;
plot ppo = if periodOK then (((fastEma – slowEma) / slowEma) * 100) else 0;
}
def conditionOne = if myPPO(1, 5, 1) > 0 then 900 else 0;
def conditionTwo = if myPPO(5, 13, 1) > 0 then 90 else 0;
def conditionThree = if myPPO(21, 34, 1) > 0 then 9 else 0;
def data = conditionOne + conditionTwo + conditionThree;
AddLabel(yes, "PPO Condition: " + data, Color.WHITE);
plot ppoOne = myPPO(1, 5, 1);
plot ppoTwo = myPPO(5, 13, 1);
plot ppoThree = myPPO(21, 34, 1);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 10, 2022 10:08 am
0
Omg thank you!!! This works exactly as I wanted! You are amazing! Thank you for the link to script structures I will read and study the material. My solution to the problem was a big mess as I tried a series of if , and if else statements that I couldn’t get right. Your solution was much more elegant and I appreciate the help! Thanks again
( at January 11, 2022 11:36 pm)