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 helpwanted, Today, 03:06 AM
            0 responses
            3 views
            0 likes
            Last Post helpwanted  
            Started by Brevo, Today, 01:45 AM
            0 responses
            6 views
            0 likes
            Last Post Brevo
            by Brevo
             
            Started by aussugardefender, Today, 01:07 AM
            0 responses
            5 views
            0 likes
            Last Post aussugardefender  
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            242 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Started by TraderG23, 12-08-2023, 07:56 AM
            9 responses
            384 views
            1 like
            Last Post Gavini
            by Gavini
             
            Working...
            X