Break even coding

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #71638 quote
    ChrisNYE
    Participant
    Senior

    I want to include code that enables a trade to go to break even after n pips. I was given this code but does not seem to work on auto back test

    if not onmarket then
    breakeven=0
    endif
    
    if longonmarket then
    if close-tradeprice(1)>=400 then
    breakeven = 1
    endif
    if breakeven then
    sell at tradeprice(1) sto
    endif
    endif

    Thank you

    Chris

    #71644 quote
    Vonasi
    Moderator
    Master

    Please try to post in the correct forum to get the best chance of assistance. I have moved your request from the ProBuilder forum which is for indicators to the ProOrder forum which is for auto-trading strategies.

    Also it is preferred if you use the ‘Insert PRT Code’ button to make your posts more readable when they include code. I have tidied up your post for you 🙂

    #71646 quote
    Vonasi
    Moderator
    Master

    I’ll assume that it is not just because there is no ‘p’ in STOP!

    Try this:

    if not longonmarket then
    breakeven = 0
    endif
     
    if longonmarket and close-tradeprice>=400 then
    breakeven = 1
    endif
    
    if longonmarket and breakeven then
    sell at tradeprice stop
    endif
    

    Are you testing in tick by tick mode?

    #71648 quote
    ChrisNYE
    Participant
    Senior

    No that was my mistake. I have used the ‘p’ in the code.

    Thank you for moving to correct forum

    #71651 quote
    ChrisNYE
    Participant
    Senior

    Vonasi, How would the code be written for short entries?

    Yes I am in tick by tick mode

    #71652 quote
    ChrisNYE
    Participant
    Senior

    What I am trying to achieve is:

    to have an initial stop of 200 pips, then once the trade movies in my favor by 400 pips I want to trail stop to break even. I have a limit order set at 600 pips to take profit.

    #71653 quote
    robertogozzi
    Moderator
    Master

    Your code can be abridged like that

    if longonmarket and close-tradeprice>=400 then
       sell at tradeprice stop
    endif

    for SHORT trades

    if shortonmarket and tradeprice-close>=400 then
       exitshort at tradeprice stop
    endif

    This code will work for DAX. S&P500 and other instruments, but not for FX pairs, since 400 will never be reached by any currency!!!! You should use 400*pipsize to make your code portable to all instruments.

    #71658 quote
    ChrisNYE
    Participant
    Senior
    / Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    indicator1 = ExponentialAverage[19](close)
    indicator2 = ExponentialAverage[63](close)
    c1 = (indicator1 CROSSES OVER indicator2)
    
    IF c1 and Not OnMarket THEN
    BUY 19 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator3 = ExponentialAverage[5](close)
    indicator4 = ExponentialAverage[68](close)
    c2 = (indicator3 CROSSES UNDER indicator4)
    
    IF c2 and Not OnMarket THEN
    SELLSHORT 19 CONTRACT AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 20
    SET TARGET pPROFIT 60
    

    This is my basic code, and now I want it to trail to break even after 40 pips in profit.

    #71660 quote
    robertogozzi
    Moderator
    Master

    Test this one, I added a few lines between 2 and 4

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    IF Not OnMarket THEN
       BreakEven = 0
    ELSIF LongOnMarket THEN
       IF (close - TradePrice) >= (40 * pipsize) THEN
          BreakEven = 1
       ENDIF
       IF BreakEven THEN
          SELL AT TradePrice STOP
       ENDIF
    ELSIF ShortOnMarket THEN
       IF (TradePrice - close) >= (40 * pipsize) THEN
          BreakEven = 1
       ENDIF
       IF BreakEven THEN
          EXITSHORT AT TradePrice STOP
       ENDIF
    ENDIF
    
    // Conditions to enter long positions
    indicator1 = ExponentialAverage[19](close)
    indicator2 = ExponentialAverage[63](close)
    c1 = (indicator1 CROSSES OVER indicator2)
     
    IF c1 and Not OnMarket THEN
       BUY 19 CONTRACT AT MARKET
    ENDIF
     
    // Conditions to enter short positions
    indicator3 = ExponentialAverage[5](close)
    indicator4 = ExponentialAverage[68](close)
    c2 = (indicator3 CROSSES UNDER indicator4)
     
    IF c2 and Not OnMarket THEN
       SELLSHORT 19 CONTRACT AT MARKET
    ENDIF
     
    // Stops and targets
    SET STOP pLOSS 20
    SET TARGET pPROFIT 60
    #71667 quote
    ChrisNYE
    Participant
    Senior

    My analysis is that if I have 20 trades reach 40 pip profit and 16 trades reach 60 pip profit you would assume that with a 40 pip trail to break even that 4 trades would be break even if I was using a 60 pip take profit limit – which is the difference of the trades that reached 40 minus 60 pip trades.

    For some reason the theory doesnt seem to apply with this or any other code I have found.

    Am I missing something?

    #71669 quote
    Vonasi
    Moderator
    Master

    Your code can be abridged like that

    for SHORT trades

    Was your intention to do away with the BreakEven condition? Used on their own these will only close the trade if profit is above 400 and then falls in the same candle all the way back to zero. If profit is a above 400 and on the next candle drops to 300 then the sell order is not placed so there is no break even stop even though the trigger level was previously reached.

    #71674 quote
    robertogozzi
    Moderator
    Master

    Vonasi, that was a simple suggestion on existing code, I did not even think about breakeven. There was no strategy, so I could only correct what was unnecessary and the way pips should be referenced.

    I later posted the whole strategy revised with Breakeven.

    #71676 quote
    robertogozzi
    Moderator
    Master

    My analysis is that if I have 20 trades reach 40 pip profit and 16 trades reach 60 pip profit you would assume that with a 40 pip trail to break even that 4 trades would be break even if I was using a 60 pip take profit limit – which is the difference of the trades that reached 40 minus 60 pip trades.

    For some reason the theory doesnt seem to apply with this or any other code I have found.

    Am I missing something?

    That code is, as requested, for trailing to Breakeven, which is done once 40 pips profit have been reached. If they go ahead and reach 60 they’ll cash 60 pips, if the go backwards they’ll exit at the entry price. If you want to trail the whole think, it’s not just a question of breakeven, it’s a question of a complete trailing stop system.

    There’s one here that has been used for some years by many users and works quite well (MTF apart which will allow to improve the system): https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.

    and official documentation https://www.prorealcode.com/blog/learning/kinds-trailing-stop-proorder/.

    By searching you should also find a combination of the trailing stop code with breakeven code.

    #71677 quote
    Vonasi
    Moderator
    Master

    Vonasi, that was a simple suggestion on existing code, I did not even think about breakeven. There was no strategy, so I could only correct what was unnecessary and the way pips should be referenced.

    I later posted the whole strategy revised with Breakeven.

    No problem. I was just a bit concerned that someone reading it would think that that was a working break even code. 🙂

    #71678 quote
    Vonasi
    Moderator
    Master

    My analysis is that if I have 20 trades reach 40 pip profit and 16 trades reach 60 pip profit you would assume that with a 40 pip trail to break even that 4 trades would be break even if I was using a 60 pip take profit limit – which is the difference of the trades that reached 40 minus 60 pip trades.

    For some reason the theory doesnt seem to apply with this or any other code I have found.

    Am I missing something?

    I would suggest graphing the close minus tradeprice and the high minus tradeprice so that you can more closely inspect what is happening when a long trade is opened (and the opposite for a short) which will give you more clues as to why it is not behaving how you think it should.

Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.

Break even coding


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
ChrisNYE @chrisnye Participant
Summary

This topic contains 21 replies,
has 4 voices, and was last updated by rmhandel
7 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/29/2018
Status: Active
Attachments: No files
Logo Logo
Loading...