How to create a strategy with pandas dataframe
-
Hi there,
If I have a dataframe with multiple futures close prices and the last column is the 'signal' which contains -1, 0 and 1. May I know that what I should do to convert that dataframe to Xarray on def strategy(data)? And what columns are needed, e.g: Assets, signal and time? Thanks -
Hello
I think these examples can help you.
https://quantiacs.com/documentation/en/examples/quick_start.html#an-example-using-pandas
def get_price_pct_change(prices): prices_pandas= prices.to_pandas() assets= data.coords["asset"].values for asset in assets: prices_pandas[asset]= prices_pandas[asset].pct_change() return prices_pandas prices= data.sel(field="close") * 1.0 prices_pct_change= get_price_pct_change(prices).unstack().to_xarray()
How to create a dataset yourself
import pandas as pd import xarray as xr import numpy as np class Fields: OPEN = "open" LOW = "low" HIGH = "high" CLOSE = "close" VOL = "vol" DIVS = "divs" SPLIT = "split" SPLIT_CUMPROD = "split_cumprod" IS_LIQUID = 'is_liquid' f = Fields class Dimensions: TIME = 'time' FIELD = 'field' ASSET = 'asset' ds = Dimensions dims = (ds.FIELD, ds.TIME, ds.ASSET) def get_base_df(): try_result = {"schema": {"fields": [{"name": "time", "type": "datetime"}, {"name": "open", "type": "integer"}, {"name": "close", "type": "integer"}, {"name": "low", "type": "integer"}, {"name": "high", "type": "integer"}, {"name": "vol", "type": "integer"}, {"name": "divs", "type": "integer"}, {"name": "split", "type": "integer"}, {"name": "split_cumprod", "type": "integer"}, {"name": "is_liquid", "type": "integer"}], "primaryKey": ["time"], "pandas_version": "0.20.0"}, "data": [ {"time": "2021-01-30T00:00:00.000Z", "open": 1, "close": 2, "low": 1, "high": 2, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, {"time": "2021-01-31T00:00:00.000Z", "open": 2, "close": 3, "low": 2, "high": 3, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, {"time": "2021-02-01T00:00:00.000Z", "open": 3, "close": 4, "low": 3, "high": 4, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, {"time": "2021-02-02T00:00:00.000Z", "open": 4, "close": 5, "low": 4, "high": 5, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, {"time": "2021-02-03T00:00:00.000Z", "open": 5, "close": 6, "low": 5, "high": 6, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, {"time": "2021-02-04T00:00:00.000Z", "open": 6, "close": 7, "low": 6, "high": 7, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, {"time": "2021-02-05T00:00:00.000Z", "open": 7, "close": 8, "low": 7, "high": 8, "vol": 1000, "divs": 0, "split": 0, "split_cumprod": 0, "is_liquid": 1}, ]} columns = [Dimensions.TIME, f.OPEN, f.CLOSE, f.LOW, f.HIGH, f.VOL, f.DIVS, f.SPLIT, f.SPLIT_CUMPROD, f.IS_LIQUID] rows = try_result['data'] for r in rows: r['time'] = np.datetime64(r['time']) pandas = pd.DataFrame(columns=columns, data=rows) pandas.set_index(Dimensions.TIME, inplace=True) prices_array = pandas.to_xarray().to_array(Dimensions.FIELD) prices_array.name = "BTC_TEST" prices_array_r = xr.concat([prices_array], pd.Index(['BTC'], name='asset')) return prices_array_r weights = get_base_df() print(weights)