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

Multi-Time Frame & instruments (Trail Stop)

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

    Multi-Time Frame & instruments (Trail Stop)


    Hello

    I have this strategy that works with NQ Information but it Executes in MNQ, the Entry Orders are executed perfectly in the MNQ instrument (SubmitOrderUnmanaged(1.....), but the change Order in the Trail Stop does not work, it does not move the Orders Please can you help me to know what step I need. Note: I work with unmanaged orders.
    thank you​










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

    // Verificar si la primera estrategia está activa
    if (!IsMainStrategyActive())
    {
    Print("La estrategia principal no está activa. Por favor, active tanto la estrategia ATMBSR como la estrategia ATMR2");
    return;
    }

    ReadSignal();

    CancelOrderForTakeProfit();

    TrailStop();

    if (!orderPlaced)
    {
    if (signalType == "Long")
    {
    double orderLimit_Trigger = Convert.ToInt32(entryPrice - (Math.Abs(entryPrice - stopLoss) * (entradaRetrocesoOrdenR2/100)));
    SubmitOrderUnmanaged(1, OrderAction.Buy, OrderType.Limit,Convert.ToInt32(cantidadComprada), orderLimit_Trigger,0,"","R2-LE");
    orderPlaced = true;
    }
    else if (signalType == "Short")
    {
    double orderLimit_Trigger = Convert.ToInt32(entryPrice + (Math.Abs(entryPrice - stopLoss) * (entradaRetrocesoOrdenR2/100)));
    SubmitOrderUnmanaged(1, OrderAction.SellShort, OrderType.Limit,Convert.ToInt32(cantidadComprada), orderLimit_Trigger,0,"","R2-SE");
    orderPlaced = true;
    }
    }
    }

    private void TrailStop()
    {
    if (MyExitType != ExitType.Static)
    {
    if (stopLossLong != null)
    {
    // Actualizamos La Variables de Activacion del Trail
    double CurrentTriggerTrailPriceLong = takeProfit * (activacionTrailforProfit/100);

    double trailStop = Close[0] - aTR[0] * ATRMultiple;

    if (GetCurrentAsk() >= CurrentTriggerTrailPriceLong - (2 * TickSize)
    && !activaPrimerMontoTrailLong)
    {
    ChangeOrder(stopLossLong,Position.Quantity,0,Curre ntTrailStopPriceLong);
    activaPrimerMontoTrailLong = true;
    }

    if (trailStop > CurrentTrailStopPriceLong)
    {
    CurrentTrailStopPriceLong = trailStop;
    ChangeOrder(stopLossLong,Position.Quantity,0, CurrentTrailStopPriceLong);
    }
    }


    else if (stopLossShort != null)
    {
    // Actualizamos La Variables de Activacion del Trail
    double CurrentTriggerTrailPriceShort = takeProfit * (activacionTrailforProfit/100);

    double trailStop = Close[0] + aTR[0] * ATRMultiple;

    if (GetCurrentBid() <= CurrentTriggerTrailPriceShort + (2 * TickSize)
    && !activaPrimerMontoTrailShort)
    {
    ChangeOrder(stopLossShort,Position.Quantity,0,Curr entTrailStopPriceShort);
    activaPrimerMontoTrailShort = true;
    }

    if (trailStop < CurrentTrailStopPriceShort)
    {
    CurrentTrailStopPriceShort = trailStop;
    ChangeOrder(stopLossShort,Position.Quantity,0,Curr entTrailStopPriceShort);
    }
    }

    else if (stopLossLong == null)
    {
    activaPrimerMontoTrailLong = false;
    }
    else if (stopLossShort == null)
    {
    activaPrimerMontoTrailShort = false;
    }
    }
    }​

    #2
    else if (State == State.Configure)
    {
    region Sumar MNQ si nuestro Principal Instrumento es NQ
    // De esta forma Tenemos un Grafico con la informacion del NQ pero todas nuestas Ordenes seran en el MNQ como instrumento Secundario

    // Add secondary data series for MNQ
    // Obtener el instrumento principal (NQ)
    string mainInstrument = Instruments[0].FullName;

    // Reemplazar el prefijo NQ con MNQ para el símbolo MNQ Si el Instrumento Principal es NQ
    string secondaryInstrument = mainInstrument.Replace("NQ","MNQ");

    if (secondaryInstrument != mainInstrument)
    {
    // Añadir la serie de datos para MNQ
    AddDataSeries(secondaryInstrument, Data.BarsPeriodType.Minute, 1); // BarPRogress == 1
    }
    #endregion
    }​

    Comment


      #3
      Hello HGTrader,

      To be able to change an order you need to have the order object set to a variable, are you using OnOrderUpdate to find the order and set it to your variable stopLossShort?

      JesseNinjaTrader Customer Service

      Comment


        #4
        protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
        {
        if (order.Name == "R2-LE")
        longEntry = order;
        else if (order.Name == "R2-SE")
        shortEntry = order;
        else if (order.Name == "StopLossLong")
        stopLossLong = order;
        else if (order.Name == "TargetLong")
        targetLong = order;
        else if (order.Name == "StopLossShort")
        stopLossShort = order;
        else if (order.Name == "TargetShort")
        targetShort = order;


        if (longEntry != null && longEntry == order)
        {
        // Reset the longTop object to null if order was cancelled without any fill
        if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
        {
        longEntry = null;
        sumFilledLong = 0;
        }
        }

        if (shortEntry != null && shortEntry == order)
        {
        // Reset the shortTop object to null if order was cancelled without any fill
        if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
        {
        shortEntry = null;
        sumFilledShort = 0;
        }
        }

        if ((targetLong != null && targetLong == order)
        ||(stopLossLong != null && stopLossLong == order)
        ||(targetShort != null && targetShort == order)
        ||(stopLossShort != null && stopLossShort == order)
        )
        {
        if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
        {
        targetLong = stopLossLong = targetShort = stopLossShort = null;
        }
        }
        }​

        Comment


          #5
          Hello HGTrader,

          Have you tried using a Print to make sure the order variable has a value and also that your training condition is becoming true?
          JesseNinjaTrader Customer Service

          Comment


            #6

            Is there a way to tell the order
            Change Order(stop Loss Short,Position.Quantity,0,Curr entTrailStopPriceShort); What does Array 1 take?​



            if (BarsInProgress == 1) TrailStop();

            But it didn't work either​

            Comment


              #7
              I was able to get the error, and that is that I need to indicate the position we have in the Second instrument (MNQ),

              ChangeOrder(stopLossShort,Position.Quantity,0,Curr entTrailStopPriceShort);

              The correct is:

              ChangeOrder(stopLossShort,Positions[1].Quantity,0,CurrentTrailStopPriceShort);


              thanks ​

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by yertle, Today, 12:24 PM
              0 responses
              3 views
              0 likes
              Last Post yertle
              by yertle
               
              Started by reekotubbs, Yesterday, 01:09 PM
              3 responses
              27 views
              0 likes
              Last Post reekotubbs  
              Started by Blaze212, 07-07-2024, 08:58 PM
              2 responses
              20 views
              0 likes
              Last Post Blaze212  
              Started by medicmiked, Today, 11:56 AM
              0 responses
              2 views
              0 likes
              Last Post medicmiked  
              Started by miaolivi9, Today, 06:59 AM
              0 responses
              9 views
              0 likes
              Last Post miaolivi9  
              Working...
              X