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

save and load all existing orders

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

    save and load all existing orders

    Hi everyone, now it's like this: when activating the script, all made purchases/sales are visible on the chart. but when the strategy is finished and restarted, all orders are gone. how can the already executed orders be saved, so that they are automatically loaded when activating the script again? probably this would have to be done via an external file, maybe someone has an example or a tip?

    Click image for larger version

Name:	Screenshot_1.jpg
Views:	286
Size:	41.5 KB
ID:	1118206
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thanks for the post.

    You mentioned that when the script is activated you can see all the trades on the chart, is that only after it has been running in realtime? From the details it sounds like the strategy is working only in realtime and places trades is that correct?

    In general when you enable/disable/enable a strategy you should see trades placed on the historical bars of the chart, removed and re added when it re processes again. If the strategy can trade historically you would see this happen, the SampleMACrossOver does this.

    If the strategy does not trade historically and can only trade in realtime you would need to do something to save that information if you wanted to reload it again. I am not aware of an example of this specifically but you could use any number of C# means to store the data and load it. In essence you would have to store whatever triggers the trades as a series so that when you start back up you could reference that data on each bar to know if a trade should have been placed there. When the historical bars process and all the trades have been replaced it would be reflected in the chart.

    Another solution may be to use Tick Replay, if the strategy works only moving forward in realtime for some reason that could potentially help to solve that depending on what is needed in your logic. More details on the strategy would be needed here to know what the solution could be.


    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi _Jesse thank you for your quick response, I use the strategy as a kind of manual order entry. instead of the atm order entry i use my script with manual order entry. so there is no historical data because there are no setups. so i have to save my manual orders somehow into a list to be able to retrieve them later. how do i access the order data? somehow i have to be able to access them, after all they are shown in the chart. is there a code sample?
      sidlercom80
      NinjaTrader Ecosystem Vendor - Sidi Trading

      Comment


        #4
        Hello sidlercom80,

        Thanks for that detail that helps.

        The easiest way in this situation would be to collect your execution details as you receive them from OnExecutionUpdate. https://ninjatrader.com/support/help...lightsub=onexe

        You could then use any C# means to write/read the data as the executions happen. In the historical processing you would need to resubmit the orders for markers to be placed again so that could lead to some differences in case the fill engine equates the order differently for any reason. If you wanted to have true markings of the actual fills that really happened you could use drawing objects for that purpose instead to just draw the raw values you collected. Keep in mind the only way for those previous orders to be reconsidered in any of the bookkeeping would be to resubmit them.

        https://ninjatrader.com/support/help...o_read_fro.htm
        https://ninjatrader.com/support/help...o_write_to.htm

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

        Comment


          #5
          Hi, I use the OnExecutionUpdate method to save the current time of the bar and the averagefillprice in an external file. when i read out this file afterwards, the data is displayed correctly, perfect! i want to compare the time in the respective bar, in order to be able to display it on the history at the exact same time from the fill of the order. my problem is that the timestamp never matches, i guess because of the different tick data. is there a solution? or maybe there is a better way to transfer the time of the filled order d into the history?

          Code:
           protected override void OnBarUpdate()
          {
          if (State == State.Historical)
          {
          
          if (!File.Exists(tradespath))
          {
          
          Print("File does not exist.");
          return;
          }
          
          try
          {
          
          readtrades = new StreamReader(tradespath);
          
          string line;
          
          while ((line = readtrades.ReadLine()) != null)
          {
          
          string[] split = line.Split(new char[] { ',' });
          
          int splitCounter = 0;
          
          foreach (string s in split)
          {
          splitCounter++;
          
          if (splitCounter == 1)
          {
          date = Convert.ToDateTime(s);
          }
          else if (splitCounter == 2)
          {
          avg = double.Parse(s);
          }
          
          [B][COLOR=#e74c3c]if (Time[0] == date)[/COLOR][/B]
          [COLOR=#e74c3c]{
          TriangleUp = Draw.TriangleUp(this, "LE" + Time[0], true, 0, avg, TriangleUpBrush);
          TriangleUp.OutlineBrush = Brushes.Gold;
          }[/COLOR]
          
          if (splitCounter == 2)
          splitCounter = 0;
          }
          }
          readtrades.Close();
          }
          catch (Exception e)
          {
          
          Log("You cannot write and read from the same file at the same time. Please remove SampleStreamWriter.", LogLevel.Error);
          Print(e.ToString());
          }
          }
          }
          Code:
          protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
          {
          writetrades = File.AppendText(tradespath); // Open the path for writing
          writetrades.WriteLine(Time[0] + "," + execution.Order.AverageFillPrice); // Append a new line to the file
          writetrades.Close(); // Close the file to allow future calls to access the file again.
          }
          the goal is, for example, if an order is filled now (14:42:00) and the strategy is reloaded, this order should be read in via the file and a marker should be drawn in the chart at exact (14:42:00) in the history.
          Attached Files
          Last edited by sidlercom80; 09-15-2020, 07:19 AM.
          sidlercom80
          NinjaTrader Ecosystem Vendor - Sidi Trading

          Comment


            #6
            Hello sidlercom80,

            One item here is that you are storing the bar time from OnExecutionUpdate which will take whatever the current bars time is at that time of the execution. You may instead want to try using executiion.Time to be more accurate to the actual execution time.

            Another factor would be the chart times, if you use a bars type that has a variable timestamp like tick series it may be more difficult to plot the locations. On a tick based chart if the starting point is different between reloading you will see new bars generated which may no longer match the previous test. Times may end up on new bars and fills may look different now. That could also affect items like ranges of prices and where fills are placed outside the range of the bar. It may have previously been correct but now based on how the bars built it is not.

            For time based series you can likely line this up more easily because specific times will always be observed. The factor here would be the granularity of the series you used. If you use 1 minute bars you will have 1 minute intervals of time which may not match the fills time with seconds and milliseconds. Logic could be used to compare the Minute and hour in that case and omit the second and millisecond for more control.


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

            Comment


              #7
              hi, i almost made it ;-) currently all relevant data is stored in a file and loaded as historical trades each time the strategy is restarted. prices and date time and so on are loaded from the data to illustrate the arrows and entries on the chart. everything works. my problem is to display the individual tradlines on the chart. there are also several partial entries that belong to a single trade, which i can't manage in the logic for the display of the individual lines. is there a tip or code example? with which logic is this done in nt8? is it calculated via the chart objects or how?

              Click image for larger version

