• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code for placing a specific dollar value for a trade

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

    Code for placing a specific dollar value for a trade

    Hi:

    First post of what I expect to be a series of newbie questions. Hoping to piece together a strategy I have developed and backtested using other software (Stratasearch). My programming skills in C# are essentially zero, but I am trying to learn as quickly as I can.

    First question - I want to use a set dollar amount for each trade (say $20,000 per trade for stocks), and have the system check the current available account funds before placing any trades. For the first part I would simply type the following:

    Code:
     
    protected override void Initialize()
    {
    AccountSize = 20000;
    }
    Now before any trades would be placed, I would want the system to check to be sure that there are sufficient funds available to trade. This is where I am not really sure what the code should be. I need to be able to access the amount of funds currently free to invest (not already in a trade) and if that amount is > $20,000 then trade. If not, then no trade is placed.

    Any suggestions on what the code would look like?

    Thanks in advance,

    Kevin

    #2
    Hello Kevin,
    Welcome to the forum and I am happy to assist you.

    You can find the buying power using the GetAccountValue function. Please refer here to know more
    http://www.ninjatrader.com/support/h...countvalue.htm

    However please do note not all connectivity provider provides the Buying Power data. Please refer here to know whether your connectivity provider provides the data or not.
    http://www.ninjatrader.com/support/h...counts_tab.htm

    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Got it. Thanks. So the code for what I described would look like this?

      Code:
       
      protected override void OnBarUpdate()
      {
      if (GetAccountValue(AccountItem.CashValue) > 20000)
      Enterlong();
      }
      Kevin

      Comment


        #4
        Hello Kevin,
        Yes, it would work.

        You should check whether the data (as obtained from the GetAccountValue) is provided by your broker or is calculated locally by Ninjatrader. Again, if the data is provided from your broker you must also check, whether the data is updated on each trade or not.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Ok. Next piece of the puzzle.

          I only want to enter these fixed dollar amount trades if the low for the day goes more than 3% below yesterday's close AND the current bar close is at or has crossed above the same threshold. Obviously this is looking for stocks that briefly dip and have now begun to return back up. I would likely use 1 min bars for this rather than tick data.

          Here is what I have come up with so far - please let me know your thoughts on whether or not this would do what I am targeting.

          Code:
           
          protected override void Initialize()
          {
          AccountSize = 20000; 
          double trigger = PriorDayOHLC.Close[0]*0.97};
           }
           
          protected override void OnBarUpdate()
          {
          if (Close[0] > 10
          && CurrentDayOHL.CurrentLow[0] < trigger
          && Close[0] >= trigger
          && GetAccountValue(AccountItem.CashValue) > 20000)
           EnterLong();
          }
          Thoughts?
          Last edited by Kevin_in_GA; 05-30-2012, 01:13 PM.

          Comment


            #6
            Hello Kevin,
            You cannot get the value of trigger from the Initialize section. Also the scope of the variable trigger is limited to the Initialize section only. You need to make it private so that it can be accessed in the OnBarUpdate event also. Why you have included Close[0] > 10 as the condition? In case the instrument price is < 10, the condition will never will be true.

            Please use the below code instead:

            Code:
            protected override void Initialize()
            {
            AccountSize = 20000; 
            
             }
             
            protected override void OnBarUpdate()
            {
            if (Close[0] > 10   //why you are setting Close[0] > 10,
            && CurrentDayOHL().CurrentLow[0] < PriorDayOHLC().Close[0]*0.97
            && Close[0] >= PriorDayOHLC().Close[0]*0.97
            && GetAccountValue(AccountItem.CashValue) > 20000)
             EnterLong();
            }
            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Thanks - learning as I go. The reason for the Close[0] > 10 and the 3% pullback setting have more to do with placeholders in the strategy than a specific value, but I usually find that the bid/ask spread hurts profitability in this type of trade if the stock value is $5 or below, since you lose a percent or so on the spread for everything other than the most highly liquid stocks. This is less of an issue on stocks greater than $10.

              Comment


                #8
                Hello Kevin,
                Thanks for the clarification. Makes sense.

                I just wanted to make double sure.

                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Ok, one more set of adds ...

                  I want to only enter any long trades before 1:30 PM EST, and make sure that all trades are exited by 3:55 PM EST. I also want to have a 3% profit target put in place once any long order is entered. No stop losses.

                  Here is where I am on this:

                  Code:
                   
                  protected override void Initialize()
                          {
                              SetProfitTarget(CalculationMode.Percent, 3);
                              AccountSize = 20000; 
                              CalculateOnBarClose = true;
                          }
                          protected override void OnBarUpdate()
                          {
                              // Conditions for Entry
                              if (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 133000
                                  && Close[0] > 10 
                                  && CurrentDayOHL().CurrentLow[0] < PriorDayOHLC().Close[0]*0.97
                                  && Close[0] >= PriorDayOHLC().Close[0]*0.97
                                  && GetAccountValue(AccountItem.CashValue) > 20000)
                   
                                      EnterLong();
                              }
                              // Conditions for Exit 
                   
                              {
                                  if (ToTime(Time[0]) == 155500)
                                      ExitLong();
                              }
                          }
                  Again, any comment or identification of issues is welcome. Just want to be sure that the time constraints are correctly coded, as well as the profit target (that was basically a cut and paste from the wizard so I am thinking it is OK).

                  Kevin
                  Last edited by Kevin_in_GA; 05-31-2012, 10:37 AM.

                  Comment


                    #10
                    Hello Kevin,
                    If you want to filter your trades within a specific time period then please refer to this sample code which demonstrates it.
                    http://ninjatrader.com/support/forum...ead.php?t=3226

                    To put a 3% stop please use the following code
                    Code:
                    SetProfitTarget(CalculationMode.Percent, 0.03);

                    Please let me know if I can assist you any further.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks. As for the time filtering, this may or may not be important. As it stands, I think the code I posted here is correct, since I used that same reference to develop it (and by develop I mean cut, paste, and change a bit).

                      Kevin

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Newtrader101, Today, 08:01 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post Newtrader101  
                      Started by Johnny Santiago, 10-11-2019, 08:21 AM
                      6 responses
                      101 views
                      0 likes
                      Last Post 4x777
                      by 4x777
                       
                      Started by pjsmith, Today, 02:42 AM
                      2 responses
                      16 views
                      0 likes
                      Last Post pjsmith
                      by pjsmith
                       
                      Started by moyerchopra, Yesterday, 11:43 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post moyerchopra  
                      Started by ttodua, Yesterday, 11:40 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post ttodua
                      by ttodua
                       
                      Working...
                      X