Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding pivot indicator to secondary instrument ?

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

    Adding pivot indicator to secondary instrument ?

    i am trying to add a second instance of pivot to the secondary instrument , so i can use that information to place a hedge trade based on that information.

    R1: 12967.3333333333 R2: 13044.4166666667 Close: 3867.5
    ​S1: 12770.5833333333 S2: 12650.9166666667 Close: 3867.5

    its getting the close from the secondary but the R1 and 2 , S1 and 2 are still coming from the primary

    how do i fix this ?

    below is pieces of the code



    else if (State == State.Configure)

    {



    // set up Pivots indicator

    Pivots1 = Pivots(Close, PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // using daily bars












    // set up RSI indicator

    rsi = RSI(RSIPeriod, 3);





    // Setup ATR INdicator

    atr = ATR(ATRperiod);






    AddDataSeries(secondaryInstrumentSymbol,secondaryI nstrumentPeriod);

    AddDataSeries("ES ##-##", Data.BarsPeriodType.Minute, 1);






    if (ShowIndicators == 1)

    {

    // Add the indicators to the chart only if ShowIndicators is set to 1

    AddChartIndicator(Pivots1);

    AddChartIndicator(rsi);

    AddChartIndicator(atr);

    }






    }



    else if (State == State.DataLoaded)

    {

    if (BarsArray[0].Count < 20 || BarsArray[1].Count < 20)

    {

    // Not enough data to start the strategy, exit or throw an exception

    return;

    }





    // Create a new instance of Pivots and set its Input property

    Pivots2 = Pivots(PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20);

    Pivots2.SetInput(BarsArray[1]);

    AddChartIndicator(Pivots2);



    // Wait for all indicators to be initialized

    if (Pivots1.State != State.Historical ||Pivots2.State != State.Historical || rsi.State != State.Historical || atr.State != State.Historical)

    return;







    }




    private void ManageHedgeEntry()

    {

    Print("ManageHedgeEntry called"); // Add this line

    Print("MarketPosition: " + Position.MarketPosition);






    if (OkToTrade && Position.MarketPosition != MarketPosition.Flat) // Added condition to check if primary position is active

    {

    if (Position.MarketPosition == MarketPosition.Long)

    {



    Print("In Long position");

    Print("R1: " + Pivots2.R1[0] + " R2: " + Pivots2.R2[0] + " Close: " + BarsArray[1].GetClose(0));



    // Add more print statements for the conditions

    Print("Condition R1: " + (BarsArray[1].GetClose(0) <= Pivots2.R1[0]));

    Print("Condition R2: " + (BarsArray[1].GetClose(0) <= Pivots2.R2[0]));





    Print("Checking hedge entry conditions for Long position"); // Added debug message

    // Hedge entry conditions for long primary position

    if (BarsArray[1].GetClose(0) <= Pivots2.R1[0])

    {

    double hedgeShortLimitPrice = Pivots2.R1[0] ;




    hedgeShort1Order = EnterShortLimit(1, true, 1, hedgeShortLimitPrice, @"HedgeShort1");

    Print("Entered HedgeShort1"); // Added debug message

    }

    if (BarsArray[1].GetClose(0) <= Pivots2.R2[0])

    {

    double hedgeShortLimitPrice = Pivots2.R1[0];




    hedgeShort2Order = EnterShortLimit(1, true, 1, hedgeShortLimitPrice, @"HedgeShort2");

    Print("Entered HedgeShort2"); // Added debug message





    }







    }

    else if (Position.MarketPosition == MarketPosition.Short)

    {





    Print("In Short position");

    Print("S1: " + Pivots2.S1[0] + " S2: " + Pivots2.S2[0] + " Close: " + BarsArray[1].GetClose(0));



    // Add more print statements for the conditions

    Print("Condition S1: " + (BarsArray[1].GetClose(0) >= Pivots2.S1[0]));

    Print("Condition S2: " + (BarsArray[1].GetClose(0) >= Pivots2.S2[0]));



    if (BarsArray[1].GetClose(0) >= Pivots2.S1[0])

    {

    double hedgeLongLimitPrice = Pivots2.S1[0] ;

    hedgeLong1Order = EnterLongLimit(1, true, 1, hedgeLongLimitPrice, @"HedgeLong1");

    Print("Entered HedgeLong1"); // Added debug message

    }

    if (BarsArray[1].GetClose(0) >= Pivots2.S2[0])

    {

    double hedgeLongLimitPrice = Pivots2.S2[0];

    hedgeLong2Order = EnterLongLimit(1, true, 1, hedgeLongLimitPrice, @"HedgeLong2");

    Print("Entered HedgeLong2"); // Added debug message





    }

    }

    }

    }

    #2
    Hello, thanks for writing in. You will need to have two instances of the Pivots indicator, SetInput is not documented so it is likely not meant to be used in this context. This should also go in State.DataLoaded:

    Pivots1 = Pivots(Closes[0], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // using daily bars
    Pivots2 = Pivots(Closes[1], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // using daily bars //crate a new Pivots2 indicator variable

    Comment


      #3
      thank you !!

      Comment


        #4
        getting this when i try to load pivots into : This should also go in State.DataLoaded:

        State: Configure

        RSI instantiated

        ATR instantiated

        Secondary data series added

        ES data series added

        State: Configure

        RSI instantiated

        ATR instantiated

        Secondary data series added

        ES data series added

        State: DataLoaded

        Pivots1 instantiated

        One or more indicators not in Historical state

        'Pivots' tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load NQ 06-23 Globex: Daily



        Comment


          #5
          then i get this if i load them in configure.state



          State: Configure

          RSI instantiated

          ATR instantiated

          Pivots1 instantiated

          Strategy 'Pivothedge': Error on calling 'OnStateChange' method: Index was outside the bounds of the array.

          State: Configure

          RSI instantiated

          ATR instantiated

          Pivots1 instantiated

          Strategy 'Pivothedge': Error on calling 'OnStateChange' method: Index was outside the bounds of the array.

          Comment


            #6
            then i get this if i place just Pivots2 in else if (State == State.DataLoaded)



            State: Configure

            RSI instantiated

            ATR instantiated

            Pivots1 instantiated

            Secondary data series added

            ES data series added

            State: Configure

            RSI instantiated

            ATR instantiated

            Pivots1 instantiated

            Secondary data series added

            ES data series added

            State: DataLoaded

            One or more indicators not in Historical state

            'Pivots' tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load ES 06-23 Globex: Daily

            Comment


              #7
              Hi, you need to use AddDataSeries within State.Configure only. This is stated in the help guide page on AddDataSeries:

              Comment


                #8
                thanks for the clarification

                Comment


                  #9
                  getting this error still code is below not sure why the primary not loading

                  'Pivots' tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load NQ 06-23 Globex: Daily



                  }

                  else if (State == State.Configure)

                  {



                  rsi = RSI(RSIPeriod, 3);

                  atr = ATR(ATRperiod);



                  if (ShowIndicators == 1)

                  {

                  // Add the indicators to the chart only if ShowIndicators is set to 1

                  AddChartIndicator(rsi);

                  AddChartIndicator(atr);

                  }



                  AddDataSeries(secondaryInstrumentSymbol, secondaryInstrumentPeriod);












                  }






                  else if (State == State.DataLoaded)

                  {

                  if (BarsArray[0].Count < 20 || BarsArray[1].Count < 20)




                  {




                  // Not enough data to start the strategy, exit or throw an exception




                  return;




                  }







                  Pivots1 = Pivots(Closes[0], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // using daily bars

                  Pivots2 = Pivots(Closes[1], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // secondary inst





                  AddChartIndicator(Pivots1);

                  AddChartIndicator(Pivots2);



                  // Wait for all indicators to be initialized

                  if (Pivots1.State != State.Historical || Pivots2.State != State.Historical || rsi.State != State.Historical || atr.State != State.Historical)

                  return;



                  }

                  Comment


                    #10
                    Hello, you are still calling code in State.Configure that needs to go into State.DataLoaded. All of this needs to go into State.DataLoaded:

                    rsi = RSI(RSIPeriod, 3);

                    atr = ATR(ATRperiod);



                    if (ShowIndicators == 1)

                    {

                    // Add the indicators to the chart only if ShowIndicators is set to 1

                    AddChartIndicator(rsi);

                    AddChartIndicator(atr);​

                    Comment


                      #11
                      ok i will try that

                      Comment


                        #12
                        ok i did what you suggested the code is below :

                        but still get this error:

                        'Pivots' tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load NQ 06-23 Globex: Daily




                        }

                        else if (State == State.Configure)

                        {







                        AddDataSeries(secondaryInstrumentSymbol, secondaryInstrumentPeriod);






                        }






                        else if (State == State.DataLoaded)

                        {

                        if (BarsArray[0].Count < 20 || BarsArray[1].Count < 20)




                        {




                        // Not enough data to start the strategy, exit or throw an exception




                        return;




                        }





                        rsi = RSI(RSIPeriod, 3);

                        atr = ATR(ATRperiod);

                        Pivots1 = Pivots(Closes[0], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // using daily bars



                        if (ShowIndicators == 1)

                        {

                        // Add the indicators to the chart only if ShowIndicators is set to 1

                        AddChartIndicator(rsi);

                        AddChartIndicator(atr);

                        AddChartIndicator(Pivots1);

                        }









                        Pivots2 = Pivots(Closes[1], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // secondary inst



                        AddChartIndicator(Pivots2);



                        // Wait for all indicators to be initialized

                        if (Pivots1.State != State.Historical || Pivots2.State != State.Historical || rsi.State != State.Historical || atr.State != State.Historical)

                        return;



                        }




                        Comment


                          #13
                          ok i got it sorted now :



                          else if (State == State.Configure)

                          {

                          AddDataSeries(BarsPeriodType.Minute, 1);




                          try

                          {

                          AddDataSeries(secondaryInstrumentSymbol, secondaryInstrumentPeriod);

                          Print("Secondary data series added"); // Debug print statement

                          }

                          catch (Exception e)

                          {

                          Print("Error adding secondary data series: " + e.Message);

                          }




                          // Add daily data series for primary and secondary instruments

                          AddDataSeries(BarsPeriodType.Day, 1);

                          AddDataSeries(secondaryInstrumentSymbol, BarsPeriodType.Day, 1);

                          }









                          else if (State == State.DataLoaded)

                          {

                          if (BarsArray[0].Count < 20 || BarsArray[1].Count < 20)




                          {




                          // Not enough data to start the strategy, exit or throw an exception




                          return;




                          }





                          rsi = RSI(RSIPeriod, 3);

                          atr = ATR(ATRperiod);

                          Pivots1 = Pivots(Closes[0], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // using daily bars

                          Pivots2 = Pivots(Closes[1], PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 20); // secondary inst



                          // Add debug print statements for the indicator instances

                          Print("RSI instance: " + rsi);

                          Print("ATR instance: " + atr);

                          Print("Pivots1 instance: " + Pivots1);

                          Print("Pivots2 instance: " + Pivots2);










                          {

                          Print("indicators added ");

                          }



                          if (ShowIndicators == 1)

                          {

                          // Add the indicators to the chart only if ShowIndicators is set to 1

                          AddChartIndicator(rsi);

                          AddChartIndicator(atr);

                          AddChartIndicator(Pivots1);

                          AddChartIndicator(Pivots2);

                          {

                          Print("indicators added to chart ");

                          }



                          }



                          // Wait for all indicators to be initialized

                          if (Pivots1.State != State.Historical || Pivots2.State != State.Historical || rsi.State != State.Historical || atr.State != State.Historical)

                          return;



                          {

                          Print("indicators to be initialized ");

                          }






                          }

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by NullPointStrategies, Today, 05:17 AM
                          0 responses
                          46 views
                          0 likes
                          Last Post NullPointStrategies  
                          Started by argusthome, 03-08-2026, 10:06 AM
                          0 responses
                          126 views
                          0 likes
                          Last Post argusthome  
                          Started by NabilKhattabi, 03-06-2026, 11:18 AM
                          0 responses
                          66 views
                          0 likes
                          Last Post NabilKhattabi  
                          Started by Deep42, 03-06-2026, 12:28 AM
                          0 responses
                          42 views
                          0 likes
                          Last Post Deep42
                          by Deep42
                           
                          Started by TheRealMorford, 03-05-2026, 06:15 PM
                          0 responses
                          46 views
                          0 likes
                          Last Post TheRealMorford  
                          Working...
                          X