Pyramiding for S&P500 doesn’t work
Forums › ProRealTime English forum › ProOrder support › Pyramiding for S&P500 doesn’t work
- This topic has 10 replies, 4 voices, and was last updated 3 years ago by
PeterSt.
-
-
01/16/2022 at 8:15 PM #185631
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
Pyramiding123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566// Definition of code parametersDEFPARAM CumulateOrders = True // Cumulating positions deactivatedDEFPARAM Preloadbars = 10000timeframe(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// AllMACrossSEMA21g = 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 2barsSEMA8d = 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>1IF (SEMA21g ) AND NOT ShortOnMarket AND ATR>1.35 THENTradeRisk = 50*ATR //+ 50ContractSize = 100/TradeRiskSELLSHORT ContractSize SHARES AT marketATR = Averagetruerange[14]// Stops and targetsSET STOP LOSS 1.0*atr //+ 1.0*pointsizeSET TARGET PROFIT 25*atr //+ 0.5*pointsizeENDIFIF (SEMA8d ) AND ShortOnMarket AND ATR>1.35 THENTradeRisk = 50*ATR //+ 50ContractSize = 100/TradeRiskSELLSHORT ContractSize SHARES AT marketATR = Averagetruerange[14]// Stops and targetsSET STOP LOSS 1.0*atr //+ 1.0*pointsizeSET TARGET PROFIT 1.5*atr //+ 0.5*pointsizeENDIFSBishop = ADX[14]<ADX[14][1] AND ADX[14][1]>ADX[14][2] AND ADX[14]>40if 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]) thenEXITSHORT at marketendifif 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]) thenEXITSHORT at high + 0.5*pointsize stopendif01/16/2022 at 11:25 PM #185636I 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.
01/17/2022 at 12:52 PM #185686Thanks 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?
1 user thanked author for this post.
01/17/2022 at 12:58 PM #185687It 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.
01/17/2022 at 1:16 PM #185694Sacha, regarding what XORANDNOT just told, try to change all into something like this :
123456789101112131415161718192021222324252627282930313233JustTraded = 0If not JustTraded then // This first one is for good habit.IF (SEMA21g ) AND NOT ShortOnMarket AND ATR>1.35 THENTradeRisk = 50*ATR //+ 50ContractSize = 100/TradeRiskSELLSHORT ContractSize SHARES AT marketJustTraded = 1 // One per bar unless we explicitly want otherwise !ATR = Averagetruerange[14]// Stops and targetsSET STOP LOSS 1.0*atr //+ 1.0*pointsizeSET TARGET PROFIT 25*atr //+ 0.5*pointsizeENDIFENDIF // Not JustTraded ?If not JustTraded thenIF (SEMA8d ) AND ShortOnMarket AND ATR>1.35 THENTradeRisk = 50*ATR //+ 50ContractSize = 100/TradeRiskSELLSHORT ContractSize SHARES AT marketJustTraded = 1ATR = Averagetruerange[14]// Stops and targetsSET STOP LOSS 1.0*atr //+ 1.0*pointsizeSET TARGET PROFIT 1.5*atr //+ 0.5*pointsizeENDIFENDIF // 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 !
1 user thanked author for this post.
01/17/2022 at 2:13 PM #185702It 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?
01/17/2022 at 2:22 PM #185709Not 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.
1 user thanked author for this post.
01/17/2022 at 2:29 PM #185711Thanks 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.
01/17/2022 at 2:33 PM #185714Not 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.
01/17/2022 at 2:52 PM #185721Thanks 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.
1 user thanked author for this post.
01/17/2022 at 6:57 PM #185753However 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 :
12345JustTraded = 1If not 1 then// Do some TradeendifYou 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 :
12345678910111213141516171819202122JustTraded = 0If not 1 then// Do some Tradeendif// It will enter a trade there, right ?// So this will do too :JustTraded = 0If not JustTraded then // Not JustTraded will be True here.// Do some Tradeendif// and :JustTraded = 1If not 1 then // It *IS* 1 here !! so the Not will give a false.// Do some Trade ... nah, no trade will be done.endifJust 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
1234567// No Position present at this moment.SELLSHORT ContractSize SHARES AT marketIf ShortOnMarket then // This will really be false when the above was executed during the call of this bar !!// [...]EndifThen 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
1DEFPARAM CumulateOrders = False // Cumulating positions deactivatedwith 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 !
-
AuthorPosts