Define (and count) a trade as a WIN or as a LOSS

Forums ProRealTime English forum ProOrder support Define (and count) a trade as a WIN or as a LOSS

Viewing 15 posts - 1 through 15 (of 26 total)
  • #241005

    Hello Traders,

    I searched a lot of pages with posts on this forum but wasn’t able to find (at least to the point where I decided to stop) a post with this topic: a code that tells you if a trade was a win or a loss. I searched on the ProBuider function list but nothing looked like addressing the P&L of a single closed trade.

    My idea is to use this code to limit my number of trades in one day:

    • if trade #1 is a WIN, no more trades in that session
    • if trade #1 is a LOSS, strategy continues to be executed but only one more time

    I already found several posts that cover the “max number of trades per day” topic, however couldn’t see anything related with how to classify a trade based on its P&L.

    Many thanks in advance for the guidance/suggestions.

     

    tiago

    #241035

    Hi!
    You can use positionperf(1) to check the performance of last trade.

    1 user thanked author for this post.
    #241079

    Many thanks Ivan.

    I followed your advice and used the function ‘positionperf’, however the strategy still executes two wins in one day. It is now stopping at two trades per day but mainly because my variable ‘MaxTrades’ is set to 2. Unless I am using the function ‘positionperf’ wrong, it makes sense to give the instruction to make the tradecount = maxtrades if the trade is a winner, hence not allowing more trades during the day, otherwise if the first trade is a loser, then add 1 trade to the tradecounter and perform the same calculations again.

     

    Any help from the community is highly appreciated. Thank you again.

     

     

     

    #241117

    StrategyProfit is the correct instruction to know whether a trade was a LOSS or a GAIN, as PositionPerf only reports the temporary performance while the trade is open.

    Moreover, the check should be done within an IF…ENDIF block of its own, prior to entry. Try this one:

     

    1 user thanked author for this post.
    #241124

    Hi Roberto and ProRealTime community,

     

    Roberto, thank you for trying to help with the function strategyprofit but it didn’t work unfortunately. I copied your block of code and when I run it, it wouldn’t place any trade…

    Unfortunately I feel I am about to hit a brickwall, which is very demoralizing. I am a resilient guy but this idea of creating a simple breakout range system that stops if the 1st trade is a win or else it just executes one last trade, is apparently way more complex than I enviosioned it…

    Right now I have theseissues (I have been working on them 24/7 for the last couple of days):

    1. I can’t find a way to tell the system to stop if the 1st trade of each day is a winner, otherwise just try a second (and last) trade for the day
    2. usually, the system places at least a trade per day but always misses several opportunities to go long or short earlier in the session (quasi in a random fashion), but when it does place a trade, the entry and exit levels are absolutely correct as per conditions defined (please see examples attached, where you can also see the indicator I created with this community’s help that shows the High and Low of the breakout range bar 09:45-10:00 until the end of the session)
    3. in some occasions, it misses all the opportunities to place a trade and ends the day with no trades at all (see last attachment)
    4. the system does not place any orders right after the breakout range bar (09:45-10:00), which should be the (10:00-10:15) bar, if price level conditions were met (and they are multiple times). The earliest it placed trade was in the 10:15-10:30 bar (2nd bar after breakout range bar)
    • my feeling is that #2, #3 and #4 are somehow related, because when it enters long or short, they are correct (above the High + 5 points or below the Low – 5 points), however it looks like it picks a random bar each day and misses many (sometimes all!) of the previous occasions that matched the conditions to enter.

    Here is the code as is, complying with doing max 2 trades a day if entry/exit conditions are met (but not checking for winner or loser) and always placing trades at the 2nd bar after the breakout bar or later.

    If anyone can give me a hand, that would be highly appreciated. I am 100% convinced that ProRealTime can handle what I am asking it to do. I will keep digging…

    Many thanks

     

    #241133

    Line

    is logically incorrect, as it will never be true since the TIME cannot be >= AND < 100000 at the same time; you probably wanted to write < 160000.

    In addition the line

    will never be true, as CLOSE cannot be > HIGH+buffer. I tested it using “minus” instead of “plus”.

    Also, the code I posted had an incorrect ELSE (it should have been ELSIF….) with STRATEGYPROFIT.
    This will do:

    #241134

    I also changed the IF…ENDIF nesting for entries.

     

    #241135

    Yes, we saw that that was wrong. But it still is 🙂

    If this is on IB it will keep on being wrong. Watanabix – which version do you use ? PRT-IG or PRT-IB ?

    #241137

    This may never occur in your situation.
    Change it to this so you can check :

     

    #241139

    Sorry, this line is incorrect

    replace it by

    1 user thanked author for this post.
    #241140

    Unfortunately I feel I am about to hit a brickwall, which is very demoralizing.

    Nah, wait … not so fast … 🙂

    I myself have been reluctant to respond to issues in your code, because there’s just too many of them. This is not necessarily your not-so-decent way of coding, but merely is about what PRT allows you to code (which can be wrong).

    But for example, I see you use variables under conditions which make it unreadable for me, because plain wrong. Here is one :

    and it is still there in Roberto’s version(s), because, well, it is allowed. But still plain wrong;

    None of there variables have been “declared” by you. This means that they will receive a random value at first, but say it will be zero, or else an infitely large negative number.
    Now, please go through your code as it is, and envision this If not to be true initially. Go on reading, and in your mind make something of this part :

    Right. So what is HI ? ah wait, this probably can’t be an issue because opentime will be <= 100000 or else ONMARKET may be false. But it it ? let’s check.
    … and then from the one comes the other and all is very tiring and the most error-prone. So for example, if now your If elsewhere with the check for > 100000 and < 100000 (as Roberto pointed out) then the snippet above suddenly has a great issue. So now you have two issues at the same time. One because of not-so-nice coding, and the other because of an accidental bug.

    So for your sheer self you should make the coding much more decent. Robust if you will. You will immediately notice that it helps.
    Let me add that inherently you do all correctly. But you will not be able to read your own code well (because of what I just said). And there it goes wrong after all.

    And of course use Graph and GraphOnPrice commands. They will be your greatest help.
    No brick walls in sight. No demoralization to be seen. 🙂 🙂

    1 user thanked author for this post.
    #241145

    None of there variables have been “declared” by you. This means that they will receive a random value at first, but say it will be zero, or else an infitely large negative number.

    This can be overcome by adding these lines just before line 27:

    #241157

    Hi Peter. I just use PRT as a backtesting software. I don’t place any trades with PRT. Is this what you were asking? Thanks for trying to help.

    #241158

    Great idea, Peter. Many thanks.  I used it and it is working well. It goes to one at midnight and also on sundays at 6pm, when it reopens after the friday close (no impact on code). Really interesting (and useful) to test it this way. Will proceed to yours and Roberts’ “longer” replies.

     

    Thanks for spending some time here and also challenging me in a constructive way. I haven’t coded for more than 30 years and am enjoying every bit of it. Definitely it will take time to also acquire some coding best practices, etc…

    Hoping to share some good news soon.

    #241167

    Hi everyone. Unfortunately, and after more than 10 hours of dedication to solving this issue based on your generous help, I have advanced very little, to say the least…

    1 – I am only focusing on trying to get the LONG side done therefore there is no point on having any piece of code for SHORT just yet
    2 – I have tried so many different ways to get a variable that shows me the accumulated orders placed for the day (tally), so that I can then add the condition to stop if first trade is a win, but I haven’t been successful. This means I haven’t tried adding the code from Robert based on ‘strategyprofit’ function just yet.
    3 – I have used Peter’s advice to graph variables and it definitely helped me a lot seeing what is going on
    4 – The first two blocks of the code, where (1) it resets everything at midnight and (2) calculates the high and low of the 0945-1000 bar is also working. I used the “graph approach” suggested by Peter and the entry point for longs (HI+buffer) shows up in the chart correctly

    5 – The issues arise right when the system starts comparing the close of any bar after the breakout bar to the entry point (high+buffer). Because the close happens at the end of the bar, it will only try to go long at the next bar, which is not ideal but I don’t think I should worry about that aspect right now while I still have several things to polish/fix.
    6 – based on the “show on graph” approach, it seems that the variable ‘tally‘ is increased by 1 after the position is exited (it should be when it is entered) but then it drops to zero and restarts the same process again, which defeats the purpose of using the variable as an accumulated number for the total number of orders placed&exited during the day

    If anyone can contribute with some further guidance, that will be highly appreciated. I have a feeling that the functions that I am using might be limiting my options but because I am new with PRT and honestly haven’t coded in so many years, this is the best thought process I can think of in terms of coding instructions.

    Thanks again for all the patience. Time to call it a day now…

Viewing 15 posts - 1 through 15 (of 26 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login