Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unexpected Character '$', code CS1056, line 143, column 27

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

    Unexpected Character '$', code CS1056, line 143, column 27

    hi. can you please help me fix the error "Unexpected Character '$', code CS1056, line 143, column 27" in this script?

    region Using declarations
    using System;
    using System.Collections.Generic;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.Strategies;
    using NinjaTrader.NinjaScript.Indicators;
    #endregion

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MNQOptimizedScalpingV2 : Strategy
    {
    private EMA emaFast;
    private EMA emaSlow;
    private VWAP vwap;
    private RSI rsi;
    private Dictionary<string, DateTime> newsEvents;

    // Optimizable Parameters
    [NinjaScriptProperty]
    [Range(5, 20), Optimize(8, 12, 1)]
    public int FastEMAPeriod { get; set; }

    [NinjaScriptProperty]
    [Range(15, 50), Optimize(20, 30, 2)]
    public int SlowEMAPeriod { get; set; }

    [NinjaScriptProperty]
    [Range(4, 15), Optimize(6, 10, 1)]
    public int StopLossTicks { get; set; }

    [NinjaScriptProperty]
    [Range(6, 20), Optimize(8, 12, 1)]
    public int ProfitTargetTicks { get; set; }

    [NinjaScriptProperty]
    [Range(4, 12), Optimize(6, 10, 1)]
    public int TrailingStopTicks { get; set; }

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Optimized MNQ Scalping with Trailing Stops & News Filters";
    Name = "MNQOptimizedScalpingV2";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    Slippage = 1;
    StartBehavior = StartBehavior.WaitUntilFlat;
    AddDataSeries(Data.BarsPeriodType.Minute, 1);

    // Define major news events (adjust as needed)
    newsEvents = new Dictionary<string, DateTime>
    {
    { "FOMC", new DateTime(2025, 2, 14, 14, 00, 0) }, // Example: FOMC statement at 2:00 PM
    { "NFP", new DateTime(2025, 2, 7, 8, 30, 0) }, // Example: Non-Farm Payrolls at 8:30 AM
    { "CPI", new DateTime(2025, 2, 13, 8, 30, 0) } // Example: CPI report at 8:30 AM
    };
    }
    else if (State == State.Configure)
    {
    emaFast = EMA(FastEMAPeriod);
    emaSlow = EMA(SlowEMAPeriod);
    vwap = VWAP(VWAPResolution.Standard, 0, 0, 0);
    rsi = RSI(14, 3);

    AddChartIndicator(emaFast);
    AddChartIndicator(emaSlow);
    AddChartIndicator(vwap);
    AddChartIndicator(rsi);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < SlowEMAPeriod) return; // Ensure enough bars exist

    // Check if current time is near a high-impact news event
    if (IsNewsEventUpcoming()) return;

    bool longCondition = Close[0] > vwap.VWAPLine[0] && emaFast[0] > emaSlow[0] && rsi[0] > 50;
    bool shortCondition = Close[0] < vwap.VWAPLine[0] && emaFast[0] < emaSlow[0] && rsi[0] < 50;

    if (longCondition && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong("LongEntry");
    SetProfitTarget("LongEntry", CalculationMode.Ticks, ProfitTargetTicks);
    SetStopLoss("LongEntry", CalculationMode.Ticks, StopLossTicks, false);
    Draw.ArrowUp(this, "BuySignal_" + CurrentBar, true, 0, Low[0] - 5 * TickSize, Brushes.Green);
    }

    if (shortCondition && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort("ShortEntry");
    SetProfitTarget("ShortEntry", CalculationMode.Ticks, ProfitTargetTicks);
    SetStopLoss("ShortEntry", CalculationMode.Ticks, StopLossTicks, false);
    Draw.ArrowDown(this, "SellSignal_" + CurrentBar, true, 0, High[0] + 5 * TickSize, Brushes.Red);
    }

    // Apply Trailing Stop for active positions
    ManageTrailingStop();
    }

    private void ManageTrailingStop()
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    double newStop = Position.AveragePrice - (TrailingStopTicks * TickSize);
    if (newStop > GetCurrentStopLoss()) SetStopLoss(CalculationMode.Price, newStop);
    }

    if (Position.MarketPosition == MarketPosition.Short)
    {
    double newStop = Position.AveragePrice + (TrailingStopTicks * TickSize);
    if (newStop < GetCurrentStopLoss()) SetStopLoss(CalculationMode.Price, newStop);
    }
    }

    private double GetCurrentStopLoss()
    {
    foreach (Order order in ActiveOrders)
    {
    if (order.OrderType == OrderType.StopMarket) return order.StopPrice;
    }
    return 0;
    }

    private bool IsNewsEventUpcoming()
    {
    foreach (var news in newsEvents)
    {
    DateTime eventTime = news.Value;
    if (Time[0].Date == eventTime.Date && Math.Abs((Time[0].Hour * 60 + Time[0].Minute) - (eventTime.Hour * 60 + eventTime.Minute)) <= 10)
    {
    Print($"Skipping trade due to {news.Key} event at {eventTime}");
    return true;
    }
    }
    return false;
    }
    }
    }​

    #2
    Hello Unclepips,

    The only $ in the code provided is in the print you have. What version of NinjaTrader are you currently using? The $ syntax for strings was only recently supported so if you are using an older version that code likely won't compile. You could use

    Print("Skipping trade due to " + news.Key + " event at " +eventTime);

    Comment


      #3
      Hi- thanks for taking a look at this. I use NT version 8.0.28.0 64-bit

      Comment


        #4
        Hello Unclepips,

        That would be the reason, you would have to update to the current release to use the newer C# features. 8.0.28 is using an older C# version than the current release which supports C# 9. The $ syntax is a newer C# feature.

        Comment


          #5
          Thanks for helping fix the "$" error. This is my first time trying to code something I had never done before. I am still learning bits and pieces.

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello Unclepips,

            That would be the reason, you would have to update to the current release to use the newer C# features. 8.0.28 is using an older C# version than the current release which supports C# 9. The $ syntax is a newer C# feature.
            I had issues with the newer version. NT supporter was supposed to call me a couple of weeks ago, but no one did. Before that when I spoke to one of your colleagues she helped me to upgrade from the older version that I had to this 8.0.28. She said I could just use it until someone called to help me straighten things out. I actually bought NT to license for multiple brokerages. Thanks.

            Comment


              #7
              Hello Unclepips,

              Unfortunately I can't look up your support logs from the forum, if you still need assistance with updating and wanted a call you can do that by emailing [email protected]. Generally when someone schedules a call they will provide a specific time and date to ensure we can reach you.

              Comment


                #8
                hi- I called and spoke to someone yesterday, he mentioned that an email will be sent to me to set up an appointment, but I have not seen any email.
                by the way, I was able to solve the crashing part. I had to uninstall and delete all ninjascripts that I had ever owned on my computer, and then the computer allowed me to install the new NT 8.1.4.1. Thanks.

                Comment


                  #9
                  would it be possible for you to look at this script making sure it is compliant with NT 8.1.4.1?

                  region Using declarations
                  using System;
                  using System.Collections.Generic;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Gui.Tools;
                  using NinjaTrader.NinjaScript;
                  using NinjaTrader.Data;
                  using NinjaTrader.NinjaScript.StrategyGenerator;
                  using NinjaTrader.NinjaScript.Strategies;
                  using NinjaTrader.NinjaScript.Indicators;
                  using NinjaTrader.Gui;
                  using NinjaTrader.Core.FloatingPoint;
                  #endregion

                  namespace NinjaTrader.NinjaScript.Strategies
                  {
                  public class MNQOptimizedScalpingV2 : Strategy
                  {
                  private EMA emaFast;
                  private EMA emaSlow;
                  private VWAP vwap;
                  private RSI rsi;
                  private Dictionary<string, DateTime> newsEvents;

                  // Optimizable Parameters
                  [NinjaScriptProperty]
                  [Range(5, 20), NinjaTrader.Gui.Tools.Display(Name = "Fast EMA Period", GroupName = "Parameters", Order = 0)]
                  public int FastEMAPeriod { get; set; }

                  [NinjaScriptProperty]
                  [Range(15, 50), NinjaTrader.Gui.Tools.Display(Name = "Slow EMA Period", GroupName = "Parameters", Order = 1)]
                  public int SlowEMAPeriod { get; set; }

                  [NinjaScriptProperty]
                  [Range(4, 15), NinjaTrader.Gui.Tools.Display(Name = "Stop Loss Ticks", GroupName = "Parameters", Order = 2)]
                  public int StopLossTicks { get; set; }

                  [NinjaScriptProperty]
                  [Range(6, 20), NinjaTrader.Gui.Tools.Display(Name = "Profit Target Ticks", GroupName = "Parameters", Order = 3)]
                  public int ProfitTargetTicks { get; set; }

                  [NinjaScriptProperty]
                  [Range(4, 12), NinjaTrader.Gui.Tools.Display(Name = "Trailing Stop Ticks", GroupName = "Parameters", Order = 4)]
                  public int TrailingStopTicks { get; set; }

                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {
                  Description = "Optimized MNQ Scalping with Trailing Stops & News Filters";
                  Name = "MNQOptimizedScalpingV2";
                  Calculate = Calculate.OnEachTick;
                  EntriesPerDirection = 1;
                  EntryHandling = EntryHandling.AllEntries;
                  StopTargetHandling = StopTargetHandling.PerEntryExecution;
                  IsExitOnSessionCloseStrategy = true;
                  ExitOnSessionCloseSeconds = 30;
                  IsFillLimitOnTouch = false;
                  Slippage = 1;
                  StartBehavior = StartBehavior.WaitUntilFlat;
                  AddDataSeries(Data.BarsPeriodType.Minute, 1);

                  // Define major news events (adjust as needed)
                  newsEvents = new Dictionary<string, DateTime>
                  {
                  { "FOMC", new DateTime(2025, 2, 14, 14, 00, 0) },
                  { "NFP", new DateTime(2025, 2, 7, 8, 30, 0) },
                  { "CPI", new DateTime(2025, 2, 13, 8, 30, 0) }
                  };
                  }
                  else if (State == State.Configure)
                  {
                  emaFast = EMA(FastEMAPeriod);
                  emaSlow = EMA(SlowEMAPeriod);
                  vwap = VWAP(VWAPResolution.Standard, 0, 0, 0);
                  rsi = RSI(14, 3);

                  AddChartIndicator(emaFast);
                  AddChartIndicator(emaSlow);
                  AddChartIndicator(vwap);
                  AddChartIndicator(rsi);
                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  if (CurrentBar < SlowEMAPeriod) return;

                  // Check if current time is near a high-impact news event
                  if (IsNewsEventUpcoming()) return;

                  bool longCondition = Close[0] > vwap.VWAPLine[0] && emaFast[0] > emaSlow[0] && rsi[0] > 50;
                  bool shortCondition = Close[0] < vwap.VWAPLine[0] && emaFast[0] < emaSlow[0] && rsi[0] < 50;

                  if (longCondition && Position.MarketPosition == MarketPosition.Flat)
                  {
                  EnterLong("LongEntry");
                  SetProfitTarget("LongEntry", CalculationMode.Ticks, ProfitTargetTicks);
                  SetStopLoss("LongEntry", CalculationMode.Ticks, StopLossTicks, false);
                  Draw.ArrowUp(this, "BuySignal_" + CurrentBar.ToString(), true, 0, Low[0] - 5 * TickSize, Brushes.Green);
                  }

                  if (shortCondition && Position.MarketPosition == MarketPosition.Flat)
                  {
                  EnterShort("ShortEntry");
                  SetProfitTarget("ShortEntry", CalculationMode.Ticks, ProfitTargetTicks);
                  SetStopLoss("ShortEntry", CalculationMode.Ticks, StopLossTicks, false);
                  Draw.ArrowDown(this, "SellSignal_" + CurrentBar.ToString(), true, 0, High[0] + 5 * TickSize, Brushes.Red);
                  }

                  // Apply Trailing Stop for active positions
                  ManageTrailingStop();
                  }

                  private void ManageTrailingStop()
                  {
                  if (Position.MarketPosition == MarketPosition.Long)
                  {
                  double newStop = Position.AveragePrice - (TrailingStopTicks * TickSize);
                  if (newStop > GetCurrentStopLoss()) SetStopLoss(CalculationMode.Price, newStop);
                  }

                  if (Position.MarketPosition == MarketPosition.Short)
                  {
                  double newStop = Position.AveragePrice + (TrailingStopTicks * TickSize);
                  if (newStop < GetCurrentStopLoss()) SetStopLoss(CalculationMode.Price, newStop);
                  }
                  }

                  private double GetCurrentStopLoss()
                  {
                  foreach (Order order in ActiveOrders)
                  {
                  if (order.OrderType == OrderType.StopMarket) return order.StopPrice;
                  }
                  return 0;
                  }

                  private bool IsNewsEventUpcoming()
                  {
                  foreach (var news in newsEvents)
                  {
                  DateTime eventTime = news.Value;
                  if (Time[0].Date == eventTime.Date && Math.Abs((Time[0].Hour * 60 + Time[0].Minute) - (eventTime.Hour * 60 + eventTime.Minute)) <= 10)
                  {
                  Print($"Skipping trade due to {news.Key} event at {eventTime}");
                  return true;
                  }
                  }
                  return false;
                  }
                  }
                  }

                  Comment


                    #10
                    Hello Unclepips,

                    If you still are waiting for an email please check your spam folder to make sure you didn't miss it. If you still don't see it you will need to email in to [email protected]. I would suggest always using email directly for any support requests so you have a case number that can be referenced, if something gets missed we can look up the case number to find out what was sent out.

                    Regarding compliant code, are you seeing any errors? The only reason you had an error originally was due to the version being used and using a newer C# feature that was not available in 8.0.28. If you aren't getting any compile errors you can try and run the code in a chart.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    558 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    324 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    101 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    545 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    547 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X