How can I add a stop loss and take profit for algorithmic trading in MATLAB?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
s = zeros(size(S1Close1));
sl1 = cctbbo20L < BBosline;
ss1 = cctbbo20S > BBobline;
sl2 = MA2>=DEMA;
ss2 = MA2<=DEMA;
sl3 = S1Close1>DEMA;
ss3 = S1Close1<DEMA;
TF = (AST + BET) == 2;
sl = sl1 + sl2 + sl3 + TF;
ss = ss1 + ss2 + ss3 + TF;
sll = sl >= 4;
sss = ss >= 4;
s(sll) = 1;
s(sss) = -1;
S1Close2 = movprod(S1Close1,[1 0])./S1Close1;
g = S1High1+S1Low1;
h = abs(S1High1-S1Close2);
j = abs(S1Low1-S1Close2);
TR = max([g j h],[],2);
atr = movavg(TR,'custom',1./atrlength);
longEntryPrice = S1Close1;
longDiffSL = abs(longEntryPrice - (((S1High1+S1Low1+S1Close1)./3) - (factor * atr)));
longpositionValue = initialCapital * i_pctStop ./ (longDiffSL ./ longEntryPrice);
longpositionSize = longpositionValue ./ longEntryPrice;
longStop = ((S1High1+S1Low1+S1Close1)./3) - (factor * atr);
longTarget = (((S1Close1 - longStop) * RR) + S1Close1);
shortEntryPrice = S1Close1;
shortDiffSL = abs(shortEntryPrice - (((S1High1+S1Low1+S1Close1)./3) + (factor * atr)));
shortpositionValue = initialCapital * i_pctStop ./ (shortDiffSL ./ shortEntryPrice);
shortpositionSize = shortpositionValue ./ shortEntryPrice;
shortStop = ((S1High1+S1Low1+S1Close1)./3) + (factor * atr);
shortTarget = (S1Close1 - ((shortStop - S1Close1) * RR));
SC = [s S1Close1];
r = [0; abs(s(1:end)).*shortpositionSize];
r = r(2:end);
w = cumsum(r);
So I'm trying to create a trading strategy in MATLAB and having some trouble, essentially I'm trying to create a stoploss and take profit for my strategy. I have the signal vector s that =1 for long positions and =-1 for short positions and I need to find some way to apply my long/short stop and target (in bold) to this vector so that Ii can properly calculate my returns in vector r, but I honestly have no idea where to start. I concatenated the Closing price (S1Close1) of the stock and my signal (s) vector and pretty much need s to stay 1 or -1 until the price (S1Close) reaches my short\long stop or target. I would greatly appreciate any help at all.
0 Kommentare
Antworten (1)
Shivam Lahoti
am 22 Dez. 2023
Hi Alex Regner,
I can understand that you want to do Algorithmic trading using MATLAB and you have a strategy ready to calculate the Targets and Stop loss for any trade that you enter. You want to understand how you could maintain the open positions (1 being long and -1 being short) data and book your returns whenever they are triggered.
I have tried to break the approach into three steps. Please have a look to understand the Workflow then you can incorporate that into your detailed strategy.
1. Monitor Open Positions: As you iterate through each day's closing prices (`S1Close1`), check if any open long or short positions have reached the stop loss (`longStop`, `shortStop`) or take profit (`longTarget`, `shortTarget`) levels.
2. Close Trades: If the current price crosses the stop loss or target for an open position, close the trade. For long positions, if `S1Close1` is less than or equal to `longStop` or greater than or equal to `longTarget`, you book the loss or profit, respectively. Similarly, for short positions, if `S1Close1` is greater than or equal to `shortStop` or less than or equal to `shortTarget`, you book the profit or loss.
3. Record Results: When a trade (short or long) is closed due to stop loss or target being hit, calculate the result, and record it in your returns vector (`r`).
Along with this, I found this documentation useful to start Algorithmic trading using MATLAB. Please go through this documentation:
I hope this was helpful.
Regards,
Shivam Lahoti.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Transaction Cost Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!