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

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


    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.


    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, 02: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   [COLOR="Red"]//why you are setting Close[0] > 10,[/COLOR]
            && CurrentDayOHL[B][COLOR="Blue"]()[/COLOR][/B].CurrentLow[0] < PriorDayOHLC[COLOR="blue"][B]()[/B][/COLOR].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, 11: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.


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

                    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 DanielSanMartin, Yesterday, 02:37 PM
                      2 responses
                      12 views
                      0 likes
                      Last Post DanielSanMartin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      4 responses
                      12 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by terofs, Today, 04:18 PM
                      0 responses
                      11 views
                      0 likes
                      Last Post terofs
                      by terofs
                       
                      Started by nandhumca, Today, 03:41 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post nandhumca  
                      Started by The_Sec, Today, 03:37 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post The_Sec
                      by The_Sec
                       
                      Working...
                      X