Counter to add values instead of counts

greco26

Active member
Hi All,

Anytime a condition is met, I'm trying to calculate a value which I want to add on top of the prior value. I am able to get this to work with using counter[1] + 1 else counter[1] however I want the + 1 to be a variable instead of the constant 1. In my code below, the code is not adding the variable to the prior but instead just using the last value it found. Can anyone advise what I may be missing so that the yellow bubbles are adding on top of the prior values? Thanks!!

Code:
def H = high;
def L = low;
def C = close;
def O = open;
input WillingToLose = 10;
input InitialStopLoss_Pct = .3;

def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
def Stop_Calc =  ((InitialStopLoss_Pct / 100) + 1) * close - close + spread;
def SharesCalc = WillingToLose / Stop_Calc;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];


def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up;
def Two_One_Up_Mag_FTCUp = if Two_One_UP and Mag_UP then Two_One_Up_Mag_FTCUp[1] + 1 else Two_One_Up_Mag_FTCUp[1];
def total21up = if Two_One_UP and Mag_UP then total21up[1] + (H[2] - H[1]) else total21up[1];
def avgmag21u = total21up / Two_One_Up_Mag_FTCUp;


AddLabel(yes, "avg mag " + Round(avgmag21u), Color.GREEN);
AddLabel(yes, "total " + total21up, Color.GREEN);
AddLabel(yes, "count " + Two_One_Up_Mag_FTCUp, Color.GREEN);
AddChartBubble(if  Two_One_UP and Mag_UP then H[2] - H[1] else Double.NaN, low, total21up, Color.yellow);
AddLabel(yes, " avg profit " + avgmag21u * SharesCalc + " | " + SharesCalc, Color.GREEN);
 
Solution
Hi All,

Anytime a condition is met, I'm trying to calculate a value which I want to add on top of the prior value. I am able to get this to work with using counter[1] + 1 else counter[1] however I want the + 1 to be a variable instead of the constant 1. In my code below, the code is not adding the variable to the prior but instead just using the last value it found. Can anyone advise what I may be missing so that the yellow bubbles are adding on top of the prior values? Thanks!!

Code:
def H = high;
def L = low;
def C = close;
def O = open;
input WillingToLose = 10;
input InitialStopLoss_Pct = .3;

def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
def Stop_Calc =  ((InitialStopLoss_Pct / 100) + 1)...
Hi All,

Anytime a condition is met, I'm trying to calculate a value which I want to add on top of the prior value. I am able to get this to work with using counter[1] + 1 else counter[1] however I want the + 1 to be a variable instead of the constant 1. In my code below, the code is not adding the variable to the prior but instead just using the last value it found. Can anyone advise what I may be missing so that the yellow bubbles are adding on top of the prior values? Thanks!!

Code:
def H = high;
def L = low;
def C = close;
def O = open;
input WillingToLose = 10;
input InitialStopLoss_Pct = .3;

def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
def Stop_Calc =  ((InitialStopLoss_Pct / 100) + 1) * close - close + spread;
def SharesCalc = WillingToLose / Stop_Calc;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];


def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up;
def Two_One_Up_Mag_FTCUp = if Two_One_UP and Mag_UP then Two_One_Up_Mag_FTCUp[1] + 1 else Two_One_Up_Mag_FTCUp[1];
def total21up = if Two_One_UP and Mag_UP then total21up[1] + (H[2] - H[1]) else total21up[1];
def avgmag21u = total21up / Two_One_Up_Mag_FTCUp;


AddLabel(yes, "avg mag " + Round(avgmag21u), Color.GREEN);
AddLabel(yes, "total " + total21up, Color.GREEN);
AddLabel(yes, "count " + Two_One_Up_Mag_FTCUp, Color.GREEN);
AddChartBubble(if  Two_One_UP and Mag_UP then H[2] - H[1] else Double.NaN, low, total21up, Color.yellow);
AddLabel(yes, " avg profit " + avgmag21u * SharesCalc + " | " + SharesCalc, Color.GREEN);
Change the bubble AddChartBubble(if Twone_UP and Mag_UP then H[2] - H[1] else Double.NaN, low, total21up, Color.yellow);
to AddChartBubble( Twone_UP and Mag_UP, low, total21up, Color.yellow);

The above change will plot all bubbles including where H[2]-H[1]==0. But if you were trying to not plot those, then replace your code with this; AddChartBubble( Twone_UP and Mag_UP and (H[2]-H[1])>0 , low, total21up, Color.yellow);
 
