trailing stops to all positions after they hit a combined profit?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #96661 quote
    bartjawien
    Participant
    New

    Hey guys. I’m keen to understand how to reset or add new trailing stops to all positions that are running after they hit a combined profit margin. Here is a some code in working with;

    //Max profit variable 
    MaxProfit=3500 //Max profit allowed (in money)
    
    // Test the current floating profit and close orders if needed
    FloatingProfit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
    
    if FloatingProfit>=MaxProfit then
    SET STOP $TRAILING 500
    endif
    

    Will the Set Stop apply a $500 Trailing Stop all positions?

    #96742 quote
    Vonasi
    Moderator
    Master

    No. Because the stop will only be in place when FloatingProfit equals 3500 exactly. A very rare thing I imagine. You need to set a flag when price >= 3500 and then only have the trailing stop in operation if that flag is set. Otherwise the trailing stop only works if price goes from >= all the way down to your stop price in one bar.  Reset the flag to zero if price falls below your stop level.

    #97056 quote
    bartjawien
    Participant
    New

    Thanks @Vonasi,

    Here’s a real snippet I’m testing at the moment across a few pairs and it seems to work sometimes and not others – what am I getting wrong?

    // test the current floating profit and close orders if needed
    FloatingProfit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
    if FloatingProfit > 1500 and floatingprofit <2500 then
    SET STOP PTRAILING 10
    endif
    if FloatingProfit > 2501 and floatingprofit <5000 then
    SET STOP PTRAILING 5
    endif
    if FloatingProfit > 5001 and floatingprofit <8000 then
    SET STOP PTRAILING 4
    endif
    if FloatingProfit > 8001 then
    SET STOP PTRAILING 3
    endif

    I’ve been monitoring it on the AUD/USD and it triggered fine. However, miss fires on the USD/CAD. Could it not be converting to the AUD for the total gain for all running positions? I’m expecting the FloatingProfit to be calculated in AUD for all open position for that individual system …

    Keen to hear your thoughts guys.

    #97057 quote
    Nicolas
    Keymaster
    Master

    The pointvalue instruction, use to calculate the floating profit, is in the currency of the current instrument, not in the currency of your account, because there is no way to get it through code. You’ll have to make a conversion yourself in the code for that.

    #97061 quote
    bartjawien
    Participant
    New

    Thanks again,

    So the code looks correct?

    The instrument I’ve been testing on is the USD/CAD where the system calculates the gain for open positions in CAD. Do you mean I’ll have to code a conversation back into AUD which what I believe you’re saying?

    As for the terminology for the system ‘latent gain’ and ‘today gain’ I’m not looking to calculate the total latent gain for the system for all time.

    All I’m looking to test is:

    My system opens short and long position, and if the combined open and running positions hit a certain value a trailing stop is activated for all running positions. If the positions are then stopped-out because the market reversed — next time the system opens a new set of positions the floatingprofit will start from zero.

    Apologies if I’m not explaining myself properly.

    Thoughts?

    #98409 quote
    bartjawien
    Participant
    New

    Hey @Nicolas – I’ve been testing the above and recently found a flaw in my code;

    // test the current floating profit and close orders if needed
    FloatingProfit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
    if FloatingProfit > 50 and floatingprofit <150 then
    SET STOP PTRAILING 4
    endif
    if FloatingProfit > 151 and floatingprofit <250 then
    SET STOP PTRAILING 2
    endif
    if FloatingProfit > 251 then
    SET STOP PTRAILING 1
    endif

    One of these functions is calculating the total gain value for the system rather than only the current open positions;

    (close-positionprice)*pointvalue)*countofposition)/pipsize

    The solution or advice I’m looking for is to find a way to reset the floatingprofit at each new position open. Is there a function to do this?

     

    Would this work … subtracting the system profit using strategyprofit function from the total accumalative floatingprofit:

    FloatingProfit = Strategyprofit – (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual open trade gains

     

    Thanks in advance.

    Bart

    #98410 quote
    bartjawien
    Participant
    New

    Sorry –

    Would this work … subtracting the system profit using strategyprofit function from the total accumalative floatingprofit:

    FloatingProfit = Strategyprofit – (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual open trade gains

    Sorry – should be the otherway around;

    FloatingProfit = ((((close-positionprice)*pointvalue)*countofposition)/pipsize) – Strategyprofit //actual open trade gains

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

trailing stops to all positions after they hit a combined profit?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
bartjawien @bartjawien Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by bartjawien
6 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/17/2019
Status: Active
Attachments: No files
Logo Logo
Loading...