4

I am learning some PineScript in my free time and I find it really diificult to work with. I am basically trying to recreate a complicated indicator I wrote in C# and I am struggling with simple things.

Ommiting the complicated stuff, lets say I want to draw arrows when MACD behaves like in the following picture:

enter image description here

This is my current attempt, it is basically supposed to check if we are at a peak, find histogram high, find below zero columns, then above zero columns and check if they contain a higher histogram value then the current one:

[macdLine, signalLine, hist] = macd(close, 12, 26, 9)
float ind = na

if hist[2] > 0 and hist[1] < hist[2] and hist < hist[1] and hist[3] < hist[2]
    bool maxHistFound = false
    bool lowerFound = false
    bool upperFound = false
    bool candidateFound = false
    float maxHist = hist[2]

    for i = 1 to 1000

        if not maxHistFound 
            if hist[i] < 0
                maxHistFound := true
            else
                if hist[i] > maxHist
                    maxHist := hist[i]

        if not lowerFound
            if hist[i] < 0
                lowerFound := true

        if not upperFound
            if hist[i] > 0
                upperFound := true

        if maxHistFound and lowerFound and upperFound
            if hist[i] > 0 and hist[i + 1] <= hist[i] and hist[i - 1] <= hist[i]
                if hist[i] > maxHist // and macdLine[i] > 0 and signalLine[i] > 0 and hist[i] > 0.10 
                    candidateFound := true
                    maxHist := hist[i]

            if hist[i] < 0
                if candidateFound
                    ind := -1
                    break

alertcondition(ind == -1, title='Short', message='Short')

plotarrow(ind, colorup=#008000, colordown=#FF3030, transp=0, minheight=50, maxheight=50)

It shows the signal even if the signal is not preceeded with what I described.

enter image description here

The main problem is that I could probably fix it myself but I don't know how to properly debug in this language and I try to do it by plotting everything but plotting to debug things is riddiculus, unfortunately in my ignorance I can't find a better solution. I could probably easily find out whats going on if I knew how to stop at breakpoint in PineScript.

rvnlord
  • 3,487
  • 3
  • 23
  • 32
  • could you add a symbol+resolution where you got the error `invalid series length` ? – Michel_T. Nov 08 '19 at 18:37
  • Thank you for showing interest in my question. The question lacked clarity so I improved the description. It actually isn't important why I was getting the error, the important part is how to make sure I can verify every piece of code to see why it isn't working as intended. Currently I have no idea how to do that. Please consider the current example. For your info, I will also happily accept an answer that will show me how to effectively debug this thing. – rvnlord Nov 08 '19 at 23:45
  • Actually, plotting a value is the main way to debug a script. http://www.pinecoders.com/faq_and_code/#debugging here is a short article about debugging in pine-script. It shows few more ways to clarify what's going on there in a script. Indeed, that's not the most convenient way to debug a code, but there's no anything better by now. – Michel_T. Nov 09 '19 at 08:52
  • It basically all shows how to plot "part" of lets call it composite indicator, you got 5 emas, rsi and 2nd ema fails the condition - you are all set. I need to iterate history, so if I had to do this that way I would need to plot many historical values for every "current point in time" and it only gets more tricky than the simple example I posted. I saw some pretty complicated things on TV, it is difficult to believe that they had to plot all of it one by one to get it working. Alright, so kindly show me how would you approach this particular example. How would you check what isn't working? – rvnlord Nov 09 '19 at 14:21
  • There's no a pine debugger, where we could put a breakpoint and then study variables and trace a script. Only hardcore, only `print`/ `plot()`. I agree, that's cumbersome, but there's no another way to debug, and till recently there were no any features to debug string values. So the people with complicated script possess incredible amount of patience, I think they've found zen and that helps them in programming. – Michel_T. Nov 11 '19 at 18:44
  • When I am writting a script, I look for a point where an indicator must give me a signal. If there's no signal, then I'm trying to figure out why it's so, which of values is wrong etc. Sometimes it's helpful to restrict area where a script works using `if time > timestamp(some_data)` and it might be helpful to mark found low/max something like this: `label.new(bar_index - i, low, text="Low " + tostring(bar_index))` – Michel_T. Nov 11 '19 at 18:44

1 Answers1

0

I was stuck with a similar problem, and I found this script already written on TradingView.

Check it out. Works pretty well. https://www.tradingview.com/script/nTOjDIxS-Cyatophilum-MACD-Hist-ALERT-SETUP/

Abhijit S
  • 319
  • 2
  • 12
  • 1
    Thanks but the script is not open sourced so I can't check or modify the code to match my needs. – rvnlord Dec 27 '20 at 16:24