Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Obtaining Exchange Rate for Strategy PnL

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

    Obtaining Exchange Rate for Strategy PnL

    I'm having difficulty keeping my strategy PnL in sync with my account PnL when the base currency is anything other than USD. In order to keep my strategy PnL consistent with my account PnL, I have been using this formula (let's assume we're just entering long positions):

    ((Sell Price*100000)-(Buy Price*100000))*Exchange Rate

    So for example, if I'm exchanging EURGBP and I buy at 0.85427 and sell at 0.85481 with an exchange rate of 1.24785 USD per GBP, my profit should be roughly $67.38. To use the formula:

    ((0.85481*100000)-(0.85427*100000))*1.24785 = 67.3839

    This is consistent with the account PnL shown in the Accounts tab.

    But my problem is how do I get the exchange rate? I have been searching and haven't been able to find any methods that I can call to obtain the exchange rate for use in that formula. How can I obtain the exchange rate so my strategy can correctly track PnL? If there is no method for obtaining the exchange rate, how are people accurately tracking PnL in their strategies?

    #2
    Hello,

    Thank you for your post.

    A NinjaScript representative will let you know of their findings.

    We look forward to assisting further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      Thank you for your patience.

      I have found this formula for calculating cross currency profit loss from this publicly available website:
      Find out more about our financing fees and charges, holding and duration charges, how they're calculated and when they're applied.


      (Closing Rate - Opening Rate) * (Closing {quote}/{home currency}) * Units

      You will need to add the GBPUSD instrument to your script to calculate its exchange rate.

      Use
      Code:
      AddDataSeries(...);
      More on AddDataSeries here:


      In your OnBarUpdate method, you will use the BarsInProgress property.

      More on BarsInProgress here:


      The BarsInProgress variable will tell you which data series is being worked on in the OnBarUpdate method. Your primary data series will be BarsInProgress == 0, secondary will be BarsInProgress == 1, and so on.

      Here is a short example:

      Code:
      ...
      
      else if (State == State.Configure)
         {
          AddDataSeries("GBPUSD",BarsPeriodType.Minute, 5); //Tailor time series to your specific needs. 
         }
        }
      
        protected override void OnBarUpdate()
        {
           
         // Check which Bars object is calling the OnBarUpdate() method 
            if (BarsInProgress == 0) 
            { 
                //Calculate EURGBP data here
            } 
            else if (BarsInProgress == 1) 
            { 
                 //Calculate GBPUSD data here
            }
         
        }
      Your formula would be something like this in:

      Code:
      protected double storage;
      
      protected double entryPrice; //assumed to have already been instantiated 
      protected double exitPrice; //assumed to have already been instantiated 
      ...
      
      else if (State == State.Configure)
         {
          AddDataSeries("GBPUSD",BarsPeriodType.Minute, 1); //Tailor time series to your specific needs. 
         }
        }
      
        protected override void OnBarUpdate()
        {
           
            // Check which Bars object is calling the OnBarUpdate() method 
            if (BarsInProgress == 0 && storage != null) 
            { 
                (exitPrice - entryPrice)*(storage)*(100000);  
            } 
            else if (BarsInProgress == 1) 
            { 
               storage = GetCurrentAsk();
            } 
         
        }
      Please let me know if I may be of any further assistance.
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        I am also trying to fetch current exchange rate into a strategy to determine position size. Is there a "best approach" to this?

        Comment


          #5
          Hi mase2005, thanks for your post.

          The exchange rate of a forex instrument would just be the price itself. You can access price data in a strategy or indicator with any of the price series arrays:



          For a guide on multi time frame scripts please see this page:



          Please let me know if you have any further questions.
          Chris L.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          435 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          3 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          18 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by frslvr, 04-11-2024, 07:26 AM
          9 responses
          127 views
          1 like
          Last Post caryc123  
          Started by selu72, Today, 02:01 PM
          1 response
          14 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Working...
          X