Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Beginner Advice Needed

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

    #16
    here is my code:
    #region Variables
    privateint ema = 5;
    privateint sma = 40;
    privateint profitfactor = 1;
    privateint stopfactor = 1;

    #endregion
    ///<summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    ///</summary>
    protectedoverridevoid Initialize()
    {
    Add(EMA(Ema));
    Add(SMA(Sma));
    Add(ATR(
    20));
    SetProfitTarget(
    "Entry1", CalculationMode.Price, (1*Profitfactor) * (ATR(20)[0]));
    SetProfitTarget(
    "Entry2", CalculationMode.Price, (2*Profitfactor) * (ATR(20)[0]));
    CalculateOnBarClose =
    true;
    }

    Comment


      #17
      cont'd:
      ///<summary>
      /// Called on each bar update event (incoming tick)
      ///</summary>
      protectedoverridevoid OnBarUpdate()
      {
      // Resets the stop loss to the original value when all positions are closed
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(
      "Entry1", CalculationMode.Ticks, Stopfactor*100, false);
      SetStopLoss(
      "Entry2", CalculationMode.Ticks, Stopfactor*100, false);
      SetStopLoss(
      "Entry3", CalculationMode.Ticks, Stopfactor*100, false);
      }

      Comment


        #18
        cont'd
        // If a long position is open, allow for stop loss modification
        elseif (Position.MarketPosition == MarketPosition.Long)
        {
        // Once 1st Profittarget attained, 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 2st Profittarget attained, set stop loss to trail by Low of (x)bars
        elseif (High[0] > Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
        &&MIN(Low ,
        3)[0] > Position.AvgPrice)
        {
        SetStopLoss(
        "Entry3", CalculationMode.Price, MIN(Low, 3)[0], true);
        }
        }

        Comment


          #19
          cont'd
          // Entry Conditions
          if (EMA(Ema)[0] > EMA(Ema)[1]
          && SMA(Sma)[
          0] > SMA(Sma)[1]
          && EMA(Ema)[
          0] > SMA(Sma)[0]
          && Close[
          0] > Open[0])
          {
          EnterLong(
          1, "Entry1");
          EnterLong(
          1, "Entry2");
          EnterLong(
          1, "Entry3");
          }
          }

          Comment


            #20
            pswarts, are there any error messages in the Control Center rightmost Log tab as you attempt to run your script?
            BertrandNinjaTrader Customer Service

            Comment


              #21
              This is the message ,seems like a problem with Initialize,I dont know what that means or how to fix it though:

              07/12/2011 12:15 AMStrategyFailed to call method 'Initialize' for strategy 'TestingEntriesProgressive1/eb21fcb29fee4622a90312ecc6dd760f': Object reference not set to an instance of an object.
              Last edited by pswarts; 12-07-2011, 12:58 PM.

              Comment


                #22
                Originally posted by pswarts View Post
                This is the message ,seems like a problem with Initialize,I dont know what that means or how to fix it though:

                07/12/2011 12:15 AMStrategyFailed to call method 'Initialize' for strategy 'TestingEntriesProgressive1/eb21fcb29fee4622a90312ecc6dd760f': Object reference not set to an instance of an object.
                Your 2 SetProfitTarget() statements are attempting to access barObjects that do not yet exist.

                Code:
                SetProfitTarget("Entry1", CalculationMode.Price, (1*Profitfactor) * (ATR(20)[COLOR=Red][B][0][/B][/COLOR]));
                SetProfitTarget("Entry2", CalculationMode.Price, (2*Profitfactor) * (ATR(20)[COLOR=Red][B][0][/B][/COLOR]));
                Initialize the SetProfitTarget() statements to static values, then set them in OnStartUp().

                Comment


                  #23
                  Just to confirm: I'm to add the following between my Initialize and OnBarUpdate ?
                  protected override void Initialize ()
                  SetProfitTarget("Entry1", CalculationMode.Ticks, 200);
                  SetProfitTarget("Entry2", CalculationMode.Ticks, 200);


                  protected override void OnStartUp ()
                  SetProfitTarget("Entry1", CalculationMode.Price, (1*Profitfactor) * (ATR(20)[0]));
                  SetProfitTarget("Entry2", CalculationMode.Price, (2*Profitfactor) * (ATR(20)[0]));


                  protected override void OnBarUpdate ()
                  asbefore........

                  Comment


                    #24
                    Originally posted by pswarts View Post
                    Just to confirm: I'm to add the following between my Initialize and OnBarUpdate ?
                    protected override void Initialize ()
                    SetProfitTarget("Entry1", CalculationMode.Ticks, 200);
                    SetProfitTarget("Entry2", CalculationMode.Ticks, 200);


                    protected override void OnStartUp ()
                    SetProfitTarget("Entry1", CalculationMode.Price, (1*Profitfactor) * (ATR(20)[0]));
                    SetProfitTarget("Entry2", CalculationMode.Price, (2*Profitfactor) * (ATR(20)[0]));


                    protected override void OnBarUpdate ()
                    asbefore........
                    Pretty much. Remember to reset your SetProfitTarget() when you go flat.

                    Comment


                      #25
                      The strategy as discussed above when backtesting , give me frequent Exit on Close orders with (to me ) random distances from the entry orders at times, at other times the orders execute properly ,but I have no ExitLong or any other Exit for that matter in my code at all. Please advise on what the problem may be. Tnx

                      On the attached image the 2nd stoploss was at low of last three bars which is correct, the first order with an ExitOnClose makes no sense. I can attach more images if you need to demonstrate
                      Attached Files
                      Last edited by pswarts; 12-08-2011, 02:34 AM.

                      Comment


                        #26
                        My current code as follows
                        protectedoverridevoid OnBarUpdate()
                        {
                        // Resets the stop loss and Targets to the original value when all positions are closed
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                        SetStopLoss(
                        "Entry1", CalculationMode.Ticks, Stopfactor*3000, false);
                        SetStopLoss(
                        "Entry2", CalculationMode.Ticks, Stopfactor*3000, false);
                        SetStopLoss(
                        "Entry3", CalculationMode.Ticks, Stopfactor*3000, false);
                        SetProfitTarget(
                        "Entry1", CalculationMode.Ticks, 3000);
                        SetProfitTarget(
                        "Entry2", CalculationMode.Ticks, 3000);
                        }

                        Comment


                          #27
                          code cont'd
                          // If a long position is open, allow for stop loss and target modification
                          elseif (Position.MarketPosition == MarketPosition.Long)
                          {
                          SetProfitTarget(
                          "Entry1", CalculationMode.Price, Position.AvgPrice+((1*Profitfactor) * (ATR(20)[0])));
                          SetProfitTarget(
                          "Entry2", CalculationMode.Price, Position.AvgPrice+((2*Profitfactor) * (ATR(20)[0])));
                          SetStopLoss(
                          "Entry1", CalculationMode.Price, MIN(Low, 3)[0], true);
                          SetStopLoss(
                          "Entry2", CalculationMode.Price, MIN(Low, 3)[0], true);
                          SetStopLoss(
                          "Entry3", CalculationMode.Price, MIN(Low, 3)[0], true);

                          Comment


                            #28
                            cont'd
                            // Once 1st Profittarget attained, 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);
                            }

                            Comment


                              #29
                              con'd
                              // Once 2st Profittarget attained, set stop loss to trail by Low of (x)bars
                              elseif (High[0] > Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
                              &&MIN(Low ,
                              3)[0] > Position.AvgPrice)
                              {
                              SetStopLoss(
                              "Entry3", CalculationMode.Price, MIN(Low, 3)[0], true);
                              }
                              }
                              // Entry Conditions
                              if (EMA(Ema)[0] > EMA(Ema)[1]
                              && SMA(Sma)[
                              0] > SMA(Sma)[1]
                              && EMA(Ema)[
                              0] > SMA(Sma)[0]
                              && Close[
                              0] > Open[0]
                              && Position.MarketPosition == MarketPosition.Flat)
                              {
                              EnterLong(
                              100, "Entry1");
                              EnterLong(
                              100, "Entry2");
                              EnterLong(
                              100, "Entry3");
                              }
                              }

                              Comment


                                #30
                                pswarts, do you intend to hold positions overnight with this strategy? Then simply add ExitOnClose = false; to your Initialize() section and recompile - and those NT end of session exits should be disabled.
                                BertrandNinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by jclose, Today, 09:37 PM
                                0 responses
                                5 views
                                0 likes
                                Last Post jclose
                                by jclose
                                 
                                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                                10 responses
                                1,413 views
                                0 likes
                                Last Post Traderontheroad  
                                Started by firefoxforum12, Today, 08:53 PM
                                0 responses
                                11 views
                                0 likes
                                Last Post firefoxforum12  
                                Started by stafe, Today, 08:34 PM
                                0 responses
                                11 views
                                0 likes
                                Last Post stafe
                                by stafe
                                 
                                Started by sastrades, 01-31-2024, 10:19 PM
                                11 responses
                                169 views
                                0 likes
                                Last Post NinjaTrader_Manfred  
                                Working...
                                X