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

Account Cash Value Account Initial Margin Instrument Initial Margin

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

    Account Cash Value Account Initial Margin Instrument Initial Margin

    Hello,

    There where no errors in the programming and no errors in the logs. BUT... when I use Strategy Analyzer or run it on the Sim101 account in real-time Errors occure. There are NO trades occuring due to this code. Currently this code outputs a false false when the values say it should be a true output.

    when 85% OverallCash value is Greater than the RiskMargin and InitialMarginHeldInAccount combined the output should read as true.

    I Created a Risk profile for my Forex.com trading called "GTB ForexDotCom"
    I used CashValue - InitialMargin > (Long/Short)MinimumCheckLotsToTradeLogicGTB * Risk.Get("GTB ForexDotCom")
    exsample Account has $1000, where there is $400 currently in use. the Next Trade will cost $60 [<Risk.Get("GTB ForexDotCom") = $20>*<Multiplier = 3>]

    these all pull correctly
    Account.Get(AccountItem.CashValue, Currency.UsDollar);
    Account.Get(AccountItem.InitialMargin, Currency.UsDollar);
    Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin;

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class GTBsAutomatedTradingStrategyAccount : Strategy
        {
            private Account account;
    
            //Account check Long and Short
            private Series<double> OverallCashValueOfAccountCurrentlyTrading;
            private Series<double> InitialMarginHeldInAccountAsTrades;
            private Series<double> RiskMarginRequirementsOnTradedInstrument;
            private Series<bool> LongMoneyInAccountToTrade;
            private Series<bool> ShortMoneyInAccountToTrade;
                }
                 else if (State == State.DataLoaded)
                {
                   //My Account
                    OverallCashValueOfAccountCurrentlyTrading         = new Series<double>(this, MaximumBarsLookBack.Infinite);
                    InitialMarginHeldInAccountAsTrades         = new Series<double>(this, MaximumBarsLookBack.Infinite);
                    RiskMarginRequirementsOnTradedInstrument         = new Series<double>(this, MaximumBarsLookBack.Infinite);
                    LongMoneyInAccountToTrade         = new Series<bool>(this, MaximumBarsLookBack.Infinite);
                    ShortMoneyInAccountToTrade         = new Series<bool>(this, MaximumBarsLookBack.Infinite);
                }
            protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
            {
                /// Real-Time Trade Logic
    
                    //My Account
                    // Check Money in Account to Trade
                    OverallCashValueOfAccountCurrentlyTrading[0]         = Account.Get(AccountItem.CashValue, Currency.UsDollar);
                    InitialMarginHeldInAccountAsTrades[0]         = Account.Get(AccountItem.InitialMargin, Currency.UsDollar);
                    RiskMarginRequirementsOnTradedInstrument[0]         = Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin;
                    if (Account.Name != "Sim101")
                        {
                        if ((LongMoneyInAccountToTrade[0] == false)
                            && (((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]) > ((12 / LongMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                              LongMoneyInAccountToTrade[0] = true;
                        else if ((LongMoneyInAccountToTrade[0] == true)
                            && (((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]) <= ((12 / LongMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                              LongMoneyInAccountToTrade[0] = false;
                        if ((ShortMoneyInAccountToTrade[0] == false)
                            && (((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]) > ((12 / ShortMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                              ShortMoneyInAccountToTrade[0] = true;
                        else if ((ShortMoneyInAccountToTrade[0] == true)
                            && (((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]) <= ((12 / ShortMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                              ShortMoneyInAccountToTrade[0] = false;
                        }
                    else if (Account.Name == "Sim101")
                        {
                               LongMoneyInAccountToTrade[0] = true;
                            ShortMoneyInAccountToTrade[0] = true;
                        }
              }
    }
    Last edited by GTBrooks; 07-13-2020, 03:55 PM.

    #2
    Hello GTBrooks, thanks for your question.

    More often than not, a false false are a bug in the custom code. Consider making a detailed "bread crumb" trail of prints to identify a problem e.g.

    Code:
    if (Account.Name != "Sim101")
                {
                    if ((LongMoneyInAccountToTrade[0] == false) && (((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]) > ((12 / LongMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                    {
                        Print("LongMoneyInAccountToTrade[0] " + LongMoneyInAccountToTrade[0]);
                        Print("((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]) " + ((OverallCashValueOfAccountCurrentlyTrading[0] * 0.85) - InitialMarginHeldInAccountAsTrades[0]));
                        Print("((12 / LongMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0])) " + ((12 / LongMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0])));
                        //rinse and repeat for all condition groups
                        LongMoneyInAccountToTrade[0] = true;
                    }      
    
                    else if ((LongMoneyInAccountToTrade[0] == true) && (((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - InitialMarginHeldInAccountAsTrades[0]) <= ((12 / LongMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                    {
                        LongMoneyInAccountToTrade[0] = false;
                    }
                    if ((ShortMoneyInAccountToTrade[0] == false) && (((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - InitialMarginHeldInAccountAsTrades[0]) > ((12 / ShortMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                    {
                        ShortMoneyInAccountToTrade[0] = true;
                    }
                    else if ((ShortMoneyInAccountToTrade[0] == true) && (((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - InitialMarginHeldInAccountAsTrades[0]) <= ((12 / ShortMinimumCheckLotsToTradeLogicGTB) * (RiskMarginRequirementsOnTradedInstrument[0]))))
                    {
                        ShortMoneyInAccountToTrade[0] = false;
                    }
                }
                else if (Account.Name == "Sim101")
                {
                    LongMoneyInAccountToTrade[0] = true;
                    ShortMoneyInAccountToTrade[0] = true;
                }
    I look forward to assisting.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      where should I expect to see the Print function output?

      Comment


        #4
        found the output = NinjaScript Output

        I was passing values between variables incorrectly. this helped out.
        thank you,

        Comment


          #5
          I worked out all the errors in the Logs and Popups Using Print, BUT when I run it on Strategy Tab under Sim101 account I found an error. The Strategy generates Historical data, BUT the error Stops the Strategy from entering any real-time trades.
          I believe the error occurs when the strategy tries to see if there is money in the account to trade
          Code:
          if(Account.Get(AccountItem.CashValue, Currency.UsDollar) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)>Close[0]
          What error could stop my Strategy from trading Real-Time?

          Comment


            #6
            Hi GTBrooks, thanks for the follow up.

            What is the exact error you get? Does anything show up in the log tab or through a popup window?

            Chris L.NinjaTrader Customer Service

            Comment


              #7
              let me ask this another way.

              In what section of me Automated Strategy should I insert this code, for it to function properly in real-time trading?
              Code:
                      //Acount check Long and Short
                      private bool LongMoneyInAccountToTrade;
                      private bool ShortMoneyInAccountToTrade;
              
                      //My Account
                      LongMoneyInAccountToTrade    = true;
                      ShortMoneyInAccountToTrade    = true;
              
                      ///AccountInformation (Copy / Paste)
                      // This method is fired on any change of an AccountItem
                      protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
                      {
                              //My Account
                              // Check Money in Account to Trade
                              if ((((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)) > ((Account.Get(AccountItem.CashValue, Currency.UsDollar) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)) / (10 / LongMinimumCheckLotsToTradeLogicGTB)))
                                  && ((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)) > Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)
                              {
                                  LongMoneyInAccountToTrade = true;
                              }
                              else
                              {
                                  LongMoneyInAccountToTrade = false;
                              }
                              if ((((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)) > ((Account.Get(AccountItem.CashValue, Currency.UsDollar) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)) / (10 / ShortMinimumCheckLotsToTradeLogicGTB)))
                                  && ((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - Account.Get(AccountItem.InitialMargin, Currency.UsDollar)) > Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)
                              {
                                  ShortMoneyInAccountToTrade = true;
                              }
                              else
                              {
                                  ShortMoneyInAccountToTrade = false;
                              }
                      }

              Comment


                #8
                Hi GTBrooks, thanks for your reply.

                All this stuff will be class level, so you would place all of this below OnBarUpdate() {}. (or anywhere at the class level)

                OnBarUpdate()
                {
                }

                //paste code block here.

                If you already have this in your script and it works in historical, then it should work in real-time.

                If you would like for me to try this on my end then please let me know/provide an example of the problem.

                Kind regards.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Are you saying there is no reason to run code in my automated strategy?
                  Code:
                          // This method is fired on any change of an AccountItem  
                           protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)         {         }

                  Comment


                    #10
                    Hi GTBrooks, thanks for your reply.

                    I am sorry but I do not understand the question. What is the issue you are seeing with OnAccountItemUpdate? Did you try printing the values coming from that method to see if you are getting what you expect?

                    Chris L.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    19 views
                    0 likes
                    Last Post algospoke  
                    Started by ghoul, Today, 06:02 PM
                    3 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    45 views
                    0 likes
                    Last Post jeronymite  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    7 responses
                    21 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    10 responses
                    181 views
                    0 likes
                    Last Post jeronymite  
                    Working...
                    X