Sounds wrong coding, anyone can help pls

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #244086 quote
    efahmy
    Participant
    Average
    // Definition of code parameters
    DEFPARAM CumulateOrders = TRUE // Cumulating positions deactivated
    // Var indicator
    ATX = CALL "PRC_Adaptive-ATR-ADX-Trend-V2"[21, 3.5, 1.75, 14, 30, 0, 1](close)
    DEM = DEMA[14](close)
    
    // Logic
    if DEM CROSSES OVER ATX then
    BUY 5 CONTRACTS AT MARKET
    ENDIF
    
    #244093 quote
    Iván González
    Moderator
    Master

    I don’t see errors. The system buys when the DEMA (orange) crosses over ATX (blue).

    // Definition of code parameters
    DEFPARAM CumulateOrders = TRUE // Cumulating positions deactivated
    // Var indicator
    ATX = CALL "PRC_Adaptive-ATR-ADX-Trend-V2"[21, 3.5, 1.75, 14, 30, 0, 1](close)
    DEM = DEMA[14](close)
    
    // Logic
    if DEM CROSSES OVER ATX then
    BUY 5 CONTRACTS AT MARKET
    ENDIF
    
    graphonprice ATX coloured("blue")
    graphonprice DEM coloured("orange")
    #244095 quote
    efahmy
    Participant
    Average

    When I use the strategy this what I get, Pls see attached

    #244100 quote
    GraHal
    Participant
    Master

    Try putting / embedding the called Indicator in with the code of the rest of Strategy.

    Also, isn’t Line 35 (see below) of the ‘Adaptive Ind’ wrong / ‘suspect’ as STR is not defined anywhere, but STR is used in the rest of the  ‘Adaptive Ind’?   This might be causing ProOrder to Reject the overall code?

    sTR = (sTR[1] – sTR[1]) / adxLen + tr

    #244101 quote
    GraHal
    Participant
    Master

    Here is the Indicator if anybody has any ideas about fixing Line 35??

    //PRC_Adaptive-ATR-ADX-Trend-V2 | indicator
    //29.06.2017
    //Nicolas @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    //translated from tradingview code
     
    // --- settings 
    atrLen = 21
    m1 = 3.5 //"ATR Multiplier - ADX Rising"
    m2 = 1.75 //"ATR Multiplier - ADX Falling"
    adxLen = 14
    adxThresh = 30 //"ADX Threshold"
    aboveThresh = 1 //true, title = "ADX Above Threshold uses ATR Falling Multiplier Even if Rising?")
    useHeiken = 1 //(false, title = "Use Heiken-Ashi Bars (Source will be ohlc4)")
    // --- end of settings 
     
    source = MedianPrice
        
    // DI-Pos, DI-Neg, ADX
     
    hR = high-high[1]
    lR = -(low-low[1])
     
    if hr>lr then
    dmPos=max(hr,0)
    else
    dmPos=0
    endif
    if lr>hr then
    dmNeg=max(lr,0)
    else
    dmNeg=0
    endif
     
    sTR = (sTR[1] - sTR[1]) / adxLen + tr
     
    sDMPos = (sDMPos[1] - sDMPos[1]) / adxLen + dmPos
    sDMNeg = (sDMNeg[1] - sDMNeg[1]) / adxLen + dmNeg
     
    DIP = sDMPos / sTR * 100
    DIN = sDMNeg / sTR * 100
    DX = abs(DIP - DIN) / (DIP + DIN) * 100
    aadx = average[adxLen](DX)
     
    // Heiken-Ashi
     
    if barindex<2 then
    xClose = close
    xOpen = open
    else 
    xClose = TotalPrice
    xOpen = (xOpen[1] + close[1]) / 2
    endif
    xHigh = max(high, max(xOpen, xClose))
    xLow = min(low, min(xOpen, xClose))
     
    // Trailing ATR
     
    v1 = abs(xHigh - xClose[1])
    v2 = abs(xLow - xClose[1])
    v3 = xHigh - xLow
     
    trueRange = max(v1, max(v2, v3))
    if useHeiken then
    atr = WilderAverage[atrLen](trueRange)
    else
    atr = AverageTrueRange[atrLen]
    endif
     
    if aadx>aadx[1] and (adx < adxThresh or not aboveThresh) then
    m=m1
    elsif aadx<aadx[1] or (adx > adxThresh and aboveThresh) then
    m=m2
    else
    m = m[1]
    endif
     
    if DIP >= DIN then
    mUp=m
    else
    mUp=m2
    endif
    if DIN >= DIP then
    mDn=m
    else
    mDn=m2
    endif
     
    if useHeiken then
    src=xClose
    c=Xclose
    t=(xHigh+xLow)/2
    else
    src=source
    c=close
    t=MedianPrice
    endif
     
    up = t - mUp * atr
    dn = t + mDn * atr
     
    if max(src[1], c[1]) > TUp[1] then
    TUp = max(up,TUp[1])
    else
    TUp = up
    endif
     
    if min(src[1], c[1]) < TDown[1] then
    TDown = min(dn, TDown[1])
    else
    TDown = dn
    endif
     
    //trend
    if min(src,min(c,close))>TDown[1] then
    trend=1
    elsif max(src,max(c,close))<TUp[1] then
    trend=-1
    else
    trend=trend[1]
    endif
     
    if trend=1 then
    sstop=TUp
    r=0
    g=255
    else
    sstop=TDown
    r=255
    g=0
    endif
     
    if trend<>trend[1] then 
    drawtext("•",barindex,sstop,Dialog,Standard,30) coloured(r,g,0)
    endif
     
    return sstop coloured(r,g,0) style(line,2)
    #244109 quote
    JS
    Participant
    Senior

    Nicolas, Can you explain the numerator logic – e.g. Line 35 is (sTR[1] – sTR[1]) which would appear to be either null or zero.

    • avatar
      Nicolas • 06/29/2018 #You are right, this is useless. Since I converted it from another programming language, I assume it was in the original code…

    #244114 quote
    GraHal
    Participant
    Master

    Thanks JS, but please could not you (anybody) rejig the code so that the subsequent use of sTR in the Strategy is removed / replaced with something meaningful?
    Examples are Line 40 & 41 –

    DIP = sDMPos / sTR * 100
    DIN = sDMNeg / sTR * 100

    I recall ages ago working up a decent backtest performance with the ‘Adaptive Indicator’, but the Algo got Rejected every time it ran due to ‘not enough historical data’ (yes, I did use Defparam PreLoadbars = 10000.

    The error messages we see are not always ‘true to the words’ and I am thinking that this useless bit of code (STR) was causing my Rejection and also the problems now experienced by the OP / efahmy

    #244115 quote
    JC_Bywan
    Moderator
    Master

    Assuming that’s the source code:

    https://www.tradingview.com/script/8TQl2WyE-Adaptive-ATR-ADX-Trend-V2-Strategy/

    Then for line 35 mentionned by GraHal, I would keep it and go instead with brackets modified like this (equivalent to “no brackets”, but just for the sake of better visibility…):

    sTR = sTR[1] - (sTR[1]) / adxLen + tr

     

    Same thing by the way for lines 37 and 38, which could be modified as follows:
    sDMPos = sDMPos[1] - (sDMPos[1]) / adxLen + dmPos
    sDMNeg = sDMNeg[1] - (sDMNeg[1]) / adxLen + dmNeg

     

    To be tested to see if it helps…
    GraHal thanked this post
    #244127 quote
    JS
    Participant
    Senior

    I think the code should be adjusted like this:

    //PRC_Adaptive-ATR-ADX-Trend-v3
    //------------
    // Parameters
    //------------
    atrLen = 21
    m1 = 3.5  // ATR Multiplier - ADX Rising
    m2 = 1.75 // ATR Multiplier - ADX Falling
    adxLen = 14
    adxThresh = 30 // ADX Threshold
    aboveThresh = 1 // Use ATR Falling Multiplier even if ADX is rising
    useHeiken = 1   // Use Heiken-Ashi bars
    
    //------------
    // Source Data
    //------------
    source = MedianPrice
    
    //------------
    // ADX Calculation
    //------------
    hR = High - High[1]
    lR = -(Low - Low[1])
    
    IF hR > lR THEN
    dmPos = MAX(hR, 0)
    ELSE
    dmPos = 0
    ENDIF
    
    IF lR > hR THEN
    dmNeg = MAX(lR, 0)
    ELSE
    dmNeg = 0
    ENDIF
    
    sTR = (sTR[1] * (adxLen - 1) + TrueRange) / adxLen
    sDMPos = (sDMPos[1] * (adxLen - 1) + dmPos) / adxLen
    sDMNeg = (sDMNeg[1] * (adxLen - 1) + dmNeg) / adxLen
    
    DIP = sDMPos / sTR * 100
    DIN = sDMNeg / sTR * 100
    DX = ABS(DIP - DIN) / (DIP + DIN) * 100
    aadx = Average[adxLen](DX)
    
    //------------
    // Heiken-Ashi Calculation
    //------------
    IF BarIndex < 2 THEN
    xClose = Close
    xOpen = Open
    ELSE
    xClose = TotalPrice
    xOpen = (xOpen[1] + Close[1]) / 2
    ENDIF
    
    xHigh = MAX(High, MAX(xOpen, xClose))
    xLow = MIN(Low, MIN(xOpen, xClose))
    
    //------------
    // Trailing ATR Calculation
    //------------
    v1 = ABS(xHigh - Close[1])
    v2 = ABS(xLow - Close[1])
    v3 = xHigh - xLow
    
    trueRange = MAX(v1, MAX(v2, v3))
    
    IF useHeiken THEN
    atr = WilderAverage[atrLen](trueRange)
    ELSE
    atr = AverageTrueRange[atrLen]
    ENDIF
    
    IF aadx > aadx[1] AND (aadx < adxThresh OR NOT aboveThresh) THEN
    m = m1
    ELSIF aadx < aadx[1] OR (aadx > adxThresh AND aboveThresh) THEN
    m = m2
    ELSE
    m = m[1]
    ENDIF
    
    IF DIP >= DIN THEN
    mUp = m
    ELSE
    mUp = m2
    ENDIF
    
    IF DIN >= DIP THEN
    mDn = m
    ELSE
    mDn = m2
    ENDIF
    
    //------------
    // Trend Calculation
    //------------
    IF useHeiken THEN
    src = xClose
    c = xClose
    t = (xHigh + xLow) / 2
    ELSE
    src = source
    c = Close
    t = MedianPrice
    ENDIF
    
    up = t - mUp * atr
    dn = t + mDn * atr
    
    IF MAX(src[1], c[1]) > TUp[1] THEN
    TUp = MAX(up, TUp[1])
    ELSE
    TUp = up
    ENDIF
    
    IF MIN(src[1], c[1]) < TDown[1] THEN
    TDown = MIN(dn, TDown[1])
    ELSE
    TDown = dn
    ENDIF
    
    //------------
    // Trend Detection
    //------------
    IF MIN(src, MIN(c, Close)) > TDown[1] THEN
    trend = 1
    ELSIF MAX(src, MAX(c, Close)) < TUp[1] THEN
    trend = -1
    ELSE
    trend = trend[1]
    ENDIF
    
    IF trend = 1 THEN
    sstop = TUp
    r = 0
    g = 255
    ELSE
    sstop = TDown
    r = 255
    g = 0
    ENDIF
    
    IF trend <> trend[1] THEN
    DRAWTEXT("•", BarIndex, sstop, Dialog, Standard, 30) COLOURED(r, g, 0)
    ENDIF
    
    RETURN sstop COLOURED(r, g, 0) STYLE(line,2)
    GraHal thanked this post
    #244180 quote
    efahmy
    Participant
    Average
    Thank you all for the coding of the indicator, it is working properly, now anyone could help in coding the strategy of crossing between DEMA and ATX Pls.
    #244182 quote
    druby
    Participant
    New
    Regarding the original error message,  and just for future reference, I think this is what’s happening. In a INDICATOR, if you just return a variable like, a[1], via the return line, you get the ‘Variable is Undefined’ error message. Alternatively, if in the main code you have a=a[1],  and Return a, the code will run, but the line for ‘a’ doesn’t display in the chart. I believe this  is because, on the first bar, Barindex = 0, the value of a[1] is undefined still, so there’s  no value to display from the bar before bar zero. This means that the equation evaluates to undefined, and maybe on the following bars the prior value is always undefined because of the prior attempted calculation.   In back-test, the a=a[1] does the same, the code runs, however, if you graph ‘a’, again no line is drawn for the variable, because it’s undefined, similar to above. One way  to check if variables are defined in the back-test code is to Graph them and look if their line displays/appears.   It’s worth noting that, if you don’t assign a value/variable to a variable, then you get the red ‘~’ line in the editor and the ‘Unknown Command’ error message if you try to ‘ADD’. But you don’t get it is you assign a variable with a undefined variable, like here to the bar before zero.   Now in ProOrder, apart from only a couple of options, there’s no user/ext interaction with the strategy, any error will ‘Stop’ the strategy. However, since the code is only run once a bar, it’s not until the end of the bar the strategy was started on, the Strategy is ‘Stopped’. On small timeframes, that would be quite quick, but on long timeframe it will be slow, both could get impression that they worked for a while, before they don’t. In fact the the strategy is not tried till the start next new bar.   In this case, the above called indicator must have return an undefined value, which triggered the strategy to STOP. So the ‘ server.strategy.probacktest.error.missing_data.call’ must mean, an undefined value was returned from the call’ed file.   If this undefined variable scenario happens with out a call file, you get the error message, ‘The Trading system was stopped because the historical data loaded was insufficient to calculate at least one indicator during the evaluation of the last candlestick.’   Leading you to a problem with PRELOADBARS. If you cannot retrieve a value from before bar zero, no amount of PLB’s will cure problem.   The simple explanation is just a undefined variable, trying to retrieve a value from the previous bar to bar zero.   In the original TV code, the variables were parameters of the called ‘nv()’ function. This function defaulted the variable to zero, I believe,  if  it was undefined or NAN, this zero value allowed the equation to derive a proper result value.   In proOrder, you get a ‘Stopped’ system, and the usual cryptic message, unless your lucky. Regards all
    GraHal thanked this post
    #244278 quote
    GraHal
    Participant
    Master
    So after druby superlative analysis of the problem … if you change Lines 35 to 38 (in JS code above) to below … Rejection for ‘historical data loaded was insufficient‘ does not occur! Now I understand why ‘If barindex > 2’ is a fix! 🙂  I have used it before to stop Rejections in other strategies, but on an intuitive basis, now I have the full understanding as to why it is a fix! Big Thank You to … js for the code amendment. druby for the analysis of [1] undefined / Strategy Rejection.
    If barindex > 2 Then
    sTR = (sTR[1] * (adxLen - 1) + TrueRange) / adxLen
    sDMPos = (sDMPos[1] * (adxLen - 1) + dmPos) / adxLen
    sDMNeg = (sDMNeg[1] * (adxLen - 1) + dmNeg) / adxLen
    Endif
    druby and JS thanked this post
    #244279 quote
    LucasBest
    Participant
    Junior
    Try putting / embedding the called Indicator in with the code of the rest of Strategy. Also, isn’t Line 35 (see below) of the ‘Adaptive Ind’ wrong / ‘suspect’ as STR is not defined anywhere, but STR is used in the rest of the ‘Adaptive Ind’? This might be causing ProOrder to Reject the overall code? sTR = (sTR[1] – sTR[1]) / adxLen + tr
    First sTR need to be defined first or in a different way than by its previous value… (sTR[1] – sTR[1]) = 0 🙂 Mean sTR = 0 anytime… Then sTR is used to divide which means division by 0…
    GraHal thanked this post
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

Sounds wrong coding, anyone can help pls


ProOrder support

New Reply
Author
author-avatar
efahmy @efahmy Participant
Summary

This topic contains 12 replies,
has 7 voices, and was last updated by LucasBest
11 months, 1 week ago.

Topic Details
Forum: ProOrder support
Language: English
Started: 02/20/2025
Status: Active
Attachments: 3 files
Logo Logo
Loading...