SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Sep 24, 2017 10:17 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Thu Jul 12, 2012 5:08 pm 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
Hi,

Could someone please help?

I’m using the Portfolio.GetCoreEquity() in a position sizing algorithm. I’m doing some testing in Simulation mode. I’m seeing some very funny values from this method. I’m modelling an account on $20,000. In one instance this is returning X.
Attachment:
Account Size - GetCoreEquity return value.JPG
Account Size - GetCoreEquity return value.JPG [ 10.93 KiB | Viewed 6910 times ]

I’ve inspected the values in VS2010. There is only one other Position open isn’t anything away from what you would expect on.
Attachment:
Account Size - Other postions.JPG
Account Size - Other postions.JPG [ 49.73 KiB | Viewed 6910 times ]

The code that is generating this value:
Attachment:
Account Size - GetCoreEquity code.JPG
Account Size - GetCoreEquity code.JPG [ 12.04 KiB | Viewed 6910 times ]

Could some please help? This doesn’t make sense.

Thanks and regards,

drolles


Top
 Profile  
 
PostPosted: Thu Jul 12, 2012 5:35 pm 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
Further to this, in this instance the position in question is a AUDJPY. Prior to this I also had an issue with AUDUSD as well, but I changed the line of code from Portfolio.GetAccountValue() to the one below (i.e. GetCoreEquity) and the problem on AUDUSD went away. This makes me suspect a problem.

Thanks and regards,

drolles


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 1:57 am 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
I've been testing another strategy, which uses a related fixed factional position sizing. The position PnL and the account PnL are not reconciling.

Below is the output of the following code:

Code:
double CAccountSize = Portfolio.GetCoreEquity();

        Console.WriteLine("Core Equity Value :" + Portfolio.GetCoreEquity());

50,000
49,850
50,310
50,210
50,110
50,158
115,823
244,889
510,569
117,448
117,048
51,272
106,694
51,336
50,769
105,435
105,185
224,222
225,536
424,896
840,506
1,924,864
1,915,964
51,691
51,591
105,567
204,211
534,773
53,243
53,267
53,477
53,585
53,435

Below is the output of the positions PnL from the Results window

-1,068
-790
-567
-534
-217
-186
-100
-93
-80
-18
-16
-8
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
24
49
50
56
72
85
98
108
108
144
166
174
210
243
304
667
936
975
1,144
1,734

Can some some please help here? Am I using this function incorrectly or is there a bug?

Thanks and regards,

drolles


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 1:00 am 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
Hi, can I please have some support here? Thanks, drolles


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 8:54 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6769
Hi, Sergey was looking into this issue yesterday and will reply later today. Sorry for delay.


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 2:28 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi Drolles,

It is strange that the PnL is increasing all the time. Are you sure you didn't sort the transaction by the PnL column?

In which handler do you print Portfolio.GetCoreEquity()?

GetCoreEquity should return your current account equity (that increase each time you sell something and decrease each time you buy something).

Regards,
Sergey.


Top
 Profile  
 
 Post subject: examples and code
PostPosted: Thu Jul 19, 2012 9:45 am 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
Sergey,

Thanks very much for the reply.

I should have been clearer; this PnL is sorted from smallest to largest. Please don’t be confused about that.

I was trying to show with that the “large” losses shouldn’t be happening. This model uses a $20,000 account with a fixed fractional position sizing algorithm of a 1% loss. Therefore, initially the losses shouldn’t exceed $200 (roughly, the spread sometimes makes the losses slightly larger).

The print is happening on the call to a method I’ve written which calculates the position size. See below. There is nothing proprietary there so I’m happy to share it.

I’ve done some further research; trying to narrow it down. I've got OQ doing a Console WriteLine on the results of GetValue, GetCoreEquity and GetTotalEquity. You can see the examples of the “bad” values from GetCoreEquity in the example below:

Current Account GetValue = 19401.6000000002
Current Account GetCoreEquity = 19377.1000000002
Current Account GetTotalEquity = 20077.8000000001
Current Account GetValue = 424352.3
Current Account GetCoreEquity = 19173.6000000002
Current Account GetTotalEquity = 19835.1000000002
Current Account GetValue = 424109.6
Current Account GetCoreEquity = 924144.1
Current Account GetTotalEquity = 19512.9000000001
Current Account GetValue = 470680.1
Current Account GetCoreEquity = 19353.6000000002
Current Account GetTotalEquity = 19483.1000000002
Current Account GetValue = 325388
Current Account GetCoreEquity = 18983.5000000002
Current Account GetTotalEquity = 18983.5000000002
Current Account GetValue = 18983.5000000002
Current Account GetCoreEquity = 18983.5000000002
Current Account GetTotalEquity = 18983.5000000002

