Modulare Algorithmus Basis
Forums › ProRealTime Deutsch forum › ProOrder Support › Modulare Algorithmus Basis
- This topic has 2 replies, 1 voice, and was last updated 2 years ago by Stefan Sticker.
-
-
01/30/2022 at 9:23 PM #187093
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ügenStefan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418//*************************************************************************////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 = 5000DEFPARAM CumulateOrders = FalseDEFPARAM FlatBefore = 080000DEFPARAM FlatAfter = 220000ONCE TradeON = 1ONCE ClusterSave = 1 // 0=OFF 1=ON MovingAverage-Clustering-FilterONCE startingsize = 1 // starting position sizeONCE ForbiddenLSFlag = 0 // 0=OFF 1=ON 2=Reverse S2/Short-R2/Long-FilterONCE RobustnessTest = 0 // 0=OFF 1=ON Robustness-TestONCE SSC = 1 // 0=OFF 1=ON Strategy-Stop-CodeONCE StartingCapital = 1000 // StartkapitalONCE Modifiers = 1 // 0=OFF 1=ON Modifikatoren auf PositionSize//*************************************************************************////Market Data ////*************************************************************************//ONCE Opening = 080000ONCE Closing = 220000Tradeday = 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 FailsaveIf ONMARKET AND (Time < Opening OR Time > Closing) THENSELL AT MarketEXITSHORT AT MarketENDIF//*************************************************************************////Algorithm Robustness-Test ////*************************************************************************//StartDate = 20000101 // ParameterQty = 5 // ParameterRndom = 3 // Parameteronce j = 0once flag = 1IF RobustnessTest = 1 THENif flag = 1 thenj = j + 1if j > qty thenflag = -1j = j - 1endifendifif flag = -1 thenj = j - 1if j = 0 thenj = j + rndomflag = 1endifendifif opendate >= startdate AND (barindex mod qty = 0 or barindex mod qty = j) thentradeon = 1ELSIF opendate >= startdate AND NOT (barindex mod qty = 0 or barindex mod qty = j) thentradeon = 0endifENDIF//*************************************************************************////Strategy-Stop-Code, Money Management/ReInvest ////*************************************************************************//barsbeforenextcheck = 30 // number of bars between performance checksdrawdownquitting = 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 happenincrease = 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 capitalstartingsize = 1 // starting position sizeminpossize = 0.2 // minimum position size allowedgaintoinc = 5 // % profit rise needed before an increase in position size is madelosstodec = 5 // % loss needed before a decrease in position size is mademaxdrawdown = 30 // maximum % draw down allowed from highest ever equity before stopping strategymaxcapitaldrop = 25 // maximum % starting capital lost before stopping strategyonce MMpositionsize = 1once psperc = MMpositionsize / capitalIF SSC = 1 THENif strategyprofit <> strategyprofit[1] thenhighestprofit = max(strategyprofit, highestprofit)if count < tradesbeforewrquit OR winrate > winratequit/100 thencount = count + 1if strategyprofit > strategyprofit[1] thenwin = win + 1endifENDIFwinrate = win/countENDIFif count >= tradesbeforewrquit AND winrate < winratequit/100 thenquitendifif barindex mod barsbeforenextcheck = 0 AND drawdownquitting AND highestprofit <> 0 thenif (capital + strategyprofit) <= (capital + highestprofit) - ((capital + highestprofit)*(maxdrawdown/100)) thenquitendifendifif count >= tradesbeforewrquit AND highestprofit = 0 thenif (capital + strategyprofit) <= capital - (capital * (maxcapitaldrop/100)) thenquitendifENDIFequity = capital + strategyprofitif increase thenif equity/lastequity >= (1+(gaintoinc/100)) thenMMpositionsize = (max(minpossize,equity*psperc))lastequity = equityendifENDIFif decrease thenif equity/lastequity <= (1-(losstodec/100)) thenMMpositionsize = (max(minpossize,equity*psperc))lastequity = equityendifENDIFENDIF//*******************************************************************************************////Position Size,Monthly,Weekly,Daily,Intra,Trend,Reversal Modifiers (optional) ////*******************************************************************************************////Berechnung am Schluss löschen ( auf 1 setzen) für Löschen des ModifikatorsONCE DaySLFlag = 0ONCE PSizeL = startingsizeONCE PSizeS = startingsizeONCE MonthSizeL = 0ONCE MonthSizeS = 0ONCE WeekSizeL = 0ONCE WeekSizeS = 0ONCE DaySizeL = 0ONCE DaySizeS = 0ONCE Monatsanfang = 0ONCE Monatsende = 0ONCE IntraSizeL = 0ONCE IntraSizeS = 0IF Modifiers = 1 THENIF CurrentMonth = 1 OR CurrentMonth = 2 THENMonthSizeS = 0.25MonthSizeL = 0ELSIF CurrentMonth = 3 OR CurrentMonth = 4 THENMonthSizeS = 0PMonthSizeL = 0.25ELSIF CurrentMonth = 5 THENMonthSizeS = 0MonthSizeL = 0ELSIF CurrentMonth = 6 THENMonthSizeS = 0.25MonthSizeL = 0ELSIF CurrentMonth = 7 THENMonthSizeS = 0MonthSizeL = 0.25ELSIF CurrentMonth = 8 THENMonthSizeS = 0MonthSizeL = 0ELSIF CurrentMonth >= 9 AND CurrentMonth <= 10 THENMonthSizeS = 0.25MonthSizeL = 0ELSIF CurrentMonth >= 11 AND CurrentMonth <= 12 THENMonthSizeS = 0MonthSizeL = 0.5ENDIF//Datumsliste jährlich anpassen und ggf erweitern!IF Time = Opening THENIf (OpenDay >= 03012021 AND Openday <= 07012021) OR (OpenDay >= 03012022 AND Openday <= 07012022) THENMonatsanfang = 1ELSIF (OpenDay >= 01022021 AND Openday <= 05022021) OR (OpenDay >= 01022022 AND Openday <= 04022022) THENMonatsanfang = 1ELSIF (OpenDay >= 01032021 AND Openday <= 05032021) OR (OpenDay >= 01032022 AND Openday <= 04032022) THENMonatsanfang = 1ELSIF (OpenDay >= 01042021 AND Openday <= 05042021) OR (OpenDay >= 01042022 AND Openday <= 08042022) THENMonatsanfang = 1ELSIF (OpenDay >= 01102021 AND Openday <= 05102021) OR (OpenDay >= 01102022 AND Openday <= 07102022) THENMonatsanfang = 1ELSIF (OpenDay >= 01112021 AND Openday <= 05112021) OR (OpenDay >= 01112022 AND Openday <= 04112022) THENMonatsanfang = 1ELSIF (OpenDay >= 01122021 AND Openday <= 05122021) OR (OpenDay >= 01122022 AND Openday <= 05122022) THENMonatsanfang = 1ENDIFELSIF Time = Closing THENMonatsanfang = 0Monatsende = 0ENDIFIf Monatsanfang = 1 THENWeekSizeL = 0.25ENDIFIf Monatsende = 1 THENWeekSizeS = 0.25ENDIFIF OpenDayofWeek = 1 THENDaySizeL = 0.25DaySizeS = 0ELSIF OpenDayofWeek = 5 ThenDaySizeL = 0DaySizeS = 0.25ELSIF OpenDayofWeek <1 OR (OpenDayofWeek >= 2 AND OpenDayOfWeek <= 4) OR OpenDayofWeek = 6 THENDaySizeL = 0DaySizeS = 0ENDIFIF Close < DLow(1) AND DaySLFlag = 1 THENIntraSizeL = 0.25IntraSizeS = 0ELSIF Close > DHigh(1) AND DaySLFlag = 1 THENIntraSizeL = 0IntraSizeS = 0.25ELSIF DaySLFlag = 0 THENIntraSizeL = 0IntraSizeS = 0ENDIFIF Time >= Opening AND Time <= Closing THENIF Close > ResR2 THENIDReversalL = 0.25ELSIF Close > ResR3 THENIDReversalL = 0.5ELSIF Close < SupS2 THENIDReversalS = 0.25ELSIF Close > SupS3 THENIDReversalS = 0.5ELSIF Close < ResR2 AND Close > SupS2 THENIDReversalL = 0IDReversalS = 0ENDIFENDIFIF Time = Closing THENTrendUp = 0TrendDown = 0ENDIFIF Time = Opening THENIF DHigh(1) > DHigh(2) AND DLow(1) > DLow(2) THENTrendUp = 0.25TrendDown = 0ELSIF DHigh(1) < DHigh(2) AND DLow(1) < DLow(2) THENTrendUp = 0TrendDown = 0.25ENDIFENDIF//Einzelne Komponenten können nach belieben hier gelöscht werden, um die Modifikationen an eigene Vorstellungen anzupassenPositionSizeLong = (PSizeL+TrendUp+IDReversalL+IntraSizeL+DaySizeL+WeekSizeL+MonthSizeL)*MMpositionsizePositionSizeShort = (PSizeS+TrendDown+IDReversalS+IntraSizeS+DaySizeS+WeekSizeL+MonthSizeS)*MMpositionsizeELSIF Modifiers = 0 THENPositionSizeLong = PSizeL*MMpositionsizePositionSizeShort = PSizeS*MMpositionsizeENDIF//*************************************************************************////Xetra-Korrektur High Low Close, Pivot, Resistance, Support ////*************************************************************************//if Time = Closing AND OPENDAYOFWEEK <6 AND OPENDAYOFWEEK >0 thenDayClose = CloseDayHigh = Highest[840](close[1])DayLow = Lowest[840](close[1])ENDIFPivot= (DayHigh + DayLow + DayClose) / 3ResR1 = 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 rangema5 = 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 THENIF (MaxMA - MinMA) <= x THENTradeon = 0ELSIF (MaxMA - MinMA) > x THENIF RobustnessTest = 0 THENTradeon = 1ELSIF opendate >= startdate AND NOT (barindex mod qty = 0 or barindex mod qty = j) THENTradeon = 0ENDIFENDIFENDIF//*************************************************************************////R2-Long/S2-Short Filter (optional) ////*************************************************************************//IF ForbiddenLSFlag = 1 THENForbiddenLong = Close < SupS2ForbiddenShort = Close > ResR2ELSIF ForbiddenLSFlag = 2 THENForbiddenLong = Close > ResR2ForbiddenShort = Close < SupS2ELSIF ForbiddenLSFlag = 0 THENForbiddenLong = 0ForbiddenShort = 0ENDIF//*************************************************************************////trailing stop function Risk, Long, Short ////*************************************************************************////wenn gewünscht die Parameter ändern oder die SET STOP-CODES löschentrailingstartL = 35 //LONG trailing will start @trailinstart points profittrailingstartS = 35 //SHORT trailing will start @trailinstart points profittrailingL = 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 positionsSaveDistanceL = 10 //Minimum Stop-Abstand 10 lt IGSaveDistanceS = 10 //Minimum Stop-Abstand 10 lt IGSaveDistanceR = 10 //Minimum Stop-Abstand 10 lt IGMinimumPlus = 3 //Anzahl Pips zum Breakeven inkl. Spread+Gebühren//reset the stoploss valueIF NOT ONMARKET THENTrailingFlag = 0NewSL = 0ENDIF//************************////manage long positions ////***********************//IF LOngONMarket AND TrailingFlag = 0 THENnewSL = tradeprice(1)-(trailingstartL*pipsize)SET STOP pTrailing trailingstartLENDIF//breakevenIF (close-tradeprice(1)) >= ((MinimumPlus+SaveDistanceL)*pipsize) AND TrailingFlag = 0 THENTrailingFlag = 1newSL = tradeprice(1)+(MinimumPlus*pipsize)SET STOP pLOSS SaveDistanceLENDIFIF (close-newSL) >= ((trailingL)*pipsize) AND TrailingFlag = 1 THENnewSL = close-(trailingL*pipsize)SET STOP pTrailing trailingLENDIFIF LongOnMarket AND TrailingFlag = 2 THENnewSL = tradeprice(1)-(trailingR*pipsize)SET STOP pTrailing trailingR//breakevenIF (close-tradeprice(1)) >= ((MinimumPlus+SaveDistanceR)*pipsize) THENnewSL = tradeprice(1)+(MinimumPlus*pipsize)TrailingFlag = 3SET STOP pLOSS SaveDistanceRENDIFIF (close-newSL) >= (trailingR*pipsize) AND TrailingFlag = 3 THENnewSL = close-(trailingR*pipsize)SET STOP pTrailing trailingRENDIFENDIF//************************////manage Short positions ////************************//IF ShortONMarket AND TrailingFlag = 0 THENnewSL = tradeprice(1)+(trailingstartS*pipsize)SET STOP pTrailing trailingstartSENDIF//breakevenIF (tradeprice(1)-close) >= ((MinimumPlus+SaveDistanceS)*pipsize) AND TrailingFlag = 0 THENTrailingFlag = 1newSL = tradeprice(1)-(MinimumPlus*pipsize)SET STOP pLOSS SaveDistanceSENDIFIF (newSL-close) >= (trailingS*pipsize) AND TrailingFlag = 1 THENnewSL = close+(trailingS*pipsize)SET STOP pTrailing trailingSENDIFIF ShortOnMarket AND TrailingFlag = 2 THENnewSL = tradeprice(1)+(trailingR*pipsize)SET STOP pTrailing trailingRENDIF//breakevenIF (tradeprice(1)-close) >= ((MinimumPlus+SaveDistanceR)*pipsize) THENnewSL = tradeprice(1)-(MinimumPlus*pipsize)TrailingFlag = 3SET STOP pLOSS SaveDistanceRENDIFIF (newSL-close) >= (trailingR*pipsize) AND TrailingFlag = 3 THENnewSL = close+(trailingR*pipsize)SET STOP pTrailing trailingRENDIF//stop order to exit the positionsIF LONGOnMarket AND newSL > 0 AND Close <= newSL THENSELL AT MARKETELSIF ShortOnMarket AND newSL > 0 AND Close >= newSL THENEXITSHORT AT MARKETENDIF//*************************************************************************////Tagesschlussrally ////*************************************************************************//If Time > 160000 AND Time < 173000 AND TradeDay AND TRADEON THENIF 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 THENTrailingFlag = 0Buy PositionSizeLong Contracts at MarketENDIFENDIF2 users thanked author for this post.
01/30/2022 at 9:55 PM #187098Addendum: 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])01/31/2022 at 6:12 AM #187104Addendum 2:
HexenSabbat-Filter.in Parameter einfügen:
ONCE HexSabFilter = 1 // 0=OFF 1=ON HexenSabbatFilterNeues 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 -
AuthorPosts