2 years ago

#57587

test-img

Brian Tung

Trading View CrossOver Function in Python

I am looking for a built-in function from trading view (crossover function) and make it happen in python, below is the trading view argument and return value:

The x-series is defined as having crossed over y-series if the value of x is greater than the value of y and the value of x was less than the value of y on the bar immediately preceding the current bar.

crossover(x, y) → series[bool]

Return:

true if `x` crossed over `y` otherwise false

Arguments:

x (float) Data series `x`.
y (float) Data series `y`.

i try to use backtrader API for crossover functions but the function only contain one argument to access data1 and data2 in the same arrays. Below are the functions:

class CrossOver(Indicator):
'''
This indicator gives a signal if the provided datas (2) cross up or down.

  - 1.0 if the 1st data crosses the 2nd data upwards
  - -1.0 if the 1st data crosses the 2nd data downwards

It does need to look into the current time index (0) and the previous time
index (-1) of both the 1t and 2nd data

Formula:
  - diff = data - data1
  - upcross =  last_non_zero_diff < 0 and data0(0) > data1(0)
  - downcross = last_non_zero_diff > 0 and data0(0) < data1(0)
  - crossover = upcross - downcross
'''
_mindatas = 2

lines = ('crossover',)

plotinfo = dict(plotymargin=0.05, plotyhlines=[-1.0, 1.0])

def __init__(self):
    upcross = CrossUp(self.data, self.data1)
    downcross = CrossDown(self.data, self.data1)

    self.lines.crossover = upcross - downcross

Above function does not meet my requirement as I have 2 series of numpy array which is x and y and x will crossover y which the requirement same as pinescript above.

Or is there any API that I can use for crossover function immediately in Python? I try to search everywhere but cant find the suitable one which only return True or False by comparing argument of x numpy arrays and y numpy arrays.

i try to built myself for crossover function as below:

def crossover(x,y):
    j = -len(x)
    while j < 0:
        if x[j] > y[j]:
            return True
        j += 1

but i am not sure the criteria from crossover function in pinescript meet my own function condition, i need help for verification.

python

tradingview-api

0 Answers

Your Answer

Accepted video resources