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

One trade for entire portfolio

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

    One trade for entire portfolio

    Hello, a few questions:

    When running one NinjaScript strategy from the Strategies tab on a list of 500 instruments how do I limit my trading to one signal on the entire portfolio? (Per day)

    e.g.

    Int TotalSignalsInPortfolio ; // Global Variable?

    if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
    TotalSignalsInPortfolio = 0 ;
    if (BuySignal && TotalSignalsInPortfolio == 0)
    {
    TotalSignalsInPortfolio = 1 ;
    EnterLong() ;
    }

    How do I match the trading date to the current date?

    i.e.

    if (bardatetime == computerdatetime // today’s date)
    {
    Actions
    }

    How do I access the account’s buying power and cash value from within a strategy?

    Thank you
    Last edited by WallSt.; 03-15-2017, 10:14 AM.

    #2
    Hello Wallst,

    Thank you for your note.

    Regarding limiting your trading to one signal question, are all these instruments included in 1 strategy or are you running your strategy 500 times on 500 instruments?

    To compare the current date/bar to a specified date, you could use,
    Code:
    if (Times[0][0] == new DateTime(2017, 3, 15))
    To get account Buying Power, use,
    Code:
    Account.Get(AccountItem.BuyingPower, Currency.UsDollar);
    To get account Cash Value, use,
    Code:
    Account.Get(AccountItem.CashValue, Currency.UsDollar);
    I've included an example strategy which uses .CashValue to determine how many shares to buy for your reference.

    I look forward to your reply.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your prompt reply Alan,

      I suppose I would be running it 500 times on 500 instruments to (hopefully) save me from having to make references to other bars objects in the code.

      Also, how do I write new DateTime(2017, 3, 15) to reflect today's date so that it updates each day automatically?

      Thanks again
      Last edited by WallSt.; 03-16-2017, 08:05 AM.

      Comment


        #4
        Hello WallSt,

        You are running 500 strategies on 500 different instruments, and when 1 strategy takes a position you wish to cancel all other working strategies, is this correct?

        To create a DateTime variable and assign it a date of today, you could use,
        Code:
        DateTime xTodayDate = DateTime.Now;
        I look forward to your reply.
        Last edited by NinjaTrader_AlanP; 03-16-2017, 10:16 AM.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Ok, thanks Alan.

          Yes, that is correct although I think I have wrapped my head around how to do this now. I should run one strategy on 500 instruments, loop through the BarsInProgress and then break out of the loop once the entry condition is true on one of the instruments and that will prevent any additional orders from being sent on the other instruments. Is that correct?

          Comment


            #6
            Hello WallSt,

            What you could do to limit trading to just one entry per day, is use a bool variable which you’ll set to false on the first bar of the session and set to true after you make a trade. Your entry condition would include a check for whether this bool was true or false.

            I have attached a sample strategy to demonstrate this.

            Please let us know if you need further assistance.
            Attached Files
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Ok, thanks for your help Alan.

              Comment


                #8
                Hello Alan,

                In follow-up to my original question I have realized now that in order to make my strategy work as intended I will have to run it 500 times on 500 instruments i.e. no additional data series added in the script.

                I would like to do the following: place one limit entry order on the first instrument that evaluates to true for its buy condition that day (first meaning strictly time based i.e. instrument #20 of 500 evaluates to true at 0935 then place order and if subsequently instrument #8 of 500 evaluates to true at 0940 then don't place order). However, if instrument #20's buy condition later on that day evaluates back to false and the order was never filled and thus gets cancelled by the strategy then I would like to place an order on the next instrument with a buy condition that is true and so on and so forth i.e. only one live order in the market at a time. If on the other hand the order was filled (even if only partially filled) then I do not want to place orders on any other instruments for that day.

                How do I go about doing this?

                Thanks in advance,

                WallSt.
                Last edited by WallSt.; 04-09-2017, 04:11 PM.

                Comment


                  #9
                  Hello WallSt.,

                  This would be possible.

                  If you were running several scripts on several different instruments and wanted 1 strategy to interact with another, you could have one strategy write to a file that another strategy reads from. For that you could see the following reference example,



                  To build in a time filter you would want to see the following reference example,



                  Please let us know if you need further assistance.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Alan,

                    Using the reference example you provided as a guide I created an indicator that writes a string to a text file - the string is initialized to "NoTrade" and changes to the instrument name if the Boolean variable is true. Reading and writing to the text file works fine when there is only one instrument and one indicator in the chart but when there are two instruments and two indicators I get an error message in the print log: Error on calling "OnBarUpdate" on bar 1151: The process cannot access the file // text file // because it is being used by another process. I have a chart with two instruments loaded in it PTEN and GOOGL 5 days loaded 1 minute bars with an end date of January 6, 2017 with the indicator running on each instrument. If the Boolean variable is true on PTEN and thus the string is "PTEN" then I want to reference that string in the indicator running on GOOGL and plot a count based on that. I am experimenting reading and writing to an indicator first before using it in my strategy but the goal is the same - when a condition is true in one script then I want to reference that in a second script.

                    Could you please provide some guidance on where I am going wrong here:

                    +Using declarations

                    using System.IO;

                    #region Variables
                    private bool TrdTdy ;
                    private int Count ;
                    private string path;
                    #endregion

                    if (State == State.SetDefaults)
                    {
                    .........
                    TrdTdy = false ;
                    path = NinjaTrader.Core.Globals.UserDataDir + "RW-MyTestFile.txt";
                    AddPlot(Brushes.Red, "ReadWrite");
                    }
                    protected override void OnBarUpdate()
                    if (ToDay(Time[0]) == 20170106)
                    {
                    string Signal = "NoTrade" ;

                    File.AppendAllText(path, Signal);

                    string readText = File.ReadAllText(path);

                    if (TrdTdy == false && Low[0] < 28.35)
                    TrdTdy = true; // generic condition is true on PTEN but not on GOOGL

                    if (TrdTdy == true)
                    Signal = Instrument.MasterInstrument.Name ;

                    Print(Signal) ;

                    if (Signal == "PTEN" && Instrument.MasterInstrument.Name != "PTEN" )
                    Count = Count + 1 ; // count on GOOGL's indicator not PTEN's

                    Value[0] = Count ;
                    }
                    }
                    Last edited by WallSt.; 04-11-2017, 01:19 PM.

                    Comment


                      #11
                      So I've read the NT as well as MSDN examples on StreamWriter, StreamReader, System.IO, etc. and I'm still unclear on whether one can read/write to the same text file from 2 or more scripts simultaneously without receiving the "in use by another process" error message or having the results be a jumbled mess.

                      Is it possible for example, to have the same script running in two separate instances simultaneously while reading/writing each bar's close to the same text file? If yes, can you kindly provide a code snippet so I can understand how to get around the issues mentioned in the first paragraph?

                      Many thanks,

                      WallSt.
                      Last edited by WallSt.; 04-13-2017, 06:44 PM.

                      Comment


                        #12
                        Hello WallSt.,

                        Thank you for the questions.

                        It is possible to run into file read/write issues if too many instances are trying to read or write a file at the same time. While it is possible to use this system to make two scripts communicate it would be a delicate system if they were not reading and writing one after the other. In general, we don't suggest making scripts communicate in this way because the syncing and error handling would be solely up to the developer.

                        For a more advanced way for scripts to "see" each other or pass data, you could use a static class but this is a concept I would highly suggest reviewing on MSDN or other C# tutorial sites before using.

                        Static classes are generally outside of the scope which we would support as this is just a C# concept that you could utilize. There is one example I had provided previously of sharing a value between scripts here: http://ninjatrader.com/support/forum...24&postcount=3

                        As noted in the post the sync is up to you in this case, so if a condition becomes true and sets a value in the class but the other script already checked the value before that, it could miss a signal. Please take caution when trying to interweave ninjascript items in this way.

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks for clearing that up for me Jesse. I'll take a look into the static class you mentioned.

                          Cheers,

                          WallSt.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by funk10101, Today, 12:02 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post funk10101  
                          Started by gravdigaz6, Yesterday, 11:40 PM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by MarianApalaghiei, Yesterday, 10:49 PM
                          3 responses
                          10 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by XXtrader, Yesterday, 11:30 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post XXtrader  
                          Started by love2code2trade, 04-17-2024, 01:45 PM
                          4 responses
                          28 views
                          0 likes
                          Last Post love2code2trade  
                          Working...
                          X