Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adaptive Market Trading

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

    Adaptive Market Trading

    I need some help analyzing why the script listed below have a few errors and would like to fix them to be able to use it on a chart. Thanks for your help.

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Adaptive trading strategy using AMA, ATR, and RSI.";
    Name = "AdaptiveMarketStrategy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    IsStopSupported = true;
    IsExitOnSessionCloseStrategy = false;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1); // Using 1-minute bars
    AddAMA(10, 30); // Fast AMA
    AddAMA(30, 60); // Slow AMA
    AddATR(14);
    AddRSI(14, 3);
    }
    }

    private AMA fastAMA;
    private AMA slowAMA;
    private ATR atr;
    private RSI rsi;

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 60) return; // Wait for enough bars

    double fastAmaValue = fastAMA[0];
    double slowAmaValue = slowAMA[0];
    double atrValue = atr[0];
    double rsiValue = rsi[0];

    // Bullish trade entry
    if (CrossAbove(fastAMA, slowAMA, 1) && rsiValue > 60)
    {
    EnterLong("LongEntry");
    SetStopLoss("LongEntry", CalculationMode.Price, Close[0] - 1.5 * atrValue, false);
    SetProfitTarget("LongEntry", CalculationMode.Price, Close[0] + 3 * atrValue);
    }

    // Bearish trade entry
    if (CrossBelow(fastAMA, slowAMA, 1) && rsiValue < 40)
    {
    EnterShort("ShortEntry");
    SetStopLoss("ShortEntry", CalculationMode.Price, Close[0] + 1.5 * atrValue, false);
    SetProfitTarget("ShortEntry", CalculationMode.Price, Close[0] - 3 * atrValue);
    }

    // Trailing stop logic for long positions
    if (Position.MarketPosition == MarketPosition.Long)
    {
    double highestHigh = MAX(High, 20)[0]; // Adjust period as needed
    SetStopLoss("LongEntry", CalculationMode.Price, highestHigh - atrValue, true);
    }

    // Trailing stop logic for short positions
    if (Position.MarketPosition == MarketPosition.Short)
    {
    double lowestLow = MIN(Low, 20)[0]; // Adjust period as needed
    SetStopLoss("ShortEntry", CalculationMode.Price, lowestLow + atrValue, true);
    }
    }

    #2
    Hello benmarkal,

    Thank you for your inquiry.

    AddAMA, AddRSI, and AddATR are not valid NinjaScript methods. If you would like to use an indicator in your strategy's calculations, you need to define it as a class level variable then assign the value in State.DataLoaded.

    For example:

    Code:
    private RSI myRSI;
    
    //in State.DataLoaded
    myRSI = RSI(14, 3);

    Please note that AMA is not a NinjaTrader indicator as well. However, if this is a third party indicator you're using, you can use it in the same way as detailed above.

    Additionally, set methods cannot be unset. This means it is important to call the Set method with CalculationMode.Ticks to reset this before calling an entry method (not after).​

    If you want to dynamically change/modify the price of the stop loss and profit target, the method should be called from within OnBarUpdate() and the price should always be reset when the strategy becomes flat again. This information is noted in the "Tips" section on the following help guide pages:The reference sample script SamplePriceModification demonstrates how to change the price of stop loss and profit target orders:



    ​If you are getting any errors in the Log tab of the Control Center or any compile errors, please provide a screenshot of the full error messages.

    Please let us know if we can assist further.

    Comment


      #3
      Thanks Gaby, for responding to my post and suggesting a fix for my issue. Could you please provide me with a code from the one listed below that will work in NinjaTrader 8.

      AddDataSeries(Data.BarsPeriodType.Minute, 15); // 15-minute bars

      //Add indicators
      EMA ema50 = EMA(50);
      EMA ema20 = EMA(20);
      RSI rsi = RSI(14, 3);
      ATR atr = ATR(14);​

      Please re-code it for me!

      Comment


        #4
        Hello benmarkal,

        To add the indicators, you would do like the example I provided in my previous post.

        If you would like to use an indicator in your strategy's calculations, you need to define it as a class level variable then assign the value in State.DataLoaded.

        For example:

        private RSI myRSI;

        //in State.DataLoaded
        myRSI = RSI(14, 3);


        As an example, take a look at the SampleMACrossover included in the NinjaTrader 8.
        Last edited by NinjaTrader_Gaby; 07-09-2024, 01:03 PM. Reason: testing

        Comment

        Latest Posts

        Collapse

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