Sell at market instead of hitting guaranteed stop
Forums › ProRealTime English forum › ProOrder support › Sell at market instead of hitting guaranteed stop
- This topic has 61 replies, 6 voices, and was last updated 3 years ago by
PeterSt.
-
-
02/11/2022 at 11:22 AM #188052
Thanks for your efforts Peter. I’ve done some more work on this. I’ve modified the code from @robertogozzi so the exits are now 99% accurate to what they were originally (I moved the “slprice” instruction to the bottom of the code), but the problem remains that trades are hitting a real stop, which means I will be charged the fee for hitting a guaranteed stop 55% of the time. How can the below code be modified so the “if longonmarket” and “if shortonmarket” instructions can sell or exitshort without it being a stop? I’m sure “sell at market” is the key here, but I can’t find a way to use “sell at market” without creating a condition that is dependent on the close of a bar (which means I might have up to 75 minutes of slippage on an exit price).
see the annotated lines of the code at the bottom for what I’m attempting with “sell at market”
Code12345678910111213141516171819202122232425if longonmarket thenslprice = tradeprice - slsell at slprice stopendifif shortonmarket thenslprice = tradeprice + slexitshort at slprice stopendifset stop loss 100//if longonmarket then//slprice = tradeprice - sl//if longonmarket and close < slprice then//sell at market//endif//endif//if shortonmarket then//slprice = tradeprice + sl//if close > slprice then//sell at market//endif//endif02/11/2022 at 12:08 PM #188059Hey Mike – A few days ago I was originally ready to hand you the code (which I still could do) but a lack of time did not permit that. So yes, I definitely have this working. But anecdotal is :
What I learned from there I applied to my own code in order to check what it would bring. And surprise !! … I worked 3-4 hours on that because something with the Limit order itself seems not to work at all. This is the same Limit for entry you use, Mike. And from there I let it rest for a while. For you the same “issue” would occur, though this is hard to check or test.
02/11/2022 at 12:22 PM #188061Mike, here you are.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120DEFPARAM CumulateOrders = FalseStarttime = 000000Limitentrytime = 220000Exclude=time >=020000 and time <=050000timeframe(225 minutes,updateonclose)if MACD[12,26,9](close) >0 thenmac4flag=1endifif MACD[12,26,9](close) <0 thenmac4flag=-1endifif close >ExponentialAverage[5](high) thenupperflag=1endifif close <ExponentialAverage[5](high) and close >ExponentialAverage[5](low) thenupperflag=0endifif close < ExponentialAverage[5](low) thenupperflag=-1endiftimeframe(75 minutes,default)Once slpricelong = 0 // 08-02-2022,PS.Once slpriceshort = 0 // 08-02-2022,PS.Averagecrosslong= Average[8](close) CROSSES OVER Average[20](close)Averagecrossshort= Average[8](close) CROSSES under Average[20](close)ONCE crossflag=0ONCE avgflag=0if close > ExponentialAverage[5](high) thenavgflag=1endifif close < ExponentialAverage[5](low)thenavgflag=-1endiflongentry=0if avgflag=1 and averagecrosslong thencrossflag=1endifc2 = close > Average[50](close)longentry= c2limitbuy=ExponentialAverage[5](low)if not onmarket and crossflag=1 and longentry=1 and mac4flag=1 and upperflag=1 then //and time >=StartTime and time <LimitEntrytime and not exclude thensl=AverageTrueRange[14]*2 // 0.00235tp=sl * 1.5 // 0.003525slpricelong= close - sl // 0.72685//Set stop loss slrisk=100/sl/10000 // 42553BUY risk PERPOINT AT limitbuy limit // Mike, Watch out for the /10000 (re my contract size).endifshortentry=0if avgflag=-1 and averagecrossshort thencrossflag=-1endifd2 = close < Average[50](close)shortentry= d2limitsell=ExponentialAverage[5](high)if not onmarket and crossflag=-1 and shortentry and mac4flag=-1 and upperflag=-1 then //and time >=StartTime and time <LimitEntrytime and not exclude thensl=AverageTrueRange[14]*2tp=sl * 1.5Set target profit tp//slpriceshort = close + slslpriceshort = sl - close // 07-02-2022,PS, Must be like this.//Set stop loss slrisk=100/sl/10000 // Mike, Watch out for the /10000 (re my contract size).Sellshort risk PERPOINT AT limitsell limitendifif onmarket thencrossflag=0endifOnce CountL = 0Once CountS = 0if longonmarket and close < slpricelong then // >//sell at Close - sl stop // This does not work out in the middle of a bar.SellShort risk PERPOINT AT Close + sl limit // This works out in the middle of a bar.LongDone = 1CountL = CountL + 1endifif shortonmarket and close > slpriceshort then // <//exitshort at Close + sl stop // This does not work out in the middle of a bar.BUY risk PERPOINT AT Close - sl limit // This works out in the middle of a bar.ShortDone = 1CountS = CountS + 1endifGraph sl coloured (255,255,0)Graph tp coloured (0,255,255)Graph CountLGraph CountS coloured (255,0,0)1 user thanked author for this post.
02/11/2022 at 1:19 PM #188062….How can the below code be modified so the “if longonmarket” and “if shortonmarket” instructions can sell or exitshort without it being a stop?…
you can’t, as when IG receives your exit from a losing position, it can’t be but a stoploss and charges you accordingly.
02/11/2022 at 1:33 PM #188068….How can the below code be modified so the “if longonmarket” and “if shortonmarket” instructions can sell or exitshort without it being a stop?…
you can’t, as when IG receives your exit from a losing position, it can’t be but a stoploss and charges you accordingly.
But that isn’t true. I’ve used code for two years that exits a losing position without hitting a guaranteed stop. It is this:
Exit code12345678if longonmarket thenslprice = tradeprice - slif longonmarket and close < slprice thensell at marketendifendifset stop loss 350You can definitely “sell at market” on a losing position without hitting a stop… I’m already doing it. The “set stop loss” above at 350 is never hit during normal trading – it is only there as an emergency. But I want to find a way to sell/exit during a bar. Maybe there is a way of doing a sell/exit on close on a lower timeframe? Like a 1 minute chart? But I’m not quite sure how I’d link an exit rule between two timeframes.
02/11/2022 at 1:48 PM #188072you can’t, as when IG receives your exit from a losing position, it can’t be but a stoploss and charges you accordingly.
I can’t judge that, because my StopLoss never gets hit. 😉
And you both didn’t miss my last post ?
02/11/2022 at 2:18 PM #188079But Peter, the code you’ve submitted has same problem as before. There are no losing trades!!
This is not what I want. In a 5-year backtest, there is a long trade which earns 35 pips which lasts nearly two years. Not only is that a massive risk if the market keeps going down, but even if in the end it comes good and you close at a 35 pip profit, you’ve spent two years burning your margin for a tiny gain.
The point of my system is that it’s day trading with small stops and small gains. It’s little nibbles in the market with a win rate of less than 50%. It works perfectly fine as a principle. I don’t want to avoid losing exits – I just simply want to avoid paying an extra commission for losing exits.
02/11/2022 at 2:50 PM #188083Yes, they are not PENDING stop orders.
I probably misunderstood your question as I thought you wanted to use pending orders, instead of orders at market.
02/11/2022 at 3:12 PM #188089Yes, they are not PENDING stop orders.
I probably misunderstood your question as I thought you wanted to use pending orders, instead of orders at market.
Roberto,
Is there a way to code an exit rule on a 1-minute timeframe to apply to an entry on a 30 or 75-minute timeframe?
02/11/2022 at 3:48 PM #188093But Peter, the code you’ve submitted has same problem as before. There are no losing trades!!
Mike, then you have a setting different than I have. 100s of losing orders. Please look back in post 187856.
https://www.prorealcode.com/topic/sell-at-market-instead-of-hitting-guaranteed-stop/page/3/#post-187856
(this link probably won’t work, but you will find it on the previous page)The screenshots in there are from this very code (though in that post I compared the two lines of exit, Pending Stop vs Buy Limit).
I can’t do more than tell you to work hard to get this working on your side. It really works over here (PRT-IG).
Ask ahead if you want to know something.
I assume you took care of that “/10000” I mention in the comment in the code. If that is wrong (not in balance of some kind), nothing may work.Please remember what I said earlier today : there seems to be something wrong with the Buy Limit lines from your code. Not that the commands are wrong, but the Limit orders don’t work out as should; not in my own code where I now applied the same.
PS: Below is a repeat of a screenshot from that post on the previous page. That would be something like 200 losses.
1 user thanked author for this post.
02/11/2022 at 5:03 PM #188112Yes Mike Boorman, you can use MTF (Multiple TimeFrame) support to use more than one TF:
123456789Timeframe(30 minute,UpdateOnClose) //or 75 minute (plural is optional)IF MyLongEntryConditions THENBUY 1 CONTRACT AT MARKETENDIF//Timeframe(default)IF LongOnMarket AND MyLongExitConditions THENSELL AT MARKETENDIFYou will have to use a 1-minute chart to run this strategy. It can be also be less than 1 minute or even greater, BUT the higher timeframe (or timeframes) in your code MUST (ALL, if more than one) be a multiple of the TF on the chart (named default TF). In such case you cannot run this code on a 7-minute TF as both 30 and 75 are not multiples of 7.
02/11/2022 at 6:40 PM #188118But Peter, the code you’ve submitted has same problem as before. There are no losing trades!!
Mike, then you have a setting different than I have. 100s of losing orders. Please look back in post 187856.
(this link probably won’t work, but you will find it on the previous page)
The screenshots in there are from this very code (though in that post I compared the two lines of exit, Pending Stop vs Buy Limit).
I can’t do more than tell you to work hard to get this working on your side. It really works over here (PRT-IG).
Ask ahead if you want to know something.
I assume you took care of that “/10000” I mention in the comment in the code. If that is wrong (not in balance of some kind), nothing may work.
Please remember what I said earlier today : there seems to be something wrong with the Buy Limit lines from your code. Not that the commands are wrong, but the Limit orders don’t work out as should; not in my own code where I now applied the same.
PS: Below is a repeat of a screenshot from that post on the previous page. That would be something like 200 losses.
That’s weird!! On my side there are no losing trades.
I eliminated the risk issue by just buying/selling at £1 per point. Dividing by 100 works fine for me but to make your code work I took it out.
The only fault I noticed with the take profit was when we added in new rules for the exits on losing positions. So I moved the exits on losing positions down to the bottom of the code and put them inside “if longonmarket/shortonmarket” (which separated them from the take profit positions, which were calculated at entry) and then that worked fine. I notice you have done the same, so I’d be surprised if it’s that issue which is making my results go strange.
Looking at your code, I just can’t see how there could ever be a losing trade, and I’d have said that even if I hadn’t generated my results. On the “longonmarket” conditions for example, the action that is taken when they are met is to “sellshort” (which is to buy a short position). And on “shortonmarket” the action is to “buy” (which I thought could only mean to open a long position?). Have I misunderstood what “sellshort” and “buy” mean? Can they be used to exit as well?
02/11/2022 at 8:43 PM #188120Yes Mike Boorman, you can use MTF (Multiple TimeFrame) support to use more than one TF:
123456789Timeframe(30 minute,UpdateOnClose) //or 75 minute (plural is optional)IF MyLongEntryConditions THENBUY 1 CONTRACT AT MARKETENDIF//Timeframe(default)IF LongOnMarket AND MyLongExitConditions THENSELL AT MARKETENDIFYou will have to use a 1-minute chart to run this strategy. It can be also be less than 1 minute or even greater, BUT the higher timeframe (or timeframes) in your code MUST (ALL, if more than one) be a multiple of the TF on the chart (named default TF). In such case you cannot run this code on a 7-minute TF as both 30 and 75 are not multiples of 7.
Is it possible to backtest more than 1 million bars? I had an idea that maybe it was, but my version of PRT won’t let me do more than 1 million.
02/11/2022 at 11:01 PM #1881241M units are only available on IG’s real accounts, not demo ones.
02/12/2022 at 5:10 AM #188128So I moved the exits on losing positions down to the bottom of the code and put them inside “if longonmarket/shortonmarket” (which separated them from the take profit positions, which were calculated at entry) and then that worked fine. I notice you have done the same
Hi Mike,
So it must be my conclusion that you did not try my code ?
I copied back the whole code for a reason. I applied several changes in there. And if this is only a snippet of your larger code, then later you can see how to integrate it. OK ?On the “longonmarket” conditions for example, the action that is taken when they are met is to “sellshort” (which is to buy a short position). And on “shortonmarket” the action is to “buy” (which I thought could only mean to open a long position?). Have I misunderstood what “sellshort” and “buy” mean? Can they be used to exit as well?
My earlier answers in this topic were exactly about that. Sadly I made mistakes myself (not reading accurately on the Pending Stop). And of you don’t understand, no worries, I see no others who understand (which does not mean they don’t exist). As I tried to explain – I use the IB version with its way larger possibilities for orders, so one learns the possibilities, which learning is not possible with the IG.
Let me add that you can see during the evolvement of your topic, that I myself only had the idea that what now works should be possible. Thus, pure theory. However, springing from manual trading in PRT-IB. And now the theory is practice.On a not unimportant side note to myself, but possibly interesting for others :
I use 1 the second timeframe. This allows for one month of history only (1M bars). If I, for example, make 2 seconds of that, nothing works any more (all is consistent for the 1 second TF). But also, if I would make it 2 second (or a bit more) I will always be too slow with whatever action. Now look at your own 75 minute vs 225 minutes. You have the 75 for the exact same reason, BUT you are unsatisfied with the SL response. With my code, this response is “instantly” which a Set StopLoss also would do. In the end exactly what you asked for. But also in the end exactly what my solution will be to use more history (like 2 months).Now try my code and let me know what comes from it. Don’t forget about the /10000 which you probably just have to remove. It is a bit tough for me to give you a consistent version because of the contract size on one hand, but the mistakes you provided in the code on the other (you used pips of profit/sl instead of their respective targets (see the Close + SL and Close – SL parts). Then nothing works but it coincidentally could with a smaller contract size. Might it help, the contract size I used was IIRC 10000 (AUD/USD Mini). And, the vagueness (for me !) is about the number of units being unknown (you calculate that). In aftermath, I think I could see that your TP’s were 10 times smaller than mine, implying that my contract size was 10x higher.
Let me know ! 🙂
-
AuthorPosts
Find exclusive trading pro-tools on