ProRealCode - Trading & Coding with ProRealTime™
I am still convinved you did not even test my code, which is doing exactly what you want. It places a pending STOP order if the current Price is ABOVE or BELOW the exit price, otherwise: a) the STOP has already been hit or b) you need to place a LIMIT order, but this is not a stop loss, it’s a profit target!
Oops, I did not read this prior to my last post. Okay …
It is all about your b) I was just talking about and that this just should work. But also :
It is all not so easy to follow and I tend to think that you are right. My hoopla is not necessary. Still I could wonder why I am using it all the time, manually. … While it is not even about that.
Roberto, for example :
if longonmarket and close > longexit1 then
sell at longexit1 stop
endif
This is part of your suggested code. But this really will execute at the close of the bar. It is, however, easy to think that it will do what you want, from the perspective of manual trading, which is unrelated to the TimeFrame (!). I just put a Stop somewhere, and you would be right. But with the Strategy code you can’t put a stop “just somewhere” … unless you apply the Limit (price) somehow. Well, that is what I tried to do.
If I am incorrect so be it. I mean, if your example works after all, then indeed I did not try it myself. This would be because I would not be able to determine LongExit1 in this case. Besides, my mind tells me that it should not happen after the close of (mind you) the next bar. It should happen IN the next bar.
I am still convinved you did not even test my code, which is doing exactly what you want. It places a pending STOP order if the current Price is ABOVE or BELOW the exit price, otherwise: a) the STOP has already been hit or b) you need to place a LIMIT order, but this is not a stop loss, it’s a profit target!
Hello Roberto. Thank you for trying to help me.
Your code works technically but creates much worse results, because the position can get worse in a 75-minute bar if I have to wait for close to calculate the exit. Your suggested code is still based on my original “if longonmarket and close > longexit1″ which means it doesn’t do what I want it to do. How can I start this instruction without using “close” as the trigger for the exitshort and the sell?
I want to exit the trade when price hits my predefined stop position of ATR * 2. I can do this with “set stop loss”, but if I do that, I will have to pay the fee for guaranteed stops each time it is hit. I want to use the “set stop loss” command for a much bigger emergency stop, and use “exitshort” and “sell” to exit 99% of my losing trades. So I want “exitshort” and “sell” to trigger when price hits the ATR * 2 figure (that’s ATR * 2 at the START of the trade – it does not update during the trade), and I want this to happen during a bar, not on close. To put it another way, I want to treat it like a take profit target for a losing position, so it’s effectively a “take loss” target. Do you know how to do this?
Now we are all cross posting. 🙂
To put it another way, I want to treat it like a take profit target for a losing position, so it’s effectively a “take loss” target. Do you know how to do this?
Without a long-winded post this time :
risk=100/sl
SellShort risk PERPOINT AT limitSTOP limit // Mind the limitSTOP.
SellShort risk PERPOINT AT limitSTOP limit
That doesn’t work. To have two conflicting entry criterias next to each other was always going to be a problem. If I understand you correctly, you’re proposing that underneath my take profit criteria on a long trade, I would put a loss criteria underneath like this?
BUY risk PERPOINT AT limitbuy limit
SellShort risk PERPOINT AT sl limit
you’re proposing that underneath my take profit criteria on a long trade
That would not be Take Profit criteria (for the code example you just gave); that would be Buy criteria. But even Take Profit Criteria (Limit) would work together with the Buy Limit and Sell Short Limit. I do this some times (IB).
That doesn’t work. To have two conflicting entry criterias
They are not conflicting at all ? they would if the Limit keyword is omitted.
What is the (error-)message you receive from it ?
The use of CLOSE in my code has nothing to do with waiting for the closing of the bar. Its purpose is just to make sure it’s a STOP order.
Pending orders last a whole bar and can get triggered while the new candle is forming.
The use of CLOSE in my code has nothing to do with waiting for the closing of the bar. Its purpose is just to make sure it’s a STOP order.
Pending orders last a whole bar and can get triggered while the new candle is forming.
Roberto,
Once I remove my original “set stop loss” and rely entirely on exiting trades using your code, I get incorrect results (see screengrab). As a reminder, here is the code in full.
DEFPARAM CumulateOrders = False
Starttime = 000000
Limitentrytime = 220000
Exclude=time >=020000 and time <=050000
timeframe(225 minutes,updateonclose)
if MACD[12,26,9](close) >0 then
mac4flag=1
endif
if MACD[12,26,9](close) <0 then
mac4flag=-1
endif
if close >ExponentialAverage[5](high) then
upperflag=1
endif
if close <ExponentialAverage[5](high) and close >ExponentialAverage[5](low) then
upperflag=0
endif
if close < ExponentialAverage[5](low) then
upperflag=-1
endif
timeframe(75 minutes,default)
Averagecrosslong= Average[8](close) CROSSES OVER Average[20](close)
Averagecrossshort= Average[8](close) CROSSES under Average[20](close)
ONCE crossflag=0
ONCE avgflag=0
if close > ExponentialAverage[5](high) then
avgflag=1
endif
if close < ExponentialAverage[5](low)then
avgflag=-1
endif
longentry=0
if avgflag=1 and averagecrosslong then
crossflag=1
endif
c2 = close > Average[50](close)
longentry= c2
limitbuy=ExponentialAverage[5](low)
if not onmarket and crossflag=1 and longentry=1 and mac4flag=1 and upperflag=1 and time >=StartTime and time <LimitEntrytime and not exclude then
sl=AverageTrueRange[14]*2
tp=sl * 1.5
slpricelong= close - sl
//Set stop loss sl
risk=100/sl
BUY risk PERPOINT AT limitbuy limit
endif
shortentry=0
if avgflag=-1 and averagecrossshort then
crossflag=-1
endif
d2 = close < Average[50](close)
shortentry= d2
limitsell=ExponentialAverage[5](high)
if not onmarket and crossflag=-1 and shortentry and mac4flag=-1 and upperflag=-1 and time >=StartTime and time <LimitEntrytime and not exclude then
sl=AverageTrueRange[14]*2
tp=sl * 1.5
Set target profit tp
slpriceshort = close + sl
//Set stop loss sl
risk=100/sl
Sellshort risk PERPOINT AT limitsell limit
endif
if onmarket then
crossflag=0
endif
if longonmarket and close > slpricelong then
sell at sl stop
endif
if shortonmarket and close < slpriceshort then
exitshort at sl stop
endif
@ Mike Boorman
Your code is executed at the end of each bar, suppose that at the end of a certain bar your conditions are true so you are LongOnMarket and Close > LongExit1 (LongExit1 = SLPrice = Close – ATR[14]*2) then Roberto’s code will place a STOP order on LongExit1.
This STOP order is a PENDING order that starts at the beginning of the new bar and is valid until the end of that bar. When the Close hits the pending Stop order IN this bar, it is executed (equivalent to a StopLoss).
If the Close does not touch the pending Stop order, it will be placed again every next bar (if your conditions are still true).
If the Close does not touch the pending Stop order, it will be placed again every next bar (if your conditions are still true).
Hi JS – Maybe you can rephrase that. Because it is there where it IMHO goes “wrong”. Why would a close not (or do) “touch” a pending stop order ? no such thing exists. And just saying : a pending stop order touching a close also does not exist. There is not even such a thing as a pending stop order unless
We are talking about #3 and nothing else that I can see. And this is the same as you putting a Stop Order manually, which has a price limit attached to it. There is nothing else for “pending stop” … (compare #1 and #2).
See the two subsequent attachments. I take it that you will agree that this is a Stop order. I place it at, well, a PRICE. Right ? at 1.14509. This makes it a Limit order to begin with.
This is not comparable at all to a Stop (without Limit) order in ProRealCode. That would make it a Market order, which for ProRealCode implies “Execute right at the start of the next bar” (when my code is handed to the broker system) and where it will wait to get filled. Net this means :
If I place a Stop Order in my code, and I will do that when my whatever conditions are met, it WILL get filled, unless nobody wants to take my (market) price. But that not assumed, it will get filled during the execute of the next bar, most probably within tenths of seconds.
What I would want, no wait, what Mike wants (ne coincidentally heading for the very same), is that the order is only given to the exchange (yep) when a certain price is met. Thus, my code is handed to the broker, but it didn’t tell to sell (when Long). It only asks to sell when a price will be met. And if that does not happen in the next bar, then I happily own the stock (etc.) still. And THEN I might like to kill the Limit Order. Btw, manually I would kill in the middle of the bar (cross away at any random time), but with PRC that is not possible again. But beware, because with the same (so far according to Mike conflicting orders/techniques) I would be able to do that too. Admitted, all still theories.
Something which would count for me surely :
If we don’t (and equally) fully understand how the PRT software interacts with the “candle system”, and when the code is executed and when it is executed again, and what can happen meanwhile within the time (of the candle) that our PRC code is not in control …
… then it is very hard to make something of this and discuss it.
but
Applying dozens of trades daily, which is not scalping at all, done in an environment which allows to learn the possibilities of PRT on one side and one of the broker systems on the other, and using a broker system with a plethora of possibilities to begin with and which is not IG …
… helps. Well, I think it does. 🙂
Below the current possibilities of PRT, which is a subset of what IB allows. Well, partly, because PRT adds “soft” orders as well. And just saying : what a broker allows for is almost all “soft” only, but a platform has to comply to that. … Within that, trickery is possible because most comprises of elements and often several elements can be applied subsequently, without they know about each other. Example : a Limit order to Buy and a Limit order to SellShort at the very same time. Which executes first is to wait and see, but if the SellShort is fist and put above the Buy (Long) they will execute both if my expectation of the market is right (price drops, but at a certain level it will retrace). So I executed two orders while I wasn’t there (both Limit orders), both set in advance. I will end up with a Long, after having earned on the Short.
This latter is a part of my perception of the system as a whole. In the earlier post I told I could not do that in IG, probably because of wrong settings somewhere. But in IB I do this regularly, especially which all is swinging and I have nothing to do but having a bit of fun with certainty. Now, if you can’t copy this way of trading (or are sure it can’t exist) then don’t expect that I can make clear what, well, I like to make clear. 🙂
If you miss some trade buttons in IG settings, it does not mean that it can’t be done by PRC code.
Ready to learn more, really …
Peter
PS: JS, see the last line in the attachment. I deliberately copied that in. That looks like what you were saying. But is it what you referred to ? (again, ready to learn more)
Hi Peter,
You can see a pending STOP order as a temporary StopLoss that is valid for 1 bar.
Just like with a StopLoss, this pending stop order does not have to be hit immediately (preferably not at all) but if it has not been hit then it must be placed again because pending stop orders are only valid for 1 bar.
So, if at the end of a certain bar your conditions are true, then with this pending stop order you place a kind of temporary StopLoss which is valid for the new (next) bar.
Hi JS,
Sincere thanks.
But how do I create such a pending stop order ?
Manually ? (special button ?)
Via code ?
@ Mike Boorman
Your code is executed at the end of each bar, suppose that at the end of a certain bar your conditions are true so you are LongOnMarket and Close > LongExit1 (LongExit1 = SLPrice = Close – ATR[14]*2) then Roberto’s code will place a STOP order on LongExit1.
This STOP order is a PENDING order that starts at the beginning of the new bar and is valid until the end of that bar. When the Close hits the pending Stop order IN this bar, it is executed (equivalent to a StopLoss).
If the Close does not touch the pending Stop order, it will be placed again every next bar (if your conditions are still true).
Okay, so what is it in the code that means that Roberto’s conditions are hit immediately for shorts (no trade of over one bar), and almost never hit for longs? (there’s a trade of over 1,000 bars, for example, which should only be 42). You can see from my screengrab the list of trades which shows the unreliable results. Three or four of those results do actually mirror what they were when I used the “set stop loss” command to execute the same exit conditions, but the great majority do not. Would you be so kind as to help me fix this so the stop order mirrors the stop loss more accurately?
DEFPARAM CumulateOrders = False
Starttime = 000000
Limitentrytime = 220000
Exclude=time >=020000 and time <=050000
timeframe(225 minutes,updateonclose)
if MACD[12,26,9](close) >0 then
mac4flag=1
endif
if MACD[12,26,9](close) <0 then
mac4flag=-1
endif
if close >ExponentialAverage[5](high) then
upperflag=1
endif
if close <ExponentialAverage[5](high) and close >ExponentialAverage[5](low) then
upperflag=0
endif
if close < ExponentialAverage[5](low) then
upperflag=-1
endif
timeframe(75 minutes,default)
Averagecrosslong= Average[8](close) CROSSES OVER Average[20](close)
Averagecrossshort= Average[8](close) CROSSES under Average[20](close)
ONCE crossflag=0
ONCE avgflag=0
if close > ExponentialAverage[5](high) then
avgflag=1
endif
if close < ExponentialAverage[5](low)then
avgflag=-1
endif
longentry=0
if avgflag=1 and averagecrosslong then
crossflag=1
endif
c2 = close > Average[50](close)
longentry= c2
limitbuy=ExponentialAverage[5](low)
if not onmarket and crossflag=1 and longentry=1 and mac4flag=1 and upperflag=1 and time >=StartTime and time <LimitEntrytime and not exclude then
sl=AverageTrueRange[14]*2
tp=sl * 1.5
slpricelong= close - sl
//Set stop loss sl
risk=100/sl
BUY risk PERPOINT AT limitbuy limit
endif
shortentry=0
if avgflag=-1 and averagecrossshort then
crossflag=-1
endif
d2 = close < Average[50](close)
shortentry= d2
limitsell=ExponentialAverage[5](high)
if not onmarket and crossflag=-1 and shortentry and mac4flag=-1 and upperflag=-1 and time >=StartTime and time <LimitEntrytime and not exclude then
sl=AverageTrueRange[14]*2
tp=sl * 1.5
Set target profit tp
slpriceshort = close + sl
//Set stop loss sl
risk=100/sl
Sellshort risk PERPOINT AT limitsell limit
endif
if onmarket then
crossflag=0
endif
if longonmarket and close > slpricelong then
sell at sl stop
endif
if shortonmarket and close < slpriceshort then
exitshort at sl stop
endif
Hi Peter,
Yes, via code…
if longonmarket and close > longexit1 then
sell at longexit1 stop
endif
Oh boy, it only now appears that I can’t even read.
sell at longexit1 stop
why did I read this all the time as
Set Stop Loss
of any form ? I really have no clue. But stupid me, mea culpa, apologies, anything.
Massive thanks JS for holding on. Roberto, you too of course.
No excuses, but I think I can see how I made the mistake of not reading well (and with that missing all explanations on top of it, like “when the stop is touched” etc. – grrr) :
If you’re still interested, Ad 3.:
Which for me confuses and obfuscates everything, because the Limit orders are exactly not working out for me on IG. So my focus was merely on how to let that work, instead of, well, READING ?
duh
In addition I don’t think I ever investigated the Sell at n Stop. But now I know …
🙂
… And I suppose I can just as well use that, never mind it should work out the same as my proposed Limit. That is … Let’s first try to solve Mike’s problem. If I am not too much disturbing (stoorzender for JS).
Sell at market instead of hitting guaranteed stop
This topic contains 61 replies,
has 6 voices, and was last updated by PeterSt
4 years ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 02/04/2022 |
| Status: | Active |
| Attachments: | 20 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.