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

What's wrong?

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

    What's wrong?

    hi,
    The Code entry with a crossing of two SMA , with a stoploss of 4% becomes 2% if the volatility is low.
    With the 1° target , sell (1 / 2 ) position and the remaining managed TrailingStop (based on a channel MAX / MIN 0.25 * ATR).
    The Code with F5 is OK, but when i test in 10 years, you see only n.1 trade for stock.
    Risk management, such as writing the code is not implemented.
    The Code:

    #region Variables
    private bool be1 = false;
    private bool be2 = false;
    #endregion

    #region Positions
    private void GoLong()
    {
    SetStopLoss("StopLoss 4% LongBuy",CalculationMode.Percent,0.04,false);
    EnterLong(500, "LongBuy");
    }
    private void GoShort()
    {
    SetStopLoss("StopLoss 4% ShortSell",CalculationMode.Percent,0.04,false);
    EnterShort(500, "ShortSell");

    }
    #endregion

    #region OrderRouting
    private void ManagerOrder ()
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Be1 && Close[0] > Position.AvgPrice && ATR(14)[0] > EMA(ATR(14), 14)[0])// High Volatility
    SetStopLoss("StopLoss 4% LongBuy",CalculationMode.Percent,0.04,false);
    int pippo = 1;


    if( Be2 && Close[0] > Position.AvgPrice && ATR(14)[0] < EMA(ATR(14), 14)[0])// Low Volatility
    SetStopLoss("StopLoss 2% LongBuy",CalculationMode.Percent,0.02,false);
    int pluto = 1;

    if( pippo == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] > EMA(ATR(14), 14)[0] )// High Volatility
    ExitLong(250, "Exit Long 1/2 Position", "LongBuy High Volatility");
    SetStopLoss("StopLoss High Volatility",CalculationMode.Price,Position.AvgPric e ,false);
    SetTrailStop("StopTrailing High Volatility",CalculationMode.Price,MIN(Low, 10)[1] - (ATR(14)[0] * 0.35),false);

    if( pluto == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] < EMA(ATR(14), 14)[0]) // Low Volatility
    ExitLong(250, "Exit Long 1/2 Position", "LongBuy Low Volatility");
    SetStopLoss("StopLoss Low Volatility",CalculationMode.Price,Position.AvgPric e ,false);
    SetTrailStop("StopTrailing Low Volatility",CalculationMode.Price,MIN(Low, 10)[1] - (ATR(14)[0] * 0.25),false);

    }

    if (Position.MarketPosition == MarketPosition.Short).......................

    protected override void OnBarUpdate()
    {
    ManagerOrder ();

    if (Position.MarketPosition != MarketPosition.Flat) return;
    if (CrossAbove(SMA(14),SMA(20),1)
    {
    GoLong();
    }

    if(CrossBelow(SMA(14),SMA(20),1)
    {
    GoShort();
    }


    why not enter the stop? What is wrong?
    Ciao.
    Italy

    #2
    Hi Italy,

    A few things...

    Take a look at your fromEntrySignal to ensure they are matching up with signalName in your Enter() statements.

    Also, keep in mind using both a SetStopLoss and a SetTrailStop should not be done, alternatively, you can modify the SetStopLoss to create a trailing stop.
    More info at - http://www.ninjatrader.com/support/f...ead.php?t=3222
    TimNinjaTrader Customer Service

    Comment


      #3
      thanks for the solution.Lo stoploss is OK. Instead, do not work for the Exit (1 / 2 position). Here's the modified code:

      #region Variables
      private bool be1 = false;
      private bool be2 = false;
      #endregion

      protected override void Initialize()
      {
      Add(ATR(14));
      Add(SMA(50));
      Add (MAX(High,10));
      Add(MIN(Low,10));
      TraceOrders = true;
      CalculateOnBarClose = true;
      SetStopLoss(CalculationMode.Percent,0.04);
      }

      #region Positions
      private void GoLong()
      {
      SetStopLoss(CalculationMode.Percent,0.04);
      EnterLong(500, "LongBuy");
      }
      private void GoShort()
      { SetStopLoss(CalculationMode.Percent,0.04);
      EnterShort(500, "ShortSell");

      }
      #endregion

      #region OrderRouting
      private void ManagerOrder ()
      {
      if (Position.MarketPosition == MarketPosition.Long)

      {
      if (Be1 && Close[0] > Position.AvgPrice && ATR(14)[0] > EMA(ATR(14), 14)[0])// High Volatility
      SetStopLoss(CalculationMode.Percent,0.04);
      int pippo = 1;
      if( Be2 && Close[0] > Position.AvgPrice && ATR(14)[0] < EMA(ATR(14), 14)[0])// Low Volatility

      SetStopLoss(CalculationMode.Percent,0.02);
      int pluto = 1;

      if( pippo == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] > EMA(ATR(14), 14)[0] )// High Volatility

      ExitLong(250, "Exit Long 1/2 Position", "LongBuy High Volatility");//Not Working this Exit
      SetStopLoss(CalculationMode.Price,MIN(Low, 10)[1] - (ATR(14)[0] * 0.35));


      if( pluto == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] < EMA(ATR(14), 14)[0]) // Low Volatility
      ExitLong(250, "Exit Long 1/2 Position", "LongBuy Low Volatility"); // Not Working this Exit
      SetStopLoss(CalculationMode.Price,MIN(Low, 10)[1] - (ATR(14)[0] * 0.25));

      }

      protected override void OnBarUpdate()
      {
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("StopLoss 4% LongBuy",CalculationMode.Percent,0.04,false);
      }

      if (Position.MarketPosition != MarketPosition.Flat)
      ManagerOrder ();
      ............
      Why not work the EXITS ?
      Ciao.
      Italy

      Comment


        #4
        italy, in order to be able to scale out of your position you would need to scale in first to, so you can then tie the scale signals via their names together to achieve what you look for -



        Also, please work with the TraceOrders feature when developing strategies, this will give you detailed feedback if orders are ignored and also for what reason then -

        Last edited by NinjaTrader_Bertrand; 06-21-2010, 11:11 AM.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thanks, Thanks Bertrand, finally everything is OK.
          Ciao.
          Italy

          Comment


            #6
            Sorry,
            there are still problems. stop loss will not be recalculated. Here's the code:
            #region Variables

            private bool be1 = false;
            private bool be2 = false;

            #endregion

            protected override void Initialize()
            {
            TraceOrders = true;
            CalculateOnBarClose = true;
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.UniqueEntries;
            }

            #region Positions
            private void GoLong()
            {
            SetStopLoss("StopLongBuy",CalculationMode.Percent, 0.04,false);
            EnterLong(250, "LongBuy1°");
            EnterLong(250, "LongBuy2°");
            }

            private void GoShort()
            {
            SetStopLoss("StopLongBuy",CalculationMode.Percent, 0.04,false);

            EnterShort(250, "ShortSell1°");
            EnterShort(250, "ShortSell2°");
            }
            #endregion
            #region OrderRouting
            private void ManagerOrder ()
            {
            if (Position.MarketPosition == MarketPosition.Long)
            {
            if (Be1 && Close[0] > Position.AvgPrice && ATR(14)[0] > EMA(ATR(50), 14)[0])// High Volatility
            SetStopLoss("StopLongBuy ",CalculationMode.Percent,0.04,false);
            int target = 1;
            if( Be2 && Close[0] > Position.AvgPrice && ATR(14)[0] < EMA(ATR(50), 14)[0])// Low Volatility
            SetStopLoss("StopLongBuy ",CalculationMode.Percent,0.02,false);
            int target2 = 1;

            if( target == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] > EMA(ATR(50), 14)[0] )// High Volatility
            ExitLong(250, "ExitLong 1/2 High Volatility", "LongBuy1°");
            SetStopLoss(CalculationMode.Price,MIN(Low, 10)[0] - (ATR(30)[0] * 0.35));



            if( target2 == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] < EMA(ATR(50), 14)[0]) // Low Volatility
            ExitLong(250, "Exit Long 1/2 Low Volatility", "LongBuy1° ");
            SetStopLoss(CalculationMode.Price,MIN(Low, 10)[0] - (ATR(30)[0] * 0.25));


            }
            }

            #endregion
            protected override void OnBarUpdate()
            {
            if (Position.MarketPosition == MarketPosition.Flat && ATR (14)[0] > EMA(ATR(30), 14)[0])
            {

            SetStopLoss(CalculationMode.Percent,0.04);
            }

            if (Position.MarketPosition != MarketPosition.Flat)
            ManagerOrder ();


            ......then there are the inputs.
            Why, Stop Loss is not correctly recalculated?
            ciao Italy

            Comment


              #7
              Hi Italy,

              Your condition for the reset likely is not being satisfied...
              if (Position.MarketPosition == MarketPosition.Flat && ATR (14)[0] > EMA(ATR(30), 14)[0])

              Use Print() statements to see if the SetStopLoss is being reset properly.

              More info on debugging at - http://www.ninjatrader.com/support/f...ead.php?t=3418
              TimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by arvidvanstaey, Today, 02:19 PM
              4 responses
              11 views
              0 likes
              Last Post arvidvanstaey  
              Started by samish18, 04-17-2024, 08:57 AM
              16 responses
              60 views
              0 likes
              Last Post samish18  
              Started by jordanq2, Today, 03:10 PM
              2 responses
              9 views
              0 likes
              Last Post jordanq2  
              Started by traderqz, Today, 12:06 AM
              10 responses
              18 views
              0 likes
              Last Post traderqz  
              Started by algospoke, 04-17-2024, 06:40 PM
              5 responses
              47 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X