Pyramiding for S&P500 doesn’t work

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #185631 quote
    Sascha
    Participant
    Average

    Hi,

    I would like to pyramid to an existing position, but I can’t make it work. Clearly I am making a mistake and I need some advice. I am trading the S&P500 on a 3 min chart.

    Below is a code that doesn’t create a second position with Setup “SEMA8d”. The setup by itself works when I delete the other setup “SEMA21g” from the code.

    Setup “SEMA8d” has a setup on 13.01.2022  at 21:06. The other setup “SEMA21g” is short and has an exit signal at the moment (21:06) when “SEMA8d” creates a short signal.

    Cumulate order is set to True.

    What do I need to change to be able to enter a new trade while another is just exiting?

     

    Thanks.

    Sascha

    // Definition of code parameters
    DEFPARAM CumulateOrders = True // Cumulating positions deactivated
    DEFPARAM Preloadbars = 10000
    
    timeframe(3 minutes, default)
    ATR = Averagetruerange[14]
    Sto14 = Stochastic[14,3](close)
    Sto8 = Stochastic[8,3](close)
    EMA8 = ExponentialAverage[8](close)
    EMA21 = ExponentialAverage[21](close)
    SMA10 = Average[10](close)
    SMA50 = Average[50](close)
    
    ignored, MFneutral, ignored = CALL "Momentum_F"
    ignored, ignored, MFbear = CALL "Momentum_F"
    ignored, ignored, ignored, PMFred, ignored = CALL "PowerMomentumFlag"
    ignored, ignored, ignored, Red82150 = CALL "8-21-50"
    ignored, PlusDecrease, ignored, ignored, ignored = CALL "PRC_TTM Squeeze 10.3"[20, 2, 20, 1.5, 0, 0]
    
    //----------------------------------------------------------------------------------------------------
    //--------------------------------------------------SHORT---------------------------------------------
    
    SALLMACross = open>EMA8 AND close<EMA8 AND open>EMA21 AND close<EMA21 AND open>SMA10 AND close<SMA10 AND open>SMA50 AND close<SMA50
    
    // AllMACross
    SEMA21g = EMA21<EMA21[1] AND EMA21[1]>EMA21[2] AND EMA21[2]>EMA21[3] AND close<open AND SALLMACross  AND EMA8>SMA10 AND MFneutral=1 AND PlusDecrease<PlusDecrease[1] AND PlusDecrease>0 AND PlusDecrease[1]>0 AND Sto14>53 AND Sto8>44 AND Sto14<Sto14[1] AND Sto8<Sto8[1] AND EMA8>EMA21
    
    // EMA8, SMA10 down 2bars
    SEMA8d = EMA8<EMA8[1] AND EMA8[1]>EMA8[2] AND close<open AND open>EMA8 AND close<EMA8 AND open>SMA10 AND close<SMA10  AND MFbear=1 AND EMA8<EMA21 AND PMFred=1 AND Red82150=1  AND EMA8>SMA10  AND SMA10<SMA10[1] AND SMA10[1]<SMA10[2] AND EMA21<EMA21[1] AND EMA21[1]<EMA21[2] AND Sto14>29 AND Sto8>43  AND Sto14<Sto14[1] AND ADX[14]<40// AND close>SMA200 AND close-SMA200>1
    
    
    IF (SEMA21g  ) AND NOT ShortOnMarket AND ATR>1.35  THEN
    TradeRisk = 50*ATR //+ 50
    ContractSize = 100/TradeRisk
    SELLSHORT ContractSize SHARES AT market
    
    ATR = Averagetruerange[14]
    
    // Stops and targets
    SET STOP LOSS 1.0*atr //+ 1.0*pointsize
    SET TARGET PROFIT 25*atr //+ 0.5*pointsize
    ENDIF
    
    
    IF (SEMA8d ) AND  ShortOnMarket AND ATR>1.35   THEN
    TradeRisk = 50*ATR //+ 50
    ContractSize = 100/TradeRisk
    SELLSHORT ContractSize SHARES AT market
    
    ATR = Averagetruerange[14]
    
    // Stops and targets
    SET STOP LOSS 1.0*atr //+ 1.0*pointsize
    SET TARGET PROFIT 1.5*atr //+ 0.5*pointsize
    ENDIF
    
    
    SBishop = ADX[14]<ADX[14][1] AND ADX[14][1]>ADX[14][2] AND ADX[14]>40
    
    if shortonmarket AND tradeprice-lowest[10](low)>2 AND (EMA8 CROSSES OVER SMA10) AND (barindex-tradeindex(1)>3) AND NOT (SBishop AND close<open AND Sto14<Sto14[1] AND Sto14[1]>Sto14[2]) then
    EXITSHORT at market
    endif
    
    if shortonmarket AND tradeprice-lowest[10](low)>2 AND (EMA8 CROSSES OVER SMA10) AND (barindex-tradeindex(1)>3) AND (SBishop AND close<open AND Sto14<Sto14[1] AND Sto14[1]>Sto14[2]) then
    EXITSHORT at high + 0.5*pointsize stop
    endif
    #185636 quote
    XORANDNOT
    Participant
    Senior

    I can’t see right now why  cumulation of orders does not work in your code, but all I can see is that you are using several times a code like “and not xxxx”. This is not the right syntax, but you need to write “and (not xxx)”, with brackets. This can make a huge difference, but it is probably not the reason why cumulation does not work.

    #185686 quote
    Sascha
    Participant
    Average

    Thanks XORANDNOT for your comment. However it didn’t solve the problem.

    But by accident I found out that it has something to do with the command “Exitshort at market”. When I delete the lines 60-62, the second trade at 21:06 is entered. But this is obviously not a solution as then the first trade wouldn’t exit when it’s supposed to (the exit is triggered at 21:06).  I don’t understand why. Maybe entering a new position while at the same time an Exitshort command is triggered somehow prevents the next trade to be taken.

    Anyone any idea?

    Midlanddave thanked this post
    #185687 quote
    XORANDNOT
    Participant
    Senior

    It could be that one position is closed by “exitshort” and another opened by a “sellshort” command at the same time. The net result is no change in position size and therefore nothing happens. Prorealtime adds up net position changes before placing any order. In this case, the old position with the old entry price will be kept.

    #185694 quote
    PeterSt
    Participant
    Master

    Sacha, regarding what XORANDNOT just told, try to change all into something like this :

    JustTraded = 0
    
    If not JustTraded then   // This first one is for good habit.
      IF (SEMA21g  ) AND NOT ShortOnMarket AND ATR>1.35  THEN
        TradeRisk = 50*ATR //+ 50
        ContractSize = 100/TradeRisk
        SELLSHORT ContractSize SHARES AT market
        JustTraded = 1   // One per bar unless we explicitly want otherwise !
     
        ATR = Averagetruerange[14]
     
        // Stops and targets
        SET STOP LOSS 1.0*atr //+ 1.0*pointsize
        SET TARGET PROFIT 25*atr //+ 0.5*pointsize
      ENDIF
    ENDIF   // Not JustTraded ?
    
    If not JustTraded then
      IF (SEMA8d ) AND  ShortOnMarket AND ATR>1.35   THEN
        TradeRisk = 50*ATR //+ 50
        ContractSize = 100/TradeRisk
        SELLSHORT ContractSize SHARES AT market
        JustTraded = 1 
    
        ATR = Averagetruerange[14]
     
        // Stops and targets
        SET STOP LOSS 1.0*atr //+ 1.0*pointsize
        SET TARGET PROFIT 1.5*atr //+ 0.5*pointsize
      ENDIF
    ENDIF   // Not JustTraded ?
    
    // And so on !

    You will be the first to understand what is happening (= what you are doing). 🙂 Next the PRT parser will understand it as well.

    Have fun !

    Sascha thanked this post
    #185702 quote
    Sascha
    Participant
    Average

    It could be that one position is closed by “exitshort” and another opened by a “sellshort” command at the same time. The net result is no change in position size and therefore nothing happens. Prorealtime adds up net position changes before placing any order. In this case, the old position with the old entry price will be kept.

    That is exactly what I was assuming. Is there any way to solve this?

    #185709 quote
    XORANDNOT
    Participant
    Senior

    Not in a direct way. You can however “remember” the new entry price of the new hypothetical position, and base all new exits (stop loss, take profit, trailing stop, etc.) on this price. Requires some coding work, however. Prorealtime will do all standard exit calculations with the old positionprice.

    Sascha thanked this post
    #185711 quote
    Sascha
    Participant
    Average

    Thanks PeterSt.

    However I am a little confused by your adjustment. As we set JustTraded=0 and then the next if condition asks for “if not 0” (line 3 and 18) that means only to take a trade if JustTraded is greater than 0, it should never take a trade at all as my conditions in line 4 and 19 will never be used and therefore JustTraded would never be set to 1.

    But in fact when I implement your suggestion nothing changes and the same trade is taken or not taken.

    #185714 quote
    Sascha
    Participant
    Average

    Not in a direct way. You can however “remember” the new entry price of the new hypothetical position, and base all new exits (stop loss, take profit, trailing stop, etc.) on this price. Requires some coding work, however. Prorealtime will do all standard exit calculations with the old positionprice.

    Thanks. I guess this is a little bit too complicated for me to code for now. I would need to improve my coding skills first.

    #185721 quote
    robertogozzi
    Moderator
    Master

    Thanks XORANDNOT for your comment. However it didn’t solve the problem.

    But by accident I found out that it has something to do with the command “Exitshort at market”. When I delete the lines 60-62, the second trade at 21:06 is entered. But this is obviously not a solution as then the first trade wouldn’t exit when it’s supposed to (the exit is triggered at 21:06). I don’t understand why. Maybe entering a new position while at the same time an Exitshort command is triggered somehow prevents the next trade to be taken.

    Anyone any idea?

    Because you enter a trade and close it immediately, before it is even entered. So it will never be entered.

    I also want to point out that, in case of additional positions, TRADEDEPRICE(1) AND TRADEINDEX(1) will refer to the most recent position. To refer to the average price calculated by ProOrder, use POSTIONPRICE, instead.

    Sascha thanked this post
    #185753 quote
    PeterSt
    Participant
    Master

    However I am a little confused by your adjustment. As we set JustTraded=0 and then the next if condition asks for “if not 0” (line 3 and 18) that means only to take a trade if JustTraded is greater than 0, it should never take a trade at all

    Haha, no … Look at this example :

    JustTraded = 1
    
    If not 1 then
      // Do some Trade
    endif

    You are just not “digging” the reality about a variable name and assign it the correct value. And I must admit, the way you explain it I can’t even see where it is wrong, but it really is … wrong.

    Thus my code above makes no sense but it shows you that only when not 1, you enter that trade. So again :

    JustTraded = 0
    
    If not 1 then 
      // Do some Trade 
    endif
    
    // It will enter a trade there, right ?
    // So this will do too :
    
    JustTraded = 0
    
    If not JustTraded then  // Not JustTraded will be True here.
      // Do some Trade 
    endif
    
    // and :
    
    JustTraded = 1
    
    If not 1 then // It *IS* 1 here !! so the Not will give a false.
      // Do some Trade ... nah, no trade will be done.
    endif
    

    Just try it. And you need to because else orders will cancel each other indeed.

    And Sacha, you do know that your code is called at each candle occurring, right ?
    And that your code is executed from top to bottom, sequentially ? –> This is why you need to this. Also :

    If OnMarktet (etc.) only receives the proper status at the next bar(‘s calling). Thus

    // No Position present at this moment.
    
    SELLSHORT ContractSize SHARES AT market
    
    If ShortOnMarket then   // This will really be false when the above was executed during the call of this bar !!
      // [...]
    Endif

    Then your Time(frame) elapses and then the If ShortOnMarket will be true. Not because of any SellShort once “again”, but because of that happening in the *previous* bar(‘s-call).

    Lastly, looking at the Topic Title (pyramiding) … No offense meant (!), but I would advise not to attempt this when the basic principles are not 100% clear. Thus, please start out with

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated

    with the notice that this time the comment matches the code (your’s really doesn’t but that will be an old comment, I’m sure).
    See what that brings you and how you will be able to buy and sell (Long and Short) with that. Covered that ? then some next steps. And my promise in advance : these aggregated orders are really not easy to do (I mean, track what’s going on when).

    And still and always : have fun !

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

Pyramiding for S&P500 doesn’t work


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Sascha @goedelsa Participant
Summary

This topic contains 10 replies,
has 4 voices, and was last updated by PeterSt
4 years, 1 month ago.

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