Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

I don't know why my strategy isn't working. I'm new to writing strategies.

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

    I don't know why my strategy isn't working. I'm new to writing strategies.

    This is my first time trying to code a strategy. When the parameters are met, it doesn't do anything. I don't know what the problem is. I've gone over the code and can't find any problems. The strategy is code that based on indicators that I currently use.


    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class ISMAEMA34WaveStrat : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Takes enties based on EMA 34 Wave Strat for NQ.";
    Name = "ISMAEMA34WaveStrat";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    StrategyOn = false;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 12200);
    }
    }

    protected override void OnBarUpdate()
    {
    if(CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot)
    return;

    double BarMADiff = Math.Abs(TSI(BarsArray[1],10, 34)[0] - EMA(TSI(BarsArray[1],10, 34), 4)[0]);

    //1ST INSTANCE--IF HTF MA IS BULLISH/BEARISH AND WAITING ON WAVE TO GO BULLISH/BEARISH
    if(TSI(BarsArray[1],10, 34)[0] > 0 && TSI(BarsArray[1],10, 34)[0] > EMA(TSI(BarsArray[1],10, 34),4)[0] && BarMADiff >= 2.3 ||
    TSI(BarsArray[1],10, 34)[0] < 0 && TSI(BarsArray[1],10, 34)[0] > EMA(TSI(BarsArray[1],10, 34),4)[0] && BarMADiff >= 2.3)
    {
    if(TSI(34,89)[7] >= 12 && TSI(34,89)[6] >= 12 && TSI(34,89)[5] >= 12 && TSI(34,89)[4] >= 12 && TSI(34,89)[3] >= 12 && TSI(34,89)[2] >= 12 && TSI(34,89)[1] >= 12 && TSI(34,89)[0] >= 12 && TSI(34,89)[0] >= EMA(TSI(34,89),93)[0] ||

    TSI(34,89)[7] >= 12 && TSI(34,89)[6] >= 12 && TSI(34,89)[5] >= 12 && TSI(34,89)[4] >= 12 && TSI(34,89)[3] >= 12 && TSI(34,89)[2] >= 12 && TSI(34,89)[1] >= 12 && TSI(34,89)[0] >= 12 /*&& TSI(34,89)[8] < EMA(TSI(34,89),93)[8]*/ &&
    TSI(34,89)[7] >= EMA(TSI(34,89),93)[7] && TSI(34,89)[6] >= EMA(TSI(34,89),93)[6] && TSI(34,89)[5] >= EMA(TSI(34,89),93)[5] && TSI(34,89)[4] >= EMA(TSI(34,89),93)[4] && TSI(34,89)[3] >= EMA(TSI(34,89),93)[3] &&
    TSI(34,89)[2] >= EMA(TSI(34,89),93)[2] && TSI(34,89)[1] >= EMA(TSI(34,89),93)[1] && TSI(34,89)[0] >= EMA(TSI(34,89),93)[0])
    {
    if(StrategyOn == true && Position.MarketPosition == MarketPosition.Flat && SMA(20)[1] >= EMA(High,34)[1] && Close[1] > SMA(20)[1] && Close[0] <= SMA(20)[0] ||
    StrategyOn == true && Position.MarketPosition == MarketPosition.Flat && SMA(20)[1] < EMA(High, 34)[1] && Close[1] > EMA(High, 34)[1] && Close[0] <= EMA(High, 34)[0])
    {
    EnterLong();
    }
    }
    }

    else if(TSI(BarsArray[1],10, 34)[0] > 0 && TSI(BarsArray[1],10, 34)[0] < EMA(TSI(BarsArray[1],10, 34),4)[0] && BarMADiff >= 2.3 ||
    TSI(BarsArray[1],10, 34)[0] < 0 && TSI(BarsArray[1],10, 34)[0] < EMA(TSI(BarsArray[1],10, 34),4)[0] && BarMADiff >= 2.3)
    {
    if(TSI(34,89)[7] <= -12 && TSI(34,89)[6] <= -12 && TSI(34,89)[5] <= -12 && TSI(34,89)[4] <= -12 && TSI(34,89)[3] <= -12 && TSI(34,89)[2] <= -12 && TSI(34,89)[1] <= -12 && TSI(34,89)[0] <= -12 && TSI(34,89)[0] <= EMA(TSI(34,89),93)[0] ||

    TSI(34,89)[7] <= -12 && TSI(34,89)[6] <= -12 && TSI(34,89)[5] <= -12 && TSI(34,89)[4] <= -12 && TSI(34,89)[3] <= -12 && TSI(34,89)[2] <= -12 && TSI(34,89)[1] <= -12 && TSI(34,89)[0] <= -12 /*&& TSI(34,89)[8] > EMA(TSI(34,89),93)[8]*/ &&
    TSI(34,89)[7] <= EMA(TSI(34,89),93)[7] && TSI(34,89)[6] <= EMA(TSI(34,89),93)[6] && TSI(34,89)[5] <= EMA(TSI(34,89),93)[5] && TSI(34,89)[4] <= EMA(TSI(34,89),93)[4] && TSI(34,89)[3] <= EMA(TSI(34,89),93)[3] && TSI(34,89)[2] <= EMA(TSI(34,89),93)[2] &&
    TSI(34,89)[1] <= EMA(TSI(34,89),93)[1] && TSI(34,89)[0] <= EMA(TSI(34,89),93)[0])
    {
    if(StrategyOn == true && Position.MarketPosition == MarketPosition.Flat && SMA(20)[1] <= EMA(Low, 34)[1] && Close[1] < SMA(20)[1] && Close[0] >= SMA(20)[0] ||
    StrategyOn == true && Position.MarketPosition == MarketPosition.Flat && SMA(20)[1] > EMA(Low, 34)[1] && Close[1] < EMA(Low, 34)[1] && Close[0] >= EMA(Low, 34)[0])
    {
    EnterShort();
    }
    }
    }

    #2
    I manually set the StrategyOn bool to true when the strategy parameters are close to being met. It still doesn't do anything.

    Comment


      #3
      Hello jamestrader21x,

      Thank you for your note.

      In testing on a 1 minute ES chart I'm seeing this make an entry, but never exit. I suspect your conditions for entry/exit are only very rarely becoming true.

      To confirm this, the first thing I would recommend would be to turn on the Order Trace function located in State.SetDefaults:

      if (State == State.SetDefaults)
      {
      TraceOrders = true;
      }

      Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.

      Here is a link to our help guide that goes into more detail on tracing orders:

      https://ninjatrader.com/support/help...aceorders2.htm

      Trace orders alone may not give you the full picture of whether or not a trade should have been entered on a given bar, so adding prints to your strategy that will show in the NinjaScript Output window, with information on what the variables you're using for your conditions are on a particular bar, can be helpful.

      This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.

      https://ninjatrader.com/support/foru...ns-not-working

      If you run into issues like we saw here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently than you would expect. With the printout information you can assess what is different between the two and determine if you need to adjust your entry conditions to enter and exit more often.

      Please let us know if we may be of further assistance to you.
      Kate W.NinjaTrader Customer Service

      Comment


        #4
        I have this applied to a 610T chart. In the label description on the chart it says "(D)ISMAEMA34WaveStrat(False)" What does the "(D)" mean? I'll try the order trace.

        Comment


          #5
          Hello jamestrader21x,

          Thank you for your reply.

          The (D) stands for disabled - sometimes, especially with indicators that use tick based data series, it can take a minute or so before the data is all loaded and the indicator reaches a state where it can begin calculations - at that point you should see the D disappear and (calculating...) to be shown after the indicator name. If the D never goes away, check the log tab for any errors that may have prevented the indicator from starting.

          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            I clicked the Enabled bool below Start Behavior. The (D) goes away when I do so.This is what printed on the NS Output:
            Enabling NinjaScript strategy 'ISMAEMA34WaveStrat/206330580' : On starting a real-time strategy - StartBehavior=WaitUntilFlat Position=MNQ 06-21 1L, MNQ 06-21 1L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Ignore all errors ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes

            Comment


              #7
              I figured out how to get it to work and how to put in stops and targets. I was under the impression that once a trade is executed, the ATM Strat would kick in.


              1. You have to have the thing Enabled. That's why it's showing a (D).

              2. I thought Market Position was the method used for indicating your current position(Long, Short, Flat), but some reason it doesn't work. You have to use IsWaitUntilFlat if you want to ignore new set ups when in a position.

              3. You have to specify the amount of units you want to trade. Again, I thought ATM Strategy would kick in.

              I tested on a simple strat.

              if(Close[1] < EMA(20)[1] && Close[0] > EMA(20)[0])
              {
              if(!IsWaitUntilFlat)
              {
              return;
              }

              else{
              EnterLong(3);
              SetStopLoss(CalculationMode.Ticks, 63);
              SetProfitTarget(CalculationMode.Ticks, 90);}

              }

              else if( Close[1] > EMA(20)[1] && Close[0] < EMA(20)[0])
              {
              if(!IsWaitUntilFlat)
              {
              return;
              }
              else{
              EnterShort(3);
              SetStopLoss(CalculationMode.Ticks, 63);
              SetProfitTarget(CalculationMode.Ticks, 90);}


              Thanks for the help!

              Comment


                #8
                Hello jamestrader21x,

                Thank you for your reply.

                ATM Strategies can be used with an automated strategy, but using those would require some advanced programming skill. You can check out the built in Sample ATM Strategy for a demonstration and further information regarding the use of ATMs within automated strategies may be found here:





                If you're using the same settings for the stop loss and profit target, you can simply put a single call to each in State.DataLoaded. I'd actually recommend creating a simple strategy with entry in each direction and a single stop loss and profit target that will be used for both directions in the Strategy Builder and unlocking the code to see that structure.

                If you're calling them when the order is placed, in On Bar Update, you should call SetStopLoss and SetProfitTarget before submitting the entry order, otherwise you may see odd behavior.

                Please let us know if we may be of further assistance to you.

                Kate W.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by The_Sec, Today, 02:29 PM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by jeronymite, 04-12-2024, 04:26 PM
                2 responses
                30 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by Mindset, 05-06-2023, 09:03 PM
                10 responses
                265 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by michi08, 10-05-2018, 09:31 AM
                5 responses
                743 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by tsantospinto, 04-12-2024, 07:04 PM
                4 responses
                63 views
                0 likes
                Last Post aligator  
                Working...
                X