Efficient way to implement a trading strategy

7 ビュー (過去 30 日間)
Aldo MS
Aldo MS 2019 年 9 月 22 日
コメント済み: Aldo MS 2019 年 9 月 25 日
Hello. I'm working on algoritmic trading of cryptocurrencies in my spare time.
Given a vector of crypto prices, and a vector of decisions (buy / sell / hold) obtained by using a specific strategy, I need to simulate the transactions for a long time to assess whether said strategy is profitable. I'm currently getting the job done with a loop, but, as I need to test lots of machine learning hyper-parameters, and hence I need to do the job many times, I'd like to do it in a more efficient way. I'm currently using this code:
N = 1000; %number of time steps
prices = rand(1,N);
signal = randi(3,[N,1])'-2; %-1: sell, 0: hold, 1: buy
curr1 = 100; %initial amount of currency 1
curr2 = 0; %initial amount of currency 2
fee = 0.1; %exchange fee
returns = nan(1,N);
for timestemp = 1:N
action = signal(timestemp);
if action == 1 && curr1 > 0 %buys
curr2 = (1-fee/100)*(curr1/prices(timestemp));
curr1 = 0;
elseif action == -1 && curr2 > 0 %sells
curr1 = (1-fee/100)*(curr2*prices(timestemp));
curr2 = 0;
end
returns(timestemp) = max(curr1,curr2*prices(timestemp)); %currency 1, or its equivalent in currency 2
end
I suspect that doing it in matricial way might not be possible because at every timestemp we're using the result of the previous step, but maybe there's a way that I'm not thinking of, or a function that I'm unaware of (I checked the documentation of the financial toolbox, but I don't seem to find anything that does that simple job).
Thanks in advance for any help :)

回答 (1 件)

Malte Golombek
Malte Golombek 2019 年 9 月 25 日
I am currently also using a loop do to just that, which looks quite similar. I couldnt find a vecorized way to do the task, I instead just used parallel computing which may accelerate your code by lets say 16 times, depending on your local CPU of course. If u have a Nvidia GPU you could also look into GPUarrays.
Besides the computational problem, I personally do not think it is a good idea do go over many many potential strategies and go for the one with the most profitable backtest, this leads to overfitting eventually and may result in even worse real time desicions.
  1 件のコメント
Aldo MS
Aldo MS 2019 年 9 月 25 日
Thanks. I tried parallelizing the code but it makes almost no difference in my shitty computer :)
Thanks for the heads up, I'm aware of that bias, my algorithm is partly stochastic so besides choosing the right parameters I'm also running hundreds of instances to estimate the mean performance and to test how sensitive it is to variations and noise. I'm also doing some sort of permutation test, that's why I'm running that many times the loop.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by