Strange if condition code error
Forums › ProRealTime English forum › ProOrder support › Strange if condition code error
- This topic has 26 replies, 5 voices, and was last updated 7 years ago by
Vonasi.
-
-
06/20/2018 at 11:14 PM #73838
Hi.
I have a problem I don’t understand (there is no logic to the problem and would work in other programming languages like vb or different c-versions).
Below there is a very simple code to show the problem. Everything works fine except the second “trade” with the same code “tradecount = tradecount + 1” which worked fine in the first trade. I started with hard coding the tradecount (tradecount = 1 and then tradecount = 2), but it ended with the same problem that it only works in the first if-code.
/Daniel
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051DEFPARAM CumulateOrders = FalseDEFPARAM FLATAFTER = 172000// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 092500timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 170000timeEnterAfter = time < noEntryAfterTimeset stop ploss 4targetprofit = 5// Clear entry variablesif time = 085000 thentradecount = 0 // Reset daily tradecountfirsttarget = 0 // Reset first target reach variableendif// Conditions to enter long positionscl1 = close > open// Long 1if firsttarget = 0 and tradecount = 0 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = tradecount + 1 // the addon works fine hereendifendif// Long 2if firsttarget = 0 and tradecount = 1 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = tradecount + 1 // the addon does work, but the buy order won't buy before the tradecount addon, but without this line the order buys. So why does the same addon code work the first time but not the second time?endifendifif longonmarket thenif firsttarget = 0 thenif close > tradeprice + targetprofit*pipsize thensell at marketfirsttarget = 1endifendifendif// End06/20/2018 at 11:52 PM #73841I think this post deals with your issue, it’s a question of ONMARKET https://www.prorealcode.com/topic/help-needed-with-code/#post-73833.
06/21/2018 at 12:45 AM #7384306/21/2018 at 7:08 AM #7384806/21/2018 at 7:15 AM #73850You have:
1DEFPARAM CumulateOrders = Falsein your code. Only one position will be allowed to be open at any one time. Set it to TRUE to open extra positions.
You will also need to add:
1IF Not OnMarket andto your first entry conditions and also set a tradecount to 1 when the first trade is opened and use that to allow the second trade to be opened and then cancel the flag once the position is opened
123456789101112131415// Long 1if Not OnMarket and firsttarget = 0 and tradecount = 0 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = 1endifendif// Long 2if TradeCount = 1 and firsttarget = 0 and tradecount = 1 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = 0endifendifIf you want to keep adding more positions than just the two then you can do this:
123456789101112131415161718192021maxtrades = 5// Long 1if Not OnMarket and firsttarget = 0 and tradecount = 0 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = 1endifendif// Long 2if OnMarket and TradeCount < maxtrades and firsttarget = 0 and tradecount = 1 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = tradecount + 1endifendifif not onmarket thentradecount = 0endifNone of it tested and written after only half a cup of coffee!
1 user thanked author for this post.
06/21/2018 at 8:55 PM #73965Late reply, been out all day + the world cup.
Nicolas.
No the tradecount variable works as I also mentioned in the comments in the code. The problem is that the second if statement is flawed (all if statements after the first one), first it works since the tradecount is 1 from Long 1 (otherwise the if statement wouldn’t go ahead), but instead of buying, which comes before the tradecount addon, the tradecount addon seem to be done first and when it’s done the Long 2 if statement aren’t correct anymore and the buy order isn’t done and it goes to the next if statement which I didn’t include in the code above.
The third statement (Long 3) had tradecount = 2 and would buy if it had no tradecount addon (as in the Long 2 statement), if it had the tradecount addon it wouldn’t buy either (same problem). The third (Long 3) couldn’t be activated if the Long 2 statement wasn’t first correct and the addon was done, the problem seem to be within the ProOrder Interpreter/Compiler (bugged), because how can the addon be done but not the buy order on the previous line in the same accepted if statement? The if statement should do everything between then and endif (unless else and such) and then go to the next if statement, not go back to the beginning of the current if statement.
To add to this, I had 4 contracts in Long 3 statement and after buying 1 contract in Long 1 (with a loss) and then Long 2 got activated, but not buying anything, the system would then go to Long 3 and buy 4 contracts instead (had buying 2 contracts in Long 2). And because of CumulateOrders = False I thought the next if statment wouldn’t get activated, but I guess it does, so I need to use “not onmarket”, I’ve tested with it though but with the same problem (addon is done but not the buy order before).
Vonasi.
The code wasn’t intended to open more positions until the previous one was done. I know how CumulateOrders work.
I was playing around with entering once during the morning (1 contract) and if a win the system was done for the day (the firsttarget = 1 in the longonmarket if statement), but if a loss it would enter again once the conditions was correct, but this time with 2 contracts. But this didn’t work because of the compiler problems within ProOrder (as I see it).
06/21/2018 at 9:02 PM #73967Forgot to mention the clearing of the variables. The code
12345// Clear entry variablesif time = 085000 thentradecount = 0 // Reset daily tradecountfirsttarget = 0 // Reset first target reach variableendifwas there as intended. Each day I want to clear the tradecount and the firsttarget because of the intention to only take 1 trade a day if the first trade was a win, hence reaching firsttarget 1, or Long 2 if a loss in the first trade (then I went on to test with Long 3 too when Long 2 didn’t work as intended).06/21/2018 at 10:08 PM #73969The code wasn’t intended to open more positions until the previous one was done. I know how CumulateOrders work.
Your description is very difficult to understand.
The code in ProOrder is read from top to bottom at the close of each candle. If CUMULATEORDERS is set to FALSE then no additional positions will be opened at the close of a candle if a position is already open. So you can put any number of BUY instructions in your code within whatever IF THEN ENDIF statements you like but they will all be ignored. Other variable changes will not be ignored unless they are protected by an IF NOT ONMARKET.
06/22/2018 at 12:20 AM #73977The code wasn’t intended to open more positions until the previous one was done. I know how CumulateOrders work.
Your description is very difficult to understand.
The code in ProOrder is read from top to bottom at the close of each candle. If CUMULATEORDERS is set to FALSE then no additional positions will be opened at the close of a candle if a position is already open. So you can put any number of BUY instructions in your code within whatever IF THEN ENDIF statements you like but they will all be ignored. Other variable changes will not be ignored unless they are protected by an IF NOT ONMARKET.
Hi again. Yes the description in my first post wasn’t very good, I didn’t want to write to much text and thought the comments in the code would help as well.
Then in your reply you once again write about opening additional positions which I just explained about in my previous two posts (and where clear about in the part below your nickname). Everything about the problem is in those two posts (06/21/2018 at 8:55 PM and 06/21/2018 at 9:02 PM). I also wrote that I’ve tried with IF NOT ONMARKET but it doesn’t change anything, it’s still the same problem.
Here is the code as of now I’m testing with to get the system to buy the second time AFTER the first trade has been stopped out.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061DEFPARAM CumulateOrders = FalseDEFPARAM FLATAFTER = 172000// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 092500timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 170000timeEnterAfter = time < noEntryAfterTimeset stop ploss 4targetprofit = 5// Clear entry variablesif time = 085000 thentradecount = 0 // Reset daily tradecountfirsttarget = 0 // Reset first target reach variable//secondtrade = 0endif// Conditions to enter long positionscl1 = close > open// Long 1if not onmarket and firsttarget = 0 and tradecount = 0 thenif cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = 1 // the addon works fine here//tradecount = tradecount + 1endifendif// Long 2if not onmarket and firsttarget = 0 and tradecount = 1 thenif not onmarket and cl1 and timeEnterBefore and timeEnterAfter thenbuy 2 contract at markettradecount = 2 // the addon does work, but the buy order won't buy before the tradecount addon, but without this line the order buys. So why does the same addon code work the first time but not the second time?//secondtrade = 1endifendif// Long 3if not onmarket and firsttarget = 0 and tradecount = 2 thenif not onmarket and cl1 and timeEnterBefore and timeEnterAfter thenbuy 4 contract at markettradecount = tradecount + 1 // the addon does work, but the buy order won't buy before the tradecount addon, but without this line the order buys. So why does the same addon code work the first time but not the second time?endifendifif longonmarket thenif firsttarget = 0 thenif close > tradeprice + targetprofit*pipsize thensell at marketfirsttarget = 1endifendifendif// EndLate now, time for zzz, will check more tomorrow.
Thanks.
06/22/2018 at 8:10 AM #73990If you just want to limit it to three separate trades in a day then why make it so complicated? One entry IF THEN and a counter and a positionsize multiple will do it.
I had to guess that you are working on the 5 minute chart for testing from the times used in your code.
1234567891011121314151617181920212223242526272829303132333435363738394041DEFPARAM CumulateOrders = FalseDEFPARAM FLATAFTER = 172000maxtrades = 3// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 092500timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 170000timeEnterAfter = time < noEntryAfterTimetargetprofit = 5// Clear entry variablesif time = 085000 thentradecount = 0 // Reset daily tradecountfirsttarget = 0 // Reset first target reach variablepositionsize = 1//secondtrade = 0endif// Conditions to enter long positionscl1 = close > open// Long 1if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter thenbuy positionsize contracts at markettradecount = tradecount + 1positionsize = positionsize * 2endifif longonmarket and firsttarget = 0 and close > tradeprice + targetprofit * pipsize thensell at marketfirsttarget = 1endifset stop ploss 4//graph tradecount//graph firsttarget1 user thanked author for this post.
06/22/2018 at 12:50 PM #74077Vonasi.
Thanks for optimizing the code, will definitely be able to use some parts of it (for the tests and in the future).
About complicating things it’s because I want the possibility to use different positionsizes at the different trades. I might want to go with 1 contract in the first trade, then 2 and finally 2 again in the third trade. Or perhaps 1 contract in the first and second trade and 2 or maybe 4 contracts in the third trade, that’s what I want to test out and play around with. That’s the reason I wanted to separate the trades. Is there a way to be able to do this?
Though I still want to be able to understand why the separate if statements I posted 06/22/2018 at 12:20 AM didn’t work. They would work in any other big programming language, hence I feel there has to be som bugs in the ProOrder language.
Thanks.
/Daniel06/22/2018 at 1:28 PM #74084Hi again.
It was possible to solve with IF statements within your Long 1 version similar to the way I wanted to do it from the beginning. For testing purposes I bought 1 contract, then 4 contracts and finally 2 contracts and it worked. Now I have something to play around with. Still wonder why my version didn’t work, but happy there is another way to do this.
12345678910111213141516// Long 1if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter thenif tradecount = 0 thenbuy 1 contracts at marketendifif tradecount = 2 thenbuy 4 contracts at marketendifif tradecount = 3 thenbuy 2 contracts at marketendiftradecount = tradecount + 1//positionsize = positionsize * 2endifHad to increase the maxtrades too from Vonasi’s latest post if someone wonder (didn’t include it to keep the code short).
Thanks.
06/22/2018 at 1:42 PM #74086Ok I didn’t need to increase the maxtrades, I accidently wrote the wrong tradecount in the second and third statement (0, 2 and 3), should be like this:
12345678910111213141516// Long 1if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter thenif tradecount = 0 thenbuy 1 contracts at marketendifif tradecount = 1 thenbuy 4 contracts at marketendifif tradecount = 2 thenbuy 2 contracts at marketendiftradecount = tradecount + 1//positionsize = positionsize * 2endifThanks again.
06/22/2018 at 1:45 PM #7408712345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152DEFPARAM CumulateOrders = FalseDEFPARAM FLATAFTER = 172000maxtrades = 3// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 092500timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 170000timeEnterAfter = time < noEntryAfterTimetargetprofit = 5// Clear entry variablesif time = 085000 thentradecount = 0 // Reset daily tradecountfirsttarget = 0 // Reset first target reach variable//secondtrade = 0endifif tradecount = 0 thenpositionsize = 1endifif tradecount = 1 thenpositionsize = 2endifif tradecount = 2 thenpositionsize = 4endif// Conditions to enter long positionscl1 = close > open// Longif not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter thenbuy positionsize contracts at markettradecount = tradecount + 1endifif longonmarket and firsttarget = 0 and close > tradeprice + targetprofit * pipsize thensell at marketfirsttarget = 1endifset stop ploss 4//graph tradecount//graph firsttargetThis should do what you want for now until someone can explain why the other code does not work.
1 user thanked author for this post.
06/22/2018 at 1:53 PM #74089I worked it out. It is the order that you put the conditions in. Long 3 must be first and Long 1 last. Each IF THEN was setting the conditions for the next ones conditions to be met. So not really a PRT bug – more of an operator error! 🙂
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556DEFPARAM CumulateOrders = FalseDEFPARAM FLATAFTER = 172000DEFPARAM PreLoadBars = 0// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 092500timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 170000timeEnterAfter = time < noEntryAfterTimeset stop ploss 4targetprofit = 5// Clear entry variablesif time = 085000 thentradecount = 0 // Reset daily tradecountfirsttarget = 0 // Reset first target reach variable//secondtrade = 0endif// Conditions to enter long positionscl1 = close > open// Long 3if not onmarket and firsttarget = 0 and tradecount = 2 and cl1 and timeEnterBefore and timeEnterAfter thenbuy 4 contract at markettradecount = 3endif// Long 2if not onmarket and firsttarget = 0 and tradecount = 1 and cl1 and timeEnterBefore and timeEnterAfter thenbuy 2 contract at markettradecount = 2endif// Long 1if not onmarket and firsttarget = 0 and tradecount = 0 and cl1 and timeEnterBefore and timeEnterAfter thenbuy 1 contract at markettradecount = 1endifif longonmarket thenif firsttarget = 0 thenif close > tradeprice + targetprofit*pipsize thensell at marketfirsttarget = 1endifendifendifgraph tradecountgraph firsttarget// End -
AuthorPosts
Find exclusive trading pro-tools on