Grid orders with one combined stop loss and limit, can it be done?

Viewing 15 posts - 256 through 270 (of 308 total)
  • Author
    Posts
  • #16631 quote
    cfta
    Participant
    Senior

    Thanks for the support guys, losing trades are an inevitable part of doing business so let’s just make sure we learn from them 🙂

    Good idea Cosmic, the code you posted is practically the same as older versions before Mike introduced the entry parameters and it is often better to use when market conditions are unclear, I use it myself sometimes too.

    It has proven quite difficult to automate entries which doesn’t require certain market conditions, I’m currently testing reduced periods and eventually removing a a condition or two in order to get entry conditions which are less sensitive and start the system but still reduce the aspect of waiting and watching to get a good entry.

    Kasper, I’m no expert on beartraps but that certainly looked like one with a resolute break of the monthly support. BTW have you made any further progress experimenting with the entry conditions?

    Yesterday I found a nice piece of code in the library uploaded by Doctrading;

    The “Nice Price” Forex Strategy”

    It has been bothering me that by default PRT does not show DI+ and DI- on the ADX and I havn’t figured out how to display it as an indicator or how to code it as a parameter but looks like Doc has the answer;

    Defparam Cumulateorders = false
    
    n = 5
    
    // ACHAT
    ca1 = ADX[50] > 10
    ca2 =  DIplus[50](close) > DIminus[50](close)
    ca3 = low < BollingerDown[20](close) and close > BollingerDown[20](close)
    
    IF ca1 AND ca2 AND ca3 THEN
     BUY n SHARES AT MARKET
    ENDIF
    
    // VAD
    cv1 = ADX[50] > 10
    cv2 =  DIplus[50](close) < DIminus[50](close)
    cv3 = high > BollingerUp[20](close) and close < BollingerUp[20](close)
    
    IF cv1 AND cv2 AND cv3 THEN
     SELLSHORT n SHARES AT MARKET
    ENDIF
    
    // Stops et objectifs
    SET STOP pLOSS 60
    SET TARGET pPROFIT 10<br><br>

     

    This way we make the ADX condition also take into account the direction of the price movement and not just it’s strenght, I will try to integrate it to the CFTA indicator but without the Bollinger band parameter.

    #16659 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Cfta. I must say that I kind of got stuck and didn’t develop it further. We agree on the many false breakout’s, and I think the majority if them is because trades goes against the main trend. I not sure, but anyway in all the textbook examples:”trend is your friend” and also “It pays to trade with the professionals”

    We need the multi timeframe support to get this to work. But anyway the gridorder with dynamic gridstep code is here, and It will actually work on any strategy. In my mind there could be some interesting ways to develop new entries on together with a change in the code:

    • entries on major support and resistance+ re-test/false breakouts/trap
    • change the code to include move of SL to break even/profit. perhaps again use the dynamic gridstep as an indicator for how close the SL need to be to insure max profit- but still allow some space.

    I don’t know how or if there is a way to use the build in support/resistance lines in the code, Anyone know of this?

    Also I been reading that the re-test + false breakouts are the way professionals trades and stops out the “non professionals” because everyone has their SL right below the support line – exactly as we saw yesterday.

    Cheers Kasper

    #16677 quote
    cfta
    Participant
    Senior

    Hey Kasper, indeed MTF support would make all the difference in the world 🙂 Entries on S/R sounds good and for a skilled coder like Nicolas it’s probably possible to code but I’m not sure if we can make it work in in real trading because too often the price don’t respect S/R. SL to BE at certain number of entries or profit should be fairly easy to code I might be able to do it myself even if it will take time since I’m still a beginner. Anyway don’t forget about the BB exit feature but make sure the RR setting is not too high, I think the BB exit is the better option most of the time since it can also save profits and not just exit on BE.

    I stripped a few things from the indicator to simplify the code and get more entries with less conditions, I think it looks pretty promising and might be good enough to make a screener out of it even though we should try a few different period values and setings since I coded it this afternoon. Hope you guys like it!

    //==================CFTA-v3.0
    //Heikin-Ash
    xClose = (Open+High+Low+Close)/4
    if(barindex>2) then
    xOpen = (xOpen[1] + xClose[1])/2
    endif
    greencandle = xClose>xOpen AND xClose[1]<xOpen[1]
    redcandle = xClose<xOpen AND xClose[1]>xOpen[1]
    
    //***************Long
    c1 = MACD > 0
    c2 = ADX[10] > 20
    c3 = CCI[10] > 0
    DIP = DIplus[10]
    DIM = DIminus[10]
    c4 = (DIP > DIM[1])
    If c1 and c2 and c3 and c4 and greencandle then
    Indicator1=1
    Else
    Indicator1=0
    Endif
    //*************Short
    c11 = MACD < 0
    c12 = ADX[10] > 20
    c13 = CCI[10] < 0
    DIP = DIplus[10]
    DIM = DIminus[10]
    c14 = (DIP < DIM[1])
    If c11 and c12 and c13 and c14 and redcandle then
    Indicator22=-1
    Else
    Indicator22=0
    Endif
    Return indicator1 COLOURED(0,255,0) as "Buy", indicator22 COLOURED(255,0,0) as "Sell"
    
    CFTA-v3.0.itf
    #16836 quote
    Elsborgtrading
    Participant
    Veteran

    Hi CFTA. I think I missed out of the BB exit? what is that? 🙂 I finally got the breakeven and one Trailingstop to work. I thought it was easy, but it took a few evenings and a weekend to complete and test. I made some re-arrangement of the code to include both manual LONG and SHORT and an auto feature with some switches in the code. Very convenient when testing. I did not find a decent auto code so I left it out- but made room for it. I have not optimized the Trailing stop in any way just assumed the following

    • RRreached=0: -1% exit
    • RRreached=1: Breakeven when reached 25% of RR
    • RRreached=2: Tralingstop moved to 45% of RR when  66% of RR reached
    • RRreached=3: RR reached

    I hope you like it. Cheers Kasper  

    NOTE! If you use the manual code it supposed to only make 1 trade cycle, however I just discovered that if it breaks even with exactly 0% it will run a 2nd round!

    // =============================================CFTA 2 sec LOW v3.11======================================================
    
    //=====================================GRID ORDER and Dynamic GridSTEP Money MGT=============================================
    
    
    //================parameters==============
    defparam preloadbars=2000
    //defparam flatbefore=090000
    //defparam flatafter=170000
    DEFPARAM CumulateOrders = true
    once RRreached = 0
    
    //=============manuel or autotrades=======
    ManuelLong=1
    Manuelshort=0
    Auto=0
    
    //===============Tradetime================
    IF (Time >= 090000 AND Time <= 170000)then
    Tradetime=1
    else
    Tradetime=0
    endif
    
    //==========Account and Gridstep Variables==========
    //
    //NOTE! Dynamic Gridstep if more correct on larger TF>10min
    accountbalance = 10000 //account balance in money at strategy start
    riskpercent = 1 //whole account risk in percent%
    RR = 2//RR- risk reward ratio (set to 0 disable this function)
    Breakevenfactor=25//When to breakeven in % of RR and Calc of RRreached1
    Trailingfactor1=66//in % of RR Used for calc of RRreached2
    Trailingfactor2=45//in % of RR Used for EXIT STOP ORDER @RRreached2
    minSTEP = 5 //minimal step of the grid
    maxSTEP = 20 //maximal step of the grid
    ATRcurrentPeriod = 5 //recent volatility 'instant' period
    ATRhistoPeriod = 100 //historical volatility period
    //========================================
    
    
    ATR = averagetruerange[ATRcurrentPeriod]
    histoATR= highest[ATRhistoPeriod](ATR)
    resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
    resultMIN = MIN(resultMAX,maxSTEP*pipsize)
    gridstep = (resultMIN)
    BEF=Breakevenfactor*RR/100//BreakevenFactor
    TF1=Trailingfactor1*RR/100
    TF2=Trailingfactor2*RR/100
    //==============Reinvestment or not=======
    reinvest=1
    if reinvest then
    if liveaccountbalance<>10000 then
    amount=1
    if liveaccountbalance >20000 then
    amount=2
    if liveaccountbalance >30000 then
    amount=3
    if liveaccountbalance >40000 then
    amount=4
    if liveaccountbalance >50000 then
    amount=5
    
    if liveaccountbalance >60000 then
    amount=6
    if liveaccountbalance >70000 then
    amount=7
    if liveaccountbalance >80000 then
    amount=8
    if liveaccountbalance >90000 then
    amount=9
    if liveaccountbalance >100000 then
    amount=10
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    endif
    else
    amount=1
    Endif
    
    
    //===============money risk management=============================
    liveaccountbalance = accountbalance+strategyprofit
    moneyrisk = (liveaccountbalance*(riskpercent/100))
    if onmarket then
    onepointvaluebasket = pointvalue*countofposition
    mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
    endif
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
    MAfloatingprofit = average[20](floatingprofit)
    BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*2
    
    //==========floating profit risk reward Trailing check======================
    if RR>0 and floatingprofit>moneyrisk*BEF then // Breakeven RRreached 1 at BreakevenFactor% of rr
    RRreached=1
    if RRreached[1]>RRreached then
    RRReached=RRreached[1]
    endif
    endif
    
    if RR>0 and floatingprofit>moneyrisk*TF1 then//RRreached 2 at Trailingfactor1% of rr
    RRreached=2
    if RRreached[1]>RRreached then
    RRReached=RRreached[1]
    endif
    endif
    
    if RR>0 and floatingprofit>moneyrisk*RR then//RR Reached
    RRreached=3
    if RRreached[1]>RRreached then
    RRReached=RRreached[1]
    endif
    endif
    
    //===============LONG CONDITIONS and INDICATOR SECTION======================
    longcondition=1
    
    //===========================================================================
    
    //=========================ENTER LONG TRADE==================================
    
    IF Not ONMARKET and auto and tradetime and longcondition then
    BUY amount CONTRACT AT MARKET
    ENDIF
    
    if NOT ONMARKET and ManuelLong AND close<close[1] AND STRATEGYPROFIT=0 then //for only one trade
    BUY amount CONTRACT AT MARKET
    ENDIF
    
    // =================case BUY - add Gridorders on LONG trend==============
    
    if longonmarket and close-tradeprice(1)>=gridstep*pipsize then
    BUY amount LOT AT MARKET
    Endif
    
    //===========================EXIT LONG POSITIONS=============================
    
    if onmarket and RRreached=0 then//-1%
    SELL AT positionprice-mindistancetoclose STOP
    
    endif
    
    if onmarket and RRreached=1 then//break even
    SELL AT positionprice STOP
    endif
    if onmarket and RRreached=2 then//Profit RR*0,66
    //if floatingprofit <=(positionprice+mindistancetoclose*Tralingfactor1*rr) then
    SELL AT positionprice+mindistancetoclose*TF2 STOP
    //exit2=positionprice+mindistancetoclose*Trailingfactor1*rr
    
    endif
    //endif
    if onmarket and RRreached=3 or RRreached=3 and floatingprofit <= BBfloatingprofit then//RR Reached
    //if onmarket and RRreached=3 or RRreached=3 and floatingprofit Crosses under BBfloatingprofit then//RR Reached
    SELL AT MARKET
    //endif
    endif
    if Time >= 170000 then
    SELL AT MARKET
    endif
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    
    
    //===============SHORT CONDITIONS and INDICATOR SECTION======================
    Shortcondition=1
    
    //===========================================================================
    
    //==========================ENTER SHORT TRADE================================
    IF Not ONMARKET and auto and tradetime and Shortcondition then
    SELLSHORT amount CONTRACT AT MARKET
    ENDIF
    
    if NOT ONMARKET and Manuelshort AND close>close[1] AND STRATEGYPROFIT=0 then //for only one trade
    SELLSHORT amount CONTRACT AT MARKET
    ENDIF
    
    //===============case SELL- add Gridorders on SHORT trend====================
    if shortonmarket and tradeprice(1)-close>=gridstep*pipsize then
    SELLSHORT amount LOT AT MARKET
    endif
    
    
    //==================================EXIT SHORT POSITIONS========================================
    
    if onmarket and RRreached=0 then
    EXITSHORT AT positionprice-mindistancetoclose STOP
    endif
    
    if onmarket and RRreached=1 then//break even
    EXITSHORT AT positionprice STOP
    endif
    if onmarket and RRreached=2 then//Profit RR*0,66
    //if floatingprofit <= positionprice+mindistancetoclose*Tralingfactor1*rr then
    EXITSHORT AT positionprice+mindistancetoclose*TF2 STOP
    //exit3=positionprice+mindistancetoclose*TF2
    endif
    //endif
    
    if onmarket and RRreached=3 or RRreached=3 and floatingprofit <= BBfloatingprofit then//RR Reached
    //if onmarket and RRreached=3 or RRreached=3 and floatingprofit crosses under BBfloatingprofit then//RR Reached
    EXITSHORT AT MARKET
    endif
    //endif
    
    if Time >= 170000 then
    EXITSHORT AT MARKET
    endif
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    //=======================================================================================
    
    //Graph gridstep COLOURED(0,0,0) AS "gridstep"
    //Graph floatingprofit  COLOURED(255,0,0) AS "floatingprofit"
    //Graph BBfloatingprofit  COLOURED(0,125,0) AS "BBfloatingprofit"
    //Graph rrreached  COLOURED(0,0,0) AS "RRreached"
    //Graph exit3  COLOURED(0,0,0) AS "ExitLong"
    //Graph positionprice  COLOURED(0,255,0) AS "ExitLong2"
    //Graph mindistancetoclose  COLOURED(255,255,0) AS "mindistancetoclose"
    

     

    CFTA-2-sec-DAX-LOW-ver-3.11.itf
    #16859 quote
    cfta
    Participant
    Senior
    Hi Kasper, that is incredbly creative, I look forward to trying the new features 🙂 The BB exit is a function which Nicolas coded, the platform paints a graph of the equity curve of a set of trades and adds a bollinger band to the curve, if the curve crosses below the lower bollinger band all trades are closed. The RR is used to determine when the BB exit becomes valid since it otherwise would cause lots of premature exits, if the risk is 1 % and we set the RR to 2 then the BB exit kicks in once we reach a 2 % floating profit. Regarding the indicator I figured out something wasn’t right since there were plenty of missed or late signals. It turned out that something wasn’t right with the Heiken Ashi section of the code, after I removed it the incdicator is more accurate, great if we can make the HA candles work too. Comparison below, the upper indicator is with HA code and the one below is without.
    //==================CFTA-v3.1
    //***************Long
    c1 = MACD > 0
    c2 = ADX[10] > 20
    c3 = CCI[10] > 0
    DIP = DIplus[10]
    DIM = DIminus[10]
    c4 = (DIP > DIM[1])
    If c1 and c2 and c3 and c4 then
    Indicator1=1
    Else
    Indicator1=0
    Endif
    //*************Short
    c11 = MACD < 0
    c12 = ADX[10] > 20
    c13 = CCI[10] < 0
    DIP = DIplus[10]
    DIM = DIminus[10]
    c14 = (DIP < DIM[1])
    If c11 and c12 and c13 and c14 then
    Indicator22=-1
    Else
    Indicator22=0
    Endif
    Return indicator1 COLOURED(0,255,0) as "Buy", indicator22 COLOURED(255,0,0) as "Sell"
    
    manel thanked this post
    CFTA-indi-comparison.png CFTA-indi-comparison.png
    #16916 quote
    cfta
    Participant
    Senior
    Hi fellows, I created a couple of screeners based on the CFTA indicator, the cross which is based on a cross over 0.5 or under -0.5 by the previous closed candle. the trend shows the instruments which the indicator give a signal for regarless of when it occurred.
    CFTA-Cross.itf CFTA-Trend.itf
    #17777 quote
    cfta
    Participant
    Senior
    Hello traders, it’s been a while since the last post so time to bump the thread, even if it has been quiet here I have been working on the developement and integrated the CFTA entry parameters to the code and added a corresponging exit. The code is intended to equal a five times higher timeframe so all values have been multiplied by 5 and the ADX threshold  has been reduced to 10 since the values decrease overall when applied to a larger number of periods. Primarily I run it on 1m TF but it may also be ran on m3 equaling m15, but remember the system acts on the most recently closed candle hence it’s risky so run it on higher TFs, we rather need to multiply the values by a larger number. It looks very promising on low timeframes with the new stop keeping losses small. Sorry Kasper but this has kept me busy and I havn’t tested your most recent developments. Note that the below verion has “AND STRATEGYPROFIT=0” on line 39 commented out so the cycle will repeat until stopped in order to re-enter after false entries which are stopped out shortly. Also note that the MA value for the BB exit has not been multiplied. Long;
    /// Definition of code parameters
    defparam preloadbars =10000
    DEFPARAM CumulateOrders = true // Cumulating positions deactivated
    MAC= MACD[60,130,45]
    CCC= CCI [100]
    AAA= ADX [10]
    DIP= DIplus[50]
    DIM= DIminus[50]
    
    once RRreached = 0
    accountbalance = 10000 //account balance in money at strategy start
    riskpercent = 1 //whole account risk in percent%
    amount = 1 //lot amount to open each trade
    rr = 2 //risk reward ratio (set to 0 disable this function)
    sd = 0.25 //standard deviation of MA floating profit
    
    
    //dynamic step grid
    minSTEP = 5 //minimal step of the grid
    maxSTEP = 30 //maximal step of the grid
    ATRcurrentPeriod = 150 //recent volatility 'instant' period
    ATRhistoPeriod = 3000 //historical volatility period
    
    ATR = averagetruerange[ATRcurrentPeriod]
    histoATR= highest[ATRhistoPeriod](ATR)
    
    resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
    resultMIN = MIN(resultMAX,maxSTEP*pipsize)
    
    gridstep = (resultMIN)
    
    // Conditions to enter long positions
    c1 = (MAC > 0)
    c2 = (CCC > 0)
    c3 = (AAA > 20)
    c4 = (DIP > DIM[1])
    
    //first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on lne 39
    if NOT ONMARKET AND HIGHEST[1](c1)=1 AND HIGHEST[1](c2)=1 AND HIGHEST[1](c3)=1 AND HIGHEST[1](c4)=1 then
    BUY amount LOT AT MARKET
    endif
    
    // case BUY - add orders on the same trend
    if longonmarket and close-tradeprice(1)>=gridstep then
    BUY amount LOT AT MARKET
    endif
    
    //money management
    liveaccountbalance = accountbalance+strategyprofit
    moneyrisk = (liveaccountbalance*(riskpercent/100))
    if onmarket then
    onepointvaluebasket = pointvalue*countofposition
    mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
    endif
    
    //floating profit
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
    
    //actual trade gains
    MAfloatingprofit = average[40](floatingprofit)
    BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
    
    //CFTA cross exit
    if LONGONMARKET AND c1=0 or c2=0 or c3=0 or c4=0 then
    SELL AT MARKET
    endif
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    SELL AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    SELL AT MARKET
    endif
    endif
    
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    
    Short;
    /// Definition of code parameters
    defparam preloadbars =10000
    DEFPARAM CumulateOrders = true // Cumulating positions deactivated
    MAC= MACD[60,130,45]
    CCC= CCI [100]
    AAA= ADX [10]
    DIP= DIplus[50]
    DIM= DIminus[50]
    
    once RRreached = 0
    accountbalance = 10000 //account balance in money at strategy start
    riskpercent = 2 //whole account risk in percent%
    amount = 1 //lot amount to open each trade
    rr = 2 //risk reward ratio (set to 0 disable this function)
    sd = 0.25 //standard deviation of MA floating profit
    
    
    //dynamic step grid
    minSTEP = 5 //minimal step of the grid
    maxSTEP = 30 //maximal step of the grid
    ATRcurrentPeriod = 150 //recent volatility 'instant' period
    ATRhistoPeriod = 3000 //historical volatility period
    
    ATR = averagetruerange[ATRcurrentPeriod]
    histoATR= highest[ATRhistoPeriod](ATR)
    
    resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
    resultMIN = MIN(resultMAX,maxSTEP*pipsize)
    
    gridstep = (resultMIN)
    
    // Conditions to enter short positions
    c11 = (MAC < 0)
    c12 = (CCC < 0)
    c13 = (AAA > 20)
    c14= (DIP < DIM[1])
    
    //first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 39
    if NOT ONMARKET AND HIGHEST[1](c11)=1 AND HIGHEST[1](c12)=1 AND HIGHEST[1](c13)=1 AND HIGHEST[1](c14)=1  then
    SELLSHORT amount LOT AT MARKET
    endif
    
    // case SELL - add orders on the same trend
    if shortonmarket and tradeprice(1)-close>=gridstep then
    SELLSHORT amount LOT AT MARKET
    endif
    
    //money management
    liveaccountbalance = accountbalance+strategyprofit
    moneyrisk = (liveaccountbalance*(riskpercent/100))
    if onmarket then
    onepointvaluebasket = pointvalue*countofposition
    mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
    endif
    
    //floating profit
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
    
    //actual trade gains
    MAfloatingprofit = average[40](floatingprofit)
    BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
    
    //CFTA exit
    if c11=0 or c12=0 or c13=0 or c14=0 then
    EXITSHORT AT MARKET
    endif
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    EXITSHORT AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    EXITSHORT AT MARKET
    endif
    endif
    
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    
      Long & Short
    /// Definition of code parameters
    defparam preloadbars =10000
    DEFPARAM CumulateOrders = true // Cumulating positions deactivated
    MAC= MACD[60,130,45]
    CCC= CCI [100]
    AAA= ADX [10]
    DIP= DIplus[50]
    DIM= DIminus[50]
    
    once RRreached = 0
    accountbalance = 10000 //account balance in money at strategy start
    riskpercent = 1 //whole account risk in percent%
    amount = 1 //lot amount to open each trade
    rr = 2 //risk reward ratio (set to 0 disable this function)
    sd = 0.25 //standard deviation of MA floating profit
    
    
    //dynamic step grid
    minSTEP = 5 //minimal step of the grid
    maxSTEP = 30 //maximal step of the grid
    ATRcurrentPeriod = 150 //recent volatility 'instant' period
    ATRhistoPeriod = 3000 //historical volatility period
    
    ATR = averagetruerange[ATRcurrentPeriod]
    histoATR= highest[ATRhistoPeriod](ATR)
    
    resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
    resultMIN = MIN(resultMAX,maxSTEP*pipsize)
    
    gridstep = (resultMIN)
    
    // Conditions to enter long positions
    c1 = (MAC > 0)
    c2 = (CCC > 0)
    c3 = (AAA > 20)
    c4 = (DIP > DIM[1])
    
    //first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on line 39
    if NOT ONMARKET AND HIGHEST[1](c1)=1 AND HIGHEST[1](c2)=1 AND HIGHEST[1](c3)=1 AND HIGHEST[1](c4)=1 then
    BUY amount LOT AT MARKET
    endif
    
    // case BUY - add orders on the same trend
    if longonmarket and close-tradeprice(1)>=gridstep then
    BUY amount LOT AT MARKET
    endif
    
    // Conditions to enter short positions
    c11 = (MAC < 0)
    c12 = (CCC < 0)
    c13 = (AAA > 20)
    c14= (DIP < DIM[1])
    
    //first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 55
    if NOT ONMARKET AND HIGHEST[1](c11)=1 AND HIGHEST[1](c12)=1 AND HIGHEST[1](c13)=1 AND HIGHEST[1](c14)=1  then
    SELLSHORT amount LOT AT MARKET
    endif
    
    // case SELL - add orders on the same trend
    if shortonmarket and tradeprice(1)-close>=gridstep then
    SELLSHORT amount LOT AT MARKET
    endif
    
    //money management
    liveaccountbalance = accountbalance+strategyprofit
    moneyrisk = (liveaccountbalance*(riskpercent/100))
    if onmarket then
    onepointvaluebasket = pointvalue*countofposition
    mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
    endif
    
    //floating profit
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
    
    //actual trade gains
    MAfloatingprofit = average[40](floatingprofit)
    BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
    
    //CFTA cross exit
    if LONGONMARKET AND c1=0 or c2=0 or c3=0 or c4=0 then
    SELL AT MARKET
    endif
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    SELL AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    SELL AT MARKET
    endif
    endif
    
    //CFTA exit
    if c11=0 or c12=0 or c13=0 or c14=0 then
    EXITSHORT AT MARKET
    endif
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    EXITSHORT AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    EXITSHORT AT MARKET
    endif
    endif
    
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    
    The latest indicator version without multiplier;
    /// Definition of code parameters
    defparam preloadbars =10000
    DEFPARAM CumulateOrders = true // Cumulating positions deactivated
    MAC= MACD[60,130,45]
    CCC= CCI [100]
    AAA= ADX [10]
    DIP= DIplus[50]
    DIM= DIminus[50]
    
    once RRreached = 0
    accountbalance = 10000 //account balance in money at strategy start
    riskpercent = 1 //whole account risk in percent%
    amount = 1 //lot amount to open each trade
    rr = 2 //risk reward ratio (set to 0 disable this function)
    sd = 0.25 //standard deviation of MA floating profit
    
    
    //dynamic step grid
    minSTEP = 10 //minimal step of the grid
    maxSTEP = 50 //maximal step of the grid
    ATRcurrentPeriod = 150 //recent volatility 'instant' period
    ATRhistoPeriod = 3000 //historical volatility period
    
    ATR = averagetruerange[ATRcurrentPeriod]
    histoATR= highest[ATRhistoPeriod](ATR)
    
    resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
    resultMIN = MIN(resultMAX,maxSTEP*pipsize)
    
    gridstep = (resultMIN)
    
    // Conditions to enter long positions
    c1 = (MAC > 0)
    c2 = (CCC > 0)
    c3 = (AAA > 20)
    c4 = (DIP > DIM[1])
    
    //first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on line 39
    if NOT ONMARKET AND HIGHEST[1](c1)=1 AND HIGHEST[1](c2)=1 AND HIGHEST[1](c3)=1 AND HIGHEST[1](c4)=1 then
    BUY amount LOT AT MARKET
    endif
    
    // case BUY - add orders on the same trend
    if longonmarket and close-tradeprice(1)>=gridstep then
    BUY amount LOT AT MARKET
    endif
    
    // Conditions to enter short positions
    c11 = (MAC < 0)
    c12 = (CCC < 0)
    c13 = (AAA > 20)
    c14= (DIP < DIM[1])
    
    //first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 55
    if NOT ONMARKET AND HIGHEST[1](c11)=1 AND HIGHEST[1](c12)=1 AND HIGHEST[1](c13)=1 AND HIGHEST[1](c14)=1  then
    SELLSHORT amount LOT AT MARKET
    endif
    
    // case SELL - add orders on the same trend
    if shortonmarket and tradeprice(1)-close>=gridstep then
    SELLSHORT amount LOT AT MARKET
    endif
    
    //money management
    liveaccountbalance = accountbalance+strategyprofit
    moneyrisk = (liveaccountbalance*(riskpercent/100))
    if onmarket then
    onepointvaluebasket = pointvalue*countofposition
    mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
    endif
    
    //floating profit
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
    
    //actual trade gains
    MAfloatingprofit = average[40](floatingprofit)
    BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
    
    //CFTA cross exit
    if LONGONMARKET AND c1=0 or c2=0 or c3=0 or c4=0 then
    SELL AT MARKET
    endif
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    SELL AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    SELL AT MARKET
    endif
    endif
    
    //CFTA exit
    if c11=0 or c12=0 or c13=0 or c14=0 then
    EXITSHORT AT MARKET
    endif
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    EXITSHORT AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    EXITSHORT AT MARKET
    endif
    endif
    
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    
    And with multiplier;
    //==================CFTA-v3.1
    
    Indicator1=0
    
    //***************Long
    c1 = MACD[60,130,45] > 0
    c2 = ADX[50] > 10
    c3 = CCI[100] > 0
    DIP = DIplus[50]
    DIM = DIminus[50]
    c4 = (DIP > DIM[1])
    
    //*************Short
    c11 = MACD[60,130,45] < 0
    c12 = ADX[50] > 10
    c13 = CCI[100] < 0
    DIP = DIplus[50]
    DIM = DIminus[50]
    c14 = (DIP < DIM[1])
    
    If c1 and c2 and c3 and c4 then
    Indicator1=1
    Endif
    
    If c11 and c12 and c13 and c14 then
    Indicator1=-1
    Endif
    
    
    Return indicator1
    
    Remember the grid step and ATR settings are at the discretion of each trader and may be adapted to different markets depending on volatility. I hope you will enjoy it and give some feedback on possible improvements 🙂
    manel, Nicolas, Cosmic1, Brage and ALE thanked this post
    #17801 quote
    Cosmic1
    Participant
    Senior
    Good work cfta, will take a look at this later.
    #17864 quote
    Elsborgtrading
    Participant
    Veteran
    Hi CFTA. No worries. I have been busy at work and trading came to a stop because I actually felt out of luck. So I put trading on hold for a while, but I keept the automated strategy running. One in particular with the CFTA indicators was executed on various TF from 1-10 sec. which belong to this post. It created 2 problems. Backtesting sucks because of data range. only max 18 day of data! and the ProOrder was stopped due to some lame error of “the status of the last order could not be verified” IG support have not come up with any plausible explanation for this, and PRT- who actually holds this account don’t receive the debugging data from the demo account when I trigger this feature. So it pretty much stuck to luck and time.  One that managed to stay alive was the strategy on a 10 sec timeframe. I know It sounds crazy to relay on indicators on a 10 sec timeframe- however the result cannot be ignored. I will still let it run for some months, but so far this is what it produced. cheers Kasper
    Screenshot_69.jpg Screenshot_69.jpg
    #17880 quote
    cfta
    Participant
    Senior
    Hey Kasper, Nice to see you back in the thread. That’s some promising test results and I’m very surprised that the 10 sec one or any for that matter turned out with a positive result. Even though I have changed the code’s deafault setting to have recurring cycles we still need to find out setups manually or with the help of screeners and only start with systems when we have the right market conditions since the indicator only takes us half the way since it may still give signals when the market is choppy or spikes. Hence I don’t think it’s possible to backtest it properly unless running it on one selected suitable period at the time. The screeners are helpful but we need further work on how to find solid setups, it helps to run the screeners on higher timeframes but when volatility is lower it lags too much to find the setups we want. Btw, your code for adapting to JPY pairs works great even though trade statistics may turn out a bit distorted but the entries and SL works which is most important 🙂 .
    #18892 quote
    Elsborgtrading
    Participant
    Veteran
    Hi CFTA, Great to hear that the JPY pair code is working. Would be great to have a live update in the code for the conversion, but as long it just more or less the daily/weekly conversionrate I guess it should be okay. About the Heikin-ashi code, I made a small indicator. It should indicate a change from green to red and vice versa, or did I miss something? 🙂 Cheers Kasper
    //Heikin-Ash
    xClose = (Open+High+Low+Close)/4
    if(barindex>2) then
    xOpen = (xOpen[1] + xClose[1])/2
    endif
    changeToGreencandle = xClose>xOpen AND xClose[1]<xOpen[1]
    ChangeToRedcandle = xClose<xOpen AND xClose[1]>xOpen[1]
    
    If changeToGreencandle then
    Indicator1=1
    Else
    Indicator1=0
    Endif
    
    If ChangeToRedcandle then
    Indicator22=-1
    Else
    Indicator22=0
    Endif
    Return indicator1 COLOURED(0,255,0) as "changeToGreencandle", indicator22 COLOURED(255,0,0) as "ChangeToRedcandle"
    #19395 quote
    sylvess
    Participant
    Average
    This long trade today reached its rr but close quite well below its high almost 30 minutes later – close at 1.05535, high is 1.0655. Believe this is due to BBfloatingprofit logic. Not good in math so can’t wrap my head around this logic. Anyone can explain, any suggestion to improve it? Another first encountered in today EUR currency spike is a position opened at more than double the gridstep setting – unfortunately at its high for my EURJPY trade. This is not necessary a problem, just be aware of this. I added in maximum position it can open and entry price (set ep to high if you want immediate opening of trade but do note it does not check for green candle which you can add it back in if you want).
    //www.prorealcode.com/topic/grid-orders-with-one-combined-stop-loss-and-limit-can-it-be-done/page/9/
    //-------------------------------------------------------------------------
    // Main code : Grid Long BB w/Ep
    //-------------------------------------------------------------------------
    //Open a single grid trade upon new candle
    defparam preloadbars = 100
    once RRreached = 0
    
    //parameters
    accountbalance = 10000 //account balance in USD
    riskpercent = 2 //whole account risk in percent%
    gridstep = 10 //grid step in point
    MaxPosition = 5 //max position to open
    amount = 1 //lot amount to open each trade
    rr = 2.5 //risk reward ratio (0 - disable)
    ep = 100000 //open position if close < ep
    
    //first trade
    //if NOT ONMARKET AND close>close[1] AND STRATEGYPROFIT=0 then
    if NOT ONMARKET AND close<ep AND STRATEGYPROFIT=0 then
    BUY amount LOT AT MARKET
    endif
    
    // case BUY - add orders on the same trend
    if longonmarket and close-tradeprice(1)>=gridstep*pipsize And CountOfPosition < MaxPosition then
    BUY amount LOT AT MARKET
    endif
    
    //money management
    liveaccountbalance = accountbalance+strategyprofit
    moneyrisk = (liveaccountbalance*(riskpercent/100))
    if onmarket then
    onepointvaluebasket = pointvalue*countofposition
    mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
    endif
    //floating profit
    floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
    
    MAfloatingprofit = average[20](floatingprofit)
    BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*2
    
    //floating profit risk reward check
    if rr>0 and floatingprofit>moneyrisk*rr then
    RRreached=1
    endif
    
    //stoploss trigger when risk reward ratio is not met already
    if onmarket and RRreached=0 then
    SELL AT positionprice-mindistancetoclose STOP
    endif
    
    //stoploss trigger when risk reward ratio has been reached
    if onmarket and RRreached=1 then
    if floatingprofit crosses under BBfloatingprofit then
    SELL AT MARKET
    endif
    SELL AT positionprice-mindistancetoclose STOP
    endif
    
    //resetting the risk reward reached variable
    if not onmarket then
    RRreached = 0
    endif
    Capture.jpg Capture.jpg
    #19400 quote
    Nicolas
    Keymaster
    Master
    @sylvess So far I can see that BBexit has done what is is supposed to do: close the orders’ basket when the profit make a pullback to the lower Bollinger band calculated on the current opened orders profit. This function was build specifically for the cfta strategy: give breath to the trades while ensure to not let too much money on the table. Because no one will never know what would be the best way to close a trade (or a trades’ basket here), the BBexit function do this job for you. You can play with the Bollinger Bands period at lines 39 and 40, to narrow them and exit faster, it is set to 20 periods by default. An idea would be to change this period dynamically, according to the recent instrument price behaviour, use a kind of adaptive MA instead of a simple moving average like I did in the code…
    sylvess thanked this post
    #19422 quote
    cfta
    Participant
    Senior
    Merry christmas and happy new year everyone all traders and coders who have contributed or just taken an interest in the thread 🙂 We have come a long way since my first post a little over 8 months ago, the system that according to the PRT support could not be coded at all turned out to be a only a modest challenge which Nicolas mastered with excellence. The holiday break is nearing its end and it is time to prepare for a new year of trading. Regretfully I don’t have any late xmas presents for you in shape of any major improvements of the system. It has proven complicated to figure out an effective automated entry even after having found a promising setup after manual screening but I’m still working on it trying different parameter settings and an occasional swith of indicators. I find the screeners I posted above very useful in particular the CFTA Trend, however the only caveat is to notice when the CFTA indicator starts giving a signal and the screener show that a new asset is givning the signal. I am aware that alerts can be set manually on the PRT platform for when the price or an indicator crosses level X but what I am most eager to find out is if someone know how to code a screener or an indicator to create a popup alert once it gives a signal?  I have browsed both the indicator and screeners manuals without figuring this out. The idea is to improve the current screeners and easier notice good trades but also create a new screener with a counterfeit MTF support by mupltiplying the values, such as the CFTA indicator gives a signal on M1, M5 and M15 simultaneously we should have a high probability trade. @ Kasper, thanks for the HA indicator it can prove to be very useful. Do you think you can modify it to show a signal for HA being green or red and not only the change? In addition do you think it is possible to somehow adjust values in order to reflect a higher timframe, for instance we run it as part of the CFTA indicator on M1 but it shows gives a value if the M5 HA is red or green? The second may be very difficult but I think we could figure out the first one… @ Sylvess, for me the BB exit function in the code has been working perfectly however I noticed that your code looks a little different from mine and it looks like you have a large standard deviation multiplier at the end on line 40. For my part I use 0.25 as standard deviation and 40 for the MA setting. Nice to see that you made a nice profit 🙂
    sylvess thanked this post
    #19454 quote
    sylvess
    Participant
    Average
    @ Cfta Thanks for highlighting. I was using the earlier version, switched to later version now.
Viewing 15 posts - 256 through 270 (of 308 total)
  • You must be logged in to reply to this topic.

Grid orders with one combined stop loss and limit, can it be done?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
cfta @cfta Participant
Summary

This topic contains 307 replies,
has 1 voice, and was last updated by OtherAttorney
1 year, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/14/2016
Status: Active
Attachments: 106 files
Logo Logo
Loading...