ProRealCode - Trading & Coding with ProRealTime™
But if I use ProOrder then I don’t have to worry if the program closes or I loose internet connection right?
Correct.
Possibly Vonasi didn’t notice this question :
There were a lot of questions Vonasi noticed but didn’t answer in the hope that others would do so first. Here is possibly the answer to another one of them.
If there is no pre-market and so no pre-market candles then looking for one at 085900 isn’t going to find one even on a minute chart. The first candle that meets the criteria is the TIME = 090100 or OPENTIME = 090000 candle and so to try to enter as early as possible we really should be using the 1 second chart.
You will need to set the closetradetime for as close to the market close as possible and for when you know there will likely be a candle. Then if you run it on the 1 second time frame you can enter 1 second after the market has opened and exit 1 second before the market closes – as long as there are candles! It is best to give yourself a bigger window on the exit time as on low liquidity stocks that might have lots of missing candles and so you could fail to find one to close the trade on otherwise.
The flag condition stops the strategy from opening a trade ever again.
Not tested.
opentradetime = 090000
closetradetime = 212959
if longonmarket and flag and time >= closetradetime then
sell at market
endif
if not longonmarket and opentime >= opentradetime and not flag then
buy 15000 cash at market
flag = 1
endif
There were a lot of questions Vonasi noticed
Vonasi, great stuff as I did not dare to come up with an answer myself (see below).
Don’t forget the
Once flag
Why didn’t I dare to come up with an answer like this/yours ? well, because I started to wonder what therookie actually wants to achieve with this. Not that this is my business, but I got intrigued somewhat. Actually my stance is : would it work (with the suggestion of Not).
Before I forget, we might get a better grasp of what’s at task when therookie would not use a 1 second chart, but a 1 tick chart (which obviously would be a total waste of server bandwidth for the whole day, apart from two ticks).
Or maybe 1 tick just does not work … 😉
See the attachment.
N.b.: I think I read somewhere that therookie wants to trade stocks like this; Notice that this is a 1 tick chart and see that it ran into a sheer 9 ticks from the opening (08:00) till 10:00. Now :
What would we have gained when this stock would not be in our possession at the opening ? I say : nothing. By now we actually will have lost on it. Still *I* gained 9% on it today (see the 8.20% in the right hand border). … If I only had the stock at yesterday’s last bar …
So whatever it is what therookie really wants with this strategy, I think it is applied “wrongly” (between large quotes). Only if a first tick stays out for a while, it would work (quite similar for the first second). But it won’t stay out …
Personally I think it actually *is* a strategy to work like therookie possibly intends; Be there at the open, bail out at the close. If you only can expect what will happen … (as usual – haha).
Might I be correct in that therookie actually wants what I propose, then the solution is simpler (and even decent, I think) : Buy at the opening of the day’s last bar, sell at the day’s one but last bar (with a 1 second chart).
ONCE FLAG is not needed. All variables are defaulted to equal zero until a different value is declared. ONCE is only needed if you want a variable to start with a different value than zero from the first bar of a chart.
Does this seem correct to you guys as an entry? FYI I will only use daily bars.
There is no 085900 if you use a daily chart. You will have to trade on the one minute time frame to find a bar with a time of 085900.
Ok, my understanding was that it would buy the next bar at market as soon as it opens with this code but I assume this is wrong. The logic in Easylanguange would be something like “if time > 0859 then buy next bar at market”. This would make Tradestation buy the next bars open (09.00) even on daily bars and even if there is no bar at 8.59. So if I understand you correctly there is no way to do this in a simple way with daily bars on PRT?
And also using minute bars wouldn’t really help because there is no bar at 08.59 as the first bar only appears at 09.00.
There were a lot of questions Vonasi noticed
Vonasi, great stuff as I did not dare to come up with an answer myself (see below).
Don’t forget the
1 Once flagWhy didn’t I dare to come up with an answer like this/yours ? well, because I started to wonder what therookie actually wants to achieve with this. Not that this is my business, but I got intrigued somewhat. Actually my stance is : would it work (with the suggestion of Not).
Before I forget, we might get a better grasp of what’s at task when therookie would not use a 1 second chart, but a 1 tick chart (which obviously would be a total waste of server bandwidth for the whole day, apart from two ticks).
Or maybe 1 tick just does not work … 😉
See the attachment.
N.b.: I think I read somewhere that therookie wants to trade stocks like this; Notice that this is a 1 tick chart and see that it ran into a sheer 9 ticks from the opening (08:00) till 10:00. Now :
What would we have gained when this stock would not be in our possession at the opening ? I say : nothing. By now we actually will have lost on it. Still *I* gained 9% on it today (see the 8.20% in the right hand border). … If I only had the stock at yesterday’s last bar …
So whatever it is what therookie really wants with this strategy, I think it is applied “wrongly” (between large quotes). Only if a first tick stays out for a while, it would work (quite similar for the first second). But it won’t stay out …
Personally I think it actually *is* a strategy to work like therookie possibly intends; Be there at the open, bail out at the close. If you only can expect what will happen … (as usual – haha).
Might I be correct in that therookie actually wants what I propose, then the solution is simpler (and even decent, I think) : Buy at the opening of the day’s last bar, sell at the day’s one but last bar (with a 1 second chart).
I am not sure I followed everything 🙂
Just to be clear I will explain again what it is I want to do and why. Observe that it doesn’t really matter to me if I use a daily bar or 1 minute bar or tick bars, as long as it buys as close to the open as possible.
So I have a strategy that I have run on various stocks quite successfully for some time. I get a buy signal on my trading platform (not PRT) in the evening after the market closes and I take the trade manually in the morning at the open and then I keep it for 24 hours that is to say that I then manually close the position on the open the next day. Now my problem is that I sometimes get many buy signals and then I have to manually close the previous days positions and then one by one buy the stocks. Now as you understand this can take 1 or 2 minutes and by then the entry price can be quite different from my back testing results. I know the difference will not be huge and I am still profitable but I wanted to see if I could get live results that are closer to backtesting results and quite also decrease my stress of having to take many trades during a very short period of time and maybe missing the opening.
Hope this explains why I want to do this 🙂
And also using minute bars wouldn’t really help because there is no bar at 08.59 as the first bar only appears at 09.00
if time = 090000
And also using minute bars wouldn’t really help because there is no bar at 08.59 as the first bar only appears at 09.00
if time = 090000
Yeah I guess this should work but since this is the CFD market then sometimes the opening can be delayed and at precisely 090000 there might not actually be a bar. Would it still work if the actual data comes in at lets say 090010?
Possibly Vonasi didn’t notice this question :
There were a lot of questions Vonasi noticed but didn’t answer in the hope that others would do so first. Here is possibly the answer to another one of them.
If there is no pre-market and so no pre-market candles then looking for one at 085900 isn’t going to find one even on a minute chart. The first candle that meets the criteria is the TIME = 090100 or OPENTIME = 090000 candle and so to try to enter as early as possible we really should be using the 1 second chart.
You will need to set the closetradetime for as close to the market close as possible and for when you know there will likely be a candle. Then if you run it on the 1 second time frame you can enter 1 second after the market has opened and exit 1 second before the market closes – as long as there are candles! It is best to give yourself a bigger window on the exit time as on low liquidity stocks that might have lots of missing candles and so you could fail to find one to close the trade on otherwise.
The flag condition stops the strategy from opening a trade ever again.
Not tested.
1234567891011 opentradetime = 090000closetradetime = 212959if longonmarket and flag and time >= closetradetime thensell at marketendifif not longonmarket and opentime >= opentradetime and not flag thenbuy 15000 cash at marketflag = 1endif
Thank you I try this and play around with this code!
Each candle on a chart has two times. OPENTIME which is the opening time of the candle and TIME which is the closing time of the candle. So a daily candle cannot have an OPENTIME or TIME of 085900 because it does not open or closeat this time.
This is why I told you to use a 1 second time frame. If there are no pre-market candles then there is also no candle with with a time of 085900 but you can get in damned close to the opening price by sending an order to open a trade on any candle with an OPENTIME greater than 085959. So if the first candle is OPENTIME = 090000 then the order will be sent and the trade opened at the next candle. This would be at 090001 or later if there is not a candle at 090001. This is as close as you will get to buying at the open.
As someone else said you could just send an order at the close of the daily candle that would open at the open of the next time which will be fine if the open time is the open of the market.
Put the following indicator on your daily chart to see what TIME and OPENTIMES your chosen instrument has.
return opentime as "opentime", time as "time"
If you want to close mid daily candle then you have no choice but to use MTF but as I said there is a risk that if you choose to exit at say TIME = 213000 and there is no one second candle at 213000 and then there is not another candle until the next day then you will stay on the market overnight. So it is better to start looking for candles that are perhaps greater than 212930 and then you will get out as soon as there is a candle in the last 30 seconds before the market closes. Less liquid instruments might need a longer period to look for a candle and so perhaps result in an earlier exit.
therookie – Please try to use quote a little less – it is making the topic very hard to read.
I am not sure I followed everything 🙂
Possibly the most crucial thing you must try to wrap your head around is :
Your code (the whole sequence from top to bottom) will be called (executed) at each new candle. Thus suppose I have a 1 minute chart, then each new minute your code will be called (executed). If you have a 1 second chart, then each new second your code will be called. If you have a one day, chart then ….
And what I said earlier, is that you should not have called your code the first thing of the next day, because most of the profit will have vanished already. Thus :
Investigate Market Orders at the opening of the market. Look at the last closing price and try to follow the opening price; you will see a gap (in 99% of cases). This is because the price changes when the market is closed (very briefly said : because outside the market there’s also influence from buyers and sellers). And thus :
You should buy at the last candle right before the market closes.
Of course your signalling should match this;
I get a buy signal on my trading platform (not PRT) in the evening after the market closes
Thus that would be wrong. You should obtain the signal so early that you are still able to program your autotrade code. Like 17:20 for a market which closes at 17:30. Or as more early as you need the time to make your code(s).
If not clear, just ask. And if this does not fit your ideas about your strategy, then fine ! Also, if in your opinion this can not work because your signalling can only work after-market, then let us know.
OPENTIME which is the opening time of the candle and TIME which is the closing time of the candle
thanks for that Vonasi, I hadn’t realised. But are the two terms used in the same way?
I have a 1 minute algo that I had thought was trading at the opening of the FTSE with ‘time = 080000’
In tests, ‘time = 075900’ is slightly worse, but still turning a decent profit, whereas ‘opentime = 080000’ bombs spectacularly – more than could be explained by any slippage between open and close.
Is something else required?
Think of OPENTIME and TIME as just a label for each candle.
If there is a candle every second then if you send an order at the close of the OPENTIME = 075959 candle then the order will be filled at the open of the next candle which will be at 080000.
If you send the same order at the close of the TIME = 080000 candle then it will be filled at the open of the next candle which will be at 080000 (plus whatever microseconds of slippage there is!)
I have a 1 minute algo that I had thought was trading at the opening of the FTSE with ‘time = 080000’
Your trade will open at 080000.
I just wanted to thank you all for your help. At the end I decided to use alerts with an associated trigger like another member suggested. That seems to be the simplest way to achieve what I want. I will run ProRealTime on my VPS which hopefully is powerful enough to runt both Tradestation and PRT without affecting Tradestation automatic trading.
had one more question but it doens’t belong in this thread so I will open another thread for that question.
Once again, thank you!
Buy/close a position right at open
This topic contains 29 replies,
has 7 voices, and was last updated by therookie
5 years, 1 month ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 12/29/2020 |
| Status: | Active |
| Attachments: | 3 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.