- ago
I'm using TD Ameritrade. I've tried the code below, but I can't seem to get live values. All I really need is the current cash available for purchasing. I get old values.
I believe I grabbed this code from the "Access broker account info programmatically" article.

Thanks!

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript39 {    public class MyCheckBroker : UserStrategyBase    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          BrokerAccount brokerAccount = GetBrokerAccount(IRA); /* IMPORTANT!!! pass your Account ID here */          if (brokerAccount != null)          {             WriteToDebugLog("Account Value = " + brokerAccount.AccountValue);             WriteToDebugLog("Buying Power = " + brokerAccount.BuyingPower);             WriteToDebugLog("Cash = " + brokerAccount.Cash);             //Instead of Cash IB sets each currency balance             foreach (KeyValuePair<string, double> kvp in brokerAccount.CurrencyBalances)             {                WriteToDebugLog(kvp.Key + " = " + kvp.Value);             }             // for IB, Turn on Streaming Last Price Updates             WriteToDebugLog("Symbol, Quantity, BasisPrice, CurrentPrice, Value");             foreach (BrokerPosition p in brokerAccount.Positions)             {                WriteToDebugLog(p.Symbol + "\t" + p.Quantity + "\t" + p.BasisPrice.ToString("N2") + "\t" + p.CurrentPrice.ToString("N2") + "\t" + p.Value.ToString("N2"));             }          }          else          {             WriteToDebugLog("Make sure WealthLab is connected to the account");          }       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {       }       private BrokerAccount GetBrokerAccount(string acct)       {          foreach (BrokerBase broker in SignalManager.Brokers)          {             BrokerAccount ba = broker.FindAccount(acct);             if (ba != null)                return ba;          }          return null;       }           //declare private variables below       //this method looks through all of the installed brokers, for the specified account       //and returns the live broker position for the specified symbol.       private BrokerPosition GetBrokerPosition(string acct, string symbol, PositionType pt)       {          foreach (BrokerBase broker in SignalManager.Brokers)          {             BrokerAccount ba = broker.FindAccount(acct);             if (ba != null)                return ba.FindPosition(symbol, pt);          }          return null;       }       private string IRA = "my account id";    } }
0
681
16 Replies

Reply

Bookmark

Sort
Cone8
 ( 7.84% )
- ago
#1
If you put your Account ID ("123456789") where it says IRA, it should work.
What problem are you seeing?
0
- ago
#2
I put in the correct account number, but I’m getting wrong values when I run it in the backtester. I really need the “available funds for trading”. That’s what I’m looking for.

Thanks!

0
Cone8
 ( 7.84% )
- ago
#3
That's BuyingPower, right?
What do you mean by "wrong values"? Are you getting values from the wrong account or something?

By the way, the Backtester has nothing to do with your live account. The Backtester uses the Starting Equity that you specify, etc.
Live account equity only comes into play when you're sizing positions for live trading signals.
0
- ago
#4
Yes, it's BuyingPower that I'm looking for. They are not the amounts I expect from the TDA account.

Will I get the correct BuyingPower when the strategy is attached to the Strategy Monitor or Chart?
0
Cone8
 ( 7.84% )
- ago
#5
It doesn't matter how you run the strategy. These are the values returned by TDA when you ask for them.

Look at it in the Accounts tool. Are Account Value, Cash, and Buying Power correct? Are any correct? Again, if you're connected to your account, these values are coming from TDA.
0
- ago
#6
For my IRA account, the account value is correct, buying power is 0 and cash is 0.
Buying power should be 14.04.

For my Cash account, all values are correct.

I am using TDA.

Thank you for your help.

Mike
0
Cone8
 ( 7.84% )
- ago
#7
If there's no error, these are the values provided by TDA.

Make sure you have the latest version of the TD Broker Provider extension.
Don't connect to your account from more than one machine.
0
- ago
#8
There are no errors. I am using Build 35 and TD Extension 13 which is the latest and greatest. I have another way of getting the info I need outside of your software. I'll go that route.

Please don’t take my comments the wrong way. Your software works great and I appreciate all the effort your team puts into it and customer support.

Thanks for your help.

Mike
0
Cone8
 ( 7.84% )
- ago
#9
No problem. It's just inconceivable at this time, programming-wise, how you can get right the Account Value, but not the cash and buying power. All of these values are returned in the same "payload", a request response. For this reason and because there are no errors, I can only assume these are the values reported by TDA.

fwiw, I trade an IRA at TDA too, and all the values are correct for my account.
0
- ago
#10
Are you using any strategy code to get the BuyingPower from TDA?

That’s where I’m seeing a zero.


0
Cone8
 ( 7.84% )
- ago
#11
Of course. I ran the example in the first post. All correct.
0
- ago
#12
Are you using a cash account or an IRA? I'm using both with TDA.
When I run the code in Post #1, it produces the following using the account id's for my IRA and then my CASH account. Why is the BuyingPower a 0 for the IRA account?

Using my CASH account, I manually purchased 1 share of TSLA. When I run code again, it doesn't update. I'm wondering why. Or is there a way to force an update? Or does it update these values once a day?


IRA account
Account Value = correct account balance
Buying Power = 0
Cash = 0
Symbol, Quantity, BasisPrice, CurrentPrice, Value
MMDA1 correct quantity 1.00 1.00 correct value
TSLA 28 160.77 182.36 5,106.08

CASH account
Account Value = correct value
Buying Power = correct value
Cash = correct value
Symbol, Quantity, BasisPrice, CurrentPrice, Value
0
Cone8
 ( 7.84% )
- ago
#13
Mine is an IRA.

Are you accessing both accounts with the same consumer key? i.e., in one instance of WealthLab?
0
- ago
#14
Yes and Yes.
I’ll try it with two instances.
0
- ago
#15
For cash, the buying power is now right in the second instance. In the first instance, it’s still wrong after buying TSLA.

For IRA, the buying power is still wrong in both instances. It’s a zero.

0
Cone8
 ( 7.84% )
- ago
#16
QUOTE:
I’ll try it with two instances.
It shouldn't be a problem with one, but I'm just looking for a reason it's different. We don't have 2 accounts to test (and all this is going to change with Schwab anyway), but the thing is, you're getting the right value for the account value. That's received in the same object as the other values; i.e., if you have one, you should have them all. There's no way to tweak that as far as I can see.

As for the update, it should be automatic, but did you try clicking the "Request Updates from Broker(s)" button?
0

Reply

Bookmark

Sort