my first strategy – beginner level

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #42041 quote
    mmichael
    Participant
    Average

    Hello,

    I am starting my first strategy, just to understand the logic of proorder and how things work.

    At this stage I am facing 2 issues:

    • how to take partiel profit (50%) at a certain level
    • have a dynamic stop loss:
      • close 100% if the bar after the entry does not confirm (red bar in case of a long trade)
      • else: stop moves below each bar low

     

    DEFPARAM CumulateOrders
    mm20 = Average[20](close)
    mm50 = Average[50](close)
    
    openLong= mm20 crosses over mm50
    
    if not longonmarket and openLong then
    buy 1 lot at market // buy 1 contracts at market
    amplitude = close - low
    endif
    
    set stop loss amplitude
    set target profit amplitude
    
    // partial profit - to be completed
    // to do
    
    // stop move - to be completed
    if longonmarket then
    newStop = low[1]
    endif
    
    sell at newStop STOP
    #42050 quote
    Nicolas
    Keymaster
    Master

    Partial closure of position is still not supported by ProOrder.

    About your stoploss, the exit at each new bar low should operate nicely the way you have coded it. In the code below I implemented the exit on red candlestick:

    DEFPARAM CumulateOrders
    mm20 = Average[20](close)
    mm50 = Average[50](close)
    
    openLong= mm20 crosses over mm50
    
    if not longonmarket and openLong then
    buy 1 lot at market // buy 1 contracts at market
    amplitude = close - low
    endif
    
    set stop loss amplitude
    set target profit amplitude
    
    // partial profit - to be completed
    // to do
    
    // stop move - to be completed
    if longonmarket then
     if tradeindex=barindex[1] and close<open then 
      sell at market 
     else
      newStop = low[1]
      sell at newStop STOP
     endif
    endif
    
    

    Not tested, please confirm.

    mmichael thanked this post
    #42142 quote
    mmichael
    Participant
    Average

    Hello,

    thx a lot Nicolas,

    I tested the proposed code and there still have an issue with the stop execution.

    normally the stop is set to the previous low (low[1]) but as you can see in the screenshot, it executes one bar later than expected. please advice if you have an idea to fix this.

    I tried both “sell at newStop STOP” between the if / endif and outside, but behaviour is the same.

    Below my code:

    DEFPARAM CumulateOrders = False // Cumul des positions désactivé
    
    mm50 = Average[50](close)
    openLong= close > mm50 and close[1] < mm50
    openShort= close < mm50 and close[1] > mm50
    graph openLong
    graph openShort
    
    if not longonmarket and openLong then
    buy 1 lot at market // buy 1 contracts at market
    endif
    
    if not shortonmarket and openShort then
    sellShort 1 lot at market // buy 1 contracts at market
    endif
    
    if longonmarket then
    if tradeindex=barindex and close<open then
    sell at market
    else
    newStop = low[1]
    sell at newStop STOP
    endif
    endif
    
    if shortonmarket then
    if tradeindex=barindex and close>open then
    buy at market
    else
    newStop = high[1]
    buy at newStop STOP
    endif
    endif
    
    graph newStop                              //debugging
    
    issue-stop.png issue-stop.png
    #42149 quote
    Madrosat
    Participant
    Master

    il  me semble qu’avec shortonmarket il faut mettre exitshort et non buy

    #42184 quote
    Nicolas
    Keymaster
    Master

    Madrosat is right, if you want to exit a short position you should use “EXITSHORT” instead of buy (errors are contained in line 28 and 32 of your last code).

    #42276 quote
    mmichael
    Participant
    Average

    Thx gentlemen,

    I have re-written my code, but still struggling with the stop.

    the stop executes one bar late and at close price (not at the defined stop level)

     

    Below my code and I also attached the ITF file

    // ================================================================================ Params
    
    defparam cumulateorders = false // Cumul des positions désactivé
    positionSize = 1
    
    // ================================================================================ calc openlong openShort closeLong closeShort moveStopLong moveStopShort
    
    mm50 = Average[50](close)
    openLong= close > mm50 and close[1] < mm50
    openShort= close < mm50 and close[1] > mm50
    graph openLong
    graph openShort
    
    closeLong = barindex = tradeindex and close < open
    closeShort = barindex = tradeindex and close > open
    
    
    // ================================================================================ position opening
    
    if not onmarket and openLong then
    buy positionSize lot at market
    endif
    
    if not onmarket and openShort then
    sellShort positionSize lot at market
    endif
    
    // ================================================================================ set stop & target
    
    if longonmarket then
    if closeLong then
    sell at market
    else
    stopLevel = low[1]
    sell at stopLevel STOP
    endif
    endif
    
    if shortonmarket then
    if closeShort then
    exitshort at market
    else
    stopLevel = high[1]
    exitshort at stopLevel STOP
    endif
    endif
    
    graph stopLevel
    
    if not onmarket then
    stopLevel=0
    endif
    
    issue-stop2.png issue-stop2.png dev_algo_mm50.itf
    #42369 quote
    Nicolas
    Keymaster
    Master

    Are you sure your “stoplevel” is correctly calculated? If the answer is yes, then the problem should come from the way you are putting your pending stop orders, because they are included in a conditional block, they may be put on market too late.

    #42490 quote
    mmichael
    Participant
    Average

    Hello Nicolas,

    thanks a lot for your help, I did several tests and it does not work correctly.

    could you please tell me simply how to set a stop at the previous low (for a long)?

    illustration in the screenshot.

    My current code (not ok):

    defparam cumulateorders = false
    
    if not onmarket and close > Average[50](close) and close[1] < Average[50](close) then
    buy 1 lot at market
    endif
    
    if not onmarket then
    lastStop=0
    endif
    
    if longonmarket then
    lastStop = low[1]
    sell at lastStop STOP
    endif
    
    stop-previous-low.png stop-previous-low.png
    #42492 quote
    mmichael
    Participant
    Average

    my solution have been found here:

    BULLISH ENGULFING + BREAKOUT + BEARISH ENGULFING

    below the corrected code:

    defparam cumulateorders = false
     
    if not onmarket and close > Average[50](close) and close[1] < Average[50](close) then
    buy 1 lot at market
    // initial stop
    lastStop = low
    sell at lastStop STOP
    endif
     
    if longonmarket then
    lastStop = low
    sell at lastStop STOP
    endif

    PS.: some strategies that I have checked during my research for the stop are wrong, meaning the “lastStop = low[1]” that we can see in several strategies is not ok!!

    #42496 quote
    Nicolas
    Keymaster
    Master

    Because the code is read at Close and position opened at the next candle open, you need to refer to Low[0]. When you find a variable not calculated correctly, use GRAPH! and the solution will come to you quickly 🙂

    mmichael thanked this post
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

my first strategy – beginner level


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
mmichael @mmichael Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by Nicolas
8 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/30/2017
Status: Active
Attachments: 4 files
Logo Logo
Loading...