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

MACD Strategy - Need Advice

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

    MACD Strategy - Need Advice

    I am trying to use a strategy to find setups like in the picture where the current bar closes above the EMA@9, the MACD and MACD AVG are above the Diff, the Diff is greater than 0 and the RSI is greater than 40. This works pretty well in backtesting how i have it but I need to tighten up my losses. My current exit is when the Price low low() ticks below the EMA@9. This is for volatile stocks like ADAP today where a stop loss is not appropriate. My enter conditions ensures the bars will always be above the EMA@9 so my exit is safe. Here is my script and there is also a pic of the current backtest results. If anyone has any advice on how to cut losses some more please help me.


    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MACDTradeAuto : Strategy
    {
    private MACD MACD1;
    private RSI RSI1;
    private EMA EMA1;
    private EMA EMA2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MACDTradeAuto";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 1;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    ProfitTarget = 20;
    StopLoss = 10;
    TradeSize = 1;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    MACD1 = MACD(Close, 12, 26, 9);
    RSI1 = RSI(Close, 14, 3);
    EMA1 = EMA(Close, 9);
    EMA2 = EMA(Close, 9);
    MACD1.Plots[0].Brush = Brushes.DarkCyan;
    MACD1.Plots[1].Brush = Brushes.Crimson;
    MACD1.Plots[2].Brush = Brushes.DodgerBlue;
    RSI1.Plots[0].Brush = Brushes.DodgerBlue;
    RSI1.Plots[1].Brush = Brushes.Goldenrod;
    EMA1.Plots[0].Brush = Brushes.Goldenrod;
    EMA2.Plots[0].Brush = Brushes.Gold;
    AddChartIndicator(MACD1);
    AddChartIndicator(RSI1);
    AddChartIndicator(EMA1);
    AddChartIndicator(EMA2);
    SetProfitTarget(@"", CalculationMode.Ticks, ProfitTarget);
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((MACD1.Diff[0] > 0)
    && (RSI1.Default[0] > 40)
    && (Close[0] > EMA1[0])
    && (MACD1.Default[0] > MACD1.Diff[0])
    && (MACD1.Avg[0] > MACD1.Diff[0]))
    {
    EnterLong(Convert.ToInt32(TradeSize), @"ENTERLong");
    }

    // Set 2
    if ((BarsSinceEntryExecution(0, "", 0) >= 15)
    || (CrossBelow(Low, EMA2, 1)))
    {
    ExitLong(Convert.ToInt32(TradeSize), @"EXITLong", "");
    }

    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="ProfitTarget", Description="Number of ticks up from entry", Order=1, GroupName="Parameters")]
    public int ProfitTarget
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="StopLoss", Description="Number of ticks down from entry ", Order=2, GroupName="Parameters")]
    public int StopLoss
    { get; set; }

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

    }
    }
    Last edited by authenticx; 05-29-2020, 10:38 PM.

    #2
    This thread should be moved to the NT8 forums.

    Comment


      #3
      One idea:

      Start the strategy in Chart A (or via Strategy tab in CC).

      In Chart B, enable Chart Trader and when trades are entered
      by the strategy in Chart A, the target/stop orders will appear
      on Chart B. You can manually adjust these target/stop orders
      via drag-n-drop on Chart B and your strategy orders will be
      updated accordingly.

      My point is: Watch (or listen) for trade entries and then (on a separate
      chart) update the stop order to the desired location via Chart Trader.

      Comment


        #4
        That won't work for me because I want to run strategies against multiple symbols and i don't want to monitor them all. I think i figured it out conditionally though without having to use a stop loss.

        Comment


          #5
          Hi authenticx, thanks for your post.

          Unfortunately, the support team can not assist with the performance of a strategy. Other members of the community are still here for their input.

          Kind regards.
          Chris L.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Rapine Heihei, Yesterday, 07:51 PM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by kaywai, Today, 06:26 AM
          1 response
          6 views
          0 likes
          Last Post kaywai
          by kaywai
           
          Started by ct, 05-07-2023, 12:31 PM
          6 responses
          205 views
          0 likes
          Last Post wisconsinpat  
          Started by kevinenergy, 02-17-2023, 12:42 PM
          118 responses
          2,780 views
          1 like
          Last Post kevinenergy  
          Started by briansaul, Today, 05:31 AM
          0 responses
          10 views
          0 likes
          Last Post briansaul  
          Working...
          X