Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Beginner Advice Needed

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

    Beginner Advice Needed

    New to this. Learning by starting w/ Wizard then Unlock code and copy/paste bits of code found in forum. Working well so far. I plan to start coding my strategy and have few question before I start wrong and have to redo all:

    I have a strategy that Enters with 3positions:
    I will have StopLoss (and Profits) initially at (x)*ATR, but then my Stops will trail at low/high of (x) bars till after 1st Target, then move to Breakeven till 2nd Target, then trail by Low/High Bars again till 3rd is stopped out.
    Q: Is it better to Enter 1 position and set stops and profits to 1/3 the amount or is it better to do 3 Unique Entries and link Stops/Profits to those.
    Also is it better to use SetStopLoss() and SetProfitTarget() or would ExitLong/Short() be more flexible

    Got most of the rest of what I need already from the Forum. Great work!!!

    Thanks

    Just realized this is in the wrong category.Sorry. Dont know how to move it
    Last edited by pswarts; 11-22-2011, 05:26 PM.

    #2
    Hello pswarts,

    Thanks for the post and welcome to the NinjaTrader forums.

    If you want to scale out of your strategy you must first scale into it, so 3 separate unique entries. A basic scale out strategy is available here:


    When you are just getting started, SetProfitTarget() and SetStopLoss() are more convenient and recommended. They are automatically submitted upon entry execution, and with mode ticks or percent, they're automatically placed in relation to your entry price.

    Once you get more experience and can use the advanced order handling, ExitLongStop() and ExitLongLimit() will offer more control and flexibility.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi
      Like most newbies I back.
      Coded my Masterpiece ...lol... unfortunately as expected it's not working. Can I please post my code here so someone can have a look at it and give some pointers?

      Comment


        #4
        Profit conditions

        Problem is the targets/stops does not execute correctly when backtesting: code as follows


        protected
        overridevoid OnBarUpdate()
        if(My Entry Conditions)
        {
        EnterLongStop(
        1, High[0], "P1");
        EnterLongStop(
        1, High[0], "P2");
        EnterLongStop(
        1, High[0], "P3");
        }
        // Profit Conditions
        if (Position.MarketPosition != MarketPosition.Flat)
        {
        SetProfitTarget(
        "P1",CalculationMode.Price, Position.AvgPrice + ((1 * Profitfactor) * (ATR(20)[0])));
        SetProfitTarget(
        "P2",CalculationMode.Price, Position.AvgPrice + ((2 * Profitfactor) * (ATR(20)[0])));
        }

        Comment


          #5
          Continued

          // StopLoss/(Profit) Condition 3
          if (Position.MarketPosition != MarketPosition.Flat
          && Close[
          0] >= Position.AvgPrice + ((2 * Profitfactor) * (ATR(20)[0]))
          && Low[
          0] >= Low[1])
          {
          ExitLongStop(Low[
          3],"P3");

          // StopLoss Condition 2
          if (Position.MarketPosition != MarketPosition.Flat
          && Close[
          0] >= Position.AvgPrice + ((1 * Profitfactor) * (ATR(20)[0])))
          {
          ExitLongStop(Position.AvgPrice,
          "P2");
          ExitLongStop(Position.AvgPrice,
          "P3");

          // StopLoss Condition 1
          if (Position.MarketPosition != MarketPosition.Flat)
          {
          ExitLongStop(Position.AvgPrice - ((
          2 * Profitfactor) * (ATR(20)[0])),"P1");
          ExitLongStop(Position.AvgPrice - ((
          2 * Profitfactor) * (ATR(20)[0])),"P2");
          ExitLongStop(Position.AvgPrice - ((
          2 * Profitfactor) * (ATR(20)[0])),"P3");
          }
          }
          }
          }

          Comment


            #6
            There is a logic problem: not in the code, but in the process. Your conditions are in the order from most restrictive to least restrictive. with no branching. Hence all blocks of the code will be executed. Naturally, that means that the least restrictive code will always be the last executed, and will, therefore, be the gate on your processing.

            Comment


              #7
              thanks koganam
              I'll swap them around ,but what do you mean with no branching.I embedded the If conditions or thought I did. Did I do it wrong,should I be using Else if or am I not understanding what exactly you mean with branching

              Comment


                #8
                Originally posted by pswarts View Post
                thanks koganam
                I'll swap them around ,but what do you mean with no branching.I embedded the If conditions or thought I did. Did I do it wrong,should I be using Else if or am I not understanding what exactly you mean with branching
                A series of "if statements" are not really branches; they are just a series of test conditions. In the sense that the block following the "if condition" may or not be executed, you could call them branching statements, but they are self contained. (I know that the general literature implies that all "if" statements are branching statements. I am just putting a finer twist on it).

                So, in the particular case that you coded, all the conditions will be tested, rather that not testing some conditions based on the truth of other conditions. So yes, when I look at your code, I expect that some "else" clauses will make your logic more robust.
                Last edited by koganam; 11-29-2011, 04:34 PM.

                Comment


                  #9
                  If you use both the ExitLongStop() and SetStopLoss() for the same position, only one of these orders will be used and the other ignored. See here for more info and expand section on Internal Order Handling rules.

                  Set methods will use the last value that is set so you should be careful with items like Position.AvgPrice as this returns a value of 0 when you're not in a position.

                  If you ever set your stop loss while in a position, you also need an additional statement that sets it in mode ticks/percent when you are flat. This is used for the initial placement but then as soon as the position update is received, then it will be modified to your ATR based stop.

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

                  Comment


                    #10
                    Thanks, makes sense.
                    Few more questions: Will it be better to rather use ExitLong()for profits and ExitLongStop()for Stops and get rid of the Set commands altogether? If I do that and use EnterLongStop() with Unique Entries, with an Exit Order both below and above the Entry but linked to the entry with the "fromEntrySignal" will one Exit order cancel the other or will I have to bring in a CancelOrder(). According to the Internal Order handling section it looks like it will be ignored (not put me in a short position) but I'm not sure if it actually gets cancelled because if not then on the next setup it will still be in effect ,or will the next steup that gets triggered reset the old Exits to the new setup values.

                    Also if I use If Else() to branch my conditions do I still use the least restrictive conditions first, my logic says that if more than one conditions is true the strategy will basically try to flip the stop back and forth to the setting for the conditions with each bar update or is that automaticaly taken care of by Ninja's internal mechanisms?

                    Comment


                      #11
                      The nice thing about set statements is they can be automatically submitted upon entry execution, and placed in relation to your entry price with mode ticks or percent.

                      To get the same timing and placement with Exit methods requires advanced handling and familiarity with IOrder properties.

                      If you have a working exit order and the position associated with it is closed: The internal strategy management will automatically close working exit orders. You do not have to send CancelOrder to manually cancel it.

                      Else if branch would be used to provide exclusivity to your conditions. When using it, both the if and else if statements would never be true on the same bar. It's a way of preventing it from doing competing actions, but there are also internal mechanisms (in backtest) that wouldn't allow, a long and short position on the same bar.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks
                        Back to the drawingboard then for me

                        Comment


                          #13
                          If I want to trail my stop at the lowest low of the last three bars do I use the SetstopLoss or SetTrailStop(that would be my 2nd else if statement in the code below) and is that the correct code to give me the lowest low of last three bars ? I been over the help manual several times but cannot grasp the whole Lows(),Lowestbar(),Low[] thing or how to properly combine these.
                          elseif (Position.MarketPosition == MarketPosition.Long)
                          {
                          // Once the price is equal or greater than 1st Profittarget, set stop loss to breakeven
                          if (High[0] >= Position.AvgPrice + ((1*Profitfactor) * (ATR(20)[0]))
                          &&High[
                          0] <= Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
                          &&Close[
                          0] > Position.AvgPrice )
                          {
                          SetStopLoss(
                          "Entry2", CalculationMode.Price, Position.AvgPrice, true);
                          SetStopLoss(
                          "Entry3", CalculationMode.Price, Position.AvgPrice, true);
                          }
                          // Once the price is equal or greater than 2st Profittarget, and has not closed lower than breakeven or lower than the lowest low of the prev three bars, set stop loss to trail by low of last three bars
                          elseif (High[0] > Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
                          &&MIN(Low ,3
                          )[0] < MIN(Low ,0)[0])
                          {
                          SetStopLoss(
                          "Entry3", CalculationMode.Ticks, Low[3], true);
                          }
                          Last edited by pswarts; 12-06-2011, 04:31 PM.

                          Comment


                            #14
                            To trail at the lowest low of the last 3 bars you would use SetStopLoss. This statement should do it:

                            SetStopLoss(CalculationMode.Price, MIN(Low, 3)[0]);
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              Oh man this is getting frustrating, I finished writing my code, compiled it and NT7 compiled without a problem but I cannot get the strategy to run or backtest, when I try and backtest it,Strategy Analyser shows it in its list of strategies but when I pick it from the list it does not load(the prev strategy's setting stays in the Parameter box) when I try and load other strategies it loads just fine. I tried to run it on simulated datafeed and the strategy loads on the charts but nothing happens, the Entries does not trigger.WHY??!!
                              Please help!!!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Christopher_R  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,235 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,415 views
                              0 likes
                              Last Post Traderontheroad  
                              Working...
                              X