ProRealCode - Trading & Coding with ProRealTime™
Anbei wie heute versprochen meine Programmierleistungen aus dem Zeitraum meines Testaccounts im Dezember, zwischenzeitlich lesbarer und eleganter gecoded.
Es handelt sich im Großen und Ganzen um das Werk vieler einzelner grandioser User, deren Ideen, Snippets und Codes ich für dieses “Framework” adaptiert habe.
Der Dank gebührt also NICHT mir.
Der Code ist möglichst modular gehalten, so daß er auf verschiedene Märkte anpassbar sein sollte.
Außerdem können Module ggf ergänzt, hinzugefügt oder herausgenommen werden, sollten sich die Anforderungen ändern.
Der Schwerpunkt meiner Arbeit lag hierbei auf den Tradingzeiten und Eigenarten des DAX.
Inspirationen für diese Arbeit waren u.a. der Pathfinder-Code, dessen Idee im Grunde großartig war, die aber überoptimiert wurde.
Ich wollte daher einen Code erstellen, der sich selbst überwacht (Strategy-Stop-Code!!) und der in entscheidenden Punkten nach Belieben auch “abgestellt” werden kann, ohne ganz neu zu programmieren.
Beim Stöbern durch das Forum stolperte ich über die Idee eines MA-Cluster-Filters und eines R2/S2-Filters, die ich dann ebenfalls einbaute.
Die Idee des Robustness-Tests fand ich für Entwicklung und Test ebenfalls ganz brauchbar.
Die Xetra-High/Low/Close-Korrektur ist tatsächlich meine eigene Idee, da die herkömmliche Berechnung via DHigh/DLow/DClose bei den Handelszeiten von IG falsche/unbrauchbare Ergebnisse liefert.
Die Trailing-Stop-Routine habe ich für getrennte Werte für Long-, Close- und Risk-Trades geschrieben mit einem eingebauten harten Breakeven-Stop. Als Basis diente ursprünglich der berühmte newSL-Trailing-Stop-Code.
Da mir die Ergebnisse im Vergleich zum klassischen/herkömmlichen SET STOP- Code etwas rätselhaft waren, konnte ich mich aber noch nicht wirklich entscheiden und habe beide Varianten GLEICHZEITIG im Code gelassen.
Die ungewünschte kann dann natürlich gelöscht werden.
Am Schluß habe ich einen kurzen (eher schlechten) Trading-Algo eingefügt, damit User direkt mit dem Code “spielen” können, um die Funktionalitäten kennenzulernen.
Natürlich kann und soll dann jeder seinen eigenen Algorithmus dort einfügen.
Viel Vergnügen
Stefan
//*************************************************************************//
//Modular Algorithm Library V1.0 //
//*************************************************************************//
//Modules:
//Market-Data-Definition+Parameter
///Monthly/Weekly/Daily-Modifiers, Xetra-HLC-Correction, Strategy-Stop-Code mit Money-Management
//Moving-Average-Clustering-Filter, Robustness-Test, Stop-Loss/Trailing-Routines Long/Short/Risk
//Long/Short-Support2/Resistance2-Break-Filter
//Trading Code
//*************************************************************************//
//Parameter //
//*************************************************************************//
DEFPARAM PreLoadBars = 5000
DEFPARAM CumulateOrders = False
DEFPARAM FlatBefore = 080000
DEFPARAM FlatAfter = 220000
ONCE TradeON = 1
ONCE ClusterSave = 1 // 0=OFF 1=ON MovingAverage-Clustering-Filter
ONCE startingsize = 1 // starting position size
ONCE ForbiddenLSFlag = 0 // 0=OFF 1=ON 2=Reverse S2/Short-R2/Long-Filter
ONCE RobustnessTest = 0 // 0=OFF 1=ON Robustness-Test
ONCE SSC = 1 // 0=OFF 1=ON Strategy-Stop-Code
ONCE StartingCapital = 1000 // Startkapital
ONCE Modifiers = 1 // 0=OFF 1=ON Modifikatoren auf PositionSize
//*************************************************************************//
//Market Data //
//*************************************************************************//
ONCE Opening = 080000
ONCE Closing = 220000
Tradeday = OpenDayOfWeek > 0 AND OpenDayOfWeek < 6 AND NOT ((Month = 5 AND Day = 1) OR (Month = 12 AND (Day = 24 OR Day = 25 OR Day = 26 OR Day = 30 OR Day = 31)))
//Flat Failsave
If ONMARKET AND (Time < Opening OR Time > Closing) THEN
SELL AT Market
EXITSHORT AT Market
ENDIF
//*************************************************************************//
//Algorithm Robustness-Test //
//*************************************************************************//
StartDate = 20000101 // Parameter
Qty = 5 // Parameter
Rndom = 3 // Parameter
once j = 0
once flag = 1
IF RobustnessTest = 1 THEN
if flag = 1 then
j = j + 1
if j > qty then
flag = -1
j = j - 1
endif
endif
if flag = -1 then
j = j - 1
if j = 0 then
j = j + rndom
flag = 1
endif
endif
if opendate >= startdate AND (barindex mod qty = 0 or barindex mod qty = j) then
tradeon = 1
ELSIF opendate >= startdate AND NOT (barindex mod qty = 0 or barindex mod qty = j) then
tradeon = 0
endif
ENDIF
//*************************************************************************//
//Strategy-Stop-Code, Money Management/ReInvest //
//*************************************************************************//
barsbeforenextcheck = 30 // number of bars between performance checks
drawdownquitting = 1 // drawdown quitting on or off (on=1 off=0)
winratequit = 50 // minimum win rate in % allowed before quitting (0 = off)
tradesbeforewrquit = 50 // number of trades required before a win rate stop of strategy is allowed to happen
increase = 0 // position size increasing on or off (on=1 off=0)
decrease = 0 // position size decreasing on or off (on=1 off=0)
capital = StartingCapital // starting capital
startingsize = 1 // starting position size
minpossize = 0.2 // minimum position size allowed
gaintoinc = 5 // % profit rise needed before an increase in position size is made
losstodec = 5 // % loss needed before a decrease in position size is made
maxdrawdown = 30 // maximum % draw down allowed from highest ever equity before stopping strategy
maxcapitaldrop = 25 // maximum % starting capital lost before stopping strategy
once MMpositionsize = 1
once psperc = MMpositionsize / capital
IF SSC = 1 THEN
if strategyprofit <> strategyprofit[1] then
highestprofit = max(strategyprofit, highestprofit)
if count < tradesbeforewrquit OR winrate > winratequit/100 then
count = count + 1
if strategyprofit > strategyprofit[1] then
win = win + 1
endif
ENDIF
winrate = win/count
ENDIF
if count >= tradesbeforewrquit AND winrate < winratequit/100 then
quit
endif
if barindex mod barsbeforenextcheck = 0 AND drawdownquitting AND highestprofit <> 0 then
if (capital + strategyprofit) <= (capital + highestprofit) - ((capital + highestprofit)*(maxdrawdown/100)) then
quit
endif
endif
if count >= tradesbeforewrquit AND highestprofit = 0 then
if (capital + strategyprofit) <= capital - (capital * (maxcapitaldrop/100)) then
quit
endif
ENDIF
equity = capital + strategyprofit
if increase then
if equity/lastequity >= (1+(gaintoinc/100)) then
MMpositionsize = (max(minpossize,equity*psperc))
lastequity = equity
endif
ENDIF
if decrease then
if equity/lastequity <= (1-(losstodec/100)) then
MMpositionsize = (max(minpossize,equity*psperc))
lastequity = equity
endif
ENDIF
ENDIF
//*******************************************************************************************//
//Position Size,Monthly,Weekly,Daily,Intra,Trend,Reversal Modifiers (optional) //
//*******************************************************************************************//
//Berechnung am Schluss löschen ( auf 1 setzen) für Löschen des Modifikators
ONCE DaySLFlag = 0
ONCE PSizeL = startingsize
ONCE PSizeS = startingsize
ONCE MonthSizeL = 0
ONCE MonthSizeS = 0
ONCE WeekSizeL = 0
ONCE WeekSizeS = 0
ONCE DaySizeL = 0
ONCE DaySizeS = 0
ONCE Monatsanfang = 0
ONCE Monatsende = 0
ONCE IntraSizeL = 0
ONCE IntraSizeS = 0
IF Modifiers = 1 THEN
IF CurrentMonth = 1 OR CurrentMonth = 2 THEN
MonthSizeS = 0.25
MonthSizeL = 0
ELSIF CurrentMonth = 3 OR CurrentMonth = 4 THEN
MonthSizeS = 0
PMonthSizeL = 0.25
ELSIF CurrentMonth = 5 THEN
MonthSizeS = 0
MonthSizeL = 0
ELSIF CurrentMonth = 6 THEN
MonthSizeS = 0.25
MonthSizeL = 0
ELSIF CurrentMonth = 7 THEN
MonthSizeS = 0
MonthSizeL = 0.25
ELSIF CurrentMonth = 8 THEN
MonthSizeS = 0
MonthSizeL = 0
ELSIF CurrentMonth >= 9 AND CurrentMonth <= 10 THEN
MonthSizeS = 0.25
MonthSizeL = 0
ELSIF CurrentMonth >= 11 AND CurrentMonth <= 12 THEN
MonthSizeS = 0
MonthSizeL = 0.5
ENDIF
//Datumsliste jährlich anpassen und ggf erweitern!
IF Time = Opening THEN
If (OpenDay >= 03012021 AND Openday <= 07012021) OR (OpenDay >= 03012022 AND Openday <= 07012022) THEN
Monatsanfang = 1
ELSIF (OpenDay >= 01022021 AND Openday <= 05022021) OR (OpenDay >= 01022022 AND Openday <= 04022022) THEN
Monatsanfang = 1
ELSIF (OpenDay >= 01032021 AND Openday <= 05032021) OR (OpenDay >= 01032022 AND Openday <= 04032022) THEN
Monatsanfang = 1
ELSIF (OpenDay >= 01042021 AND Openday <= 05042021) OR (OpenDay >= 01042022 AND Openday <= 08042022) THEN
Monatsanfang = 1
ELSIF (OpenDay >= 01102021 AND Openday <= 05102021) OR (OpenDay >= 01102022 AND Openday <= 07102022) THEN
Monatsanfang = 1
ELSIF (OpenDay >= 01112021 AND Openday <= 05112021) OR (OpenDay >= 01112022 AND Openday <= 04112022) THEN
Monatsanfang = 1
ELSIF (OpenDay >= 01122021 AND Openday <= 05122021) OR (OpenDay >= 01122022 AND Openday <= 05122022) THEN
Monatsanfang = 1
ENDIF
ELSIF Time = Closing THEN
Monatsanfang = 0
Monatsende = 0
ENDIF
If Monatsanfang = 1 THEN
WeekSizeL = 0.25
ENDIF
If Monatsende = 1 THEN
WeekSizeS = 0.25
ENDIF
IF OpenDayofWeek = 1 THEN
DaySizeL = 0.25
DaySizeS = 0
ELSIF OpenDayofWeek = 5 Then
DaySizeL = 0
DaySizeS = 0.25
ELSIF OpenDayofWeek <1 OR (OpenDayofWeek >= 2 AND OpenDayOfWeek <= 4) OR OpenDayofWeek = 6 THEN
DaySizeL = 0
DaySizeS = 0
ENDIF
IF Close < DLow(1) AND DaySLFlag = 1 THEN
IntraSizeL = 0.25
IntraSizeS = 0
ELSIF Close > DHigh(1) AND DaySLFlag = 1 THEN
IntraSizeL = 0
IntraSizeS = 0.25
ELSIF DaySLFlag = 0 THEN
IntraSizeL = 0
IntraSizeS = 0
ENDIF
IF Time >= Opening AND Time <= Closing THEN
IF Close > ResR2 THEN
IDReversalL = 0.25
ELSIF Close > ResR3 THEN
IDReversalL = 0.5
ELSIF Close < SupS2 THEN
IDReversalS = 0.25
ELSIF Close > SupS3 THEN
IDReversalS = 0.5
ELSIF Close < ResR2 AND Close > SupS2 THEN
IDReversalL = 0
IDReversalS = 0
ENDIF
ENDIF
IF Time = Closing THEN
TrendUp = 0
TrendDown = 0
ENDIF
IF Time = Opening THEN
IF DHigh(1) > DHigh(2) AND DLow(1) > DLow(2) THEN
TrendUp = 0.25
TrendDown = 0
ELSIF DHigh(1) < DHigh(2) AND DLow(1) < DLow(2) THEN
TrendUp = 0
TrendDown = 0.25
ENDIF
ENDIF
//Einzelne Komponenten können nach belieben hier gelöscht werden, um die Modifikationen an eigene Vorstellungen anzupassen
PositionSizeLong = (PSizeL+TrendUp+IDReversalL+IntraSizeL+DaySizeL+WeekSizeL+MonthSizeL)*MMpositionsize
PositionSizeShort = (PSizeS+TrendDown+IDReversalS+IntraSizeS+DaySizeS+WeekSizeL+MonthSizeS)*MMpositionsize
ELSIF Modifiers = 0 THEN
PositionSizeLong = PSizeL*MMpositionsize
PositionSizeShort = PSizeS*MMpositionsize
ENDIF
//*************************************************************************//
//Xetra-Korrektur High Low Close, Pivot, Resistance, Support //
//*************************************************************************//
if Time = Closing AND OPENDAYOFWEEK <6 AND OPENDAYOFWEEK >0 then
DayClose = Close
DayHigh = Highest[840](close[1])
DayLow = Lowest[840](close[1])
ENDIF
Pivot= (DayHigh + DayLow + DayClose) / 3
ResR1 = Pivot + (Pivot - DayLow)
ResR2 = Pivot + (Dayhigh - Daylow)
ResR3 = Dayhigh + (2 * (Pivot - Daylow))
SupS1 = Pivot - (Dayhigh - Pivot)
SupS2 = Pivot - (Dayhigh - Daylow)
SupS3 = Daylow - (2 * (Dayhigh - Pivot))
//*************************************************************************//
//Moving-Average-Clustering-FilterCode (optional) //
//*************************************************************************//
x = 10*pipsize //10-pip range
ma5 = average[5,0](close)
ma50 = average[50,0](close)
ma100 = average[100,0](close)
ma200 = average[200,0](close)
MaxMA = max(ma5,max(ma50,max(ma100,ma200)))
MinMA = min(ma5,min(ma50,min(ma100,ma200)))
IF ClusterSave = 1 THEN
IF (MaxMA - MinMA) <= x THEN
Tradeon = 0
ELSIF (MaxMA - MinMA) > x THEN
IF RobustnessTest = 0 THEN
Tradeon = 1
ELSIF opendate >= startdate AND NOT (barindex mod qty = 0 or barindex mod qty = j) THEN
Tradeon = 0
ENDIF
ENDIF
ENDIF
//*************************************************************************//
//R2-Long/S2-Short Filter (optional) //
//*************************************************************************//
IF ForbiddenLSFlag = 1 THEN
ForbiddenLong = Close < SupS2
ForbiddenShort = Close > ResR2
ELSIF ForbiddenLSFlag = 2 THEN
ForbiddenLong = Close > ResR2
ForbiddenShort = Close < SupS2
ELSIF ForbiddenLSFlag = 0 THEN
ForbiddenLong = 0
ForbiddenShort = 0
ENDIF
//*************************************************************************//
//trailing stop function Risk, Long, Short //
//*************************************************************************//
//wenn gewünscht die Parameter ändern oder die SET STOP-CODES löschen
trailingstartL = 35 //LONG trailing will start @trailinstart points profit
trailingstartS = 35 //SHORT trailing will start @trailinstart points profit
trailingL = 25 //trailing step to move the "stoploss"
trailingS= 25 //trailing step to move the "stoploss"
trailingR= 15 //trailing start+step to move the "stoploss" for risky positions
SaveDistanceL = 10 //Minimum Stop-Abstand 10 lt IG
SaveDistanceS = 10 //Minimum Stop-Abstand 10 lt IG
SaveDistanceR = 10 //Minimum Stop-Abstand 10 lt IG
MinimumPlus = 3 //Anzahl Pips zum Breakeven inkl. Spread+Gebühren
//reset the stoploss value
IF NOT ONMARKET THEN
TrailingFlag = 0
NewSL = 0
ENDIF
//************************//
//manage long positions //
//***********************//
IF LOngONMarket AND TrailingFlag = 0 THEN
newSL = tradeprice(1)-(trailingstartL*pipsize)
SET STOP pTrailing trailingstartL
ENDIF
//breakeven
IF (close-tradeprice(1)) >= ((MinimumPlus+SaveDistanceL)*pipsize) AND TrailingFlag = 0 THEN
TrailingFlag = 1
newSL = tradeprice(1)+(MinimumPlus*pipsize)
SET STOP pLOSS SaveDistanceL
ENDIF
IF (close-newSL) >= ((trailingL)*pipsize) AND TrailingFlag = 1 THEN
newSL = close-(trailingL*pipsize)
SET STOP pTrailing trailingL
ENDIF
IF LongOnMarket AND TrailingFlag = 2 THEN
newSL = tradeprice(1)-(trailingR*pipsize)
SET STOP pTrailing trailingR
//breakeven
IF (close-tradeprice(1)) >= ((MinimumPlus+SaveDistanceR)*pipsize) THEN
newSL = tradeprice(1)+(MinimumPlus*pipsize)
TrailingFlag = 3
SET STOP pLOSS SaveDistanceR
ENDIF
IF (close-newSL) >= (trailingR*pipsize) AND TrailingFlag = 3 THEN
newSL = close-(trailingR*pipsize)
SET STOP pTrailing trailingR
ENDIF
ENDIF
//************************//
//manage Short positions //
//************************//
IF ShortONMarket AND TrailingFlag = 0 THEN
newSL = tradeprice(1)+(trailingstartS*pipsize)
SET STOP pTrailing trailingstartS
ENDIF
//breakeven
IF (tradeprice(1)-close) >= ((MinimumPlus+SaveDistanceS)*pipsize) AND TrailingFlag = 0 THEN
TrailingFlag = 1
newSL = tradeprice(1)-(MinimumPlus*pipsize)
SET STOP pLOSS SaveDistanceS
ENDIF
IF (newSL-close) >= (trailingS*pipsize) AND TrailingFlag = 1 THEN
newSL = close+(trailingS*pipsize)
SET STOP pTrailing trailingS
ENDIF
IF ShortOnMarket AND TrailingFlag = 2 THEN
newSL = tradeprice(1)+(trailingR*pipsize)
SET STOP pTrailing trailingR
ENDIF
//breakeven
IF (tradeprice(1)-close) >= ((MinimumPlus+SaveDistanceR)*pipsize) THEN
newSL = tradeprice(1)-(MinimumPlus*pipsize)
TrailingFlag = 3
SET STOP pLOSS SaveDistanceR
ENDIF
IF (newSL-close) >= (trailingR*pipsize) AND TrailingFlag = 3 THEN
newSL = close+(trailingR*pipsize)
SET STOP pTrailing trailingR
ENDIF
//stop order to exit the positions
IF LONGOnMarket AND newSL > 0 AND Close <= newSL THEN
SELL AT MARKET
ELSIF ShortOnMarket AND newSL > 0 AND Close >= newSL THEN
EXITSHORT AT MARKET
ENDIF
//*************************************************************************//
//Tagesschlussrally //
//*************************************************************************//
If Time > 160000 AND Time < 173000 AND TradeDay AND TRADEON THEN
IF NOT OnMarket AND NOT ForbiddenLong AND Close > Highest[3](close[1]) AND Close > Close[1] AND Aroonup crosses over aroondown AND RSI > 60 AND MACD > 0 AND Close > Pivot THEN
TrailingFlag = 0
Buy PositionSizeLong Contracts at Market
ENDIF
ENDIF
Addendum: Basis TimeFrame ist 1 Minute.
Änderung für Anpassung an andere Timeframes:
Einfügen in Zeile 33:
ONCE TF = 1 // TimeFrame in Minuten
Ändern in Zeilen 268+269:
DayHigh = Highest[(840/TF)](close[1])
DayLow = Lowest[(840/TF)](close[1])
Addendum 2:
HexenSabbat-Filter.
in Parameter einfügen:
ONCE HexSabFilter = 1 // 0=OFF 1=ON HexenSabbatFilter
Neues Modul:
//*************************************************************************//
//HexenSabbat-Filter //
//*************************************************************************//
//Datumsliste ggf jährlich anpassen
IF HexSabFilter = 1 THEN
IF Date = (19032021 OR 18062021 OR 17092021 OR 17122021 OR 18032022 OR 17062022 OR 16092022 OR 16122022) THEN
Tradeon = 0
ELSIF RobustnessTest = 0 AND Opening AND Tradeday THEN
TradeON = 1
ENDIF
ENDIF
Modulare Algorithmus Basis
This topic contains 2 replies,
has 1 voice, and was last updated by Stefan Sticker
4 years ago.
| Forum: | ProOrder: Automatischer Handel & Backtesting |
| Language: | German |
| Started: | 01/30/2022 |
| Status: | Active |
| Attachments: | No files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.