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
- This topic has 25 replies, 6 voices, and was last updated 1 week ago by PeterSt.
-
-
12/03/2024 at 12:26 AM #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
12/03/2024 at 12:10 PM #24103512/04/2024 at 7:05 AM #241079Many 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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849DEFPARAM FlatAfter = 160000DEFPARAM CUMULATEORDERS =FALSEdefparam flatbefore = 094500Once FirstTradeResult = 0 // 0 for not yet determined, 1 for win, -1 for lossOnce TradeCount = 0//// reset the TradeCount each new day//IF IntraDayBarIndex = 0 THENTradeCount = 0ENDIF//// enter a trade when conditions are met AND the trade to be opened doesn't exceed MaxTradesIf OpenTime >= 094500 and OpenTime < 100000 thenHI = highLO = lowCandlelength = HI - LOEndIfIF Opentime >= 100000 AND NOT ONMARKET THENIF close > HI+buffer AND (TradeCount < MaxTrades) THENBUY CON CONTRACTS AT HI+buffer limitSET STOP PRICE LO - Candlelength * StopsmootherSET TARGET pProfit PTif POSITIONPERF (1)> 0 THENTradeCount = MaxTradesElseTradeCount = TradeCount + 1ENDIFELSEIF close < LO-buffer AND (TradeCount < MaxTrades) THENSELLSHORT CON CONTRACTS AT LO-buffer limitSET STOP PRICE HI + Candlelength * StopsmootherSET TARGET pProfit PTif POSITIONPERF (1)> 0 THENTradeCount = MaxTradesELSETradeCount = TradeCount + 1ENDIFENDIFENDIFENDIF12/04/2024 at 7:22 PM #241117StrategyProfit 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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556DEFPARAM FlatAfter = 160000DEFPARAM CUMULATEORDERS =FALSEdefparam flatbefore = 094500Once FirstTradeResult = 0 // 0 for not yet determined, 1 for win, -1 for lossOnce TradeCount = 0//// reset the TradeCount each new day//IF IntraDayBarIndex = 0 THENTradeCount = 0ENDIFif StrategyProfit > StrategyProfit[1] THENTradeCount = MaxTradesELSETradeCount = TradeCount + 1ENDIF//// enter a trade when conditions are met AND the trade to be opened doesn't exceed MaxTradesIf OpenTime >= 094500 and OpenTime < 100000 thenHI = highLO = lowCandlelength = HI - LOEndIfIF Opentime >= 100000 AND NOT ONMARKET THENIF close > HI+buffer AND (TradeCount < MaxTrades) THENBUY CON CONTRACTS AT HI+buffer limitSET STOP PRICE LO - Candlelength * StopsmootherSET TARGET pProfit PT//if POSITIONPERF (1)> 0 THEN//TradeCount = MaxTrades//Else//TradeCount = TradeCount + 1//ENDIFELSEIF close < LO-buffer AND (TradeCount < MaxTrades) THENSELLSHORT CON CONTRACTS AT LO-buffer limitSET STOP PRICE HI + Candlelength * StopsmootherSET TARGET pProfit PT//if POSITIONPERF (1)> 0 THEN//TradeCount = MaxTrades//ELSE//TradeCount = TradeCount + 1//ENDIFENDIFENDIFENDIF1 user thanked author for this post.
12/05/2024 at 7:24 AM #241124Hi 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):
- 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
- 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)
- in some occasions, it misses all the opportunities to place a trade and ends the day with no trades at all (see last attachment)
- 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
1234567891011121314151617181920212223242526272829303132333435363738394041// Variables for reference PT = 40, CON = 1, Buffer = 5, MaxTrades = 2, StopAdjuster = 0.25DEFPARAM FlatAfter = 160000DEFPARAM CUMULATEORDERS =FALSEdefparam flatbefore = 100000ONCE Tally = 0//// reset the TALLY each new day//IF IntraDayBarIndex = 0 THENTally = 0ENDIF//// enter a trade when conditions are met AND the trade to be opened doesn't exceed MaxTradesIf OpenTime >= 100000 and OpenTime < 100000 thenHI = highLO = lowCandlelength = HI - LOEndIf//IF Opentime >= 100000 AND NOT ONMARKET THENif opentime >= 100000 and not ONMARKET thenIF close > HI+buffer AND (Tally < MaxTrades) THENBUY CON CONTRACTS AT HI+buffer limitSET STOP PRICE LO - Candlelength * StopAdjusterSET TARGET pProfit PTTally = Tally + 1ELSEIF close < LO-buffer AND (Tally < MaxTrades) THENSELLSHORT CON CONTRACTS AT LO-buffer limitSET STOP PRICE HI + Candlelength * StopAdjusterSET TARGET pProfit PTtally = Tally + 1ENDIFENDIFENDIF12/05/2024 at 10:22 AM #241133Line
1If OpenTime >= 100000 and OpenTime < 100000 thenis 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
1IF close > HI+buffer AND (Tally < MaxTrades) THENwill 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:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// Variables for reference PT = 40, CON = 1, Buffer = 5, MaxTrades = 2, StopAdjuster = 0.25DEFPARAM FlatAfter = 160000DEFPARAM CUMULATEORDERS = FALSEdefparam flatbefore = 100000ONCE Tally = 0ONCE MaxTrades = 2ONCE Buffer = 5 * PipSizeONCE CON = 1ONCE PT = 200ONCE StopAdjuster = 1.0//// reset the TALLY each new dayIF IntraDayBarIndex = 0 THENTally = 0ENDIFif StrategyProfit > StrategyProfit[1] THENTally = MaxTradesELSif StrategyProfit > StrategyProfit[1] THENTally = Tally + 1ENDIF//// enter a trade when conditions are met AND the trade to be opened doesn't exceed MaxTradesIf OpenTime >= 100000 and OpenTime < 160000 thenHI = highLO = lowCandlelength = HI - LOEndIf//IF Opentime >= 100000 AND NOT ONMARKET THENif opentime >= 100000 and not ONMARKET thenIF close > HI-buffer AND (Tally < MaxTrades) THENBUY CON CONTRACTS AT HI+buffer limitSET STOP PRICE LO + Candlelength * StopAdjusterSET TARGET pProfit PTTally = Tally + 1ENDIFIF close < LO-buffer AND (Tally < MaxTrades) THENSELLSHORT CON CONTRACTS AT LO-buffer limitSET STOP PRICE HI + Candlelength * StopAdjusterSET TARGET pProfit PTtally = Tally + 1ENDIFENDIF12/05/2024 at 10:25 AM #241134I also changed the IF…ENDIF nesting for entries.
12/05/2024 at 10:53 AM #24113512345StrategyProfit > StrategyProfit[1] THENTally = MaxTradesELSif StrategyProfit > StrategyProfit[1] THENTally = Tally + 1ENDIFYes, 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 ?
12/05/2024 at 10:57 AM #241137123IF IntraDayBarIndex = 0 THENTally = 0ENDIFThis may never occur in your situation.
Change it to this so you can check :123456Graph 0 as "Bar0Check" // This will show in the bottom pane as a flat line.IF IntraDayBarIndex = 0 THENTally = 0Graph 1 as "Bar0check" // This will show in the bottom pane as a peak.ENDIF12/05/2024 at 11:13 AM #241139Sorry, this line is incorrect
1ELSif StrategyProfit > StrategyProfit[1] THENreplace it by
1ELSif StrategyProfit < StrategyProfit[1] THEN1 user thanked author for this post.
12/05/2024 at 11:19 AM #241140Unfortunately 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 :
12345If OpenTime >= 094500 and OpenTime < 100000 thenHI = highLO = lowCandlelength = HI - LOEndIfand 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 :12if opentime >= 100000 and not ONMARKET thenIF close > HI-buffer AND (Tally < MaxTrades) THENRight. 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.
12/05/2024 at 11:47 AM #241145None 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:
123ONCE HI = highONCE LO = lowONCE Candlelength = HI - LO12/05/2024 at 6:44 PM #24115712/05/2024 at 7:33 PM #241158Great 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.
12/06/2024 at 3:49 AM #241167Hi 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 correctly5 – 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 dayIf 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…
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162// Variables for reference PT = 40, CON = 1, Buffer = 5, MaxTrades = 2, StopAdjuster = 0.25DEFPARAM FLATAFTER = 160000DEFPARAM CUMULATEORDERS = FALSE // to prevent adding to positionsdefparam FLATBEFORE = 100000Graph 10 as "StartOfSystem" // I added this one to "see" which section of the code is being processed at a specific point in time in the chartGraph 0 as "Bar0Check" // suggested by Peter// Instructions to reset the following variables on a daily basisIF IntraDayBarIndex = 0 THENTally = 0HI = 0LO = 0LongEntryPoint=0Graph 1 as "Bar0check"Graph Tally as "Tally" // Added a few of these to see where the 'tally' variable was in the chartGRAPHONPRICE LongEntryPoint as "DailyLongEntryPoint" // This will check the daily entrypoint for Longs is reset at midnightENDIF// Instructions to calculate the High and Low of the breakout candle 0945-1000 and also the entry points based on those High and Low values, on a daily basisIF OpenTime >= 094500 and OpenTime < 100000 THENHI = HighLO = LowCandlelength = HI-LOLongEntryPoint = HI+BUFFERGRAPHONPRICE LongEntryPoint as "DailyLongEntryPoint" // This will check the daily entrypoint for Longs is updated from the start of the breakout bar onwards (it is working as intended)Graph 11 as "StartOfSystem" // to show me that the code has moved into this section when we get to the breakout bar (working as intended)Graph Tally as "Tally"ENDIF// Instructions to enter the market on a daily basis if conditions are metGraph 0 as "ReadyForTrade1Check"Graph Tally as "Tally"Graph 0 as "OrderPlaced"if OpenTime >= 100000 and opentime < 160000 and not ONMARKET THENGraph 12 as "StartOfSystem"Graph 1 as "ReadyForTrade1Check"Graph Tally as "Tally"IF close >= LongEntryPoint AND Tally < MaxTrades THENGraph 2 as "ReadyForTrade1Check"BUY CON CONTRACTS At LongEntryPoint limitGraph Tally as "Tally"SET TARGET PPROFIT PTSET STOP PRICE LO - Candlelength * StopAdjusterif LONGTRIGGERED THEN // tried to use this function to help with the tally variable accumulated number but didn't go much fartherGraph 5 as "OrderPlaced"Tally = Tally +1ENDIFENDIFENDIF -
AuthorPosts
Find exclusive trading pro-tools on