[beta-testing] multi timeframe support for automatic trading, ideas are welcome!

Viewing 15 posts - 151 through 165 (of 288 total)
  • Author
    Posts
  • #77788 quote
    Nicolas
    Keymaster
    Master
    #77878 quote
    Marc
    Participant
    Average

    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

    #77987 quote
    Gianluca
    Participant
    Master
    Offline
    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.
    #77989 quote
    robertogozzi
    Moderator
    Master
    Yes, 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.
    #78355 quote
    robertogozzi
    Moderator
    Master
    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:
    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]
    .
    .
    #78548 quote
    Vonasi
    Moderator
    Master
    It would be nice if we could optimize the time frame. For example:
    //x = 52 //(variable to be optimized)
    
    timeframe (x weeks, default)
    a = weightedclose[1]
    
    timeframe (weekly, default)
    (strategy here with filter using 'a')
    #78717 quote
    Gianluca
    Participant
    Master
    Ok, 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
    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?
    #78719 quote
    Vonasi
    Moderator
    Master
    can i post the code?
    That would be helpful – every problem can be solved with the maximum amount of information! 🙂
    #78720 quote
    Gianluca
    Participant
    Master
    Ok, 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: and that referencing past bars would still be allowed:
    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:
    // 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”]
    #78732 quote
    robertogozzi
    Moderator
    Master
    Gianluca, 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?
    #78747 quote
    Nicolas
    Keymaster
    Master
    with: 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.
    #78845 quote
    Gianluca
    Participant
    Master
    In 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
    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?
    #78855 quote
    Vonasi
    Moderator
    Master
    You must put
    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.
    #78889 quote
    Gianluca
    Participant
    Master
    You must put 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?
    #78912 quote
    Vonasi
    Moderator
    Master
    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.
Viewing 15 posts - 151 through 165 (of 288 total)
  • You must be logged in to reply to this topic.

[beta-testing] multi timeframe support for automatic trading, ideas are welcome!


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Nicolas @nicolas Keymaster
Summary

This topic contains 287 replies,
has 47 voices, and was last updated by Brianoshea
5 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/19/2018
Status: Active
Attachments: 48 files
Logo Logo
Loading...