When examining some problems with my ProOrder code, I found some very strange behaviour using the ONCE statement. I have shortened those problems to some demonstrative examples.
First example:
1
2
3
4
5
6
oncei=0
ifi=1then
buy1contractsatmarket
else
sellshort1contractsatmarket
endif
Expected behaviour: Generating only short orders.
Real behaviour: Generating only short orders.
That is ok
Second example, adding the line i = 1:
1
2
3
4
5
6
7
oncei=0
ifi=1then
buy1contractsatmarket
else
sellshort1contractsatmarket
i=1
endif
Expected behaviour: First should be a short order, after that only long orders.
Real behaviour: Generating only long orders.
That is not ok. Why is the first sellshort statement skipped although i is not equal to 1?
Third example, adding the line i = 0:
1
2
3
4
5
6
7
8
oncei=0
ifi=1then
buy1contractsatmarket
i=0
else
sellshort1contractsatmarket
i=1
endif
Expected behaviour: Alternating short and long orders, starting with a short order.
Real behaviour: Alternating short and long orders, starting with a short order.
That is ok again.
Fourth example, something with a counter:
1
2
3
4
5
6
7
oncei=0
ifi<3then
buy1contractsatmarket
else
sellshort1contractsatmarket
endif
i=i+1
Expected behaviour: First 3 orders are long orders, after that only short orders.
Real behaviour: Generating only long orders
That is not ok again.
You can try those examples on any market in any timeframe. The result is the same every time.
My conclusion is: The ONCE statement does not work correct. Or did I overlook something? Can anyone provide a clue what went wrong here?
Oh yes! I was not aware that the code is being executed even before the startdate of the strategy. Therefore your advice certainly clarifies the situation. Many thanks for that, Nicolas.
Unfortunately, it does not solve my issue completely. After including the defparam statement, I would expect 3 buy orders in the fourth example (i = 0 / 1 / 2). But I only get 2 buy orders before the system moves to trigger sellshort orders. I have shown this in attachment Once1. And the second example does not work at all. So it seems, that the first round of calculation still happens before starting the strategy. And I am not able to guess why. Could you be of help once again?
P.S.: When changing the starttime of the strategy to 00:00, the result does make me mad. Why is it starting 2 days earlier? See attachment Once2.
To help us continually offer you the best experience on ProRealCode, we use cookies. By clicking on "Continue" you are agreeing to our use of them. You can also check our "privacy policy" page for more information.Continue