How to use "input" options in thinkScript code

Wannaspeed

New member
Is there a way to make a color adjustable using an input variable. I've tried searching but I can only find it for plots. Like lets say I have multiple Labels and I want a global color selection. Thanks in advance
 

tomsk

Well-known member
VIP
@Wannaspeed There absolutely is a slick way to implement this. Here's an example using an example I just pieced together for you to illustrate this. All you need to do is adjust the color direct from the user interface

Code:
# Recent High with User Selectable Colors
# tomsk
# 11.25.2019

input LabelColor = {default "MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", "GRAY", "WHITE"};

def cond = close > close[1];
def bar = if cond then BarNumber() else bar[1];

plot data = if barNumber() >= HighestAll(bar) and cond then high else Double.NaN;
     data.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
     data.SetLineWeight (5);
     data.SetDefaultColor(GetColor(LabelColor));
# End Recent High with User Selectable Colors
 

Wannaspeed

New member
After much trial and error I got it! You can see my various mistakes in the comment out areas of the code.
Code:
#LABEL COLOR TEST
input LabelColor = {default "MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", "GRAY", "WHITE"};

def dollarvol = VWAP* volume;
#labelColor (yes, dollarvol);
Addlabel (yes, dollarvol,getColor(labelColor));
#dollarvol.SetDefaultColor(GetColor(LabelColor));
 

RMEdgar

New member
I have tried this and indeed it does change the color but it is not the color requested. Any suggestions? This is code for the Coppock indicator. I would like to change the color of the zero line when I configure the indicator.
Code:
#
#Coppock Indicator
#
declare lower;

input RateOfChangeSlowPeriod = 14;
input RateOfChangeFastPeriod = 11;
input WeightedMAPeriod = 10;
input ZeroLineColor = {default "BLACK" , "WHITE" , "GREEN"};

def AggregationPeriod = if (GetAggregationPeriod() < AggregationPeriod.WEEK) then AggregationPeriod.WEEK else GetAggregationPeriod();

def price = close(period = AggregationPeriod);

def ROC1 = if price[RateOfChangeSlowPeriod] != 0 then (price / price[RateOfChangeSlowPeriod] - 1) * 100 else 0;
def ROC2 = if price[RateOfChangeFastPeriod] != 0 then (price / price[RateOfChangeFastPeriod] - 1) * 100 else 0;

plot Coppock = WMA(ROC1 + ROC2, WeightedMAPeriod);
    Coppock.AssignValueColor(if Coppock > Coppock[1] then Color.GREEN else Color.RED);
    Coppock.SetDefaultColor(GetColor(1));
    Coppock.SetLineWeight(2);
    Coppock.HideBubble();

plot ZeroLine = 0;
    ZeroLine.SetDefaultColor(GetColor(ZeroLineColor));
    ZeroLine.HideBubble();

AddChartBubble(Coppock[1] < 0 and Coppock > Coppock[1] and Coppock[1] < Coppock[2], Coppock, "Buy", Color.CYAN, no);
 

horserider

Well-known member
VIP
Delete following 2 lines:

Code:
input ZeroLineColor = {default "WHITE" , "GREEN"};
ZeroLine.SetDefaultColor(GetColor(ZeroLineColor));

Add this line and put in color of your choice if do not want white.

Code:
ZeroLine.SetDefaultColor(Color.WHITE);
 

RMEdgar

New member
That does not allow me to configure the color without having to edit the code. If I have a black zero line and change the background to black it does not show up. Also, If I share the indicator with someone else I do not know what background they are using. This also gives me the freedom to change colors on other indicators If I know how to do it.
 

rad14733

Well-known member
VIP
Is there anyway you can input a value on a chart label without going to the study setup??
Edited: You can select Text Note from the Tools menu... Select the "T" symbol from the Drawing Set to make it your Active Tool... Right Click where you want the upper left corner of the note and select Add Drawing... Select "T" again and a popup box will appear... Type in the text you want to display and press Ok...
 
Last edited:

machie38

New member
Edited: You can select Text Note from the Tools menu... Select the "T" symbol from the Drawing Set to make it your Active Tool... Right Click where you want the upper left corner of the note and select Add Drawing... Select "T" again and a popup box will appear... Type in the text you want to display and press Ok...

What I'm looking to do is this:
I have a Label on the chart that calculates the shares to buy based on the amount of money. I have.
What I'd like to do is be able to edit the amount on the label on the fly so I don't have to keep going into the study setup changing the amount in the input field there
 

rad14733

Well-known member
VIP
What I'm looking to do is this:
I have a Label on the chart that calculates the shares to buy based on the amount of money. I have.
What I'd like to do is be able to edit the amount on the label on the fly so I don't have to keep going into the study setup changing the amount in the input field there
Ok... Well, not sure how well any of the Portfolio Thinkscript functions work in realtime... So you essentially want a study or strategy to calculate based on your currently available funds... Can't really play with that today due to how quirky TOS is working... I'll check around as I think there was a recent topic regarding Portfolio functions...
 

rad14733

Well-known member
VIP
@machie38

What you need is the following snippet of code in your script... Note: Make sure you adhere to the limitations of Portfolio Functions... Each function may also have limitations, the main one being that Show Extended-Hours Trading must be enabled for the instrument type being displayed on your chart... There are also aggregation period limitations...

Code:
def price = close;
def MaxShares = Round(GetTotalCash() / price, 0);
AddLabel(yes, "MaxShares = " + MaxShares, Color.GRAY);
 

Similar threads

Top