Efficient way to implement a trading strategy
7 ビュー (過去 30 日間)
古いコメントを表示
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 :)
0 件のコメント
回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!