[beta-testing] multi timeframe support for automatic trading, ideas are welcome!
Forums › ProRealTime English forum › ProOrder support › [beta-testing] multi timeframe support for automatic trading, ideas are welcome!
- This topic has 287 replies, 47 voices, and was last updated 4 years ago by Brianoshea.
Tagged: mtf, multitimeframe
-
-
08/08/2018 at 3:58 PM #77788
Or in this article I wrote today:
Partial closure of positions when price is retracing – A complete function
08/09/2018 at 4:14 PM #7787808/10/2018 at 9:18 PM #77987Offline
Ok, but i tried with a Daily code like Pathfinder Swing, and using the Timeframe 1h for the trailing stop funcion, PRT said that the the all the timeframes should be multiplies of the main TF.
08/10/2018 at 9:54 PM #77989Yes, they need to be multiple of the main timeframe, which is the lowest one, the one you choose to launch your strategy.
If you use the daily TF, you are allowed to launch it from a 1-hour chart.
If you are using Daily+1-hour TF you will not be allowed to launch your strategy from a 4-hour chart because it’s not the lowest TF,
If you are using 4-hour TF + 15-minute TF you won’t be allowed to launch your strategy from a 10-minute because 15 is not a multiple of 10, in this case use 5 minutes instead, or change 15 to 30.
08/16/2018 at 11:00 AM #78355BARINDEX always keeps track of the bars in the lowest TF (the one used to launch a strategy, or default TF), so we need to set up variables to count bars on higher TFs as time goes by.
To avoid this overhead I suggest that, mirroring the TIMEFRAME syntax, also BARINDEX accepts a TF within parenthesis, like:
123BarIndex (10 seconds)BarIndex (1 hours)Barindex (Monthly)and that referencing past bars would still be allowed:
1234BarIndex (4 hours)[0] //or just BarIndex (4 hours)BarIndex (4 hours)[1]..08/18/2018 at 3:13 PM #78548It would be nice if we could optimize the time frame. For example:
1234567//x = 52 //(variable to be optimized)timeframe (x weeks, default)a = weightedclose[1]timeframe (weekly, default)(strategy here with filter using 'a')08/21/2018 at 6:04 PM #78717Ok, but results are not the same, i tried to use a 4h strategy in a 30m Timeframe, at the beginning of the code i’ve wrote
1timeframe(4h)so the strategy should work exactly as a 4hr TF but not, doesn’t happened and results are totally differents, can i post the code?
08/21/2018 at 7:00 PM #78719can i post the code?
That would be helpful – every problem can be solved with the maximum amount of information! 🙂
08/21/2018 at 8:22 PM #78720Ok, let us take the Pathfinder Code of the Eur/Usd
BARINDEX always keeps track of the bars in the lowest TF (the one used to launch a strategy, or default TF), so we need to set up variables to count bars on higher TFs as time goes by.
To avoid this overhead I suggest that, mirroring the TIMEFRAME syntax, also BARINDEX accepts a TF within parenthesis, like:
123BarIndex (10 seconds)BarIndex (1 hours)Barindex (Monthly)and that referencing past bars would still be allowed:
1234BarIndex (4 hours)[0] //or just BarIndex (4 hours)BarIndex (4 hours)[1]..i think is the problem that i found with the code like pathfinder, but if i change the Barindex as you said there is a sintax error:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345// ProOrder code parameterDEFPARAM CUMULATEORDERS = true // cumulate orders if not turned offDEFPARAM PRELOADBARS = 10000timeframe (4h, default)// define intraday trading windowONCE startTime = 90000 // start time of trading window in CETONCE endTime = 210000 // end time of trading window in CET// define instrument signalline with help of multiple smoothed averagesONCE periodFirstMA = 5 // 5 is center of gravity, do not changeONCE periodSecondMA = 10 // 10 is center of gravity, do not changeONCE periodThirdMA = 3 // heartbeat of the instrument// define filter parameterONCE periodLongMA = 150 // period lenght of the long moving average that works as filterONCE periodShortMA = 210 // period lenght of the short moving average that works as filter// define money and position management parameter// dynamic scaling of the chance/risk profile depending on account sizeONCE startRisk = 1//1 // start risk level e.g 0.25 - 25%, 0.5 - 50%, 0.75 - 75%, 1 - 100% and so onONCE maxRisk = 20 // max risk level e.g 1.5 - 150%ONCE increaseRiskLevel = 500// amount of profit from which the risk is to be increasedONCE increaseRiskStep = 1.5//1//1//1 // step by which the risk should be increased// size calculation: size = positionSize * trendMultiplier * saisonalPatternMultiplier * scaleFactorONCE positionSize = 1 // default start sizeONCE trendMultiplier = 2 // >1 with dynamic position sizing; 1 withoutONCE maxPositionSizePerTrade = 500 // maximum size per tradeONCE maxPositionSizeLong = 500 // maximum size for long positionsONCE maxPositionSizeShort = 500 // maximum size for short positionsONCE stopLossLong = 1.6//2.8 //in %ONCE stopLossShort = 2.4//2.4 //in %ONCE takeProfitLong = 1.7//1 //in %ONCE takeProfitShort =0.8// 0.8 //in %ONCE trailingStartLong = 0.2 //in %ONCE trailingStartShort = 0.1 //in %ONCE trailingStepLong = 0.1 //in %ONCE trailingStepShort = 0.1 //in %timeframe (4h)ONCE maxCandlesLongWithProfit = 1//1//1 //take long profit latest after x candlesONCE maxCandlesShortWithProfit = 2//2 // take short profit latest after x candlesONCE maxCandlesLongWithoutProfit = 18 // limit long loss latest after x candlesONCE maxCandlesShortWithoutProfit = 25 // limit short loss latest after x candles// define saisonal position multiplier for each month 1-15 / 16-31 (>0 - long / <0 - short / 0 no trade)ONCE January1 = 3ONCE January2 = 0ONCE February1 = 1ONCE February2 = 3ONCE March1 = 0ONCE March2 = 3ONCE April1 = 3ONCE April2 = 3ONCE May1 = 3ONCE May2 = 1ONCE June1 = 3ONCE June2 = 3ONCE July1 = 0ONCE July2 = 0ONCE August1 = 3ONCE August2 = 3ONCE September1 = 3ONCE September2 = 0ONCE October1 = 3ONCE October2 = 3ONCE November1 = 3ONCE November2 = 2ONCE December1 = 3ONCE December2 = 3// calculate the scaling factor based on the parameterscaleFactor = MIN(maxRisk, MAX(startRisk, ROUND(StrategyProfit / increaseRiskLevel) * increaseRiskStep))// dynamic position sizing based on weekly performanceONCE profitLastWeek = 0IF DayOfWeek <> DayOfWeek[1] AND DayOfWeek = 1 THENIF StrategyProfit > profitLastWeek + 1 THENpositionSize = MIN(trendMultiplier, positionSize + 1) // increase riskELSEpositionSize = MAX(1, positionSize - 1) // decrease riskENDIFprofitLastWeek = strategyProfitENDIF// calculate daily high/low (include sunday values if available)dailyHigh = DHigh(1)dailyLow = DLow(1)previousDailyHigh = DHigh(2)// calculate weekly high, weekly low is a poor signalIf DayOfWeek < DayOfWeek[1] AND lastweekbarindex = 0 THENlastWeekBarIndex = BarIndexELSEIF DayOfWeek < DayOfWeek[1] THENweeklyHigh = Highest[BarIndex - lastWeekBarIndex](dailyHigh)lastWeekBarIndex = BarIndexENDIFENDIF// calculate monthly high/lowIF Month <> Month[1] AND lastMonthBarIndex=0 THENlastMonthBarIndex=barindexELSIF Month <> Month[1] THENmonthlyHigh = Highest[BarIndex - lastMonthBarIndex](dailyHigh)monthlyLow = Lowest[BarIndex - lastMonthBarIndex](dailyLow)lastMonthBarIndex = BarIndexENDIF// calculate instrument signalline with multiple smoothed averagesfirstMA = WilderAverage[periodFirstMA](close)secondMA = TimeSeriesAverage[periodSecondMA](firstMA)signalline = TimeSeriesAverage[periodThirdMA](secondMA)// save position before trading window is openIF Time < startTime THENstartPositionLong = COUNTOFLONGSHARESstartPositionShort = COUNTOFSHORTSHARESENDIF// trade only in defined trading windowIF Time >= startTime AND Time <= endTime THEN// set saisonal multipliercurrentDayOfTheMonth = OpenDaymidOfMonth = 15IF CurrentMonth = 1 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = January1ELSEsaisonalPatternMultiplier = January2ENDIFELSIF CurrentMonth = 2 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = February1ELSEsaisonalPatternMultiplier = February2ENDIFELSIF CurrentMonth = 3 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = March1ELSEsaisonalPatternMultiplier = March2ENDIFELSIF CurrentMonth = 4 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = April1ELSEsaisonalPatternMultiplier = April2ENDIFELSIF CurrentMonth = 5 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = May1ELSEsaisonalPatternMultiplier = May2ENDIFELSIF CurrentMonth = 6 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = June1ELSEsaisonalPatternMultiplier = June2ENDIFELSIF CurrentMonth = 7 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = July1ELSEsaisonalPatternMultiplier = July2ENDIFELSIF CurrentMonth = 8 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = August1ELSEsaisonalPatternMultiplier = August2ENDIFELSIF CurrentMonth = 9 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = September1ELSEsaisonalPatternMultiplier = September2ENDIFELSIF CurrentMonth = 10 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = October1ELSEsaisonalPatternMultiplier = October2ENDIFELSIF CurrentMonth = 11 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = November1ELSEsaisonalPatternMultiplier = November2ENDIFELSIF CurrentMonth = 12 THENIF currentDayOfTheMonth <= midOfMonth THENsaisonalPatternMultiplier = December1ELSEsaisonalPatternMultiplier = December2ENDIFENDIF// define trading filters// 1. use fast and slow averages as filter because not every breakout is profitablef1 = close > Average[periodLongMA](close)f2 = close < Average[periodLongMA](close)f3 = close > Average[periodShortMA](close)// 2. check if position already reduced in trading window as additonal filter criteriaalreadyReducedLongPosition = COUNTOFLONGSHARES < startPositionLongalreadyReducedShortPosition = COUNTOFSHORTSHARES < startPositionShort// long position conditionsl1 = signalline CROSSES OVER monthlyHighl2 = signalline CROSSES OVER weeklyHighl3 = signalline CROSSES OVER dailyHighl4 = signalline CROSSES OVER monthlyLow// short position conditionss1 = signalline CROSSES UNDER monthlyHighs2 = signalline CROSSES UNDER dailyLows3 = signalline CROSSES UNDER previousDailyHigh// long entry with order cumulationIF ( l1 OR l4 OR l2 OR (l3 AND f2) ) AND NOT alreadyReducedLongPosition THEN// check saisonal booster setup and max position sizeIF saisonalPatternMultiplier > 0 THENnumberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize * saisonalPatternMultiplier) * scaleFactor)IF (COUNTOFPOSITION + numberContracts) <= maxPositionSizeLong * scaleFactor THENIF SHORTONMARKET THENEXITSHORT AT MARKETENDIFBUY numberContracts CONTRACT AT MARKETENDIFELSIF saisonalPatternMultiplier <> 0 THENnumberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize) * scaleFactor)IF (COUNTOFPOSITION + numberContracts) <= maxPositionSizeLong * scaleFactor THENIF SHORTONMARKET THENEXITSHORT AT MARKETENDIFBUY numberContracts CONTRACT AT MARKETENDIFENDIFstopLoss = stopLossLongtakeProfit = takeProfitLongENDIF// short entry with order cumulationIF ( (s1 AND f3) OR (s2 AND f1) OR (s3 AND f3) ) AND NOT alreadyReducedShortPosition THEN// check saisonal booster setup and max position sizeIF saisonalPatternMultiplier < 0 THENnumberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize * ABS(saisonalPatternMultiplier)) * scaleFactor)IF (ABS(COUNTOFPOSITION) + numberContracts) <= maxPositionSizeShort * scaleFactor THENIF LONGONMARKET THENSELL AT MARKETENDIFSELLSHORT numberContracts CONTRACT AT MARKETENDIFELSIF saisonalPatternMultiplier <> 0 THENnumberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize) * scaleFactor)IF (ABS(COUNTOFPOSITION) + numberContracts) <= maxPositionSizeShort * scaleFactor THENIF LONGONMARKET THENSELL AT MARKETENDIFSELLSHORT numberContracts CONTRACT AT MARKETENDIFENDIFstopLoss = stopLossShorttakeProfit = takeProfitShortENDIF// stop and profit managementposProfit = (((close - positionprice) * pointvalue) * countofposition) / pipsizenumberCandles = (BarIndex - TradeIndex)m1 = posProfit > 0 AND numberCandles >= maxCandlesLongWithProfitm2 = posProfit > 0 AND numberCandles >= maxCandlesShortWithProfitm3 = posProfit < 0 AND numberCandles >= maxCandlesLongWithoutProfitm4 = posProfit < 0 AND numberCandles >= maxCandlesShortWithoutProfit// take profit after max candlesIF LONGONMARKET AND (m1 OR m3) THENSELL AT MARKETENDIFIF SHORTONMARKET AND (m2 OR m4) THENEXITSHORT AT MARKETENDIF// trailing stop function (convert % to pips)trailingStartLongInPoints = tradeprice(1) * trailingStartLong / 100trailingStartShortInPoints = tradeprice(1) * trailingStartShort / 100trailingStepLongInPoints = tradeprice(1) * trailingStepLong / 100trailingStepShortInPoints = tradeprice(1) * trailingStepShort / 100// reset the stoploss valueIF NOT ONMARKET THENnewSL = 0ENDIF// manage long positionsIF LONGONMARKET THEN// first move (breakeven)IF newSL = 0 AND close - tradeprice(1) >= trailingStartLongInPoints * pipsize THENnewSL = tradeprice(1) + trailingStepLongInPoints * pipsizestopLoss = stopLossLong * 0.1takeProfit = takeProfitLong * 2ENDIF// next movesIF newSL > 0 AND close - newSL >= trailingStepLongInPoints * pipsize THENnewSL = newSL + trailingStepLongInPoints * pipsizeENDIFENDIF// manage short positionsIF SHORTONMARKET THEN// first move (breakeven)IF newSL = 0 AND tradeprice(1) - close >= trailingStartShortInPoints * pipsize THENnewSL = tradeprice(1) - trailingStepShortInPoints * pipsizeENDIF// next movesIF newSL > 0 AND newSL - close >= trailingStepShortInPoints * pipsize THENnewSL = newSL - trailingStepShortInPoints * pipsizeENDIFENDIF// stop order to exit the positionsIF newSL > 0 THENIF LONGONMARKET THENSELL AT newSL STOPENDIFIF SHORTONMARKET THENEXITSHORT AT newSL STOPENDIFENDIF// superordinate stop and take profitSET STOP %LOSS stopLossSET TARGET %PROFIT takeProfitENDIF08/21/2018 at 10:56 PM #78732Gianluca, I don’t understand what use is my post for your startegy. Mine were jus suggestions for PRT, what’s wrong with your code?
You are uning 1 TF (4 hours), not MTF.
If you want to make it MTF, what TFs would you like to run it?
08/22/2018 at 9:28 AM #78747with: timeframe (4h, default)
Your strategy has a different behavior than using it without the timeframe instruction in a 4 hours timeframe. “Default” means that the code is read at Close of the timeframe you launch the strategy on.
08/23/2018 at 12:04 PM #78845In order to use the MTF in a trailing stop like using a strategy 4h and a trailing stop code every 30 minutes, we have to launch the default code (originally from 4h) in the 30m TF, then use the function
1timeframe (4h)Right? First of all i tried to compare the behavior of the sistem (the code i posted) to see if it work in the same way but it didn’t.
If i launch a code (originally 4h) in the TF 30m or 1h… and i use the function “timeframe (4h)” that should work in the same way of the originally 4h no?
08/23/2018 at 3:29 PM #78855You must put
12345timeframe(4 hour)(your strategy code that works in 4 hour time frame)timeframe (default)(any code such as your trailing stop that needs to work in the 30 minute time frame)and then run the strategy on a 30 minute chart.
08/24/2018 at 7:58 AM #78889You must put
12345timeframe(4 hour)(your strategy code that works in 4 hour time frame)timeframe (default)(any code such as your trailing stop that needs to work in the 30 minute time frame)and then run the strategy on a 30 minute chart.
yes, and the code of the 4hrs should work normally like in the 4hrs right? and if it doesn’t? should be a bug right?
08/24/2018 at 9:41 AM #78912yes, and the code of the 4hrs should work normally like in the 4hrs right? and if it doesn’t? should be a bug right?
or it could be that you are not using the correct setting DEFAULT or UPDATEONCLOSE.
123timeframe(4 hour,default)timeframe(4 hour,updateonclose)UPDATEONCLOSE will only change the 4 hour codes values, conditions being met etc when the 4 hour candle closes.
DEFAULT will change them when the candle closes in the timeframe that you are trading on.
-
AuthorPosts
Find exclusive trading pro-tools on