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

Cannot get strategy to behave as it does in real-time

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

    Cannot get strategy to behave as it does in real-time

    I am new to NinjaTrader, having come over from Tradestation, and I'm trying to learn the ropes. I really wanted to avoid the hassle of learning a whole new language, so I've been using the Strategy Builder wizard. I have built a strategy that submits entry orders on the first tick of a bar and submits exit orders intrabar, basically a ratcheting stop. I have the resolution set to "tick." The strategy did not perform in backtest, or checking strategy performance on a chart. Although the resolution is set to "tick", it doesn't seem to recognize market position changes until a new bar is started. However, when I tried market replay for the strategy behaved as it should. I decided to run the strategy real-time as I saw no other way to debug; running four hour bars the strategy won't enter a trade on the first bar of the day or exit during the bar of entry, unless it is run real-time (or market replay). I have tried enabling "tick replay" and that didn't seem to make a difference. I am concerned because almost daily I'll have disconnects and I want the strategy to pick up where it left off when I reconnect. I cannot find a combination of settings that yields the results I'm expecting. I've also had no luck with any of the start behavior settings I've adjusted. I tried unlocking the code and entering some adopt position logic, but in the middle of the bar it will re-enter a trade. I just want it to submit accurate exit orders should I have to reconnect. Any help would be greatly appreciated. I'm sure this is a case of user error, but this subtlety is the difference between a consistently winning strategy and a loser. I've been really impressed by the helfulness of the forum thus far, so I am optimistic.

    #2
    Hi zrobfrank, thanks for posting. The best tool you can use to debug your strategy is using the Misc>Print() function in the Actions menu. This will allow you to print out specific data from your strategy at specific times so you can see how the strategy is processing data. E.g. Add a print to the actions list that contains your entry order so you can see the time and price of each entry order. We have more details on using Print here:


    Here are some builder examples that are also helpful:
    https://forum.ninjatrader.com/forum/...der#post806596 //strategy with stops and targets (uses Exit orders rather than the Stops and Targets section.)
    https://forum.ninjatrader.com/forum/...reakeven/page2 //
    Hi!, I have a problem that I thought it was a simple one but I can't manage to find a solution. With the strategy builder I made a strategy that basically has to go long when the price goes over the EMA envelope, then comes back, touches the EMA and closes above of the previous candle close.. Those are the conditions. I also

    Hi, I'm currently working on a strategy that is utilizing OnTickChange data using the linked method. I am using a Tick Chart. The strategy uses onTickChange to make intrabar entry, and does so fine with the Set1 and Set2, but if/when profit target is hit, it will submit another order after the profit target if the


    Chris L.NinjaTrader Customer Service

    Comment


      #3
      ChrisL,
      Thank you for your help. I am in the process of reading the linked articles you included. One thing I don't understand, that I'm not sure how to debug via printlog, is why if my logic says to enter "iffirsttickofbar = true" and start behavior is "wait until flat", why everytime I restart the strategy, in the middle of a bar, it is re-entering the trade? I also don't understand why when it places stops it doesn't do so for the entire position, just 1 share?

      Comment


        #4
        ChrisL,
        Changing the quantity from "default quantity" to "current position size" was a no brainer. However, if my condition to submit exit orders says, for example, says "position.marketposition = market position.long", and it's set to run on ever tick, shouldn't it pick up the market position immediately and submit those orders? Is there some other logic I need to add to my entry besides "isfirsttickofbar = true" to get it to fire only once per bar? Thanks.

        Rob

        Comment


          #5
          Hi Rob, When you check for IsFirstTickOfBar and Tick Replay is not enabled, it will trigger on the first real time tick so that might be what you are seeing. You need to turn on Tick Replay to get the true first tick of the bar in historical mode. If you need to restart (re-enable) a strategy that was already managing an open position, use the "Immediately Submit" start behavior and NinjaTrader should recognize the open order as "Matching" meaning it was created by the strategy, so the strategy will continue to manage the position.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            ChrisL,

            Thank you so much. I will play with that. If I add the logic "state = state.realtime" will that make it only fire on the true first tick without having the CPU drain of tick replay?

            Comment


              #7
              ChrisL,

              I enabled tick replay and used immediately submit start behavior and as soon as the strategy started in the middle of a bar it entered a trade that it should not have?

              Comment


                #8
                Hi Rob, Please use prints within your entry condition to see why it is becoming true e.g. print out every condition element to see what is true/false. Your sets are just C# code that generate IF statements. You can add a print through the Misc folder in the Actions list and this will be equivalent to adding the Print() method within an IF block e.g.

                if(Close[0] > Close[1] && IsFirstTickOfBar) //Set 1
                {
                EnterLong();
                Print(Close[0]);
                Print(Close[1]);
                Print(IsFirstTickOfBar);
                }
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  ChrisL,

                  I added the prints as you suggested. Even with tick replay enabled, the time stamps on tickisfirstofbar were all for the end time of the bar,​ not the start time. The only work around I could figure out to get through today was to add state = state.realtime to my entry logic, and then unlock the code and add adopt account position. I still wish it would submit exits for however many shares there are, and to be able to somewhat accurately backtest would be a dream. Let me know if you have any advice. Thank you for your help.

                  Rob

                  Comment


                    #10
                    Hi Rob, Please make sure you are connected to a live data source that will provide historical tick data. I am not getting this behavior in my test script. I attached it here for reference. Also, make sure you are running the script with Calculate = Calculate.OnEachTick. To submit exits for the total quantity, you need to make sure SetStopLoss or your Exit method is being used correctly. I would need to see a snippet of your script that shows how you are using the stop and target for me to be able to help with this.
                    Attached Files
                    Last edited by NinjaTrader_ChrisL; 04-12-2023, 12:24 PM.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      ChrisL,

                      Thank you so much for your help. When I applied your indicator, it had dots(which I assume represent first tick of bar) all on the close of bars. I am using Rithmic brokerage, as far as I know it's a live data source. And according to the settings I have loads of historical tick data downloaded. I am attaching some script if you wouldn't mind looking at it to see where I'm going wrong. The script I'm attaching has the state = state.realtime logic because that's the only way I can get my entries to work correctly in real time. Thanks so much for taking a look at this.


                      Code:
                      [HASHTAG="t3322"]region[/HASHTAG] Using declarations
                      
                      using System;
                      
                      using System.Collections.Generic;
                      
                      using System.ComponentModel;
                      
                      using System.ComponentModel.DataAnnotations;
                      
                      using System.Linq;
                      
                      using System.Text;
                      
                      using System.Threading.Tasks;
                      
                      using System.Windows;
                      
                      using System.Windows.Input;
                      
                      using System.Windows.Media;
                      
                      using System.Xml.Serialization;
                      
                      using NinjaTrader.Cbi;
                      
                      using NinjaTrader.Gui;
                      
                      using NinjaTrader.Gui.Chart;
                      
                      using NinjaTrader.Gui.SuperDom;
                      
                      using NinjaTrader.Gui.Tools;
                      
                      using NinjaTrader.Data;
                      
                      using NinjaTrader.NinjaScript;
                      
                      using NinjaTrader.Core.FloatingPoint;
                      
                      using NinjaTrader.NinjaScript.Indicators;
                      
                      using NinjaTrader.NinjaScript.DrawingTools;
                      
                      #endregion
                      
                      
                      
                      
                      //This namespace holds Strategies in this folder and is required. Do not change it.
                      
                      namespace NinjaTrader.NinjaScript.Strategies
                      
                      {
                      
                      public class AAAobviog : Strategy
                      
                      {
                      
                      private double Stop;
                      
                      private double Profit;
                      
                      
                      
                      
                      
                      
                      private OBV OBV1;
                      
                      private ATR ATR1;
                      
                      private Series<double> profit1;
                      
                      private Series<double> stop1;
                      
                      
                      
                      
                      protected override void OnStateChange()
                      
                      {
                      
                      if (State == State.SetDefaults)
                      
                      {
                      
                      Description = @"Enter the description for your new custom Strategy here.";
                      
                      Name = "AAAobviog";
                      
                      Calculate = Calculate.OnEachTick;
                      
                      EntriesPerDirection = 1;
                      
                      EntryHandling = EntryHandling.AllEntries;
                      
                      IsExitOnSessionCloseStrategy = true;
                      
                      ExitOnSessionCloseSeconds = 30;
                      
                      IsFillLimitOnTouch = false;
                      
                      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                      
                      OrderFillResolution = OrderFillResolution.High;
                      
                      OrderFillResolutionType = BarsPeriodType.Tick;
                      
                      OrderFillResolutionValue = 1;
                      
                      Slippage = 0;
                      
                      StartBehavior = StartBehavior.AdoptAccountPosition;
                      
                      TimeInForce = TimeInForce.Day;
                      
                      TraceOrders = true;
                      
                      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                      
                      StopTargetHandling = StopTargetHandling.PerEntryExecution;
                      
                      BarsRequiredToTrade = 20;
                      
                      // Disable this property for performance gains in Strategy Analyzer optimizations
                      
                      // See the Help Guide for additional information
                      
                      IsInstantiatedOnEachOptimizationIteration = true;
                      
                      Profitmultiply = 2;
                      
                      Shortprofitmultiply = 2;
                      
                      Stop = 1;
                      
                      Profit = 1;
                      
                      
                      
                      
                      
                      if (State == State.SetDefaults)
                      
                      {
                      
                      IsAdoptAccountPositionAware = true;
                      
                      }
                      
                      }
                      
                      else if (State == State.Configure)
                      
                      {
                      
                      }
                      
                      else if (State == State.DataLoaded)
                      
                      {
                      
                      profit1 = new Series<double>(this);
                      
                      stop1 = new Series<double>(this);
                      
                      OBV1 = OBV(Close);
                      
                      ATR1 = ATR(Close, 14);
                      
                      }
                      
                      }
                      
                      
                      
                      
                      protected override void OnBarUpdate()
                      
                      {
                      
                      if (BarsInProgress != 0)
                      
                      return;
                      
                      
                      
                      
                      if (CurrentBars[0] < 2)
                      
                      return;
                      
                      
                      
                      
                      // Set 1
                      
                      if (Close[0] != Close[1])
                      
                      {
                      
                      profit1[0] = (ATR(Close, 14)[1] * Profitmultiply) ;
                      
                      }
                      
                      
                      
                      // Set 2
                      
                      if ((Times[0][0].TimeOfDay >= new TimeSpan(6, 0, 0))
                      
                      && (Times[0][0].TimeOfDay <= new TimeSpan(13, 0, 0))
                      
                      && (OBV1[1] > OBV1[2])
                      
                      && (IsFirstTickOfBar == true)
                      
                      && (State == State.Realtime))
                      
                      {
                      
                      EnterLong(Convert.ToInt32(DefaultQuantity), "");
                      
                      Print(Convert.ToString(IsFirstTickOfBar) + " " + Convert.ToString(Position.Quantity) + " " + Convert.ToString(Times[0][0].TimeOfDay));
                      
                      }
                      
                      
                      
                      // Set 3
                      
                      if (Times[0][0].TimeOfDay > new TimeSpan(13, 0, 0))
                      
                      {
                      
                      ExitShort(Convert.ToInt32(Position.Quantity), "", "");
                      
                      ExitLong(Convert.ToInt32(Position.Quantity), "", "");
                      
                      }
                      
                      
                      
                      // Set 4
                      
                      if ((Position.MarketPosition == MarketPosition.Long)
                      
                      && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.3) )) ))
                      
                      {
                      
                      ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), (Position.AveragePrice - ((ATR1[1] * 0.5) )) , @"SLlong", "");
                      
                      }
                      
                      
                      
                      // Set 5
                      
                      if ((Position.MarketPosition == MarketPosition.Long)
                      
                      && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.5) )) )
                      
                      && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.3) )) ))
                      
                      {
                      
                      ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), Position.AveragePrice, @"SLeven", "");
                      
                      }
                      
                      
                      
                      // Set 6
                      
                      if ((Position.MarketPosition == MarketPosition.Long)
                      
                      && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.5) )) )
                      
                      && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.8) )) ))
                      
                      {
                      
                      ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), (Position.AveragePrice + ((profit1[0] * 0.25) )) , @"SL50", "");
                      
                      }
                      
                      
                      
                      // Set 7
                      
                      if ((Position.MarketPosition == MarketPosition.Long)
                      
                      && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.8) )) )
                      
                      && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.9) )) ))
                      
                      {
                      
                      ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), (Position.AveragePrice + ((profit1[0] * 0.64) )) , @"SL80", "");
                      
                      }
                      
                      
                      
                      // Set 8
                      
                      if ((Position.MarketPosition == MarketPosition.Long)
                      
                      && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.9) )) ))
                      
                      {
                      
                      ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), (Position.AveragePrice + ((profit1[0] * 0.81) )) , @"SL90", "");
                      
                      }
                      
                      
                      
                      // Set 9
                      
                      if ((Times[0][0].TimeOfDay >= new TimeSpan(6, 0, 0))
                      
                      && (Times[0][0].TimeOfDay <= new TimeSpan(13, 0, 0))
                      
                      && (OBV1[1] < OBV1[2])
                      
                      && (IsFirstTickOfBar == true)
                      
                      && (State == State.Realtime))
                      
                      {
                      
                      EnterShort(Convert.ToInt32(DefaultQuantity), "");
                      
                      Print(Convert.ToString(IsFirstTickOfBar) + " " + Convert.ToString(Position.Quantity) + " " + Convert.ToString(Times[0][0].TimeOfDay));
                      
                      }
                      
                      
                      
                      // Set 10
                      
                      if ((Position.MarketPosition == MarketPosition.Short)
                      
                      && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.3) )) ))
                      
                      {
                      
                      ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Position.AveragePrice + ((ATR1[1] * 0.5) )) , @"SLstop", "");
                      
                      }
                      
                      
                      
                      // Set 11
                      
                      if ((Position.MarketPosition == MarketPosition.Short)
                      
                      && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.5) )) )
                      
                      && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.3) )) ))
                      
                      {
                      
                      ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), Position.AveragePrice, @"SLeven", "");
                      
                      }
                      
                      
                      
                      // Set 12
                      
                      if ((Position.MarketPosition == MarketPosition.Short)
                      
                      && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.5) )) )
                      
                      && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.8) )) ))
                      
                      {
                      
                      ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Position.AveragePrice - ((profit1[0] * 0.25) )) , @"SL50", "");
                      
                      }
                      
                      
                      
                      // Set 13
                      
                      if ((Position.MarketPosition == MarketPosition.Short)
                      
                      && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.8) )) )
                      
                      && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.9) )) ))
                      
                      {
                      
                      ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Position.AveragePrice - ((profit1[0] * 0.64) )) , @"SL80", "");
                      
                      }
                      
                      
                      
                      // Set 14
                      
                      if ((Position.MarketPosition == MarketPosition.Short)
                      
                      && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.9) )) ))
                      
                      {
                      
                      ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Position.AveragePrice - ((profit1[0] * 0.81) )) , @"SL90", "");
                      
                      }
                      
                      
                      
                      }
                      
                      
                      
                      
                      [HASHTAG="t3322"]region[/HASHTAG] Properties
                      
                      [NinjaScriptProperty]
                      
                      [Range(0.25, double.MaxValue)]
                      
                      [Display(Name="Profitmultiply", Order=1, GroupName="Parameters")]
                      
                      public double Profitmultiply
                      
                      { get; set; }
                      
                      
                      
                      
                      [NinjaScriptProperty]
                      
                      [Range(0.25, double.MaxValue)]
                      
                      [Display(Name="Shortprofitmultiply", Order=2, GroupName="Parameters")]
                      
                      public double Shortprofitmultiply
                      
                      { get; set; }
                      
                      #endregion
                      
                      
                      
                      
                      }
                      
                      }
                      ​

                      Comment


                        #12
                        I just enabled tick replay on the chart, and now your indicator correctly identifies the firsttickofbar, but if I run my strategy it still does not operate as exected in regards to firsttickofbar

                        Comment


                          #13
                          Hi Rob, The high order fill resolution can not be used with tick repay enabled, so it looks like you are testing this strategy with tick replay off. Try disabling High order fill resolution by switching it back to standard in the script, then open a new chart and test the script again. To get both at the same time, you will need to add a 1 tick series to your script and fill orders upon that series, please see the guide on intrabar granularity here:

                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            ChrisL,

                            Thank you for your help and patience. I read through the link you suggested in your last post. I can wrap my head around adding intrabar granularity. What I cannot figure out how to do is have the entry submitted only once per bar. If I cannot use iffirsttickofbar (because with the granularity every tick is the first tick), and I cannot use onbarclose because I will miss the first bar of the day if I'm using 240 minute bars on the Equities RTH schedule, then how do I make the entry only submit once at the beginning of a bar?

                            Comment


                              #15
                              ChrisL,
                              This was my attempt to implement the ideas you suggested. I cannot get it to submit any entries now. If you wouldn't mind taking a look and telling me where I'm screwing it up, it would be much appreciated.

                              Code:
                              #region Using declarations
                              
                              using System;
                              
                              using System.Collections.Generic;
                              
                              using System.ComponentModel;
                              
                              using System.ComponentModel.DataAnnotations;
                              
                              using System.Linq;
                              
                              using System.Text;
                              
                              using System.Threading.Tasks;
                              
                              using System.Windows;
                              
                              using System.Windows.Input;
                              
                              using System.Windows.Media;
                              
                              using System.Xml.Serialization;
                              
                              using NinjaTrader.Cbi;
                              
                              using NinjaTrader.Gui;
                              
                              using NinjaTrader.Gui.Chart;
                              
                              using NinjaTrader.Gui.SuperDom;
                              
                              using NinjaTrader.Gui.Tools;
                              
                              using NinjaTrader.Data;
                              
                              using NinjaTrader.NinjaScript;
                              
                              using NinjaTrader.Core.FloatingPoint;
                              
                              using NinjaTrader.NinjaScript.Indicators;
                              
                              using NinjaTrader.NinjaScript.DrawingTools;
                              
                              #endregion
                              
                              
                              
                              
                              //This namespace holds Strategies in this folder and is required. Do not change it.
                              
                              namespace NinjaTrader.NinjaScript.Strategies
                              
                              {
                              
                              public class AAAobviog04124 : Strategy
                              
                              {
                              
                              private double Stop;
                              
                              private double Profit;
                              
                              
                              
                              
                              
                              
                              private OBV OBV1;
                              
                              private ATR ATR1;
                              
                              private Series<double> profit1;
                              
                              private Series<double> stop1;
                              
                              
                              
                              
                              protected override void OnStateChange()
                              
                              {
                              
                              if (State == State.SetDefaults)
                              
                              {
                              
                              Description = @"Enter the description for your new custom Strategy here.";
                              
                              Name = "AAAobviog04124";
                              
                              Calculate = Calculate.OnEachTick;
                              
                              EntriesPerDirection = 1;
                              
                              EntryHandling = EntryHandling.AllEntries;
                              
                              IsExitOnSessionCloseStrategy = true;
                              
                              ExitOnSessionCloseSeconds = 30;
                              
                              IsFillLimitOnTouch = false;
                              
                              MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                              
                              OrderFillResolution = OrderFillResolution.Standard;
                              
                              Slippage = 0;
                              
                              StartBehavior = StartBehavior.WaitUntilFlat;
                              
                              TimeInForce = TimeInForce.Day;
                              
                              TraceOrders = true;
                              
                              RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                              
                              StopTargetHandling = StopTargetHandling.PerEntryExecution;
                              
                              BarsRequiredToTrade = 20;
                              
                              // Disable this property for performance gains in Strategy Analyzer optimizations
                              
                              // See the Help Guide for additional information
                              
                              IsInstantiatedOnEachOptimizationIteration = true;
                              
                              Profitmultiply = 2;
                              
                              Shortprofitmultiply = 2;
                              
                              Stop = 1;
                              
                              Profit = 1;
                              
                              }
                              
                              else if (State == State.Configure)
                              
                              {
                              
                              AddDataSeries(Data.BarsPeriodType.Tick, 1);
                              
                              }
                              
                              else if (State == State.DataLoaded)
                              
                              {
                              
                              profit1 = new Series<double>(this);
                              
                              stop1 = new Series<double>(this);
                              
                              OBV1 = OBV(Close);
                              
                              ATR1 = ATR(Close, 14);
                              
                              }
                              
                              }
                              
                              
                              
                              
                              protected override void OnBarUpdate()
                              
                              {
                              
                              if (BarsInProgress != 0)
                              
                              return;
                              
                              
                              
                              
                              if (CurrentBars[0] < 2)
                              
                              return;
                              
                              
                              
                              
                              // Set 1
                              
                              if (Close[0] != Close[1])
                              
                              {
                              
                              profit1[0] = (ATR(Close, 14)[1] * Profitmultiply) ;
                              
                              }
                              
                              
                              
                              // Set 2
                              
                              if ((Times[0][0].TimeOfDay >= new TimeSpan(6, 0, 0))
                              
                              && (Times[0][0].TimeOfDay <= new TimeSpan(13, 0, 0))
                              
                              && (OBV1[1] > OBV1[2])
                              
                              && (IsFirstTickOfBar == true)
                              
                              && (BarsSinceEntryExecution(0, "", 0) < 1))
                              
                              {
                              
                              EnterLong(Convert.ToInt32(DefaultQuantity), "");
                              
                              Print(Convert.ToString(IsFirstTickOfBar) + " " + Convert.ToString(Position.Quantity) + " " + Convert.ToString(Times[0][0].TimeOfDay));
                              
                              }
                              
                              
                              
                              // Set 3
                              
                              if (Times[0][0].TimeOfDay > new TimeSpan(13, 0, 0))
                              
                              {
                              
                              ExitShort(Convert.ToInt32(Position.Quantity), "", "");
                              
                              ExitLong(Convert.ToInt32(Position.Quantity), "", "");
                              
                              }
                              
                              
                              
                              // Set 4
                              
                              if ((Position.MarketPosition == MarketPosition.Long)
                              
                              && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.3) )) ))
                              
                              {
                              
                              ExitLongStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice - ((ATR1[1] * 0.5) )) , @"SLlong", "");
                              
                              }
                              
                              
                              
                              // Set 5
                              
                              if ((Position.MarketPosition == MarketPosition.Long)
                              
                              && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.5) )) )
                              
                              && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.3) )) ))
                              
                              {
                              
                              ExitLongStopMarket(Convert.ToInt32(Position.Quantity), Position.AveragePrice, @"SLeven", "");
                              
                              }
                              
                              
                              
                              // Set 6
                              
                              if ((Position.MarketPosition == MarketPosition.Long)
                              
                              && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.5) )) )
                              
                              && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.8) )) ))
                              
                              {
                              
                              ExitLongStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + ((profit1[0] * 0.25) )) , @"SL50", "");
                              
                              }
                              
                              
                              
                              // Set 7
                              
                              if ((Position.MarketPosition == MarketPosition.Long)
                              
                              && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.8) )) )
                              
                              && (Close[0] < (Position.AveragePrice + ((profit1[0] * 0.9) )) ))
                              
                              {
                              
                              ExitLongStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + ((profit1[0] * 0.64) )) , @"SL80", "");
                              
                              }
                              
                              
                              
                              // Set 8
                              
                              if ((Position.MarketPosition == MarketPosition.Long)
                              
                              && (Close[0] > (Position.AveragePrice + ((profit1[0] * 0.9) )) ))
                              
                              {
                              
                              ExitLongStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + ((profit1[0] * 0.81) )) , @"SL90", "");
                              
                              }
                              
                              
                              
                              // Set 9
                              
                              if ((Times[0][0].TimeOfDay >= new TimeSpan(6, 0, 0))
                              
                              && (Times[0][0].TimeOfDay <= new TimeSpan(13, 0, 0))
                              
                              && (OBV1[1] < OBV1[2])
                              
                              && (IsFirstTickOfBar == true)
                              
                              && (BarsSinceEntryExecution(0, "", 0) < 1))
                              
                              {
                              
                              EnterShort(Convert.ToInt32(DefaultQuantity), "");
                              
                              Print(Convert.ToString(IsFirstTickOfBar) + " " + Convert.ToString(Position.Quantity) + " " + Convert.ToString(Times[0][0].TimeOfDay));
                              
                              }
                              
                              
                              
                              // Set 10
                              
                              if ((Position.MarketPosition == MarketPosition.Short)
                              
                              && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.3) )) ))
                              
                              {
                              
                              ExitShortStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + ((ATR1[1] * 0.5) )) , @"SLstop", "");
                              
                              }
                              
                              
                              
                              // Set 11
                              
                              if ((Position.MarketPosition == MarketPosition.Short)
                              
                              && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.5) )) )
                              
                              && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.3) )) ))
                              
                              {
                              
                              ExitShortStopMarket(Convert.ToInt32(Position.Quantity), Position.AveragePrice, @"SLeven", "");
                              
                              }
                              
                              
                              
                              // Set 12
                              
                              if ((Position.MarketPosition == MarketPosition.Short)
                              
                              && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.5) )) )
                              
                              && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.8) )) ))
                              
                              {
                              
                              ExitShortStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice - ((profit1[0] * 0.25) )) , @"SL50", "");
                              
                              }
                              
                              
                              
                              // Set 13
                              
                              if ((Position.MarketPosition == MarketPosition.Short)
                              
                              && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.8) )) )
                              
                              && (Close[0] > (Position.AveragePrice - ((profit1[0] * 0.9) )) ))
                              
                              {
                              
                              ExitShortStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice - ((profit1[0] * 0.64) )) , @"SL80", "");
                              
                              }
                              
                              
                              
                              // Set 14
                              
                              if ((Position.MarketPosition == MarketPosition.Short)
                              
                              && (Close[0] < (Position.AveragePrice - ((profit1[0] * 0.9) )) ))
                              
                              {
                              
                              ExitShortStopMarket(Convert.ToInt32(Position.Quantity), (Position.AveragePrice - ((profit1[0] * 0.81) )) , @"SL90", "");
                              
                              }
                              
                              
                              
                              }
                              
                              
                              
                              
                              #region Properties
                              
                              [NinjaScriptProperty]
                              
                              [Range(0.25, double.MaxValue)]
                              
                              [Display(Name="Profitmultiply", Order=1, GroupName="Parameters")]
                              
                              public double Profitmultiply
                              
                              { get; set; }
                              
                              
                              
                              
                              [NinjaScriptProperty]
                              
                              [Range(0.25, double.MaxValue)]
                              
                              [Display(Name="Shortprofitmultiply", Order=2, GroupName="Parameters")]
                              
                              public double Shortprofitmultiply
                              
                              { get; set; }
                              
                              #endregion
                              
                              
                              
                              
                              }
                              
                              }
                              ​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by carnitron, Today, 08:42 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Today, 07:51 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,974 views
                              3 likes
                              Last Post jhudas88  
                              Started by rbeckmann05, Today, 06:48 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post rbeckmann05  
                              Started by rhyminkevin, Today, 04:58 PM
                              4 responses
                              58 views
                              0 likes
                              Last Post dp8282
                              by dp8282
                               
                              Working...
                              X