USDJPY automated MA trailing exit for discretionary short entry

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #204373 quote
    Seb
    Participant
    Average

    This code helps you to trail a discretionary short position in USDJPY or any other USDXXX pairs. You enter the level where you want to go short with a stop entry order and you enter the level where you want your stoploss. Additionally you enter the spread and your accountsize and the EURUSD conversion (for euro accounts). Your positionsize is calculated based on 0.5% risk of your accountsize. If you use a 1 minute timeframe, the stop entry order will be placed within a minute after running the strategy. You will be filled once the price hits your stop entry order. The stoploss order will now be placed with a 0.5% risk of your accountsize. Once the position comes into profit, half of your position will be exited when the close of the 1h chart crosses above the moving average 10. The remaining position will be closed once the close of the 1h chart crosses above the moving average 20. In case the 1h closes above the moving average 20 without closing above the moving average 10 first, your full position will be closed at once.

    This code is quite specific, but hopefully it can be useful for those who want do do something similar.

    (This is not a fully automated strategy)

    defparam preloadbars = 10000
    defparam cumulateorders = false
    
    
    // manualy enter these variables
    spread = 0.01
    accountsize = 25000
    entrylevel = 146.662
    stoplosslevel = 146.937
    eurusd = 1.0387
    
    // calculate the desired loss and adjust the entrylevels for spread
    loss = 0.005*accountsize*eurusd
    entrylevel = entrylevel -0.5*spread
    stoplosslevel = stoplossniveau+0.5*spread
    
    // calculate the desired position size
    Positionsize = (loss / ((stoplosslevel-entrylevel) * (pipvalue/pipsize)/close))
    
    // can only do 1 trade
    c1 = TradeIndex(1) = 0
    
    // Short entry order placement with stoploss
    IF NOT ShortOnMarket and c1 THEN
    Sellshort Positionsize CONTRACTS AT entrylevel stop
    Exitshort at stoplosslevel STOP
    ENDIF
    
    // Initial stoploss placement
    IF ShortOnMarket THEN
    Exitshort at stoplosslevel STOP
    ENDIF
    
    timeframe(1h, updateonclose)
    // Trailingstop exits based on 1h MA10 and MA20 once in profit
    once fullposition = 1
    
    IF ShortOnMarket and trailingstop <= entryniveau THEN
    if close crosses over average[20] then
    Exitshort countofposition contracts at market
    else
    if close crosses over average[10] and fullposition = 1 then
    Exitshort (0.5*COUNTOFPOSITION) contracts at market
    fullposition = 0
    SET STOP $LOSS 0
    endif
    endif
    ENDIF
    robertogozzi thanked this post
    #204753 quote
    robertogozzi
    Moderator
    Master

    Thank you for sharing your code 🙂

    It can be valuable for those who want to monitor conditions manually, then enter a TS to automatically manage the position.

    #204763 quote
    GraHal
    Participant
    Master

    Link to above ‘Trade Manager‘ added as Log 345 here …

    Snippet Link Library

    Big Thanks to Seb for sharing with us All!

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

USDJPY automated MA trailing exit for discretionary short entry


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Seb @seb Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by GraHal
3 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/24/2022
Status: Active
Attachments: No files
Logo Logo
Loading...