Home > Net >  Pinescript Arrays- multidimensional arrays
Pinescript Arrays- multidimensional arrays

Time:01-12

I am creating a neural net in Pine script which has two input neurons but I am having trouble creating a properly structured training data array. The net expects a two dimensional array. I am using the inbuilt neuralnetwork functions of Pine Script which I have been able to find some limited documentation for. I know how to define the weights array, declare the neural network function defining the netwrok structure and how to train the network but I need to get the training data into the correct format.

The structure of the trainingData should contain two series (say for the sake of argument they are close prices and volume for the time series). I am not concerned at this point about data standardization or normalisation as I will deal with those issues once I work out how to create the correct data array structure. Is it possible to create an array with something like this structure? This code was suggested to me by someone but it doesn't work and throws errors. I'm sure its way off beam. Any pointers would be greatly appreciated. I haven't used arrays much yet in Pine Script - I've used them in other types of scripting but I cant work out how to declare a two dimensional array that will do this.

trainingData = (0)
for i = 0 to bar_index-1
    inputData = [closePrices[i], volumes[i]]
    outputData = [closePrices[i 1], volumes[i 1]]
    trainingData.push([inputData, outputData])

See previous description and code. (Edit: Please not that the structure currently refers to two outputs price and volume but in reality of course Im not interested in predicting volume so there will likely only be one output neuron. )

Edit in response to the answer given. I had thought that the problem was solved but it isn't. The code still isnt compiling without error. The following is the full code for my training function:

trainNN() =>

trainingData = []
for i = 0 to bar_index-1
inputData = [closePrices[i], volumes[i]]
outputData = [closePrices[i 1]]
trainingData.append([inputData, outputData])

The error "syntax error at '['" is thrown on compiling.and the compiler indicates the problem is at the [ in the trainingData array decalaration.

CodePudding user response:

It looks like you're on the right track with creating a 2D training data array for your neural network in Pine Script. The trainingData variable should be declared as an array, and then you can use a for loop to iterate through the data and add the input and output data for each time step to the array.

The code you've provided seems to have some errors, as the trainingData variable is being declared as an integer instead of an array. In addition, the push() function is not a method that is available for integers.

Here's an example of how you might initialize the training data array and add input and output data to it:

trainingData = []
for i = 0 to bar_index-1
inputData = [closePrices[i], volumes[i]]
outputData = [closePrices[i 1]]
trainingData.append([inputData, outputData])

Note that:

I've corrected the initial declaration of trainingData to be empty array using trainingData = [] instead of trainingData = (0).
As in the previous message, for outputData, you can select only one output, so in this example I have used outputData = [closePrices[i 1]]
append is a method of python list type and it will add the data to the end of list each time the loop iterates.

This should create a 2D array where each element is an array containing the input data and output data for one time step. Keep in mind that in this example, closePrices and volumes should be lists.

This should be the correct format of 2D array and it should work fine with the neural network function. Remember that you also have to work on data standardization or normalization for better training and predicting results.

CodePudding user response:

While you can't have an array as elements of another array and create a 2D array, you can use matrix to "stack up" multiple arrays. You can set the return of your function as a new row that has column of the all the results you got:

a = array.from(close, volume)

m = matrix.new<float>()
matrix.add_row(m, matrix.rows(m), a)

if barstate.islast
    label.new(bar_index, high, str.tostring(matrix.get(m, matrix.rows(m) - 1, 0)))

If the return type of the function you are using is a tuple you'll need one more step to convert the result from a tuple to an array:

foo() => 
    [close, volume]

[_close, _volume] = foo()

a = array.from(_close, _volume)

m = matrix.new<float>()
matrix.add_row(m, matrix.rows(m), a)

if barstate.islast
    label.new(bar_index, high, str.tostring(matrix.get(m, matrix.rows(m) - 1, 0)))
  • Related