Home > other >  Counting pinescript
Counting pinescript

Time:11-18

First of all I want to thank the community for helping me and many others, and apologies for my bad English.

In the screenshots, I want to count number of signal to enter the trade. The problem is when I got first signal, I want to reset counting to zero if signal passed 100 bars and want to count from Zero again.

But when second signal came bars since condition resetting and as you see my counting will be 3 again. What I need is to see 2.

Here is my code

inLong = strategy.position_size > 0

bartime = int(ta.change(time))

bars = math.floor((timenow - i_startTime) / bartime)

if na(bar_index - strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) )
    bars :=  math.floor((timenow - i_startTime) / bartime)
else
    bars := bar_index - strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1)

tDOWN = close < open 

counting(bars) =>
    deepAlert_count = 0
    green_count = 0
    for i= 0 to bars    
        if (deepAlert[i])
            deepAlert_count := deepAlert_count   1
        
        if (tDOWN[i])
            green_count := green_count   1
    if not inLong and deepAlert_since  > 100
        deepAlert_count := deepAlert_count - 1
        // also i tried   deepAlert_count :=0


    [deepAlert_count, green_count]
    
[deepAlert_count, green_data] = counting(bars)

I also tried :

counting(bars) =>
    while deepAlert_since < 100
        deepAlert_count = 0
        green_count = 0
        for i= 0 to bars    
            if (deepAlert[i])
                deepAlert_count := deepAlert_count   1
            
            if (tDOWN[i])
                green_count := green_count   1
        if not inLong and deepAlert_since  > 100
            deepAlert_count := deepAlert_count - 1

        [deepAlert_count, green_count]
    
[deepAlert_count, green_data] = counting(bars)

but I get an error like loop is getting too much time to execute

What I am expecting is to find out how can I count properly and what must my approach be to solve this puzzle?

Many thanks again...

CodePudding user response:

You can check if the number of bars since the last time the condition has been met is less then 100 using the ta.barssince() function. If so, then you can add 1 to counter variable. Don't forger to use the var keyword that helps to retain the variable value between bars.

In my method I used another variable in order to check if there is a need to wait for a new signal/condition (for example, after 100 bars has passed since the condition has been met) or not (for example during those 100 bars):

var counter = 0
var wait_for_new_signal = true

if (ta.barssince(condition) == 1 and wait_for_new_signal) or (ta.barssince(condition) < 100 and not wait_for_new_signal)
    counter  = 1
    wait_for_new_signal := false
else
    counter := 0
    wait_for_new_signal := true

if counter >= 100
    counter := 0
    wait_for_new_signal := true

CodePudding user response:

many thanks for your time, as i know i am at very beginning. so this answer thought me alot, it completely solved my problem. Whilke i was waiting i find a way to forward my backtest date this aproach solved my another problem, even i have premium account i couldnt able solved bar referans over 5001. But with your answer my code startted to look like a real code. here is my amator solution for this

Screenshot

  • Related