Difference between countOfPosition and actual positions open

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #204890 quote
    Johan
    Participant
    Average

    I have some weird stuff happening with count of positions. First i calculate how many lots to trade – which can include decimal, like numLotsFull = 4,6 lots. Then when i compare numLotsFull with countOfPosition i sometimes (not on all trades) get a difference of 0.1 lot.

    numLotsFull = 4.6
    countOfPosition = 4.59

    Why is this happening and why is it random? The difference of 0.1 lot is always the same but the difference is only on some of the trades.

    #204893 quote
    Johan
    Participant
    Average

    I manually went through and defined lot size and saw the following pattern…

    2.3 = 2.29
    4.6 = 4.59
    9.2 = 9.19
    18.4 = 18.39

    The patterns is that if you double 2.3 you get 4.6 and if you double that you get 9.2 and so on. All these numbers get the 0.1 diff. But if you put 2.4 or 2.2 there’s no diff.

    #204895 quote
    Johan
    Participant
    Average

    4.1 also gets the difference which is even more weird! countOfPositions = 4.09

    What is going on 😀

    #204897 quote
    PeterSt
    Participant
    Master

    What is going on 😀

    Internal rounding errors. I will try to find in my code how I solved them …

    #204898 quote
    Johan
    Participant
    Average

    Thanks that would be very appreciated!!

    #204900 quote
    PeterSt
    Participant
    Master

    It is not easy to track down (too many code versions – and today it does not bother me any more). Below you can see the effect (undoubtedly the same you see).
    If you follow from bottom to top, you can see an Entry of 10.19 contracts that Exits per means of CountOfPosition but that comes up with 10.18 contracts. That leaves 0.01. That bit goes out later, when the program decides to Exit again (OnMarket remains true so it will keep on trying to exit that position).

    The descriptive text from myself  I found resides in thee 2nd screenshot. For now this is for reference. It also mentions in which version of the code I solved it. So, on to that … (see upcoming post).

    image_2022-11-27_161431952.png image_2022-11-27_161431952.png image_2022-11-27_162106661.png image_2022-11-27_162106661.png
    #204904 quote
    Johan
    Participant
    Average

    I guess i could do like this:

    if countOfPositionSync = 0 then
    numLotsFull = abs(countOfPosition)
    countOfPositionSync = 1
    endif
    #204906 quote
    PeterSt
    Participant
    Master
          //Sell MyCountOfPosition contracts at Market
          Sell LotSizeBase contracts at Market        // 22-04-2022,PS, Like this. Deactivated for Test. 23-04-2022,PS, Base version.
    

    Notice that MyCountOfPosition is just equal to CountOfPosition (though the ABSolute of it).

    LotSizeBase – the one without issue, is defined like this :

    Once LotSizeBase = PositionAmount / PriceForPointValue * CurrencyFactor

    And PositionAmount is easily defined like this :

    Once PositionAmount = 140000

    You will see this value back in that descriptive post I showed.

    Thus with some hoopla between that PositionAmount and what CountOfPosition makes of it – BUT with an

    If OnMarket then
      MyCountOfPosition = Abs(CountOfPosition)
    endif

    in between it, it can well be that the Abs function implies the culprit (looking at it today).

    You should be using Abs just as well. You could change it to

    If ShortOnMarkt then
      ExitShort -CountOfPosition At Market   // Mind the minus sign.
    endif

    … which theoretically could help.

     

    PS: For me it was relatively easy to track down because not long before I started to use CountOfPosition to more easily exit all (after adding position underway), but otherwise … see that descriptive post and it is exactly the same as with you … it depends totally on the values itself whether it would misbehave or not and you’d have no clue …

    #204914 quote
    Johan
    Participant
    Average

    Interesting!

    I dont think it’s an abs() problem. I’ve tested this:

    if not longonmarket then
    buy 4.1 lots at market
    endif
    
    if longonmarket then
    sell at market
    endif

    And buying 4.1 lots still makes 4.09 open positions. No abs() used. So it’s obvious to me that this is some PRT/IG problem. Some decimal combinations of lot sizes is just being wrongly opened with this 0.01 difference.

    #204922 quote
    PeterSt
    Participant
    Master

    And buying 4.1 lots still makes 4.09 open positions.

    All right, careful now. I am never exactly testing *that*, so you very well could be (will be ?) correct on that.
    Fact is : I never had the problem since (see posts), and I never had the problem prior to using CountOfPosition as a means to exit (at ~30 trades per day).

    In the end this could be a matter of not getting exactly what you asked for (ask 4.1 and get 4.09), while CountOfPosition rounds that the wrong way. Or maybe even : rounds it correctly but the negative quantity (for Short) works out differently for positive (Long) – while at the same time no rounding may be in order but cutting (taking the integer).

    If you can isolate the issue with a few lines of sample code, you can make a Technical Report of it by means of the Help menu. Will that help you ? maybe in a year of time, maybe sooner, maybe never. So the moral : work around it (*and* make that report). That will help instantly. Just an advice based on experience. 🙂

    #204927 quote
    Nicolas
    Keymaster
    Master

    You are right, I can replicate with 4.1 lots, not below. COUNTOFPOSITION is correct, because order opened size is 4.09. I’m reporting it also.

    #204947 quote
    PeterSt
    Participant
    Master
    You are right, I can replicate with 4.1 lots, not below. COUNTOFPOSITION is correct, because order opened size is 4.09.
    If CountOfPosition is correct then how can I

      Sell CountOfPosition Contracts At Market

    and be left with 0.1 contracts of positition ?
    image_2022-11-28_142121685.png image_2022-11-28_142121685.png
    #204949 quote
    Nicolas
    Keymaster
    Master
    That’s really weird 😆 The issue had been identified and a fix is in the pipe 😉
    #205010 quote
    Johan
    Participant
    Average
    The issue had been identified and a fix is in the pipe 😉
    Great Nicolas. Thanks! 🙂
    #205011 quote
    Johan
    Participant
    Average
    Until the fix is rolled out may be you can help me with a work around… I’m trying to implement partial close on EUR/USD Mini – that take into account minimum of 1 lot size. Trade if criteria’s are met. tradeLots is set based on 2% of  capital and the stop risk. Minimum 1 lot.
    if tradeLong then
    numLotsFull = tradeLots
    buy numLotsFull lot at market
    priceTarget1 = priceEntry + targetLevel
    priceTarget2 = priceEntry + (targetLevel * 2)
    endif
    
    numLotsHalf = numLotsFull / 2
    numLotsBigPart = floor(numLotsFull)
    numLotsSmallPart = numLotsFull - floor(numLotsFull)
    
    Exit is made like this… First i check if numLotsFull is equal to abs(countOfPosition) so i can decide how many lots the exit. If abs(countOfPosition) is 1 i exit full trade at priceTarget1. If abs(countOfPosition) is greater then 1 but under 2 (ex 1.6) then i exit 1 lot (numLotsBigPart) at priceTarget1 and then the remaining 0.6 lots (numLotsSmallPart) at priceTarget2. If abs(countOfPosition) is greater then 2 i exit half of the lots at the two price targets. After first target is met i move stop to break even. Thats why numLotsFull = abs(countOfPosition) is so important.
    if onMarket then
    if numLotsFull = abs(countOfPosition) then
    tradePriceInit = tradePrice
    if abs(countOfPosition) = 1 then
    sell numLotsFull lots at priceTarget1 limit
    exitshort numLotsFull lots at priceTarget1 limit
    elsif abs(countOfPosition) > 1 and abs(countOfPosition) < 2 then
    sell numLotsBigPart lots at priceTarget1 limit
    sell numLotsSmallPart lots at priceTarget2 limit
    exitshort numLotsBigPart lots at priceTarget1 limit
    exitshort numLotsSmallPart lots at priceTarget2 limit
    elsif abs(countOfPosition) >= 2 then
    sell numLotsHalf lots at priceTarget1 limit
    sell numLotsHalf lots at priceTarget2 limit
    exitshort numLotsHalf lots at priceTarget1 limit
    exitshort numLotsHalf lots at priceTarget2 limit
    endif
    else
    priceStop = tradePriceInit
    sell at priceTarget2 limit
    exitshort at priceTarget2 limit
    endif
    
    sell at priceStop stop
    exitshort at priceStop stop
    endif
    So the problem is… If numLotsFull is 4.1 but abs(countOfPosition) is 4.09 my exit code fails.
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.

Difference between countOfPosition and actual positions open


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Johan @johan_holmberg Participant
Summary

This topic contains 14 replies,
has 3 voices, and was last updated by Johan
3 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/27/2022
Status: Active
Attachments: 3 files
Logo Logo
Loading...