Strange if condition code error

Viewing 15 posts - 1 through 15 (of 27 total)
  • Author
    Posts
  • #73838 quote
    Daniel_H
    Participant
    Senior

    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

    DEFPARAM CumulateOrders = False
    DEFPARAM FLATAFTER = 172000
    
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 092500
    timeEnterBefore = time >= noEntryBeforeTime
    
    // Prevents the system from placing new orders to enter the market or increase position size after the specified time
    noEntryAfterTime = 170000
    timeEnterAfter = time < noEntryAfterTime
    
    set stop ploss 4
    targetprofit = 5
    
    // Clear entry variables
    if time = 085000 then
    tradecount = 0 // Reset daily tradecount
    firsttarget = 0 // Reset first target reach variable
    endif
    
    // Conditions to enter long positions
    cl1 = close > open
    
    // Long 1
    if firsttarget = 0 and tradecount = 0 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = tradecount + 1 // the addon works fine here
    endif
    endif
    
    // Long 2
    if firsttarget = 0 and tradecount = 1 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = 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?
    endif
    endif
    
    
    if longonmarket then
    if firsttarget = 0 then
    if close > tradeprice + targetprofit*pipsize then
    sell at market
    firsttarget = 1
    endif
    endif
    endif
    
    
    // End
    #73841 quote
    robertogozzi
    Moderator
    Master

    I think this post deals with your issue, it’s a question of ONMARKET https://www.prorealcode.com/topic/help-needed-with-code/#post-73833.

    #73843 quote
    Daniel_H
    Participant
    Senior

    I had already tried with “Not OnMarket” earlier but with no change/improvement, don’t know if that what you meant? Well late now and I will check for answers later tomorrow when I’m back at home. Thanks.

    #73848 quote
    Nicolas
    Keymaster
    Master

    Is your problem that the “tradecount” variable is not increasing? Because you are resetting it to zero, each day at 08h50 AM. I’m sure you know it, but just in case ..

    #73850 quote
    Vonasi
    Moderator
    Master

    You have:

    DEFPARAM CumulateOrders = False

    in 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:

    IF Not OnMarket and

    to 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

    // Long 1
    if Not OnMarket and firsttarget = 0 and tradecount = 0 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = 1
    endif
    endif
     
    // Long 2
    if TradeCount = 1 and firsttarget = 0 and tradecount = 1 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = 0
    endif
    endif

    If you want to keep adding more positions than just the two then you can do this:

    maxtrades = 5
    
    // Long 1
    if Not OnMarket and firsttarget = 0 and tradecount = 0 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = 1
    endif
    endif
     
    // Long 2
    if OnMarket and TradeCount < maxtrades and firsttarget = 0 and tradecount = 1 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = tradecount + 1
    endif
    endif
    
    if not onmarket then
    tradecount = 0
    endif

    None of it tested and written after only half a cup of coffee!

    Nicolas thanked this post
    #73965 quote
    Daniel_H
    Participant
    Senior

    Late 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).

    #73967 quote
    Daniel_H
    Participant
    Senior

    Forgot to mention the clearing of the variables. The code

    // Clear entry variables
    if time = 085000 then
    tradecount = 0 // Reset daily tradecount
    firsttarget = 0 // Reset first target reach variable
    endif
    was 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).
    #73969 quote
    Vonasi
    Moderator
    Master

    The 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.

    #73977 quote
    Daniel_H
    Participant
    Senior

    The 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.

    DEFPARAM CumulateOrders = False
    DEFPARAM FLATAFTER = 172000
    
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 092500
    timeEnterBefore = time >= noEntryBeforeTime
    
    // Prevents the system from placing new orders to enter the market or increase position size after the specified time
    noEntryAfterTime = 170000
    timeEnterAfter = time < noEntryAfterTime
    
    set stop ploss 4
    targetprofit = 5
    
    // Clear entry variables
    if time = 085000 then
    tradecount = 0 // Reset daily tradecount
    firsttarget = 0 // Reset first target reach variable
    //secondtrade = 0
    endif
    
    // Conditions to enter long positions
    cl1 = close > open
    
    // Long 1
    if not onmarket and firsttarget = 0 and tradecount = 0 then
    if cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = 1 // the addon works fine here
    //tradecount = tradecount + 1
    endif
    endif
    
    // Long 2
    if not onmarket and firsttarget = 0 and tradecount = 1 then
    if not onmarket and cl1 and timeEnterBefore and timeEnterAfter then
    buy 2 contract at market
    tradecount = 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 = 1
    endif
    endif
    
    // Long 3
    if not onmarket and firsttarget = 0 and tradecount = 2  then
    if not onmarket and cl1 and timeEnterBefore and timeEnterAfter then
    buy 4 contract at market
    tradecount = 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?
    endif
    endif
    
    if longonmarket then
    if firsttarget = 0 then
    if close > tradeprice + targetprofit*pipsize then
    sell at market
    firsttarget = 1
    endif
    endif
    endif
    
    
    // End

    Late now, time for zzz, will check more tomorrow.

    Thanks.

    #73990 quote
    Vonasi
    Moderator
    Master

    If 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.

    DEFPARAM CumulateOrders = False
    DEFPARAM FLATAFTER = 172000
    
    maxtrades = 3
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 092500
    timeEnterBefore = time >= noEntryBeforeTime
    
    // Prevents the system from placing new orders to enter the market or increase position size after the specified time
    noEntryAfterTime = 170000
    timeEnterAfter = time < noEntryAfterTime
    
    targetprofit = 5
    
    // Clear entry variables
    if time = 085000 then
    tradecount = 0 // Reset daily tradecount
    firsttarget = 0 // Reset first target reach variable
    positionsize = 1
    //secondtrade = 0
    endif
    
    // Conditions to enter long positions
    cl1 = close > open
    
    // Long 1
    if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter then
    buy positionsize contracts at market
    tradecount = tradecount + 1
    positionsize = positionsize * 2
    endif
    
    if longonmarket and firsttarget = 0 and close > tradeprice + targetprofit * pipsize then
    sell at market
    firsttarget = 1
    endif
    
    set stop ploss 4
    
    //graph tradecount
    //graph firsttarget
    Daniel_H thanked this post
    #74077 quote
    Daniel_H
    Participant
    Senior

    Vonasi.

    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.
    /Daniel

    #74084 quote
    Daniel_H
    Participant
    Senior

    Hi 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.

    // Long 1
    if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter then
    if tradecount = 0 then
    buy 1 contracts at market
    endif
    
    if tradecount = 2 then
    buy 4 contracts at market
    endif
    
    if tradecount = 3 then
    buy 2 contracts at market
    endif
    tradecount = tradecount + 1
    //positionsize = positionsize * 2
    endif

    Had to increase the maxtrades too from Vonasi’s latest post if someone wonder (didn’t include it to keep the code short).

    Thanks.

    #74086 quote
    Daniel_H
    Participant
    Senior

    Ok 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:

    // Long 1
    if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter then
    if tradecount = 0 then
    buy 1 contracts at market
    endif
     
    if tradecount = 1 then
    buy 4 contracts at market
    endif
     
    if tradecount = 2 then
    buy 2 contracts at market
    endif
    tradecount = tradecount + 1
    //positionsize = positionsize * 2
    endif

    Thanks again.

    #74087 quote
    Vonasi
    Moderator
    Master
    DEFPARAM CumulateOrders = False
    DEFPARAM FLATAFTER = 172000
     
    maxtrades = 3
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 092500
    timeEnterBefore = time >= noEntryBeforeTime
     
    // Prevents the system from placing new orders to enter the market or increase position size after the specified time
    noEntryAfterTime = 170000
    timeEnterAfter = time < noEntryAfterTime
     
    targetprofit = 5
     
    // Clear entry variables
    if time = 085000 then
    tradecount = 0 // Reset daily tradecount
    firsttarget = 0 // Reset first target reach variable
    
    //secondtrade = 0
    endif
    
    if tradecount = 0 then 
    positionsize = 1
    endif
    
    if tradecount = 1 then 
    positionsize = 2
    endif
    
    if tradecount = 2 then 
    positionsize = 4
    endif
     
    // Conditions to enter long positions
    cl1 = close > open
     
    // Long 
    if not onmarket and firsttarget = 0 and tradecount < maxtrades and cl1 and timeEnterBefore and timeEnterAfter then
    buy positionsize contracts at market
    tradecount = tradecount + 1
    endif
     
    if longonmarket and firsttarget = 0 and close > tradeprice + targetprofit * pipsize then
    sell at market
    firsttarget = 1
    endif
     
    set stop ploss 4
     
    //graph tradecount
    //graph firsttarget

    This should do what you want for now until someone can explain why the other code does not work.

    Daniel_H thanked this post
    #74089 quote
    Vonasi
    Moderator
    Master

    I 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! 🙂

    DEFPARAM CumulateOrders = False
    DEFPARAM FLATAFTER = 172000
    DEFPARAM PreLoadBars = 0
     
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 092500
    timeEnterBefore = time >= noEntryBeforeTime
     
    // Prevents the system from placing new orders to enter the market or increase position size after the specified time
    noEntryAfterTime = 170000
    timeEnterAfter = time < noEntryAfterTime
     
    set stop ploss 4
    targetprofit = 5
     
    // Clear entry variables
    if time = 085000 then
    tradecount = 0 // Reset daily tradecount
    firsttarget = 0 // Reset first target reach variable
    //secondtrade = 0
    endif
     
    // Conditions to enter long positions
    cl1 = close > open
     
     
    // Long 3
    if not onmarket and firsttarget = 0 and tradecount = 2 and cl1 and timeEnterBefore and timeEnterAfter then
    buy 4 contract at market
    tradecount = 3
    endif
     
    // Long 2
    if not onmarket and firsttarget = 0 and tradecount = 1 and cl1 and timeEnterBefore and timeEnterAfter then
    buy 2 contract at market
    tradecount = 2
    endif
    
    // Long 1
    if not onmarket and firsttarget = 0 and tradecount = 0 and cl1 and timeEnterBefore and timeEnterAfter then
    buy 1 contract at market
    tradecount = 1
    endif
     
    if longonmarket then
    if firsttarget = 0 then
    if close > tradeprice + targetprofit*pipsize then
    sell at market
    firsttarget = 1
    endif
    endif
    endif
     
    graph tradecount
    graph firsttarget
    // End
    
Viewing 15 posts - 1 through 15 (of 27 total)
  • You must be logged in to reply to this topic.

Strange if condition code error


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Daniel_H @hagel666 Participant
Summary

This topic contains 26 replies,
has 5 voices, and was last updated by Vonasi
7 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/20/2018
Status: Active
Attachments: No files
Logo Logo
Loading...