Solution
Change the bubble AddChartBubble(if Twone_UP and Mag_UP then H[2] - H[1] else Double.NaN, low, total21up, Color.yellow);
to AddChartBubble( Twone_UP and Mag_UP, low, total21up, Color.yellow);

The above change will plot all bubbles including where H[2]-H[1]==0. But if you were trying to not plot those, then replace your code with this; AddChartBubble( Twone_UP and Mag_UP and (H[2]-H[1])>0 , low, total21up, Color.yellow);
Actually, I'm using those yellow bubbles just for troubleshooting. I should have clarified that so my bad. The real thing im trying to fix is the following code. I can't get it to add properly. It just shows me the last execution of H[2]-H[1] and doesn't add it to the prior value of total21up[1].
Code:
def total21up = if Two_One_UP and Mag_UP then total21up[1] + (H[2] - H[1]) else total21up[1];
 
Actually, I'm using those yellow bubbles just for troubleshooting. I should have clarified that so my bad. The real thing im trying to fix is the following code. I can't get it to add properly. It just shows me the last execution of H[2]-H[1] and doesn't add it to the prior value of total21up[1].
Code:
def total21up = if Two_One_UP and Mag_UP then total21up[1] + (H[2] - H[1]) else total21up[1];
I checked it before posting the above and It already is correct. The total in the last bubble matches the total in your label also.

Here is your revised code I gave you. I added another bubble below each on top that is your tootal21uup. You can see that it includes the sum of the lower bubbles.

Capture.jpg
Ruby:
def H = high;
def L = low;
def C = close;
def O = open;
input WillingToLose = 10;
input InitialStopLoss_Pct = .3;

def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
def Stop_Calc =  ((InitialStopLoss_Pct / 100) + 1) * close - close + spread;
def SharesCalc = WillingToLose / Stop_Calc;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];


def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up;
def Two_One_Up_Mag_FTCUp = if Two_One_UP and Mag_UP then Two_One_Up_Mag_FTCUp[1] + 1 else Two_One_Up_Mag_FTCUp[1];
def total21up = if Two_One_UP and Mag_UP then total21up[1] + (H[2] - H[1]) else total21up[1] ;
def avgmag21u = total21up / Two_One_Up_Mag_FTCUp;


AddLabel(yes, "avg mag " + Round(avgmag21u), Color.GREEN);
AddLabel(yes, "total " + total21up, Color.GREEN);
AddLabel(yes, "count " + Two_One_Up_Mag_FTCUp, Color.GREEN);
AddChartBubble( Two_One_UP and Mag_UP and (H[2]-H[1])>0  , low, total21up, Color.yellow);
AddChartBubble( Two_One_UP and Mag_UP , low, (H[2]-H[1]), Color.yellow, no);
AddLabel(yes, " avg profit " + avgmag21u * SharesCalc + " | " + SharesCalc, Color.GREEN);
 
Ah, ok. So I suppose I had it correct but I just didn't realize I had the cumulative bubble already displaying. duh! For some reason, if I do a scan for avgmag21u > 1 it won't return anything. Is this a limitation in TOS do you know? At first I though perhaps it couldn't scan counts but I realized that it does return a scan for total21up > 5 so im stumped. ...
 
Ah, ok. So I suppose I had it correct but I just didn't realize I had the cumulative bubble already displaying. duh! For some reason, if I do a scan for avgmag21u > 1 it won't return anything. Is this a limitation in TOS do you know? At first I though perhaps it couldn't scan counts but I realized that it does return a scan for total21up > 5 so im stumped. ...
I had no problem getting results with scan of avgmag21u > 1 set to Daily on SP500 and get 88 results. Remember to set a high enough agg on your scan as some of the avg intraday are smaller than 1.
Ruby:
def H = high;
def L = low;
def C = close;
def O = open;
input WillingToLose = 10;
input InitialStopLoss_Pct = .3;

def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
def Stop_Calc =  ((InitialStopLoss_Pct / 100) + 1) * close - close + spread;
def SharesCalc = WillingToLose / Stop_Calc;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];


def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up;
def Two_One_Up_Mag_FTCUp = if Two_One_UP and Mag_UP then Two_One_Up_Mag_FTCUp[1] + 1 else Two_One_Up_Mag_FTCUp[1];
def total21up = if Two_One_UP and Mag_UP then total21up[1] + (H[2] - H[1]) else total21up[1] ;
def avgmag21u = total21up / Two_One_Up_Mag_FTCUp;
plot scan = avgmag21u > 1;
 
yea, I was able to get it to work as all. my expectations of the results were so off base that I thought there was something wrong. it turns out that I had an error in the code which was causing the numbers to return much lower than expected. Thank you so much for your help!
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
505 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top