Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Saving price data and using it in a strategy

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

    Saving price data and using it in a strategy

    I want to store the High and Low price level of a bar when the Volume is 2x larger than the average.

    Then I want to use those price data within a strategy as price zones to trade from. If prices go past the price zone, the strategy will delete the price level.

    So my code is something like this

    Save Price Level
    Code:
    if (Vol1[0] > (VOLMA1[0] * 2))
    {High[0] = HighPriceLevel
     Low[0] = LowPriceLevel)
    Entry Code
    Code:
    if (Close[0] > Open[0] && Close[0] > LowPriceLevel))

    Delete PriceLevel
    Code:
    if (Close[0] < LowPriceLevel))


    How would I go about saving the price levels and using it within a strategy and then deleting it if no longer valid? There will be a lot of levels that will need to be saved.

    Thanks!
    Last edited by Ousher; 02-01-2023, 04:11 AM.

    #2
    After some research, I am able to print out the price levels but the new value seem to be overwriting the old ones. Here is my code

    Code:
    List<double> highDemandList = new List<double>();
    List<double> lowDemandList = new List<double>();
    
    if (demandCount > 230)
    {
    highDemandList.Add(High[0]);
    lowDemandList.Add(Low[0]);
    
    
    }
    
    for (int i = 0; i < lowDemandList.Count; i++)
    {
    Print(Time[0] + " Instance " + i + ": Low Demand: " + lowDemandList[i] + " High Demand: " + highDemandList[i]);
    
    if (Close[0] < lowDemandList[i])
    {
    lowDemandList.RemoveAt(i);
    highDemandList.RemoveAt(i);
    i--;
    
    }
    }​
    Here is what my output shows

    Click image for larger version  Name:	image.png Views:	0 Size:	33.7 KB ID:	1233564

    So it's just showing 1 instance, and seem to be overwriting the old one. Any help?​

    Comment


      #3
      Hello Ousher,

      The way you are using the loop is not normal, you normally never adjust the iterator like you are:

      Code:
      i--;
      You are also removing items during the loop which can have unexpected results. As I don't know what the actual end result should be I couldn't really make any suggestions here aside from writing down how you want the loop and end result to be and then compare that to what you coded.

      In your first post I can see some other issues:

      Code:
      High[0] = HighPriceLevel
      Low[0] = LowPriceLevel


      In this code you are assigning values to the already existing high and low series, your script has High and Low as series by default. These should not be used for storing custom data, these are the actual market data series. You could make your own series like MyHigh or MyLow if you wanted to store data. You also need to ensure to not make your own variables with the names Open High Low or Close because those are taken.



      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello Ousher,

        The way you are using the loop is not normal, you normally never adjust the iterator like you are:

        Code:
        i--;
        You are also removing items during the loop which can have unexpected results. As I don't know what the actual end result should be I couldn't really make any suggestions here aside from writing down how you want the loop and end result to be and then compare that to what you coded.

        In your first post I can see some other issues:

        Code:
        High[0] = HighPriceLevel
        Low[0] = LowPriceLevel


        In this code you are assigning values to the already existing high and low series, your script has High and Low as series by default. These should not be used for storing custom data, these are the actual market data series. You could make your own series like MyHigh or MyLow if you wanted to store data. You also need to ensure to not make your own variables with the names Open High Low or Close because those are taken.


        Sorry for the confusion, let me try to simplify what I'm trying to do

        When this condition is met
        Code:
         if (demandCount > 230)
        I want to save that bar's Low value to a list index named "lowDemandList".

        If the condition is met again, I want to also save that new Low value to the list index without overwriting the old one.

        But if the price ever Closes below any of the Low values on the list, I want to remove the corresponding value from the list.

        Comment


          #5
          Hello Ousher,

          As a starting point I would suggest removing the loop you added, you are re modifying the list on each bar after adding to it. Your loop is likely causing the result that you are seeing. Before doing any of the other actions you would want to make sure that the data is being stored as you wanted, once you confirm that you could look at doing the other operations to remove elements. Using i--; wouldn't be needed if you are just trying to remove an element based on some criteria, you would want to keep iterating the loop instead of stepping the iterator backwards.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          65 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