Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Basc problem stop loss breakeven

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

    Basc problem stop loss breakeven

    Hello

    I am a beginner in programming and have several days to bother with software lines.
    If you look at my code I have problems breakeven stop loss.

    I tried three different ways and do not succeed. Can you tell me what I'm doing wrong.
    Im using Renko BAR Candlestick

    1.Example:
    if (Close [0]> Position.AvgPrice + 6 * TickSize)
    (
    SetStopLoss (CalculationMode.Price, Position.AvgPrice);
    )

    2.Example
    if (Close [0] == Position.AvgPrice + (6 * TickSize))
    (
    SetStopLoss ("", CalculationMode.Price, Position.AvgPrice, false);
    )

    3 my whole code :

    protected override void Initialize()
    {
    SetProfitTarget("", CalculationMode.Ticks, TP);
    SetStopLoss("", CalculationMode.Ticks, SL, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossBelow(RSI(14, 3).Avg, 60, 1))
    {
    DrawVerticalLine("My vertical line" + CurrentBar, 0, Color.DimGray);
    EnterLong(1, "");
    }
    // Once the price is greater than entry price +6 ticks, set stop loss to breakeven
    //Here I have the problem............????
    if (Close[0] == Position.AvgPrice + (6 * TickSize))
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }
    }

    #2
    Hello marko7,

    It looks like you have the logic expressed correctly for a breakeven move. This reference sample provides help with this.

    I notice you're using CalculateOnBarClose = true. How are you evaluating this breakeven strategy? Is it with backtesting or realtime? Probably the best way to evalutate this is with the simulated data feed, real time, with CalculateOnBarClose = false.

    What is probably happening in your case is it's trying to adjust the stop loss even when you are flat and position price = 0. Try adding the following check:


    if (Position.MarketPosition != MarketPosition.Flat)
    {
    if (Close [0]> Position.AvgPrice + 6 * TickSize)
    {
    SetStopLoss (CalculationMode.Price, Position.AvgPrice);
    }
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      Im was backtesting, and now im testing on simulator live.
      i change CalculateOnBarClose = true to false.

      i tray your code and system stop out.system return me: What is thet mean??

      mesage 1
      "Strategy....submittedan order that generated the following error "OrderRejected" Strategy has sent cancel request, attempt to close the position and teminated itself,

      mesage 2
      Simulated Data Feed, Sell stop or stop limit orders vant be placed above the market. Affected order: Sell 1 Stop @ 1,5099


      this is my code......

      protected override void OnBarUpdate()
      {

      // Condition set 1
      if (CrossAbove(RSI(14, 3).Avg, 30, 1))
      {
      EnterLong(1, "");
      }

      if (Position.MarketPosition != MarketPosition.Flat)
      {
      if (Close [0]> Position.AvgPrice + 4 * TickSize)
      {
      SetStopLoss (CalculationMode.Price, Position.AvgPrice);
      }
      }
      {

      I m tray my priveus(code witch I write yesterday) code on live simulator, and still do not working.

      Please help me.

      Comment


        #4
        Hello Marko7,

        What settings do you enabled under Tools > Options > Strategies > NinjaScript > On starting a real time strategy.



        I would use Print statements on your Position.AvgPrice value to be sure this value is what you expect. Check the value through Tools > Output Window.

        if (Position.MarketPosition != MarketPosition.Flat)
        {
        if (Close [0]> Position.AvgPrice + 4 * TickSize)
        {
        Print(Position.AvgPrice);
        SetStopLoss (CalculationMode.Price, Position.AvgPrice);
        }
        }
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Settings in Ninja Script/On starting a real time strategy are: WAIT UNTIL FLAT BEFORE EXECUTING LIVE

          Comment


            #6
            Marko7,

            Code is working properly here. From the message it sounds like you are submitting an invalid stop order. You could add an additional check to make sure the stop price is lower than close (last price), but will more than likely need to look at the series you're running it against.

            Put TraceOrders = true in your Initialize method and then track strategy orders by clicking Tools > Output Window. Print values to verify they're what you expect.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              One additional thing to check is that you are resetting your stop loss to an initial value when you're flat.

              if (Position.MarketPosition == MarketPosition.Flat)
              SetStopLoss(CalculationMode.Ticks, 10);
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                RyanM,

                Thank you very much....im testig all weak and it is working.....thank you.

                I have one more question, abouth woodiesCCI indicator.
                I try to use trend indicator-Sidewinder. When is green go long, and nothing happen. why
                is something wrong with Color.Green??

                if (WoodiesCCI(2, 5, 14, 34, 25, 6, 60, 100, 2).Sidewinder[0] == Color.Green)
                {
                EnterLong(1,"");
                }

                thank you very much..

                Comment


                  #9
                  marko7, you will have to check what the actual conditions are that make the indicator green (the numerical values) and then check for those conditions.
                  AustinNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by tkaboris, Today, 08:01 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post tkaboris  
                  Started by BarzTrading, Today, 07:25 AM
                  1 response
                  11 views
                  1 like
                  Last Post NinjaTrader_Clayton  
                  Started by EB Worx, 04-04-2023, 02:34 AM
                  7 responses
                  161 views
                  0 likes
                  Last Post VFI26
                  by VFI26
                   
                  Started by Mizzouman1, Today, 07:35 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by Radano, 06-10-2021, 01:40 AM
                  20 responses
                  616 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Working...
                  X