Importing orders #219
-
From #219
Hello,
Is there a way to import orders from an array? I have them in an array that looks like [0,0,1,0,0,-1,-1...], length of this array is the same as the length of the data(one signal for each timeframe).This is for backtesting only, the calculations are done outside of the testing script.
Or simpler, signals are saved in a csv with lines like this
2016-01-09;100200;-1 2016-01-09;110200;1
etc
-
As explained above order importing is not a feature of the the platform.
If you have the data in that format:
.... 2016-01-09;100200;-1 2016-01-10;110200;1 ...
there are several approaches:
-
Make a custom data feed with it containing only 2 fields
datetime
trigger
(or any other name you wish)
In this case you would add the data feed to the system and could at each moment in time and synchronized with the data read if an order has to be executed or not
-
Read that input with something like a
pandas.read_csv
module and during each call tonext
in the strategy, look for the correspondingdatetime
index in the dataframe to see if an order has to be executed or not.
-
-
Initialize an empty list to store the orders.
Loop through each signal in the array. If the signal is not equal to zero, append a tuple to the list with the timestamp, price, and signal value.
Here's some sample code to illustrate this approach:
signals = [0, 0, 1, 0, 0, -1, -1, ...]
orders = []for i in range(len(signals)):
if signals[i] != 0:
timestamp = # calculate the timestamp based on the index i
price = # calculate the price based on the index i and your data
signal = signals[i]
orders.append((timestamp, price, signal))import csv
orders = []
with open('signals.csv', 'r') as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
timestamp = row[0]
price = float(row[1])
signal = int(row[2])
orders.append((timestamp, price, signal))