Code:
    public int PositionSize(double stopsize, double rpercent)
    {


        double CAccountSize = Portfolio.GetTotalEquity();

        Console.WriteLine("Current Account GetCoreEquity = " + Portfolio.GetCoreEquity());
        Console.WriteLine("Current Account GetTotalEquity = " + Portfolio.GetTotalEquity());
        Console.WriteLine("Current Account GetValue = " + Portfolio.GetValue());

        if (debug)
        {

            Console.WriteLine("Current Account Size = " + CAccountSize);
            Console.WriteLine("Stop size " + stopsize);

        }


        // Check to see what we have been passed as stop size
        // if it is in terms of an int i.e. 30.0 or is
        // is it in pricing terms e.g. 0.0030 or 0.30
        // N.B: For Yen crosses this is only robust
        // for anything less than 100 yen i.e. 1.00

        if (GetTickSize(Instrument) == 6)
        {

            stopsize = stopsize * 100;

        }
        else if (GetTickSize(Instrument) == 7)
        {

            stopsize = stopsize * 100;

        }
        else if (GetTickSize(Instrument) == 4)
        {

            stopsize = stopsize * 10;

        }
        else if (GetTickSize(Instrument) == 5)
        {

            stopsize = stopsize * 10000;

        }
        else if (GetTickSize(Instrument) == 3)
        {

            stopsize = stopsize * 100;

        }

        if (debug)
        {

            Console.WriteLine("Current Account Size = " + CAccountSize);
            Console.WriteLine("Stop size " + stopsize);

        }

        double PSize = (double)Math.Round((CAccountSize * rpercent) / stopsize, 2);

        // Can't trade less than a single lot
        // This means that risk management has kicked in
        // we need to reject this trade.

        if (Instrument.Type == InstrumentType.FX)
        { // trade mini - lots

            if (PSize <= 0.09)
            {

                PSize = 0;
                return (int)PSize;

            }
            else
            {

                if (GetTickSize(Instrument) == 6)
                {

                    PSize = (Math.Round(PSize, 0) * 1000);

                }
                else if (GetTickSize(Instrument) == 5)
                {

                    PSize = (Math.Round(PSize, 0) * 10000);

                }
                else if (GetTickSize(Instrument) == 3)
                {

                    PSize = (Math.Round(PSize, 0) * 100);

                }
                else if (GetTickSize(Instrument) == 4)
                {

                    PSize = (Math.Round(PSize, 0) * 1000);

                }
                else if (GetTickSize(Instrument) == 2)
                {

                    PSize = (Math.Round(PSize, 0) * 100);

                }

                return (int)PSize;

            }

        }
        else if (Instrument.Type == InstrumentType.Stock)
        {

            if (PSize <= 0.09)
            {

                PSize = 0;
                return (int)PSize;

            }
            else
            {

                PSize = (Math.Round(PSize, 1) * 100);

                return (int)PSize;

            }


        }
        else
        {


            Console.WriteLine("Error - unknown instrument type - 70 @ Risk Manager");

            return (int)PSize;


        }

        //return 0.0;

    }


Thanks for your help on this issue.

Kind regards,

drolles


Top
 Profile  
 
PostPosted: Thu Jul 19, 2012 10:39 am 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi Drolles,

1) Are you using several currencies in your system?

2) Do you have non-zero Margin in the instrument(s) you trade?

3) I've noticed that you print GetCoreEquity first, then GetTotalEquity and finally GetValue. While in the sample output you attached I see that GetValue comes first. Should I just ignore the first line? I am trying to match all 3 values.

Regards,
Sergey.


Top
 Profile  
 
PostPosted: Thu Jul 19, 2012 1:42 pm 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
Sergey,

Thanks for the reply.

1) Yes, very much so. To do a fair test in this instance I used about 4 (e.g. as I don’t remember specifically AUDJPY, AUDUSD, GBPJPY, etc) FX crosses.
2) Yes, please see my posts on modelling margin here: viewtopic.php?f=59&t=10227. All of these instruments use 0.05 as margin.
3) Yes, very good spot. I suspect that I’ve made some adjustments to the code since running that test. I think it was a few days ago as I was trying to find some workarounds as I didn’t have a solution to the problem. Please take note of the printed values, it describes which of the calls is being done.

Thanks and regards,

drolles


Top
 Profile  
 
 Post subject: Further testing
PostPosted: Thu Jul 19, 2012 11:49 pm 
Offline

Joined: Sat Feb 25, 2012 8:51 pm
Posts: 154
Sergey,

Further to this, I’ve done some testing after your through provoking questions.

I realise that one of the things I’d been working on recently in the margining calculations to better replicate Currenex’s / FXDD’s (Anton has been really helpful in getting some connectively issues fixed).

I’ve backtested a strategy with 4 instruments in it – AUDUSD, AUDJPY, EURUSD and EURGBP. But before I backtested I zeroed the margin requirements and left the code the same. The following results occurred:

Current Account GetValue = 18704.2
Current Account GetCoreEquity = -237374.4
Current Account GetTotalEquity = 18732.0999999999
Current Account GetValue = 18732.0999999999
Current Account GetCoreEquity = 18700.3
Current Account GetTotalEquity = 18700.3
Current Account GetValue = 18700.3
Current Account GetCoreEquity = -496934.7
Current Account GetTotalEquity = 18725.3
Current Account GetValue = 18725.3
Current Account GetCoreEquity = -496826.7
Current Account GetTotalEquity = 19558.3
Current Account GetValue = 19558.3
Current Account GetCoreEquity = -496971.9
Current Account GetTotalEquity = 19323.1
Current Account GetValue = 19323.1
Current Account GetCoreEquity = -344421.5
Current Account GetTotalEquity = 19898.5000000001
Current Account GetValue = 19898.5000000001
Current Account GetCoreEquity = 19495.9000000001
Current Account GetTotalEquity = 19495.9000000001
Current Account GetValue = 19495.9000000001

How could Core Equity turn negative, then back to positive by that amount? Doesn’t make sense.

You can also see from the PnL of the positions (again sorted in the same way as the previous post) that none of the losses are significantly over $200; meaning that the positions sizes calculated by the PositionSize method is generating the correct positions sizes and the account equity (GetTotalEquity) being generated is correct.

-237.6
-219
-209
-203.5
-196.1
-192.5
-191.4
-190
-189.6
-189.1
-189.1
-189.1
-189
-187.5
-185.6
-184.8
-183.6
-180.6
-180
-178.5
-176.3
-175.5
-165
-161.2
-145.2
-138.6
-137.6
-128.8
-100.8
-94.3
-72.2
-60
-58.5
-48.4
-48.4
-48.3
-36
-7
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3.1
26.4
108
142
289.8
381.3
411.6
680.6
945


Thanks and regards,

Dan


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group