Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy problem.

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

    Strategy problem.

    Hello.
    I have a simple strategy when Ema>Sma enter long and Ema<Sma enter short.
    I want initialy a stop loss fix and i want to trail the stop loss every tick.
    The problem is, my strategy not work.
    What is wrong?
    protected override void Initialize()
    {
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    SetProfitTarget("", CalculationMode.Ticks, 0);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    }
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] > Position.AvgPrice + 1 * TickSize)
    {
    SetTrailStop("", CalculationMode.Ticks, 1, true);
    }
    }
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    if (Close[0] < Position.AvgPrice - 1 * TickSize)
    {
    SetTrailStop("", CalculationMode.Ticks, 1, true);
    }
    }

    // Condition set 1
    if (EMA(Ema)[0] > SMA(Sma)[0])
    {
    EnterLongLimit(DefaultQuantity, 0, "");
    }

    // Condition set 2
    if (EMA(Ema)[0] < SMA(Sma)[0])
    {
    EnterShortLimit(DefaultQuantity, 0, "");
    }
    }

    #2
    Hello Mario2306,

    Thank you for your post.

    You cannot use the TrailStop and StopLoss at the same time. The StopLoss will override over the TrailStop when they are used together.

    You will need to use one or the other.

    If you wanting to get the trail effect for your stop loss then you will want to build the logic to update the stop loss depending on the price movement.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      And can you show me please how can i do this?
      Because i do not know.

      Comment


        #4
        Mario,

        You will want to set the CalculateOnBarClose to false so that we getting the OnBarUpdate() to calculate on each tick.

        You would then store the incoming close price and compare that to the previous stored value -
        Code:
        if(Close[0] > previousClose)
        {
              SetStopLoss("", CalcuationMode.Ticks, Close[0] - 5 * TickSize, false);
              previousClose = Close[0];
        }
        This will check the last traded price and if the price is greater than the previous value. If so, then we want to move our SetStopLoss() and give the new close price to previousClose.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Cal, thank you for your answer.
          I change the strategy how you told me, but i receive an error message - the name "previousClose" does not exist in the current context
          I can not understand what is wrong.
          I put the the strategy here to see if is good

          {
          SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
          SetProfitTarget("", CalculationMode.Ticks, 0);

          CalculateOnBarClose = false;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
          }
          else if (Position.MarketPosition == MarketPosition.Long)
          {
          if(Close[0] > previousClose)
          {
          SetStopLoss("", CalculationMode.Ticks, Close[0] - 1 * TickSize, false);
          previousClose = Close[0];
          }
          }
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
          }
          else if (Position.MarketPosition == MarketPosition.Short)
          {
          if (Close[0] < previousClose)
          {
          SetStopLoss("", CalculationMode.Ticks, Close[0] + 1 * TickSize, false);
          previousClose = Close[0];
          }
          }

          // Condition set 1
          if (EMA(Ema)[0] > SMA(Sma)[0])
          {
          EnterLongLimit(DefaultQuantity, 0, "");
          }

          // Condition set 2
          if (EMA(Ema)[0] < SMA(Sma)[0])
          {
          EnterShortLimit(DefaultQuantity, 0, "");
          }
          }

          Comment


            #6
            Mario,

            That was a placeholder I gave you. You need to create the variable previousClose in order for this to work. Create the variable in the top variables section

            Additionally, you will want to have two different checks for the StopLoss(), one for short and one for long

            Check for if(Position.MarketPosition == MarketPosition.Long or Short and adjust the Set() accordingly for the market.
            Cal H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by wzgy0920, 04-20-2024, 06:09 PM
            2 responses
            27 views
            0 likes
            Last Post wzgy0920  
            Started by wzgy0920, 02-22-2024, 01:11 AM
            5 responses
            32 views
            0 likes
            Last Post wzgy0920  
            Started by wzgy0920, 04-23-2024, 09:53 PM
            2 responses
            49 views
            0 likes
            Last Post wzgy0920  
            Started by Kensonprib, 04-28-2021, 10:11 AM
            5 responses
            193 views
            0 likes
            Last Post Hasadafa  
            Started by GussJ, 03-04-2020, 03:11 PM
            11 responses
            3,235 views
            0 likes
            Last Post xiinteractive  
            Working...
            X