Name:	Screenshot_1.jpg
Views:	257
Size:	35.7 KB
ID:	1119636Click image for larger version

Name:	Screenshot_2.jpg
Views:	241
Size:	22.4 KB
ID:	1119637
              sidlercom80
              NinjaTrader Ecosystem Vendor - Sidi Trading

              Comment


                #8
                Hello sidlercom80,

                I am not certain that I understand from the image what problem you are describing here. it looks like you have lines going from the entries to the exits there, is the second image what you are having difficulty with?

                Also are you having trouble with some logic you tried or are you saying that so far you have not been able to come up with logic for part fills? I am not sure how it would be done in the platform as that is internal code but we could take a look at what you are trying and the results to try and find out a solution.

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

                Comment


                  #9
                  hi _Jesse, on the first picture you can see the trades drawn by nt8. these trades are not visible after calling the strategy again, therefore my own logic. on the second picture you can see the trades drawn by my logic, the lines are missing, because i can't deal with the needed logic for the lines. how and where in the script the lines are created by nt8, this might help me for my understanding
                  sidlercom80
                  NinjaTrader Ecosystem Vendor - Sidi Trading

                  Comment


                    #10
                    Hello sidlercom80,

                    Thanks for clarifying that.

                    Yes in this case you would have to also draw your own line if you wanted lines. The existing logic for the chart is not public so this is not something you can view the code for. You could take a look at using Draw.Line to draw your own lines between two points.


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

                    Comment


                      #11
                      Hi _Jesse, i am aware that i have to draw the lines myself, i just wasn't sure with which logic. i have now found a solution myself, but thanks for your help!
                      sidlercom80
                      NinjaTrader Ecosystem Vendor - Sidi Trading

                      Comment


                        #12
                        Originally posted by sidlercom80 View Post
                        Hi _Jesse, i am aware that i have to draw the lines myself, i just wasn't sure with which logic. i have now found a solution myself, but thanks for your help!
                        Can you post it?

                        Comment


                          #13
                          Originally posted by UltraNIX View Post

                          Can you post it?
                          this will not help you, because my solution is related to my project.
                          sidlercom80
                          NinjaTrader Ecosystem Vendor - Sidi Trading

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by kevinenergy, 02-17-2023, 12:42 PM
                          116 responses
                          2,757 views
                          1 like
                          Last Post kevinenergy  
                          Started by franatas, 12-04-2023, 03:43 AM
                          7 responses
                          106 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by jpapa, Today, 07:22 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post jpapa
                          by jpapa
                           
                          Started by Jltarrau, Today, 05:57 AM
                          3 responses
                          7 views
                          0 likes
                          Last Post Jltarrau  
                          Started by f.saeidi, Today, 05:56 AM
                          2 responses
                          8 views
                          0 likes
                          Last Post NinjaTrader_Erick  
                          Working...
                          X