ProRealCode - Trading & Coding with ProRealTime™
Or in this article I wrote today:
Partial closure of positions when price is retracing – A complete function
Hola together, this is probably a MTF system for which I need some help from you. Pls find more information attached Rgds Marc
P.s.: there is already a thread within this forum for this idea I’ve opened yesterday
OfflineOk, 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.
BarIndex (10 seconds)
BarIndex (1 hours)
Barindex (Monthly)
and that referencing past bars would still be allowed:
BarIndex (4 hours)[0] //or just BarIndex (4 hours)
BarIndex (4 hours)[1]
.
.
//x = 52 //(variable to be optimized)
timeframe (x weeks, default)
a = weightedclose[1]
timeframe (weekly, default)
(strategy here with filter using 'a')
timeframe(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?
can i post the code?That would be helpful – every problem can be solved with the maximum amount of information! 🙂
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: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:and that referencing past bars would still be allowed:
123 BarIndex (10 seconds)BarIndex (1 hours)Barindex (Monthly)
1234 BarIndex (4 hours)[0] //or just BarIndex (4 hours)BarIndex (4 hours)[1]..
// ProOrder code parameter
DEFPARAM CUMULATEORDERS = true // cumulate orders if not turned off
DEFPARAM PRELOADBARS = 10000
timeframe (4h, default)
// define intraday trading window
ONCE startTime = 90000 // start time of trading window in CET
ONCE endTime = 210000 // end time of trading window in CET
// define instrument signalline with help of multiple smoothed averages
ONCE periodFirstMA = 5 // 5 is center of gravity, do not change
ONCE periodSecondMA = 10 // 10 is center of gravity, do not change
ONCE periodThirdMA = 3 // heartbeat of the instrument
// define filter parameter
ONCE periodLongMA = 150 // period lenght of the long moving average that works as filter
ONCE 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 size
ONCE startRisk = 1//1 // start risk level e.g 0.25 - 25%, 0.5 - 50%, 0.75 - 75%, 1 - 100% and so on
ONCE maxRisk = 20 // max risk level e.g 1.5 - 150%
ONCE increaseRiskLevel = 500// amount of profit from which the risk is to be increased
ONCE increaseRiskStep = 1.5//1//1//1 // step by which the risk should be increased
// size calculation: size = positionSize * trendMultiplier * saisonalPatternMultiplier * scaleFactor
ONCE positionSize = 1 // default start size
ONCE trendMultiplier = 2 // >1 with dynamic position sizing; 1 without
ONCE maxPositionSizePerTrade = 500 // maximum size per trade
ONCE maxPositionSizeLong = 500 // maximum size for long positions
ONCE maxPositionSizeShort = 500 // maximum size for short positions
ONCE 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 candles
ONCE maxCandlesShortWithProfit = 2//2 // take short profit latest after x candles
ONCE maxCandlesLongWithoutProfit = 18 // limit long loss latest after x candles
ONCE 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 = 3
ONCE January2 = 0
ONCE February1 = 1
ONCE February2 = 3
ONCE March1 = 0
ONCE March2 = 3
ONCE April1 = 3
ONCE April2 = 3
ONCE May1 = 3
ONCE May2 = 1
ONCE June1 = 3
ONCE June2 = 3
ONCE July1 = 0
ONCE July2 = 0
ONCE August1 = 3
ONCE August2 = 3
ONCE September1 = 3
ONCE September2 = 0
ONCE October1 = 3
ONCE October2 = 3
ONCE November1 = 3
ONCE November2 = 2
ONCE December1 = 3
ONCE December2 = 3
// calculate the scaling factor based on the parameter
scaleFactor = MIN(maxRisk, MAX(startRisk, ROUND(StrategyProfit / increaseRiskLevel) * increaseRiskStep))
// dynamic position sizing based on weekly performance
ONCE profitLastWeek = 0
IF DayOfWeek <> DayOfWeek[1] AND DayOfWeek = 1 THEN
IF StrategyProfit > profitLastWeek + 1 THEN
positionSize = MIN(trendMultiplier, positionSize + 1) // increase risk
ELSE
positionSize = MAX(1, positionSize - 1) // decrease risk
ENDIF
profitLastWeek = strategyProfit
ENDIF
// 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 signal
If DayOfWeek < DayOfWeek[1] AND lastweekbarindex = 0 THEN
lastWeekBarIndex = BarIndex
ELSE
IF DayOfWeek < DayOfWeek[1] THEN
weeklyHigh = Highest[BarIndex - lastWeekBarIndex](dailyHigh)
lastWeekBarIndex = BarIndex
ENDIF
ENDIF
// calculate monthly high/low
IF Month <> Month[1] AND lastMonthBarIndex=0 THEN
lastMonthBarIndex=barindex
ELSIF Month <> Month[1] THEN
monthlyHigh = Highest[BarIndex - lastMonthBarIndex](dailyHigh)
monthlyLow = Lowest[BarIndex - lastMonthBarIndex](dailyLow)
lastMonthBarIndex = BarIndex
ENDIF
// calculate instrument signalline with multiple smoothed averages
firstMA = WilderAverage[periodFirstMA](close)
secondMA = TimeSeriesAverage[periodSecondMA](firstMA)
signalline = TimeSeriesAverage[periodThirdMA](secondMA)
// save position before trading window is open
IF Time < startTime THEN
startPositionLong = COUNTOFLONGSHARES
startPositionShort = COUNTOFSHORTSHARES
ENDIF
// trade only in defined trading window
IF Time >= startTime AND Time <= endTime THEN
// set saisonal multiplier
currentDayOfTheMonth = OpenDay
midOfMonth = 15
IF CurrentMonth = 1 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = January1
ELSE
saisonalPatternMultiplier = January2
ENDIF
ELSIF CurrentMonth = 2 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = February1
ELSE
saisonalPatternMultiplier = February2
ENDIF
ELSIF CurrentMonth = 3 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = March1
ELSE
saisonalPatternMultiplier = March2
ENDIF
ELSIF CurrentMonth = 4 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = April1
ELSE
saisonalPatternMultiplier = April2
ENDIF
ELSIF CurrentMonth = 5 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = May1
ELSE
saisonalPatternMultiplier = May2
ENDIF
ELSIF CurrentMonth = 6 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = June1
ELSE
saisonalPatternMultiplier = June2
ENDIF
ELSIF CurrentMonth = 7 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = July1
ELSE
saisonalPatternMultiplier = July2
ENDIF
ELSIF CurrentMonth = 8 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = August1
ELSE
saisonalPatternMultiplier = August2
ENDIF
ELSIF CurrentMonth = 9 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = September1
ELSE
saisonalPatternMultiplier = September2
ENDIF
ELSIF CurrentMonth = 10 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = October1
ELSE
saisonalPatternMultiplier = October2
ENDIF
ELSIF CurrentMonth = 11 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = November1
ELSE
saisonalPatternMultiplier = November2
ENDIF
ELSIF CurrentMonth = 12 THEN
IF currentDayOfTheMonth <= midOfMonth THEN
saisonalPatternMultiplier = December1
ELSE
saisonalPatternMultiplier = December2
ENDIF
ENDIF
// define trading filters
// 1. use fast and slow averages as filter because not every breakout is profitable
f1 = 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 criteria
alreadyReducedLongPosition = COUNTOFLONGSHARES < startPositionLong
alreadyReducedShortPosition = COUNTOFSHORTSHARES < startPositionShort
// long position conditions
l1 = signalline CROSSES OVER monthlyHigh
l2 = signalline CROSSES OVER weeklyHigh
l3 = signalline CROSSES OVER dailyHigh
l4 = signalline CROSSES OVER monthlyLow
// short position conditions
s1 = signalline CROSSES UNDER monthlyHigh
s2 = signalline CROSSES UNDER dailyLow
s3 = signalline CROSSES UNDER previousDailyHigh
// long entry with order cumulation
IF ( l1 OR l4 OR l2 OR (l3 AND f2) ) AND NOT alreadyReducedLongPosition THEN
// check saisonal booster setup and max position size
IF saisonalPatternMultiplier > 0 THEN
numberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize * saisonalPatternMultiplier) * scaleFactor)
IF (COUNTOFPOSITION + numberContracts) <= maxPositionSizeLong * scaleFactor THEN
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
BUY numberContracts CONTRACT AT MARKET
ENDIF
ELSIF saisonalPatternMultiplier <> 0 THEN
numberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize) * scaleFactor)
IF (COUNTOFPOSITION + numberContracts) <= maxPositionSizeLong * scaleFactor THEN
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
BUY numberContracts CONTRACT AT MARKET
ENDIF
ENDIF
stopLoss = stopLossLong
takeProfit = takeProfitLong
ENDIF
// short entry with order cumulation
IF ( (s1 AND f3) OR (s2 AND f1) OR (s3 AND f3) ) AND NOT alreadyReducedShortPosition THEN
// check saisonal booster setup and max position size
IF saisonalPatternMultiplier < 0 THEN
numberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize * ABS(saisonalPatternMultiplier)) * scaleFactor)
IF (ABS(COUNTOFPOSITION) + numberContracts) <= maxPositionSizeShort * scaleFactor THEN
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
SELLSHORT numberContracts CONTRACT AT MARKET
ENDIF
ELSIF saisonalPatternMultiplier <> 0 THEN
numberContracts = MAX(1, MIN(maxPositionSizePerTrade, positionSize) * scaleFactor)
IF (ABS(COUNTOFPOSITION) + numberContracts) <= maxPositionSizeShort * scaleFactor THEN
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
SELLSHORT numberContracts CONTRACT AT MARKET
ENDIF
ENDIF
stopLoss = stopLossShort
takeProfit = takeProfitShort
ENDIF
// stop and profit management
posProfit = (((close - positionprice) * pointvalue) * countofposition) / pipsize
numberCandles = (BarIndex - TradeIndex)
m1 = posProfit > 0 AND numberCandles >= maxCandlesLongWithProfit
m2 = posProfit > 0 AND numberCandles >= maxCandlesShortWithProfit
m3 = posProfit < 0 AND numberCandles >= maxCandlesLongWithoutProfit
m4 = posProfit < 0 AND numberCandles >= maxCandlesShortWithoutProfit
// take profit after max candles
IF LONGONMARKET AND (m1 OR m3) THEN
SELL AT MARKET
ENDIF
IF SHORTONMARKET AND (m2 OR m4) THEN
EXITSHORT AT MARKET
ENDIF
// trailing stop function (convert % to pips)
trailingStartLongInPoints = tradeprice(1) * trailingStartLong / 100
trailingStartShortInPoints = tradeprice(1) * trailingStartShort / 100
trailingStepLongInPoints = tradeprice(1) * trailingStepLong / 100
trailingStepShortInPoints = tradeprice(1) * trailingStepShort / 100
// reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
ENDIF
// manage long positions
IF LONGONMARKET THEN
// first move (breakeven)
IF newSL = 0 AND close - tradeprice(1) >= trailingStartLongInPoints * pipsize THEN
newSL = tradeprice(1) + trailingStepLongInPoints * pipsize
stopLoss = stopLossLong * 0.1
takeProfit = takeProfitLong * 2
ENDIF
// next moves
IF newSL > 0 AND close - newSL >= trailingStepLongInPoints * pipsize THEN
newSL = newSL + trailingStepLongInPoints * pipsize
ENDIF
ENDIF
// manage short positions
IF SHORTONMARKET THEN
// first move (breakeven)
IF newSL = 0 AND tradeprice(1) - close >= trailingStartShortInPoints * pipsize THEN
newSL = tradeprice(1) - trailingStepShortInPoints * pipsize
ENDIF
// next moves
IF newSL > 0 AND newSL - close >= trailingStepShortInPoints * pipsize THEN
newSL = newSL - trailingStepShortInPoints * pipsize
ENDIF
ENDIF
// stop order to exit the positions
IF newSL > 0 THEN
IF LONGONMARKET THEN
SELL AT newSL STOP
ENDIF
IF SHORTONMARKET THEN
EXITSHORT AT newSL STOP
ENDIF
ENDIF
// superordinate stop and take profit
SET STOP %LOSS stopLoss
SET TARGET %PROFIT takeProfit
ENDIF
[attachment file=”diff ts.JPG”]
timeframe (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?
timeframe(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.
You must putyes, and the code of the 4hrs should work normally like in the 4hrs right? and if it doesn’t? should be a bug right?and then run the strategy on a 30 minute chart.
12345 timeframe(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)
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?or it could be that you are not using the correct setting DEFAULT or UPDATEONCLOSE.
timeframe(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.
[beta-testing] multi timeframe support for automatic trading, ideas are welcome!
This topic contains 287 replies,
has 47 voices, and was last updated by Brianoshea
5 years, 10 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 03/19/2018 |
| Status: | Active |
| Attachments: | 48 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.