Move stop loss level to opening level

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #78262 quote
    foxfutures
    Participant
    New

    Hi there,

    I was wondering if somebody could help me out. I would like to have my stoploss moved to the level of entry as soon as price hits 70% of my target. This has to be implemented in my code. My code so far works really well. The code is shown below. It’s fairly simple: when the conditions are met, 1 contract will be bought. Stop loss lies 5 pips below the candle previous from entry, and target profit lies at the exact same distance.

    DISTANCELONG=(abs(close - low) / pipsize) +5
    
    IF STRATEGYLONG AND Not OnMarket THEN
    BUY 1 CONTRACT AT MARKET
    SET STOP PLOSS (abs(close - low) / pipsize) + 5 //DISTANCELONG
    SET TARGET PPROFIT PositionPrice + DISTANCELONG
    ENDIF

    Now I just would like to have added that if the price moves in the favourable direction and reaches 70% of the target level (positionprice + (0.7*distancelong??), the stop loss will be moved from the original level to the level of entry (positionprice). Of course maintaing the same target profit level.

    Ik hope someone can help me out. Thanks in advance!

     

    Daniel (foxfutures)

    #78268 quote
    robertogozzi
    Moderator
    Master

    To write code, please use the <> “insert PRT code” button, to make code easier to read and understand. Thank you.

    #78269 quote
    robertogozzi
    Moderator
    Master

    There you go (not tested, though, let me know of any issue):

    IF Not OnMarket THEN
       StopLoss = 0
    ENDIF
    DISTANCELONG=(abs(close - low) / pipsize) +5
    IF STRATEGYLONG AND Not OnMarket THEN
       BUY 1 CONTRACT AT MARKET
       SET STOP PLOSS (abs(close - low) / pipsize) + 5 //DISTANCELONG
       SET TARGET PPROFIT PositionPrice + DISTANCELONG
    ENDIF
    IF LongOnMarket AND (close - TradePrice) >= (70 * pipsize) THEN
       StopLoss = TradePrice
    ENDIF
    IF LongOnMarket AND StopLoss THEN
       SELL AT StopLoss STOP
    ENDIF

    Beware that line 8  will only be executed next bar, since ProOrder takes a bar to know it’s On Market. Also, PPROFIT doesn’t want a price, but the number of PIPS!

    #78270 quote
    foxfutures
    Participant
    New
    DISTANCELONG=(abs(close - low) / pipsize) +5
     
    IF STRATEGYLONG AND Not OnMarket THEN
    BUY 1 CONTRACT AT MARKET
    SET STOP PLOSS (abs(close - low) / pipsize) + 5 //DISTANCELONG
    SET TARGET PPROFIT PositionPrice + DISTANCELONG
    ENDIF

    Thank you Robertogozzi, I was not aware of that. I hope this works better.

    robertogozzi thanked this post
    #78271 quote
    Vonasi
    Moderator
    Master
    IF LongOnMarket AND (close - TradePrice) >= (70 * pipsize) THEN

    The above line of your code is not correct Robert. Foxfutures wanted it at 70% of the target distance and not 70 pips.

    Also this line is wrong as it sets a price rather than a distance:

    SET TARGET PPROFIT PositionPrice + DISTANCELONG

    I think this is what you meant to write:

    IF Not OnMarket THEN
       StopLoss = 0
    ENDIF
    
    DISTANCELONG=(abs(close - low) / pipsize) +5
    
    IF STRATEGYLONG AND Not OnMarket THEN
       BUY 1 CONTRACT AT MARKET
       SET STOP PLOSS DISTANCELONG
       SET TARGET PPROFIT DISTANCELONG
    ENDIF
    IF LongOnMarket AND (close - TradePrice) >= (0.70 * DISTANCELONG) THEN
       StopLoss = TradePrice
    ENDIF
    IF LongOnMarket AND StopLoss THEN
       SELL AT StopLoss STOP
    ENDIF
    #78272 quote
    Vonasi
    Moderator
    Master

    Beware that line 8  will only be executed next bar, since ProOrder takes a bar to know it’s On Market.

    I’m not sure that is correct either. All the three orders will be sent to market, the buy, the target and the stop loss as at that time you are not ONMARKET which is one of the conditions.

    #78278 quote
    robertogozzi
    Moderator
    Master

    Thank you for spotting my error about 70%, I misread it.

    As for TRADEPRICE, ProOrder will know it next candle, so I guess it evakuates it to ZERO, thus leaving DISTANCELONG as SL. I see you amended it.

    #78279 quote
    Vonasi
    Moderator
    Master

    I’m not sure that I fully follow you. At the time when the orders are placed on the market this bit of code will be ignored as you are not long on the market so the value of TRADEPRICE is irrelevant:

    IF LongOnMarket AND (close - TradePrice) >= (0.70 * DISTANCELONG) THEN
       StopLoss = TradePrice
    ENDIF

    At the close of the first bar you are now LONGONMARKET so  it checks to see if it is above the TRADEPRICE which is now a known value.

    #78281 quote
    Vonasi
    Moderator
    Master

    Foxfutures – you may want to consider using MTF for this sort of break even idea. I don’t know what time frame you are testing it on but if for example it is an hourly chart then price could go above your 70% threshold several times in that hour but then actually close below it so your move to break even would not be triggered. A faster time frame would allow you to trigger it as soon as the 70% threshold was hit – although at the cost of less data to back test on of course.

    #78282 quote
    robertogozzi
    Moderator
    Master

    Line 6 of the original code was

    SET TARGET PPROFIT PositionPrice + DISTANCELONG

    ProOrder will send it to the broker as

    SET TARGET PPROFIT 0 + DISTANCELONG

    which, in this particular case, may be working because he was adding a price to pips using PPROFIT.

    #78283 quote
    Vonasi
    Moderator
    Master

    Aaah! We are confusing each other – you wrote TRADEPRICE when you meant POSITIONPRICE and I was looking at my amended code where I had fixed that problem that you were trying to refer to!

    #78288 quote
    foxfutures
    Participant
    New

    Hi guys,

    Thank you both for your solutions and feedback. However, I am not a specialist and have trouble following you both completely, but you’ve helped me a lot.

    Thanks!

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

Move stop loss level to opening level


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
foxfutures @foxfutures Participant
Summary

This topic contains 11 replies,
has 3 voices, and was last updated by foxfutures
7 years, 6 months ago.

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