turning indicator into custom watchlist


Category:
0
0

hi pete. so glad you feel much better now!

i have this indicator(code below) called the accountant written by kumobob.

im trying to get the watchlist to turn red when the indicator hits a “sellpoint” and green if the indicator hits a “buypoint”. the very last line of the code (assignbackground) is my attempt on figuring it out but it only seems to work for the green but never works for the red. i would love your help with this!

Ru

code:

 

def LastBar = IsNaN(close);
input DemoLength = 30;
def Price_Data = if LastBar then Double.NaN else close;
def price = if LastBar then 0 else close;
def MA_Data = if LastBar then Double.NaN else Average(close, DemoLength);
def ma = if LastBar then 0 else Average(close, DemoLength);

input PlotArrows = No;
def DnSignal = if LastBar then Double.NaN else
if PlotArrows and Crosses(( Price_Data > MA_Data) != 0, 0.5, no) then Price_Data else Double.NaN;
#DnSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
def UpSignal = if LastBar then Double.NaN else
if PlotArrows and Crosses(( Price_Data < MA_Data) != 0, 0.5, no) then Price_Data else Double.NaN;
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

# *** Describe the conditions for Buy and Sell to your indicator ***
def Buy = price > ma; # if Price is above MA then TRUE
def Sell = price < ma; # if Price is below MA then TRUE

input Accountant = Yes;
input Buy_SellPoints = Yes;
input CashPosition = No;

declare lower;

# Returns 1 if first trade is Buy and -1 if first trade is sell
# _____________________________________________________________
def Long = if Crosses(( price > ma) != 0, 0.5, yes) then -ma else 0;
def Short = if Crosses(( price < ma) != 0, 0.5, yes) then ma else 0;
def L = if Long < 0 then -1 else 0;
def S = if Short > 0 then 1 else 0;

rec TradeCount = compoundValue(1, -L + S + TradeCount[1], 0) ;

rec First_Trade = if compoundValue(1, First_Trade[1], 0) == 0 then
compoundValue(1, Long + Short, 0) else compoundValue(1, First_Trade[1], 0);

rec TTL2 = if TradeCount > 1 then compoundValue(1, Long + Short + TTL2[1], 0) else 0;
def Profit2 = if TradeCount <= 1 then 0 else if !Accountant then Double.NaN else TTL2;

def CurrentValue = if Buy then low() else if Sell then high() else 0;

def Balance = First_Trade + Profit2 + Profit2;
def Cash = if !Accountant or !CashPosition then Double.NaN else Balance;
#Cash.SetDefaultColor(Color.WHITE);
#Cash.SetPaintingStrategy(PaintingStrategy.LINE);
#Cash.SetLineWeight(1);

def Net = if LastBar then Double.NaN else if Sell then Balance – close else if Buy then Balance + close else 0;
#Net.AssignValueColor(if Buy then Color.GREEN else Color.RED);

# ____________ Buy and Sell Points __________
plot BuyPoint = if !Buy_SellPoints then Double.NaN else if Long then Net else Double.NaN;

BuyPoint.SetPaintingStrategy(PaintingStrategy.POINTS);
BuyPoint.SetLineWeight(1);
plot SellPoint = if !Buy_SellPoints then Double.NaN else if Short then Net else Double.NaN;

#SellPoint.SetPaintingStrategy(PaintingStrategy.POINTS);
#SellPoint.SetLineWeight(1);
# ____________ END Buy and Sell Points __________

AddLabel(Accountant, Concat( Net, Concat(” “, if Net > 0 then “Good Job!” else “Oh No!”)), if Net > 0 then Color.GREEN else Color.RED);

# Keep this comment or TOS will eat it
#AddChartLabel(Accountant, concat( Net, concat(” “, If Net > 0 then “Good Job!” else “Oh No!”)), if Net > 0 then Color.Green else Color.Red);

AssignBackgroundColor(if BuyPoint[1] then Color.dark_GREEN else if sellpoint then Color.dark_RED else color.black);

Marked as spam
Posted by (Questions: 6, Answers: 5)
Asked on August 6, 2019 9:46 am
232 views
0
Private answer

This code is excruciatingly painful to look at. After studying this for far too long I figured out this is nothing more than a mock strategy that is triggered based on price crossing a simple moving average. Looks like someone tried to build a Chart Strategy but wrote the code as a Chart Study. Not sure why someone would do that. Perhaps they didn't realize that there is a tool in Thinkorswim that will do all that grunt work for them.

So instead if providing the solution using the code you provided I will provide the most basic piece of code required to accomplish your request. Price crossing above and below the 30 period simple moving average:

def ma = Average(close, 30);
def crossAbove = close[1] < ma[1] and close > ma;
def crossBelow = close[1] > ma[1] and close < ma;
rec counter = if crossAbove then 1 else if close > ma then counter[1] + 1 else if crossBelow then -1 else if close < ma then counter[1] - 1 else 0;
plot value = counter;
value.AssignValueColor(if value < 4 and value > -4 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if value > 0 and value < 4 then Color.GREEN else if value < 0 and value > -4 then Color.RED else Color.CURRENT);

Screenshot below shows the result.

In closing, I will explain why your attempt to accomplish this failed.

The two variables you selected "BuyPoint" and "SellPoint". Those variables contain values. The values they contain are used to plot the symbols at the high/low of the candle that triggered the signal. You are trying to use them in a true/false condition within an if/then/else statement. You can only use true/false variables as the condition of the if/then/else statement. (I purposely stated that two different ways in case you didn't catch that the first time through).

The code you provided makes it extremely difficult to locate the true/false variables you need to use for this. So don't beat yourself up for not figuring it out. This is the very reason why I ended up writing my own solution from scratch.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 6, 2019 12:36 pm
0
thank you so much. yeah the code is definitely very weird but i like the look of it as a study. your explanation makes perfect sense.thank you so much for your time! i appreciate it
( at August 6, 2019 12:46 pm)