For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Initialize Line

  • In some cases I need to set different status in a line.
    For example

    s1 = indicator > 0.2
    s2 = indicator < -0.2
    signal = s1 - s2 

    the value of signal will be

     [1, nan, nan, -1, -1, nan, -1, 1, nan, nan...]

    I would like to know how to avoid nan in this case?

    Today I use the code below

    signal = bt.Max(0, 0)
    singal = b - s

    I wonder is there an efficient way to initialize signal with value 0?

  • administrators

    Your problem is that the indicators seem to be producing NaN values (which is of course the case for many indicators before the warm-up period is done)

    Or else the indicators in that snippet are producing NaN values after the warm-up period is done. To overcome that you need to wrap the calculations in your own custom indicator.

    There is no initialization. The line you are calculating is the result of the operations on the other lines. If you need more you need to wrap it.

  • I've used line = bt.LineNum(float(True)) to set initial True values to the line. But can't find the reference now. You can try with the number instead of float(True).

  • administrators

    @ab_trader said in Initialize Line:

    line = bt.LineNum(float(True))

    That doesn't initialize a line to True (or True converted to a float). That creates a line which delivers a constant value of float(True)

Log in to reply