Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IB - Account.Get() Currency does not appear to work

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

    IB - Account.Get() Currency does not appear to work

    Using Interactive Brokers (IB), in the method Account.Get() the parameter Currency does not appear to work. (https://ninjatrader.com/support/helpGuides/nt8/get.htm)

    Based on JoshG's post (https://ninjatrader.com/support/foru...61#post1048061),
    Locking the Account does not appear to resolve the issue.

    Code:
    public class SampleStrat : Strategy
    {
        private Account myAccount;
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Calculate = Calculate.OnPriceChange;
                lock (Account.All)
                      myAccount = Account.All.FirstOrDefault(a => a.Name == "[Live Account Name]");
            }
        }
        protected override void OnBarUpdate()
        {          
            if (CurrentBar != 100) return;
            Print("BuyingPower USD : " + myAccount.Get(AccountItem.BuyingPower, Currency.UsDollar));
            Print("BuyingPower AUD : " + myAccount.Get(AccountItem.BuyingPower, Currency.AustralianDollar));
        }
    }
    I understand "NinjaTrader will attempt to convert currency for forex and futures trades". (https://ninjatrader.com/support/help...counts_tab.htm)

    The IB connection Market Data Subscriptions include US Securities Snapshot and Futures Value Bundle and IDEAL FX. The connection has access to the required cross rate, AUDUSD. The IB Base Currency is AUD (set in IB) (I am in Australia). The Account Denomination is AUD (set in NT8). The Account.Get method returns an AUD value regardless of the parameter Currency (i.e. UsDollar or AustralianDollar).

    Please assist.
    Last edited by Shansen; 01-09-2020, 05:44 PM.

    #2
    Hello Shansen,

    Thank you for your reply.

    If you try something like this, where we are initially getting the values in State.SetDefaults, and then updating them in OnAccountStatusUpdate, do you see the Buying Power coming through in the prints?

    Code:
        public class aaaTestIBAcctInfo : Strategy
        {
            private Account myAccount;
            private double usdValue;
            private double audValue;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "aaaTestIBAcctInfo";
                    Calculate                                    = Calculate.OnPriceChange;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
    
                    lock (Account.All)
                        myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
    
                        // Subscribe to static events. Remember to unsubscribe with -= when you are done
                           Account.AccountStatusUpdate += OnAccountStatusUpdate;
    
                    Print("Account Locked");
                    if (myAccount != null)
                    {
                         usdValue = myAccount.Get(AccountItem.BuyingPower, Currency.UsDollar);
                         audValue = myAccount.Get(AccountItem.BuyingPower, Currency.AustralianDollar);
                        Print("BuyingPower USD in State==State.SetDefaults: " + usdValue);
                        Print("BuyingPower AUD in State==State.SetDefaults: " + audValue);
                    }
                    else if (myAccount == null)
                    {
                        Print("Account is null");
                    }
    
    
                }
                else if (State == State.Terminated)
                {
                    // Unsubscribe to events
                    Account.AccountStatusUpdate -= OnAccountStatusUpdate;
                }
            }
    
            protected override void OnBarUpdate()
            {
                 if (CurrentBar != 100) return;
                    Print("BuyingPower USD : " + usdValue);
                    Print("BuyingPower AUD : " + audValue);
    
            }
    
            private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
            {
                usdValue = e.Account.Get(AccountItem.BuyingPower, Currency.UsDollar);
                audValue = e.Account.Get(AccountItem.BuyingPower, Currency.AustralianDollar);
            }
        }
    Obviously you'll need to replace the reference to Sim101 with your account name.

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Subscribing to static events does not resolve the issue.
      The Account.Get method returns an AUD value regardless of the parameter Currency (i.e. UsDollar or AustralianDollar).
      Please find below the output.
      Code:
      Account Locked
      BuyingPower USD in State==State.SetDefaults: 123.45
      BuyingPower AUD in State==State.SetDefaults: 123.45
      BuyingPower USD : 123.45
      BuyingPower AUD : 123.45

      Comment


        #4
        Hello Shansen,

        Thank you for your reply.

        Ah, I see, you're expecting a different value there - I was assuming no values were coming through, that's my fault. The reason you're not seeing a difference is that the Currency parameter there has no affect on the value, only the display of the value throughout the platform.

        There is no method in NinjaScript to convert the currency. However, there's a script on this thread on the forums that may be helpful in trying to do those calculations yourself:



        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          As I understand it, this solution depends on iGoogle.
          iGoogle was retired on November 1, 2013 (https://stackoverflow.com/questions/...google-finance)
          Last edited by Shansen; 01-15-2020, 07:20 PM.

          Comment


            #6
            Hello Shansen,

            Thank you for your reply.

            That's correct, it's an older example that would need to be adapted to use incoming Forex data.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Please find below my (crude) solution. Any improvements from the forum are welcome.
              Code:
              protected override void OnStateChange()
              {
                  ...
                  else if (State == State.DataLoaded)
                  {
                      var audUsdFxRate = GetFxRate("AUD", "USD");
                      Print(audUsdFxRate);
                  }
              }
              
              public static double GetFxRate(string fromCurrency, string toCurrency)
              {
                  // TODO async?, tryblock?, process using Newtonsoft.Json?
                  if (fromCurrency == toCurrency) return 1;
                  var web = new WebClient();
                  var url = string.Format("https://api.exchangeratesapi.io/latest?base={0}&symbols={1}", fromCurrency.ToUpper(), toCurrency.ToUpper());
                  var response = web.DownloadString(url);
                  web.Dispose();
                  return Convert.ToDouble(response.Substring(16, 8));
              }

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,789 views
              0 likes
              Last Post aligator  
              Started by Jimmyk, 01-26-2018, 05:19 AM
              6 responses
              837 views
              0 likes
              Last Post emuns
              by emuns
               
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,294 views
              1 like
              Last Post jgualdronc  
              Started by Touch-Ups, Today, 10:36 AM
              0 responses
              13 views
              0 likes
              Last Post Touch-Ups  
              Started by geddyisodin, 04-25-2024, 05:20 AM
              11 responses
              63 views
              0 likes
              Last Post halgo_boulder  
              Working...
              X