Display condition and condition count

pine

New member
Hello,

I am on an hourly chart. I would like to gather the statistics of specific conditions at specific times throughout the day. Currently I have a script to capture the condition once at market open at 9:30am, then use AddLabel to display the stats. I would like to have the stats displayed on every hour, above the bar, perhaps using AddChartBubble.

The current code I have is below:

Code:
condition = if SecondsFromTime(0930) == 0 and [some condition criteria] then 1 else 0;
conditioncnt = if condition then conditioncnt[1] + 1 else conditionct[1];
AddLabel(1, condition + "-" + conditioncnt, Color.white);

Is there an efficient way to do this or creating a separate condition variable for each hour is the only way to go? Also what if I need to flip to another time frame, like 30 min? I think I don't want to remove the 'SecondsFromTime(0930) == 0', because it will not capture the count correctly.

Appreciate any help in advance.
 
Solution
Hello,

I am on an hourly chart. I would like to gather the statistics of specific conditions at specific times throughout the day. Currently I have a script to capture the condition once at market open at 9:30am, then use AddLabel to display the stats. I would like to have the stats displayed on every hour, above the bar, perhaps using AddChartBubble.

The current code I have is below:

Code:
condition = if SecondsFromTime(0930) == 0 and [some condition criteria] then 1 else 0;
conditioncnt = if condition then conditioncnt[1] + 1 else conditionct[1];
AddLabel(1, condition + "-" + conditioncnt, Color.white);

Is there an efficient way to do this or creating a separate condition variable for each hour is the only way to go? Also...
Hello,

I am on an hourly chart. I would like to gather the statistics of specific conditions at specific times throughout the day. Currently I have a script to capture the condition once at market open at 9:30am, then use AddLabel to display the stats. I would like to have the stats displayed on every hour, above the bar, perhaps using AddChartBubble.

The current code I have is below:

Code:
condition = if SecondsFromTime(0930) == 0 and [some condition criteria] then 1 else 0;
conditioncnt = if condition then conditioncnt[1] + 1 else conditionct[1];
AddLabel(1, condition + "-" + conditioncnt, Color.white);

Is there an efficient way to do this or creating a separate condition variable for each hour is the only way to go? Also what if I need to flip to another time frame, like 30 min? I think I don't want to remove the 'SecondsFromTime(0930) == 0', because it will not capture the count correctly.

Appreciate any help in advance.

This should get you started:

Capture.jpg
Ruby:
def x = if secondsfromTime(0930)==0 or
          (SecondsFromTime(0) / 3600) - Round(SecondsFromTime(0) / 3600, 0) == 0
        then BarNumber()
        else x[1];

AddChartBubble(BarNumber() == x,
               high,
               if secondsfromTime(0930)==0
               then "9:30"
               else SecondsFromTime(0)/3600 + ":00");
 
Solution

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

Referencing this thread

A condition is met within x bars, how do I count this condition within x bars only once?

I have the code below and the stats labels seem to show higher success/failure counts than possible count.

Code:
buy = condition;

def buyCount = if buy then buyCount[1] + 1 else buyCount[1];
def buyPrice = if buy then price else buyPrice[1];
def buySuccess = if buy within withinBars bars then if (price - buyPrice) >= pointsDiffSuccess then 1 else 0 else 0;
def buySuccessCount = if buySuccess then buySuccessCount[1] + 1 else buySuccessCount[1];
def buyFailure = if buy within withinBars bars then if (price - buyPrice) <= -pointsDiffFailure then 1 else 0 else 0;
def buyFailureCount = if buyFailure then buyFailureCount[1] + 1 else buyFailureCount[1];

AddLabel(show_stats, "buySuccess " + buySuccessCount + "/" + buyCount + " " + aspercent(buySuccessCount/buyCount), color.white);
AddLabel(show_stats, "buyFailure " + buyFailureCount + "/" + buyCount + " " + aspercent(buyFailureCount/buyCount), color.white);
 
Referencing this thread

A condition is met within x bars, how do I count this condition within x bars only once?

I have the code below and the stats labels seem to show higher success/failure counts than possible count.

Code:
buy = condition;

def buyCount = if buy then buyCount[1] + 1 else buyCount[1];
def buyPrice = if buy then price else buyPrice[1];
def buySuccess = if buy within withinBars bars then if (price - buyPrice) >= pointsDiffSuccess then 1 else 0 else 0;
def buySuccessCount = if buySuccess then buySuccessCount[1] + 1 else buySuccessCount[1];
def buyFailure = if buy within withinBars bars then if (price - buyPrice) <= -pointsDiffFailure then 1 else 0 else 0;
def buyFailureCount = if buyFailure then buyFailureCount[1] + 1 else buyFailureCount[1];

AddLabel(show_stats, "buySuccess " + buySuccessCount + "/" + buyCount + " " + aspercent(buySuccessCount/buyCount), color.white);
AddLabel(show_stats, "buyFailure " + buyFailureCount + "/" + buyCount + " " + aspercent(buyFailureCount/buyCount), color.white);

Without the whole code to test, I would recommend replacing in both addlabels, buycount with (buySuccesssCount + buyFailureCount).

As the latter two use 'within' then there are likely more buys being counted than the buycount which only counts one buy per condition.
 
Referencing this thread

A condition is met within x bars, how do I count this condition within x bars only once?

I have the code below and the stats labels seem to show higher success/failure counts than possible count.

Code:
buy = condition;

