Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Running strategy but nothing shows -Error on calling 'OnBarUpdate' method

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

    Running strategy but nothing shows -Error on calling 'OnBarUpdate' method

    Nothing shows when running optimization in strategy base on 2 custom indicators. Error in output window:

    Error on calling 'OnBarUpdate' method for indicator 'Vswing' on bar 86: Object reference not set to an instance of an object.


    Code as follow:

    public class Vscraper : Strategy
    {
    #region Variables
    private int target = 24;
    private int stop = 12;
    private bool be2 = false;
    private bool be3 = false;
    private int vswingStrength = 20;

    #endregion

    bool _initialized = false;

    protected override void Initialize()
    {
    Add(Vscraper());
    Add(Vswing(true, true, vswingStrength, Color.Navy, Color.Gold));

    CalculateOnBarClose = true;
    EntryHandling = EntryHandling.UniqueEntries;
    EntriesPerDirection = 1;
    TraceOrders = true;

    }

    private void GoLong()
    {
    SetStopLoss("target", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    SetProfitTarget("target", CalculationMode.Price, Close[0] + (Target*TickSize));
    EnterLong("Target");
    }

    private void GoShort()
    {
    SetStopLoss("target", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    SetProfitTarget("target", CalculationMode.Price, Close[0] - (Target*TickSize));
    EnterShort("Target");
    }



    protected override void OnBarUpdate()
    {
    if(CurrentBar <= 30)
    return;
    if(!_initialized)


    //Vswing vswing = Vswing(true, true, 20, Color.DarkCyan, Color.Magenta);

    if (Position.MarketPosition != MarketPosition.Flat) return;


    // Condition set 1
    if (Vscraper().Signal[0] == 1
    && Vswing(true, true, vswingStrength, Color.Navy, Color.Gold).LowRay[0] - 4 * TickSize < Low[0]
    && Vswing(true, true, vswingStrength, Color.Navy, Color.Gold).LowRay[0] + 4 * TickSize > Low[0])
    {
    EnterLong();
    }

    // Condition set 2
    if (Vscraper().Signal[0] == -1
    && Vswing(true, true, 20, Color.Navy, Color.Gold).HighRay[0] - 4 * TickSize < High[0]
    && Vswing(true, true, 20, Color.Navy, Color.Gold).HighRay[0] + 4 * TickSize > High[0])
    {
    EnterShort();
    }

    _initialized = true;
    }

    #2
    Hello, you would need to start by debugging the custom indicator called and where it would run into accessing an empty object leading to the error message.

    Comment


      #3
      SwingRaysV1 support and resistance levels for trading strategy

      I am trying to build a strategy to trade on the levels, my code seems to be working on only one value? How do I deal with 2 values for EnterLong and EnterShort (support and resistance) in the strategy code? I have tried to find similar examples but realized that the forum has become so large that I lost in the maze. Any keywords I can search in the forum that you can suggest?

      Comment


        #4
        Sorry I'm not following your issue: if you would like to use 2 different levels for your strategy then you would need to enhance indicator offering those calcs, for example by plotting 2 values from it and then accessing each individual plot as part of your conditions.

        Comment


          #5
          I am try to write a strategy to trade the closest support and resistance level in the "SwingRayV1" indicator which has not got any plot at the moment. The "lastSwingHigh" at [0] will print out all the resistance levels. How do I Plot the lowest resistance value to be use in the strategy?

          Comment


            #6
            You would then need to expose the dataseries value you're attempting to access, Plots would be exposed per default, but this would not be the case with custom dataseries as such in your case :

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            646 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            367 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            107 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            569 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X