SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Aug 16, 2018 9:33 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Mon Sep 29, 2008 6:02 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Whenever an order is completed (OnOrderDone?) or when a position is closed (OnPositionClosed?), I would like to compare the net strategy profit with some profit target parameters.

I'm assuming I would add code in OnOrderDone or OnPositionClosed to do the test, but I get lost in all the Portfolio.GetAccountValue, GetDebt, and so on. I think I need something something simple like this, instead of me trying to walk the list of transactions to figure out a net PnL:

Code:
// simple way to check on strategy profit at end of every order/position
if (strategy/portfolio profit/loss at this point > some number/pct) {
   .. then do something
}


What is the best way of seeing if (1) the whole solution or (2) a particular strategy project is profitable at the time(s) when an order is complete or a position is closed? I'm hoping that you can give me particular code bits that explicitly use the right Portfolio.GetXXX methods...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 9:08 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6808
Hi Kevin,

What about Strategy.Performance ?

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 9:09 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
It's probably worth saying a bit more to more clearly define the problem. Suppose for now that I'm trading IBM in a single strategy, so there is only ever one instrument in my strategy portfolio at a time. If my position is 0, then I'm out of the market, and there is no position.

I'm trying to find values to fill out this formula:
Code:
Profits = PnL from closed positions (today, or any period)
    + PnL of current open position (if any)
or
Profits = RealizedPnL + UnRealizedPnL


After spending some time working with the various Portfolio.XXX and Transaction methods, it seems to me that the following things seem true:

There is no API function that gives me direct access to either term in the formula. It looks like p.GetPnL would be equivalent to GetRealizedPnL, but it is not. There is no GetRealizedPnL, and GetPnL includes a mark to market GetValue of the current open position. So GetPnL and GetValue include the current market position, instead of providing isolated access to just the Realized profits of past transactions.

GetUnrealizedPnL always returns zero for my tests, so I don't know what's going on there. I was thinking of Realized = M2Mkt - UnRealized, but Unrealized is always zero for me, so I get nowhere.

I might be able to calculate Unrealized myself, as p.Amount * (bar.Price - p.AvgPrice), and then subtract it from GetPnL to get Realized. But it seems to me at this point that I don't understand the API meanings or usage (to avoid Unrealized always zero, for example), assuming that they are all correctly implemented.

My intuitive tests for sanity are that GetRealized should not change value with each bar price change. (GetNetCashFlows is close, but it includes the cash flow to establish the current position. So if you could determine the cost of the current position, you could subtract it to get GetRealized... but you see the point. It's really harder than necessary to get these Realized and Unrealized values out of the API.

I hope all this makes sense, this is quite a detailed post dealing with picky numbers...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 9:37 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6808
I think it would be a good idea to ask Sergey and wirte some detaild docs with examples explaining all this below...

Position class members:

Name Description
GetCashFlow Returns positon cash flow
GetDebtValue Returns marked to market position debt value
GetDuration Returns position duration
GetLeverage Returns marked to market position leverage
GetMarginValue Returns marked to market position margin value
GetNetCashFlow Returns position net cash flow
GetNetPnL Returns marked to market position net PnL (profit and loss)
GetNetPnLPercent Returns marked to market position net PnL (profit and loss) in %
GetPnL Returns marked to market position PnL (profit and loss)
GetPnLPercent Returns marked to market position PnL (profit and loss) in %
GetPrice Returns marked to market position price
GetUnrealizedPnL Returns marked to market position unrealized PnL (profit and loss)
GetValue Returns marked to market position value
Top

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2008 2:16 am 
Offline

Joined: Mon Nov 24, 2008 8:44 pm
Posts: 36
Agree...any updates? In particular, what is the different between PnL, NetPnL, and unrealizedPnL...?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2008 8:11 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
If it helps any, in answer to my original question(s), Sergey kindly gave me some sample calculations that he used to test the API. From those couple of examples, I concluded the following (Anton or Sergey, please correct me if I'm wrong).

Here's how I explained the many API calls to myself:

Portfolios are modeled with several pieces (chunks of cash or value). There are three main pieces to the account:

• PositionValue – the amount of equity in open positions
• AccountValue – the amount of cash NOT in open positions
• DebtValue – the amount of liabilities borrowed on margin

Various combinations of these three basic values can be accessed by the following API calls:

• GetPositionValue gets the value of all open positions
• GetAccountValue gets the dollar value of cash not in open positions.
• GetValue gets the value of the assets = GetPositionValue + GetAccountValue.
• GetDebtValue = value of account debt liabilities (margin used)
• GetTotalEquity gets the dollar value of everything (GetValue – GetDebtValue)

Finally, GetCoreEquity seems to be a duplicate for GetAccountValue.

• GetCoreEquity gets the dollar value of cash not in open positions.

If I was to rename some API calls for better clarity, I would replace 'Account' with 'Cash' in all the GetAccountXXX calls, to give GetPositionValue, GetCashValue, GetDebtValue, because Position, Cash, and Debt are all very clear compared to Account. For example, does the Account value mean Net Liquidation Value (everything included), or Positions and Cash only, or maybe Debt included too? I think the use of the word Account is the source of almost all of the confusion.

The other source of confusion for me was the use of the word Net in some of the API calls. I could never intuitively figure out if Net meant "commissions included" or "commissions not included". If it was me, I would keep Net out of all API names except one, which could be used to fetch GetCommissionsExpense (or something like that), and then let users decide to add/subtract it from the numbers that they wanted to calculate.

Here's the code that I now use to watch for a daily profit target. It captures the account value (my definition of account value includes everything) at the BOD (beginning of day), and then compares the PnL on every bar. If the account reaches its daily profit target, the strategy quits for the day.

Code:
/// <summary>
/// Initialize the structure at BOD beginning of day.
/// </summary>
public void
InitAtBOD () {
    // reset all variables
    m_AcctBOD = me.Portfolio.GetTotalEquity ();
}

// get current profit at the arrival of each new bar
public double AcctPnl {
    get { return (me.Portfolio.GetTotalEquity () - m_AcctBOD); }
}

// quit trading if daily profit target was reached
    // calculate current daily profit
    double profit = AcctPnl;

    // quit for the day if profit target has been reached
    if (profit >= ProfitLimitPerDay) {
        fmt = "{0}  Reached daily profit target. {1}";
        Console.WriteLine (fmt, BD (bar), ProfitLimitPerDay);
        .. shut down the strategy
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 24, 2009 6:03 pm 
Offline

Joined: Fri May 06, 2005 1:40 am
Posts: 520
kkkwj wrote:
GetUnrealizedPnL always returns zero for my tests, so I don't know what's going on there. I was thinking of Realized = M2Mkt - UnRealized, but Unrealized is always zero for me, so I get nowhere.


Hi just a quick note on this: from memory this problem is (or was it may be fixed by now?) a small bug in the core framework: GetUnrealizedPnL is the only Position class method that does not check if Instrument.Factor is zero. As a work around set your instrument factor to 1 and you should get correct answers.

Its a simple fix in the core framework with:
Code:
   if (fInstrument.Factor != 0)         
         return pnL * Instrument.Factor;
    return pnL;


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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