def buyCount = if buy then buyCount[1] + 1 else buyCount[1];
def buyPrice = if buy then price else buyPrice[1];
def buySuccess = if buy within withinBars bars then if (price - buyPrice) >= pointsDiffSuccess then 1 else 0 else 0;
def buySuccessCount = if buySuccess then buySuccessCount[1] + 1 else buySuccessCount[1];
def buyFailure = if buy within withinBars bars then if (price - buyPrice) <= -pointsDiffFailure then 1 else 0 else 0;
def buyFailureCount = if buyFailure then buyFailureCount[1] + 1 else buyFailureCount[1];

AddLabel(show_stats, "buySuccess " + buySuccessCount + "/" + buyCount + " " + aspercent(buySuccessCount/buyCount), color.white);
AddLabel(show_stats, "buyFailure " + buyFailureCount + "/" + buyCount + " " + aspercent(buyFailureCount/buyCount), color.white);

here is a test code to check if a signal occured in the previous x bars. if it did, then ignore current signal.

Code:
#did a signal occur within a range of x bars?
input bars = 5;
# whatever your signal formula is
# def s = signal....  true/false

# test signal
def r = random() * 20;
def s = ( r <= 3 );

# add up the signals over past x bars
def t = sum(s, bars);
# if a signal occured within x bars, ignore current signal
def s2 = if t > 0 then 0 else s;
def cnt = if barnumber() == 1 then 0 else cnt[1] + s2;

addchartbubble(1, low,
r + " r\n" +
s + " s\n" +
t + " t\n" +
s2 + " s2\n" +
cnt + " cnt"
, (if t>0 then color.gray else color.yellow), no);
 
@SleepyZ
@halcyonguy

Thank you both for your assistance with my previous questions. I would like to follow up on the question I asked earlier.

My objectives are as follows:

  1. Count the number of buy/sell signals and their corresponding winning/losing conditions. I believe I have implemented this correctly, as the winning/losing percentages are being displayed accurately as labels.
  2. Ensure that the winning/losing conditions are being met and counted accurately, as there seems to be an issue with this. Specifically, after a buy/sell signal, I would like the code to wait for a certain number of bars, and within that timeframe, if the price difference between the current price and the buyPrice/sellPrice meets a specific threshold, then it should count it as a success or failure condition.
Here is the code I am currently using. Any assistance would be greatly appreciated. Thank you!

Code:
# stats

buy = buyCondition and !buyCondition[1];
sell = sellCondition and !sellCondition[1];

input price = open;
input show_stats = yes;
input show_bs_count = yes;
input withinBarsBuy = 5;
input withinBarsSell = 5;
input pointsDiffBuySuccess = 10;
input pointsDiffBuyFailure = 10;
input pointsDiffSellSuccess = 10;
input pointsDiffSellFailure = 10;

def buyCount = if buy then buyCount[1] + 1 else buyCount[1];
def buyPrice = if buy then price else buyPrice[1];
def buyDiffSuccess = if buy then highest(price - buyPrice, withinBarsBuy) else 0;
def buyDiffFailure = if buy then lowest(price - buyPrice, withinBarsBuy) else 0;
def buyFailure = if buyDiffFailure <= -pointsDiffBuyFailure then 1 else 0;
def buyFailureCount = if buyFailure then buyFailureCount[1] + 1 else buyFailureCount[1];
def buySuccess = if buyDiffSuccess >= pointsDiffBuySuccess and !buyFailure then 1 else 0;
def buySuccessCount = if buySuccess then buySuccessCount[1] + 1 else buySuccessCount[1];

def sellCount = if sell then sellCount[1] + 1 else sellCount[1];
def sellPrice = if sell then price else sellPrice[1];
def sellDiffSuccess = if sell then lowest(price - sellPrice, withinBarsSell) else 0;
def sellDiffFailure = if sell then highest(price - sellPrice, withinBarsSell) else 0;
def sellFailure = if sellDiffFailure >= pointsDiffSellFailure then 1 else 0;
def sellFailureCount = if sellFailure then sellFailureCount[1] + 1 else sellFailureCount[1];
def sellSuccess = if sellDiffSuccess <= -pointsDiffSellSuccess and !sellFailure then 1 else 0;
def sellSuccessCount = if sellSuccess then sellSuccessCount[1] + 1 else sellSuccessCount[1];

AddLabel(show_stats,
stratName + " buy " + GetSymbol() + " @ " + price + " and wait max " + withinBarsBuy + " bars then exit when "
+ GetSymbol() + " moved +" + pointsDiffBuySuccess + "pts for profit or when "
+ GetSymbol() + " moved -" + pointsDiffBuyFailure + "pts for loss",
if buy within withinBarsBuy bars then Color.GREEN else Color.WHITE);
AddLabel(show_stats,
stratName + " sell " + GetSymbol() + " @ " + price + " and wait max " + withinBarsSell + " bars then exit when "
+ GetSymbol() + " moved -" + pointsDiffSellSuccess + "pts for profit or when "
+ GetSymbol() + " moved +" + pointsDiffSellFailure + "pts for loss",
if sell within withinBarsSell bars then Color.RED else Color.WHITE);
AddLabel(show_stats, stratName + " buySuccess " + buySuccessCount + "/" + buyCount + " " + AsPercent(buySuccessCount / buyCount), Color.CYAN);
AddLabel(show_stats, stratName + " buyFailure " + buyFailureCount + "/" + buyCount + " " + AsPercent(buyFailureCount / buyCount), Color.ORANGE);
AddLabel(show_stats, stratName + " sellSuccess " + sellSuccessCount + "/" + sellCount + " " + AsPercent(sellSuccessCount / sellCount), Color.CYAN);
AddLabel(show_stats, stratName + " sellFailure " + sellFailureCount + "/" + sellCount + " " + AsPercent(sellFailureCount / sellCount), Color.ORANGE);
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
409 Online
Create Post

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