Coding of a trend following strategy with pullback counter

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #210265 quote
    phoentzs
    Participant
    Master

    Trend following strategies all work similarly. I’ll use the simplest example. I need coding for the following.
    Condition 1: EMA20 crosses over EMA50

    After condition 1 is met, I want a counter for condition 2.

    Condition 2 means stochastic crosses over 20. There we buy at market.

     

    So AFTER the EMACross, a pullback of the stochastic should be bought, whereby this pullback includes a counter. I want to know how many pullbacks are profitable after an EMACross. Often only the first pullback is profitable, sometimes there are 2 or 3. This counter should therefore be optimisable.

    Can someone code this for me please?

    #210597 quote
    robertogozzi
    Moderator
    Master

    There you go:

    DEFPARAM CumulateOrders = False
    ONCE Winners    = 0
    ONCE Losers     = 0
    ONCE CrossOVER  = 0
    ONCE CrossUNDER = 0
    Ema20 = average[20,1](close)
    Ema50 = average[50,1](close)
    
    myST  = Stochastic[10,6](close)
    //
    IF Not OnMarket AND OnMarket[1] THEN
       IF StrategyProfit > StrategyProfit[1] THEN
          Winners = Winners + 1
       ELSE
          Losers  = Losers + 1
       ENDIF
    ENDIF
    //
    IF CrossOVER = 0 THEN
       CrossOVER = Ema20 CROSSES OVER Ema50
       IF CrossOVER THEN
          CrossUNDER = 0
          Winners    = 0
          Losers     = 0
       ENDIF
    ENDIF
    IF CrossUNDER = 0 THEN
       CrossUNDER = Ema20 CROSSES UNDER Ema50
       IF CrossUNDER THEN
          CrossOVER = 0
       ENDIF
    ENDIF
    //
    STcross = myST CROSSES OVER 20
    IF STcross AND CrossOVER THEN
       IF LongOnMarket THEN
          IF PositionPerf > 0 THEN
             Winners = Winners + 1
          ELSE
             Losers  = Losers + 1
          ENDIF
          SELL AT Market
       ENDIF
       BUY AT Market
    ENDIF
    //
    graph Winners coloured("Blue")
    graph Losers  coloured("Red")
    CROSSwinnerslosers.itf
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Coding of a trend following strategy with pullback counter


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
phoentzs @phoentzs Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by robertogozzi
2 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/23/2023
Status: Active
Attachments: 1 files
Logo Logo
Loading...