Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to draw horizontal lines on chart from script only once?

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

    How to draw horizontal lines on chart from script only once?

    I want to draw a bunch of horizontal lines from a file on a chart occasionally.

    Since "Draw methods will not work if they are called from the OnStateChange() method" I'm wondering where to put the code to just run it once the first time the strategy is enabled.

    If the code is placed OnBarUpdate(), it will run repeatedly, reading from the file and creating duplicate lines each time so where is the best place to have 'one time' code?

    I'm a coding newbie so the simplest explanation would be best.
    Thanks!
    Last edited by hillborne; 11-19-2016, 02:04 AM.

    #2
    Hello,

    I had posted on your other question here: http://ninjatrader.com/support/forum...ad.php?t=92211

    If you find the sample provided does not work in some situation, you may try placing the logic to load the lines inside of OnBarUpdate in a condition like the following:

    Code:
    if(CurrentBar == 0)
    {
    // load
    }
    This would only happen once on bar 0 preventing the load from happening over and over. The State.Historical is used in the other example which is also only called once.

    Please let me know if I may be of further assistance.

    Comment


      #3
      Temporary fix

      Hi Jesse,

      Since I already exported the 200+ horizontal lines from NT7 to a text file, I need a way to get them back on to a chart in NT8 in order to begin to use your serialization method (and continue trading!).

      I've got my first script working and it puts all the lines back in place but I think that they are attached to the strategy because they disappear when I disable it.

      Is there a way to change the ninjascriptbase owner to be from the me? Or is there a different method to have the lines persist after disabling my strategy?

      Thank you!

      Below is the relevant code that works:
      Code:
      -
      
      			if(CurrentBar == 0)
      			{
      				while(!sr.EndOfStream)
      				{
      					string line = sr.ReadLine();
      					if (!line.Contains("*"))
      					{
      						strList.Add(line);
      					}
      				}
      				for (int i = 0; i < strList.Count; i++)
      				{
      					dubList.Add(double.Parse(strList[i]));
      				}
      				
      				for (int i = 0; i < dubList.Count; i++)
      				{
      					Print(dubList[i]);					
      					HorizontalLine myLine = Draw.HorizontalLine(this,"L_" + dubList[i], dubList[i], Brushes.Blue);
      					myLine.IsLocked = false;
      					//AttachedToType value = AttachedToType.GlobalInstrument;
      					
      				}
      				
      			}

      Comment


        #4
        Hello,

        Thank you for the post.

        There is not a way to change the "this" from the statement provided, It would also be expected that any objects added by the strategy are removed when it is disabled. Unfortunately I am unsure of any way you could prevent that when using a strategy specifically.

        To allow the lines to persist without the strategy, you would need to use a separate indicator that is not associated to the strategy so when the strategy is disabled the indicator remains on the chart.

        You could also make two versions depending on what you need, a indicator to view the lines when not trading or on any chart, the strategy could also use the same logic if it needs the lines to place trades.

        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        63 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        139 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X