Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with errors in strategy.

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

    Need help with errors in strategy.

    Hello,

    I am working on creating a strategy and I'm trying to get the code working. I am using code developed in Pinescript (TradingView) and trying to convert it into working code for C# (NinjaTrader). I have gotten the current version of my code down to 4 errors, but I don't understand enough about C# to troubleshoot them. This is my first attempt at C# but I've done a little Python and C++ programming. Thanks in advance for any help you can provide.

    ----------------------------------







    81 int p = 10; //ATR Length (p) ***No longer an input, set in code, fix later?
    82 int x = 1; // ATR Coefficient (x) ***No longer an input, set in code, fix later?
    83 int q = 9; //Stop Length (q) ***No longer an input, set in code, fix later?
    84 double A_T_R = ATR(p)[0];
    85 Series<double> first_high_stop;
    86 Series<double> first_low_stop;
    87
    88
    89 first_high_stop[0] = HighestBar(High, p) - x * A_T_R;
    90 first_low_stop[0] = LowestBar(Low, p) + x * A_T_R;
    91 double stop_short = HighestBar(first_high_stop, q);
    92 double stop_long = LowestBar(first_low_stop, q);
    93 //plot(stop_long, color=#2962FF, title="Stop Long"); ***Figure out plots later
    94 //plot(stop_short, color=#FF6D00, title="Stop Short"); ***Figure out plots later
    95
    96
    97
    98 int length1 = 32;
    99 int offset = 0;
    100 double src = Close[0];
    101 double lsma = LinReg(src, length1)[offset];
    102 double lsma2 = LinReg(lsma, length1)[offset];
    103 double eq = lsma-lsma2;
    104 double zlsma = lsma+eq;

    #2
    Hello Orion815,

    Thank you for your post.

    I recommend looking at this support article that goes over getting started with NinjaScript:



    Is the code you have posted the PineScript or the NinjaScript? Please post the full version of the NinjaScript code so we can examine the lines referenced in the compile errors.

    The Help Guide page below goes over creating custom series in NinjaScript:



    You'll need to define a variable at the class level of type Series<double>, then in State.DataLoaded create a new Series<double> object and assign it to your variable.

    The first two compile errors suggest you are trying to assign a series to a double. For example,

    Code:
    double myDouble
    
    //this would not work
    myDouble = SMA(14);
    
    //this would work
    myDouble = SMA(14)[0];
    The second two errors suggest you are trying to use a variable before it has been assigned a value.

    For example;

    Code:
    double myDouble;
    double sum;
    
    //this wouldn't work because my double hasn't been assigned a value yet
    sum = myDouble + 2;
    Please let us know if you have any further questions.

    Comment


      #3
      Thank you for your response. I am still struggling to get the data types to match up properly. Below is the full code:

      -----------------------------------------

      region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class TradeStrategy : Strategy
      {

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "TradeStrategy";
      Calculate = Calculate.OnEachTick;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;


      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {

      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;


      //settings
      int TradeIndicator = 0;

      //Chande Kroll
      int p = 10; //ATR Length (p) ***No longer an input, set in code, fix later?
      int x = 1; // ATR Coefficient (x) ***No longer an input, set in code, fix later?
      int q = 9; //Stop Length (q) ***No longer an input, set in code, fix later?
      double A_T_R = ATR(p)[0];
      Series<double> first_high_stop;
      Series<double> first_low_stop;


      first_high_stop[0] = HighestBar(High, p) - x * A_T_R;
      first_low_stop[0] = LowestBar(Low, p) + x * A_T_R;
      double stop_short = HighestBar(first_high_stop, q);
      double stop_long = LowestBar(first_low_stop, q);
      //plot(stop_long, color=#2962FF, title="Stop Long"); ***Figure out plots later
      //plot(stop_short, color=#FF6D00, title="Stop Short"); ***Figure out plots later



      double src = Close[0];
      double lsma = LinReg(src, 32)[0];
      double lsma2 = LinReg(lsma, 32)[0];
      double eq = lsma-lsma2;
      double zlsma = lsma+eq;

      //plot(zlsma, color=color.yellow, linewidth=3); ***Figure out plots later


      // setup
      double currentprice = GetCurrentBid();
      bool ZLSMAcompare = false;
      bool CKBuycompare = false;
      bool CKSellcompare = false;



      //Chande Kroll Comparison
      if (stop_long < currentprice)
      {
      CKBuycompare = true;
      }
      else
      {
      CKBuycompare = false;
      }
      //Chande Kroll Comparison
      if (stop_short > currentprice)
      {
      CKSellcompare = true;
      }
      else
      {
      CKSellcompare = false;
      }
      //ZLSMA Comparison
      if (zlsma < currentprice)
      {
      ZLSMAcompare = true;
      }
      else
      {
      ZLSMAcompare = false;
      }
      //parameters to adjust
      int tradeqty = 1; //number of contracts
      int loss2 = 50; //stoploss in number of ticks
      int vol2 = 300; //minimum trading volume
      long vol1 = GetCurrentBidVolume();

      //trading

      if (TradeIndicator == 0 & CKBuycompare == true & vol1 > vol2 & ZLSMAcompare == true)
      {
      EnterLong(tradeqty, "Enter Long");
      SetStopLoss(CalculationMode.Ticks, loss2);
      TradeIndicator = 1;
      }
      if (ZLSMAcompare == false & CKSellcompare == true)
      {
      ExitLong("Enter Long");
      }

      if (TradeIndicator == 0 & CKSellcompare == true & vol1 > vol2 & ZLSMAcompare == false)
      {
      EnterShort(tradeqty, "Enter Short");
      SetStopLoss(CalculationMode.Ticks, loss2);
      TradeIndicator = 1;
      }
      if (ZLSMAcompare == true & CKBuycompare == true)
      {
      ExitShort("Enter Short");
      }

      }

      region Properties
      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="PositionSize", Order=1, GroupName="Parameters")]
      public int PositionSize
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="LongTarget", Order=2, GroupName="Parameters")]
      public int LongTarget
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name="ShortTarget", Order=3, GroupName="Parameters")]
      public int ShortTarget
      { get; set; }
      #endregion

      }
      }​

      Comment


        #4
        Hello,

        If you are still getting compile errors, can you post the full errors so we can examine the lines the errors are indicating are the issue?

        Thank you in advance.

        Comment

        Latest Posts

        Collapse

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