Cancelling pending orders

Viewing 4 posts - 16 through 19 (of 19 total)
  • Author
    Posts
  • #148736 quote
    kaq52
    Participant
    Average

    Replace line 4 with

    Once C1=1

    After line 264, add line

    C1=0

    What this will do is that for the first candle, C1 being true will place a long order. However, for second candle, C1 being false will not place order but first order should be cancelled automatically. This will explain how the candle loops work.

    You are facing this confusion because you have hardcoded the condition to be ALWAYS TRUE regardless of what happens. Your strategy need to update your conditions to be “true” or “false” based on some criteria (whatever criteria you chose) and it will work like a breeze.

     

    Rgds, kaq

    Khaled and Nicolas thanked this post
    #148738 quote
    Khaled
    Participant
    Veteran

    Thank you very much Kaq. Will try…

    #148803 quote
    Khaled
    Participant
    Veteran

    @kaq52 @juanj @Nicolas @martentornquist @robertogozzi @Bard

    Dear All, thank you very much for your support. The idea suggested by kaq52 is very much what I was looking for. For what it’s worth, I’m sharing here my code and the result of the backtest. Please bear with me if you find erros or room for improvement.

    I’m not proficient in PRT, but I tired to assemble a few pieces here and there found on this forum. I used ML based on code published by Bard here https://www.prorealcode.com/reply/128486/

    Variables have obviously been over-optimized for DAX 15 minutes 1€ with IG CFDs. WF provides a relatively high result… Not sure how this will work in live trading.

    I’d apprecite if some of you can test it (live if possible) and share their feedback. I’ve doubts about:

    1/ the price distance between certain orders calculated by the system vs what IG accepts.

    2/ the interaction between Stop Loss calculated by this system, Spread and actual execution price… I’ve seen that for orders with nearly breakeven results in backtest I ended losing 1 or 2 euros in actual trading.

    3/ overnight holding cost.

    4 /not sure how these impressive Backtest results will translate in reality over a few weeks

    4/ Last but not least… I tried ML with one and with two variables on a very simple SMA cross Algo and I’ve observed on different TF that ML with only one variable provides better results than ML with two variables… I’m very interested in your thoughts on this.

    Next steps: add conditions/optimize entry points to reduce false signals, try wider SL (?),

    Again, thank you and look forward to hear your feedback.

     

    // DAX M15 Test with ML
    DEFPARAM CUMULATEORDERS = FALSE
    DEFPARAM PRELOADBARS    = 10000
    
    N = 1
    
    ONCE C1 = 0
    ONCE C2 = 0
    
    iMACD = MACD[12,26,9](close)
    iRSI = RSI[14](close)
    StochK = Stochastic[14,3](close)
    StochD = Average[5](Stochastic[14,3](close))
    indicator7, indicator6 = CALL KST1 //Standard KST Indicator
    
    c1L = (iMACD > 0) AND (iMACD > iMACD[1])
    c2L = (iRSI > iRSI[1])
    c3L = (StochK > StochD)
    c4L = (StochD > StochD[1])
    c5L = (indicator6 < indicator7)
    
    c1S = (iMACD < 0) AND (iMACD < iMACD[1])
    c2S = (iRSI < iRSI[1])
    c3S = (StochK < StochD)
    c4S = (StochD < StochD[1])
    c5S = (indicator6 > indicator7)
    
    IF c1L AND c2L AND c3L AND c4L AND c5L THEN
    C1 = 1
    ENDIF
    
    IF c1S AND c2S AND c3S AND c4S AND c5S THEN
    C2 = 1
    ENDIF
    
    // ALOG AND ML
    heuristicscyclelimit = 7
    
    once heuristicscycle = 0
    once heuristicsalgo1 = 1
    once heuristicsalgo2 = 0
    
    if heuristicscycle >= heuristicscyclelimit then
    if heuristicsalgo1 = 1 then
    heuristicsalgo2 = 1
    heuristicsalgo1 = 0
    elsif heuristicsalgo2 = 1 then
    heuristicsalgo1 = 1
    heuristicsalgo2 = 0
    endif
    heuristicscycle = 0
    else
    once valuex = startingvalue
    once valuey = startingvalue2
    endif
    
    if heuristicsalgo1 = 1 then
    
    //heuristics algorithm 1 start
     
    if (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) then
    optimise = optimise + 1
    endif
    
    //Settings 1 & 2
    
    startingvalue   = 5 //5 //10 //40 //5, 100, 10 //LONG BOXSIZE
    ResetPeriod     = 1 //3 //1, 0.5 Specify no of months after which to reset optimisation
    increment       = 5 //10 //5 //10 //10 //5, 20, 10
    maxincrement    = 5 //20 //5, 10 limit of no of increments either up or down
    reps            = 6 //3  //1 number of trades to use for analysis //2
    maxvalue        = 5 //5 //10 //70 //50 //50, 20, 300, 150 //maximum allowed value
    minvalue        = increment //15, 5, minimum allowed value
    
    startingvalue2  = 5 //5 //10 //50 //15 //40 //5, 100, 50 //SHORT BOXSIZE
    ResetPeriod2    = 3 //1, 0.5 Specify no of months after which to reset optimisation
    increment2      = 5 //10 //10 //5, 10
    maxincrement2   = 5 //20 //1, 30 limit of no of increments either up/down //4
    reps2           = 2 //3  //1, 2 nos of trades to use for analysis //3
    maxvalue2       = 5 //5 //50, 20, 300, 200 maximum allowed value
    minvalue2       = increment2 //15, 5, minimum allowed value
    
    once monthinit = month
    once yearinit = year
    If (year = yearinit and month = (monthinit + ResetPeriod)) or (year = (yearinit + 1) and ((12 - monthinit) + month = ResetPeriod)) Then
    ValueX = StartingValue
    WinCountB = 0
    StratAvgB = 0
    BestA = 0
    BestB = 0
    monthinit = month
    yearinit = year
    EndIf
    
    once valuex  = startingvalue
    once pincpos = 1 //positive increment position
    once nincpos = 1 //negative increment position
    once optimise = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
    once mode1 = 1 //switches between negative and positive increments
    //once wincountb = 3 //initialize best win count
    //graph wincountb coloured (0,0,0) as "wincountb"
    //once stratavgb = 4353 //initialize best avg strategy profit
    //graph stratavgb coloured (0,0,0) as "stratavgb"
     
    if optimise = reps then
    wincounta = 0 //initialize current win count
    stratavga = 0 //initialize current avg strategy profit
    heuristicscycle = heuristicscycle + 1
     
    for i = 1 to reps do
    if positionperf(i) > 0 then
    wincounta = wincounta + 1 //increment current wincount
    endif
    stratavga = stratavga + (((positionperf(i)*countofposition[i]*close)*-1)*-1)
    next
    stratavga = stratavga/reps //calculate current avg strategy profit
    //graph (positionperf(1)*countofposition[1]*100000)*-1 as "posperf1"
    //graph (positionperf(2)*countofposition[2]*100000)*-1 as "posperf2"
    //graph stratavga*-1 as "stratavga"
    //once besta = 300
    //graph besta coloured (0,0,0) as "besta"
    if stratavga >= stratavgb then
    stratavgb = stratavga //update best strategy profit
    besta = valuex
    endif
    //once bestb = 300
    //graph bestb coloured (0,0,0) as "bestb"
    if wincounta >= wincountb then
    wincountb = wincounta //update best win count
    bestb = valuex
    endif
     
    if wincounta > wincountb and stratavga > stratavgb then
    mode1 = 0
    elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 1 then
    valuex = valuex - (increment*nincpos)
    nincpos = nincpos + 1
    mode1 = 2
    elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 1 then
    valuex = valuex + (increment*pincpos)
    pincpos = pincpos + 1
    mode1 = 1
    elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 2 then
    valuex = valuex + (increment*pincpos)
    pincpos = pincpos + 1
    mode1 = 1
    elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 2 then
    valuex = valuex - (increment*nincpos)
    nincpos = nincpos + 1
    mode1 = 2
    endif
     
    if nincpos > maxincrement or pincpos > maxincrement then
    if besta = bestb then
    valuex = besta
    else
    if reps >= 10 then
    weightedscore = 10
    else
    weightedscore = round((reps/100)*100)
    endif
    valuex = round(((besta*(20-weightedscore)) + (bestb*weightedscore))/20) //lower reps = less weight assigned to win%
    endif
    nincpos = 1
    pincpos = 1
    elsif valuex > maxvalue then
    valuex = maxvalue
    elsif valuex < minvalue then
    valuex = minvalue
    endif
     
    optimise = 0
    endif
     
    // heuristics algorithm 1 end
    
    elsif heuristicsalgo2 = 1 then
    
    // heuristics algorithm 2 start
     
    if (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) then
    optimise2 = optimise2 + 1
    endif
    
    //Settings 2
     
    once monthinit2 = month
    once yearinit2 = year
    If (year = yearinit2 and month = (monthinit2 + ResetPeriod2)) or (year = (yearinit2 + 1) and ((12 - monthinit2) + month = ResetPeriod2)) Then
    ValueY = StartingValue2
    WinCountB2 = 0
    StratAvgB2 = 0
    BestA2 = 0
    BestB2 = 0
    monthinit2 = month
    yearinit2 = year
    EndIf
     
    once valuey = startingvalue2
    once pincpos2 = 1 //positive increment position
    once nincpos2 = 1 //negative increment position
    once optimise2 = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
    once mode2 = 1 //switches between negative and positive increments
     
    if optimise2 = reps2 then
    wincounta2 = 0 //initialize current win count
    stratavga2 = 0 //initialize current avg strategy profit
    heuristicscycle = heuristicscycle + 1
     
    for i2 = 1 to reps2 do
    if positionperf(i2) > 0 then
    wincounta2 = wincounta2 + 1 //increment current wincount
    endif
    stratavga2 = stratavga2 + (((positionperf(i2)*countofposition[i2]*close)*-1)*-1)
    next
    stratavga2 = stratavga2/reps2 //calculate current avg strategy profit
    
    if stratavga2 >= stratavgb2 then
    stratavgb2 = stratavga2 //update best strategy profit
    besta2 = valuey
    endif
    
    if wincounta2 >= wincountb2 then
    wincountb2 = wincounta2 //update best win count
    bestb2 = valuey
    endif
     
    if wincounta2 > wincountb2 and stratavga2 > stratavgb2 then
    mode2 = 0
    elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 1 then
    valuey = valuey - (increment2*nincpos2)
    nincpos2 = nincpos2 + 1
    mode2 = 2
    elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 1 then
    valuey = valuey + (increment2*pincpos2)
    pincpos2 = pincpos2 + 1
    mode2 = 1
    elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 2 then
    valuey = valuey + (increment2*pincpos2)
    pincpos2 = pincpos2 + 1
    mode2 = 1
    elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 2 then
    valuey = valuey - (increment2*nincpos2)
    nincpos2 = nincpos2 + 1
    mode2 = 2
    endif
     
    if nincpos2 > maxincrement2 or pincpos2 > maxincrement2 then
    if besta2 = bestb2 then
    valuey = besta2
    else
    if reps2 >= 10 then
    weightedscore2 = 10
    else
    weightedscore2 = round((reps2/100)*100)
    endif
    valuey = round(((besta2*(20-weightedscore2)) + (bestb2*weightedscore2))/20) //lower reps = less weight assigned to win%
    endif
    nincpos2 = 1
    pincpos2 = 1
    elsif valuey > maxvalue2 then
    valuey = maxvalue2
    elsif valuey < minvalue2 then
    valuey = minvalue2
    endif
     
    optimise2 = 0
    endif
    // heuristics algorithm 2 end
    endif
    
    //
    boxsizeL = max ( ValueX , (maxvalue+minvalue)/2)
    boxsizeS = max ( ValueY , (maxvalue2+minvalue2)/2)
    //
    renkomaxl = round(close / boxsizel) * boxsizel
    renkominl = renkomaxl - boxsizel
    
    renkomaxs = round(close / boxsizes) * boxsizes
    renkomins = renkomaxs - boxsizes
    //
    if high > renkomaxl + boxsizel then
    renkomaxl = renkomaxl + boxsizel
    renkominl = renkominl + boxsizel
    endif
    if low < renkomins - boxsizes then
    renkomaxs = renkomaxs - boxsizes
    renkomins = renkomins - boxsizes
    endif
    
    IF time>=001500 AND time>060000 then
    spread = 2
    ELSIF (time>=060000 AND time>070000) AND (time>=153000 AND time>200000)then
    spread = 1
    ELSIF time>=070000 AND time>153000 then
    spread = 0.6
    ELSIF time>=200000 AND time>001500 then
    spread = 2.5
    ENDIF
    
    IF C1=1 THEN
    BUY N CONTRACT at (renkoMaxL + boxSizeL + spread) STOP
    ENDIF
    
    IF C2=1 THEN
    SELLSHORT N CONTRACT at (renkoMinS - boxSizeS - spread) STOP
    ENDIF
    
    C1 = 0
    C2 = 0
    
    SET STOP pTRAILING 2
    SET TARGET pPROFIT 200
    2020-10-28-17h51.png 2020-10-28-17h51.png 2020-10-28-17h52.png 2020-10-28-17h52.png
    #210671 quote
    GraHal
    Participant
    Master

    I’d love to try it, but where is KST1 Indicator?

    KST1 is not the standard Indicator as Line 14 (copied below) needs 2 values and the standard KST returns only 1 value.

    indicator7, indicator6 = CALL KST1 //Standard KST Indicator

Viewing 4 posts - 16 through 19 (of 19 total)
  • You must be logged in to reply to this topic.

Cancelling pending orders


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Khaled @khaled Participant
Summary

This topic contains 18 replies,
has 4 voices, and was last updated by GraHal
2 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/26/2020
Status: Active
Attachments: 4 files
Logo Logo
Loading...