• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Mean reversion spread strategy

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Mean reversion spread strategy

    I have the below c# code for creating a Ornstein-Uhlenbeck model. Could anyone advise how easy it is to convert this into something usable in NT7?



    namespace YourNameSpaceHere
    {
    internal sealed class OrnsteinUhlenbeck
    {
    internal Dictionary<string, double> CalibrateOrnsteinUhlenbeck(GeneralMatrix generalMatrix, int delta)
    {
    int n = generalMatrix.RowDimension;
    double sX = 0;
    double sY = 0;
    double sXX = 0;
    double sXY = 0;
    double sYY = 0;

    for (int i = 0; i < n - 1; i++)
    {
    sX += generalMatrix.GetElement(i, 0);
    sY += generalMatrix.GetElement(i + 1, 0);
    sXX += System.Math.Pow(generalMatrix.GetElement(i, 0), 2);
    sXY += generalMatrix.GetElement(i, 0)*generalMatrix.GetElement(i + 1, 0);
    sYY += System.Math.Pow(generalMatrix.GetElement(i + 1, 0), 2);
    }
    double mu = (sY*sXX - sX*sXY)/(n*(sXX*sXY) - (System.Math.Pow(sX, 2) - sX - sY));
    double lambda = -System.Math.Log((sXY - mu*sX - mu*sY + n*System.Math.Pow(mu, 2)));
    double a = System.Math.Exp(-lambda*delta);
    double sigmah2 = (sYY - 2.0D*a*sXY + System.Math.Pow(a, 2.0D)*sXX - 2.0D*mu*(1.0D - a)*(sY - a*sX) +
    n*System.Math.Pow(mu, 2.0D)*System.Math.Pow(1.0D - a, 2.0D))/n;
    double sigma = System.Math.Pow(sigmah2*2.0D*lambda/(1.0D - System.Math.Pow(a, 2.0D)), 0.5D);
    var results = new Dictionary<string, double> {{"mu", mu}, {"sigma", sigma}, {"lambda", lambda}};
    return results;
    }
    }
    }

    #2
    novamatic, we would unfortunately not be familiar with the model or it uses but the core C# code using the dictionary should be easily usable in NinjaScript / C#.
    BertrandNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by U0000999, Yesterday, 11:49 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_ChrisSch  
    Started by Mberg, Today, 03:29 AM
    0 responses
    4 views
    0 likes
    Last Post Mberg
    by Mberg
     
    Started by TazoTodua, Today, 03:24 AM
    0 responses
    4 views
    0 likes
    Last Post TazoTodua  
    Started by ahadari28, Yesterday, 04:01 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_ChrisSch  
    Started by NinjaCustomer, Today, 12:14 AM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Marco  
    Working...
    X