Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Difficulty trying to start automated trading

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

    Difficulty trying to start automated trading

    I have been developing and backtesting strategies for several months and am now ready to transition to live trading, but I want to first use the Sim101 account to understand how things work in real-time trading. Frankly, this has been very perplexing to me.

    To understand how things work in real time, I am using the SampleMACrossover strategy on 1 minute bars to generate plenty of activity. I have applied it to an instrument list of 30 Dow stocks. I am adding the strategy to the Strategies tab of Control Center and then enabling each of the 30 rows.

    The first issue that occurs is that I get a whole series of error messages such as "Your strategy position(s) for instrument(s) CAT are not in sync with your account position(s). This could cause serious issues as the strategy places live orders." It appears that this is because NT is running the strategy for some period of time prior to when I started the strategy. Is there a way to prevent it from doing this? I tried setting "Days to Load" = 0, but it changes back to 1. I have specified "Wait until flat before executing live, Sync account position = false". I don't understand why it is desirable to run the strategy prior to starting it in real time; it seems to directly and unnecessarily cause sync problems. Am I misunderstanding the "Days to Load" parameter?

    If I understand correctly, I can just ignore those error messages, provided my account has no open positions. But, it is extremely annoying to have to close each of those message boxes one at a time. Is there a way to suppress generating them?

    Is there something very basic about using NinjaTrader that I am missing? It does not seem that it should be so cumbersome to run. I have read several times the parts of the user manual that deal with the differences between Strategy positions and Account positions.

    #2
    Hi rmt81,

    Yes, some find it desirable to only run strategy on real time data, to prevent a historical strategy position and always start the strategy in a flat state ready to take the first entry signal.

    To force a real time only strategy requires a code change. To make modifications to the sample, Right Click in the NinjaScript editor > Save as to provide a name.

    Add the following line to the top OnBarUpdate()
    if (Historical) return;



    With this configuration, you should increase days to load to allow proper indicator calculations and for the minimum bars required to be satisfied. Min bars required is still satisfied from the first x specified historical bars even though you're not running any of the main strategy logic on these historical bars.

    This should clear up a lot of the issues you may be having and the operation should be more like you expect. There's unfortunately no way to suppress the NT pop-ups.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      The next problem I encountered is that I began getting a series of message boxes like "TD Ameritrade, Order has exceeded available account equity: Please reduce order size affected Order: Sell 196 Market". I will start by pointing out that it would be more helpful if the message box indicated which instrument it pertained to.

      Following this message, I immediately get another message box similar to "Strategy 'SampleMACrossover/7e4262ec90cb4743b922221fa54ca4e1a' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests, attempted to close this position and terminated itself.

      This is apparently because I specified "Set order quantity" = "by account size". I did this because setting a fixed number of shares seems impractical; for some stocks the cost would exceed the account buying power, while for others the trade size would be too small to justify the commision. I would prefer to have the option of specifying the dollar value of the trade, perhaps rounded to the nearest round lot of shares. What I really expected to be able to do was to invest in about six different stocks at any given point in time each with a value equal to about 1/6 the value of the account. I assumed that in the end, I would need to set the quantity in the strategy, but I have understood that NT does not support providing that information to the user. Is that correct, or does NT have a supported way to reach my objective? It does not seem that it would be that uncommon.

      I assume the reason I am getting this pair of error messages routinely is that an entry condition occurs while nearly the full value of the account is invested in a different stock. I had assumed that an order that exceeded buying power would be rejected and the strategy would continue until the first position was closed and then another could be taken. Therefore, closing the strategy seems very inconvenient. The implication is that I can not keep strategies open very long. Does NT have a supported way to deal with this situation?

      Comment


        #4
        Ryan::

        Thanks. Will Historical == true when I am backtesting? Is there another variable to determine whether I am backtesting or not?

        Comment


          #5
          First is to work with the Cash Value field in your simulated account. If you've run out of simulated money you can increase cash value and reset using these steps.

          Resetting a simulation account will clear all historical trade data from this account. Please follow the instructions below to reset a simulation account.

          * From the NinjaTrader Control Center window select the menu Tools > Options
          * Select the "Simulator" tab
          * Adjust your cash value
          * Press the "Reset" button

          Setting order quantity by AccountSize is expanded here:


          There's no restrictions on order quantity for order methods except it must be a whole number (no decimals). If you have a different calculation or preference, can custom code it.

          Yes, Historical is always true in a backtest. There is not a dedicated property to check specifically for backtest.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            OK, while it is easy to add simulated money to the account, the situation I am trying to simulate is that I have invested what I have available to invest, so adding real money to the real account is not so easy. Furthermore, the point of using automated trading software is to not have to do all of these manual steps. Therefore, I will try to restate my question:

            From within a strategy, how do you determine whether the account has enough cash and/or margin to support taking an additional new position?

            I tried using GetAccountValue(AccountItem.BuyingPower), but it behaves much different than I expected, apparently getting larger when I sell short.

            Thanks

            Comment


              #7
              Hi there, on live accounts, the GetAccountValue() properties are reported directly from the broker. That is odd that TDA reports larger values for short positions. Would AccountItem.CashValue help out here?

              You could create a variable that keeps track of how much money is in your account and then base the quantity off of that, adding and subtracting as necessary from your trade profits/losses. You'd want to work with the Peformance objects for that - http://www.ninjatrader.com/support/h...erformance.htm.
              AustinNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_RyanM View Post
                Hi rmt81,

                Yes, some find it desirable to only run strategy on real time data, to prevent a historical strategy position and always start the strategy in a flat state ready to take the first entry signal.

                To force a real time only strategy requires a code change. To make modifications to the sample, Right Click in the NinjaScript editor > Save as to provide a name.

                Add the following line to the top OnBarUpdate()
                if (Historical) return;



                With this configuration, you should increase days to load to allow proper indicator calculations and for the minimum bars required to be satisfied. Min bars required is still satisfied from the first x specified historical bars even though you're not running any of the main strategy logic on these historical bars.

                This should clear up a lot of the issues you may be having and the operation should be more like you expect. There's unfortunately no way to suppress the NT pop-ups.
                What is the most reliable way to test within strategy code whether that strategy was called for backtesting by Strategy Analyzer or for "real-time" trading (e.g. from the Control Center, even if the data feed is a replay of previously recorded data.

                There are too many aspects of NT that depend on this context. Examples include using Historical to turn off unwanted historical data prior to starting "real-time" trading. (I still do not understand why "Days to Load" can't be zero). It also seems that GetAccountValue() returns only zero when running in a backtest (if that is not true, please help me figure out what I am doing wrong there).

                I am tired of commenting out lines of code depending on the context in which I am trying to run a strategy.

                Thanks



                Comment


                  #9
                  GetAccountValue() produces wrong results with Sim101

                  Originally posted by NinjaTrader_Austin View Post
                  Hi there, on live accounts, the GetAccountValue() properties are reported directly from the broker. That is odd that TDA reports larger values for short positions. Would AccountItem.CashValue help out here?

                  You could create a variable that keeps track of how much money is in your account and then base the quantity off of that, adding and subtracting as necessary from your trade profits/losses. You'd want to work with the Peformance objects for that - http://www.ninjatrader.com/support/h...erformance.htm.
                  Austin:

                  I was running in the Sim101 account and using TDA only as the data feed; therefore this is a problem with the bookkeeping of the Sim101 account and not a TDA problem.

                  Running with replay data over the weekend, I also discovered that Buying Power seems to be consistently twice what the cash value is (I have the Margin set at 50%). If I buy a stock long, the cash value is decremented by the amount of the purchase (that seems correct to me). But, then the new buying power is twice the cash value. I would have thought that the buying power would also have been decremented by the amount of the purchase. This, together with the increase in buying power to completely unreasonable levels after executing several short sells, suggests to me that GetAccountValue() does not work correctly with the Sim101 account.

                  Admittedly, I am new to trading on margin and have briefly read, but not studied in depth all the different types of margin requirements. I intended to use the Sim101 account to understand these before trading with real money.

                  I have not yet spent any money on NinjaTrader, although I have invested hundreds of hours on it. I don’t plan to spend any money on it until I can get a simulation to work correctly. Frankly, I am getting pretty frustrated with NinjaTrader. My experience with backtesting was good, although subject to some serious constraints that I thought should be removed by the simulation mode. Since I began trying to use it, I have had nothing but problems.

                  I have a few other related questions:

                  It appears that GetAccountValue() returns only 0 when backtesting in Strategy Analyzer. Is that correct?

                  GetAccountValue() returns nonzero values in OnStartup() sometimes, but not usually. Is there a reason why I cannot consistently get my account values in OnStartup()?

                  Thanks for your help. There are many things I really like about NT, but I am beginning to wonder whether it will actually be able to do the job, or whether it is worth the effort.

                  Comment


                    #10
                    Now I am running my strategy on 30 stocks in the Sim101 Account using the TDA data feed.

                    The strategy has generated about a dozen EnterLong() transactions of just under $10,000 each. Following each of these transactions, I printed the CashValue and BuyingPower. After each of these transactions, CashValue = $20,000 and BuyingPower = $40,000, the initial settings for the account. In other words, they don't seem to be changing. I got several message boxes indicating the account was out of money, but not enough to account for all of these transactions. The transactions tab indicates that six of these transactions executed.

                    The Accounts tab for Sim101 indicates BuyingPower = Sim101 TD Ameritrade -82639.94; Cash Value = -41319.97; Excess Equity = 1604.01; Initial Margin = 30659.99. Does this make any sense? It does not to me.

                    When I tried this previously, GetAccountValues() at least returned values that changed as a result of the previous transaction, even if they were wrong. Now I can't even get that to happen consistently.

                    Comment


                      #11
                      Hello,

                      Buying power on the simulator will always be twice the value of the cash value.

                      This is hard coded into NT and is expected when using the simulator.

                      I realize that this may not be what is expected in a live market condition, however is limitation at this time that development is aware of with simulation in NT. If theres anything further you need me to look into on this please let me know.

                      -Brett

                      Comment


                        #12
                        Questions still not answered

                        Originally posted by NinjaTrader_Brett View Post
                        Hello,

                        Buying power on the simulator will always be twice the value of the cash value.

                        This is hard coded into NT and is expected when using the simulator.

                        I realize that this may not be what is expected in a live market condition, however is limitation at this time that development is aware of with simulation in NT. If theres anything further you need me to look into on this please let me know.

                        -Brett
                        OK, I could live with working with the CashValue instead of BuyingPower when trading in the Sim101 account by setting the CashValue of the Sim101 account to my actual buying power. But, it will be yet one more hassle in running NT because my strategy code will have to make decisions based on CashValue when running in Sim101 but based on BuyingValue when running on a real margin account.

                        I still don’t have an answer as to why the CashValue was not working in my post at 9:16 AM (CDT?) Aug 1. It is clear that GetAccountValues() returned values that did not change as transactions occurred and that were not the same as reported on the Accounts tab.

                        I also do not have an answer to the question in my post at 6:50 AM (CDT?) Aug 1.

                        To be candid, I am struggling to figure out whether NinjaTrader is capable of doing what I need or whether it would be a better use of my time to explore other trading platforms. I hate to give up on NinjaTrader, but I am not getting much encouragement this week.

                        Thanks

                        Comment


                          #13
                          Hello,

                          This all depends on when OnBarUpdate runs. It will pull the same value but do it at certain times when you call it.

                          Also, Cash Value does not change when you are in a trade. It will wait until you exit the trade to update with the new cash value after the unrealized trade PnL is applied. I just did a test and this is the case.


                          GetAccountValue() will always be 0 in backtest. GetAccountValue is a live only value. GetAccountValue is something that was added to NT7. To use GetAccountValue you need to be fully aware of its limitations and what it can and cannot do for you. Its good for a realtime pull of the account value at live/run time only. Cannot be used in OnStartUp(), cannot be used in backtesting, cannot be used in historical.

                          -Brett

                          Comment


                            #14
                            How does a strategy determine whether the account has room to enter new position?

                            Let me try to ask my question a different way:

                            When a strategy determines that entering a new position is expected to be profitable, it apparently is necessary to determine whether the account has sufficient buying power for the order to be accepted by the broker without being rejected.

                            In practice, how do traders determine this when programming a strategy?

                            I would greatly appreciate some help with this.

                            Comment


                              #15
                              Hello,

                              When trading futures, etc, most traders program there stratgies to use a set amount for there entries and exits this way each PnL of each trade will be the same and they always submit orders for 1 - 10 contracts for example. Whatever there risk is set too.

                              The strategy will trade this and has no bearing on the account itself. Its simply submitting the orders. If the account does not have enough buying power for the trade to be accepted and you run out of margin then the order is simply rejected and the strategy cancels for you to then readjust your QTY size if needed manually or stop trading since the account is diminished.

                              NinjaTraders strategy design is setup in a way that the strategy does not care about the account it runs on or the account position. It simply runs inside its own box and submits orders as its told to do so.

                              Often for sake of historical results being the same as live results we do not like to take live variables that change the outcome of the strategy. For example live you might use GetAccountValue() to change size to 500 shares of GOOG. However in backtest it is always using 100 shares of GOOG for example giving you different results backtest and live.

                              There are various other reason why Ninjatrader is setup this way and the above is just one of these reasons. However the idea here is that the strategy runs independent of the account environment.

                              Now traders that don't want this and want to adjust QTY sizes based on account needed to manually program this in and take additional steps. Either by using GetAccountValue() at the start of the strategy and setting QTY size and then when in a trade add the trade PnL to this value to keep an up to date variable and keep the PnL or account value count from there. Or using a parameter where you simply input in your own account buying power and then do all the math you need to keep that current with your strategy trades and also in real time subtracting or adding PnL to that value then before trading check to see if you have enough account value or adjust for your QTY size.

                              These are the two method available to you to try to program this into the strategy. However you will notice there is still a disconnect between the strategy and the live account(Since you have to manually track your own buying power inside the strategy). This is by NinjaTrader design.

                              -Brett

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by suroot, 04-10-2017, 02:18 AM
                              4 responses
                              3,019 views
                              0 likes
                              Last Post truepenny  
                              Started by Stanfillirenfro, Today, 07:23 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              2 responses
                              22 views
                              0 likes
                              Last Post cmtjoancolmenero  
                              Started by olisav57, Yesterday, 07:39 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cocoescala, 10-12-2018, 11:02 PM
                              7 responses
                              944 views
                              0 likes
                              Last Post Jquiroz1975  
                              Working...
                              X