but never again later, even when the system is “not onmarket” ?
So, I would like to open a position immediately after the start of a trading system (in demo mode), only once at the end of the first bar.
I tried :
ONCE flag = 0
If flag = 0 then
buy 1 contract at market
flag = 1
endif
but unfortunately this does not work and it does not open any position at any time.
This works :
If not onmarket then
buy 1 contract at market
endif
It buys 1 contract immediately after the start of the system, at the end of the first bar. But this code would also buy later on again, when there is no position open, not only once.
The simplest code
ONCE buy 1 contract at market
is not allowed and recognized as a syntax error.
My rationale is that I would like to restart a trading system that has been stopped erroneously, by some external interaction or error, for example.
So, when I know that the normal regular system is currently long, but the long position has been erroneously closed because the system stopped, I would like to restart the system and open this long position immediately at the end of the first bar and treat it as the regular long position that was open before. But of course, such a “manual” position should never be opened later on again, and the system should just continue on its regular path. Has anyone any ideas ? Or do I not see the simplest solution ?
At the close of the first bar then barindex = 1 so work barindex into the code somehow?
Also at barindex = 1 then tradeindex = 1 but never will again.
I tried something with “If barindex = 1”, but it did not work. Maybe in conjunction with tradeindex ? I will try. Thank you, GraHal.
Also, I am not sure that barindex = 1 in the very first bar, because I think that the pre-loaded bars are also counted.
At the close of the first bar then barindex = 1 so work barindex into the code somehow? Also at barindex = 1 then tradeindex = 1 but never will again.
Haha, the following works :
defparam preloadbars = 1000
If barindex = 1000 then
buy 1 contract at market
endif
This opens a position in the DAX 1 minute chart immediately after the start of the system ! Thank you, GraHal, again !
This works, but you have to exit the next bar!
ONCE Flag = 0
if OnMarket then
sell at market
flag = 1
endif
If not OnMarket and Flag = 0 then
buy 1 contracts at market
endif
This works, but you have to exit the next bar!
Well, this is what I don’t want, I want to treat the position just as a regular position of a complete trading system. Maybe with the barindex trick, it will work.
Verdi55 are you going to work the code so it caters for where a Long or a Short was the Trade type when external interaction or error stopped the system?
I think the problem are the pre-loaded bars. The reason why
ONCE flag = 0
If flag = 0 then
buy 1 contract at market
flag = 1
endif
does not work, is that the ONCE commands are not executed at the beginning of the trading system, but at barindex = 1, which is the first of the pre-loaded bars in the past.
Verdi55 are you going to work the code so it caters for where a Long or a Short was the Trade type when external interaction or error stopped the system?
Yes. One of my demo systems was stopped today (or I stopped it erroneously because I used the new IG platform I was not familiar with), with a short position open and a gain of 145 points. So I wanted to restart the whole system 5 minutes later just by adding some lines at the beginning, saying, please open this short position immediately again. But it did not. I’ll try again with the barindex commands, I think it should work then.
I think the problem are the pre-loaded bars. The reason why
|
|
ONCE flag = 0
If flag = 0 then
buy 1 contract at market
flag = 1
endif
|
does not work, is that the ONCE commands are not executed at the beginning of the trading system, but at barindex = 1, which is the first of the pre-loaded bars in the past.
I think even the lines
If flag = 0 then
buy 1 contract at market
flag = 1
endif
are being executed on the pre-loaded bars before the start of the trading system. This is why flag = 1 is always true before the first real bar, and so no position is ever opened.
We need to know the number of preloaded bars and count the first bar after opening as this number.
LeoParticipant
Veteran
What if you use a condition like
if CurrentMinute=time then
It will cheating the preload bars.
So the system now opened a new short position immediately. But I forgot to change the stops and limits, because of course the entry price will be different from the regular original position. It is a little more complicated.
What if you use a condition like
|
|
if CurrentMinute=time then
|
It will cheating the preload bars.
good idea, thank you. Needs to be combined with today’s date, too, and time must be the time of the first bar.
LeoParticipant
Veteran
Needs to be combined with today’s date, too, and time must be the time of the first bar.
not at all, you need to create a varible, like robertogozzi ‘s code.
CurrentMinute=time will be true for every bar except the preloaded bars