Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate sum of previous wicks

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

    #46
    eDanny NinjaTrader_ChrisL
    Hello, i have modified and simplified code using ternary operators and for some reason strategy still takes trades against my logic. i am stating not to take short trades if sum of wicks is greater then 4 in last 4 bars. Output prints correct number of ticks in wicks. Maybe i am not using sytax right? Could it be improved? If we look at screenshot we see two short trades first one is right(no wicks) second one is not right (15 tick wicks). LimitWicks is my bool that i am evaluating sum of wicks. Trade34B is the strategy among other strategies. Thank you

    && (Trade34B && LimitWicks ? (LowerSum <= (CumulativeWicks * TickSize)) : Close[0] > 0)

    Code:
    if (Trade34B
    
                            && ((Trade34B == true)
                            && (!IsInShortTrade) && (!IsInLongTrade) && (stopOrder == null)    && (Position.Quantity == 0)        
                            && (AngulationOn ? (emaAngulation <= (-EMAAngulation * TickSize)) : Close[0] > 0)
                            && (AngulationOn ? (emaAngulationXBarsBefore < (-EMAAngulationXBarsBefore * TickSize)) : Close[0] > 0)  
                            && (FiveBarRetracement ? ((Close[1] > Open[1]) && (Close[2] > Open[2]) && (Close[3] > Open[3])  
                            && (Close[4] > Open[4]) && (Close[5] > Open[5])) : Close[0] > 0)                      
                            && (ThreeBarRetracement ? ((Close[1] > Open[1]) && (Close[2] > Open[2]) && (Close[3] > Open[3])) : Close[0] > 0)                                                
                            && (High[0] > High[1]) && (High[0] >= slowEMA[0]) && (Close[0] < (slowEMA[0] - ABEMA34B * TickSize) && fastEMA[0] < slowEMA[0])                          
                            && (NoTradeAgainstWick ? (Close[0] < Low[1]) : Close[0] > 0)                                                                  
                            && (FilterEMA ? (filterEMA[0] >= slowEMA[0] && (distancefilterEMAand34 >= (6 * TickSize))) : Close[0] > 0)
                            && (MarginToRececntSwingHL ? (marginToRecentSwingL > (MarginToSwing * TickSize)) : Close[0] > 0)    
                            && (VolumeFilter ? (VOL()[0] > VolumeReading) : Close[0] > 0)
                            && (StochFilter ? ((StochasticsFast1.D[0] <= StochLongLevels) && (StochasticsFast1.D[0] >= StochShortLevels)) : Close[0] > 0)
                            && (Trade34B && LimitWicks ? (LowerSum <= (CumulativeWicks * TickSize)) : Close[0] > 0)
                            && (DistanceSettlement ? (distToSettlment >= (DistToSettlment * TickSize)) : Close[0] > 0)
                            && (DeltaClose ? (Deltaclose < Deltaopen) : Close[0] > 0)
                            && (DeltaEngulf ? (Deltaclose < Deltaopen) && (Deltaclose < Math.Min(DeltaopenPr, DeltaclosePr)) : Close[0] > 0)
                            && (DistanceOHL ? (distToOHLL) > (DistanceToOHL * TickSize) : Close[0] > 0)
                            && (BarsSinceExitExecution(0, "", 0) > 2) || (BarsSinceExitExecution(0, "", 0) == -1)
                            && (Position.MarketPosition == MarketPosition.Flat)))
    
                        {
                            entryOrder = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, TradeSize, 0, 0, "", "34B");
                            IsInShortTrade = true;                        
                            LastClose = Close[0];
                            if (SystemPrint == true && Trade34B == true)
                            {
                                Print("------------------");    
                                  Print("Trade34B SHorts: " + Trade34B + " " + Time[0] + " " + "Sum Lower Wicks " + sumLowerWicks);
                                Print("DeltaClose " + Deltaclose + " " + "DeltaClosePr " + DeltaclosePr);
                                  Print("------------------");    
                            }
                            return;
                        }
                        if ((UseTrailingStop) && (IsInShortTrade) && (LastClose > Close[0]) && (stopOrder != null) && (Close[0] < CurrentTriggerPrice))
                        {
                            //double slL = Instrument.MasterInstrument.RoundToTickSize(Close[0] + (StopLoss * TickSize));                    
                            CurrentTriggerPrice = (Close[0] - (TrailFrequency * TickSize)) ;
                            CurrentStopPrice = (Close[0] - (TrailStopDistance * TickSize)) ;                        
                            if(entryOrder != null)ChangeOrder(stopOrder, Position.Quantity, 0, CurrentStopPrice);                        
                            LastClose = Close[0];
    
                            if(SystemPrint == true && Trade34B == true)
                            Print("Short 34B Trade Trailing Stop         sIL = " + CurrentStopPrice + "        Position.Quantity = " + Position.Quantity + "        CurrentBar = " + CurrentBar);
    
                        }    
                        if ((Position.MarketPosition == MarketPosition.Short)
                             && (Close[0] < CurrentTriggerPrice))
                        {
                            CurrentTriggerPrice = (Close[0] - (TrailFrequency * TickSize)) ;
                            CurrentStopPrice = (Close[0] - (TrailStopDistance * TickSize)) ;
                        }​
    Click image for larger version  Name:	image.png Views:	0 Size:	1.12 MB ID:	1240721
    Last edited by tkaboris; 03-16-2023, 09:28 AM.

    Comment


      #47
      Hi Boris, the best way to evaluate flaws in a logic operation is to analyze the output. Unfortunately, the support team will not be able to perform the debugging process through this forum. This is so we can help all requests in a timely manner. This is a large combination of logic statements that needs to be analyzed at each condition element. e.g. print out each thing in between a logical operatior (&&, ||)

      Print(Time[0]);
      Print(Trade34B);
      Print((Trade34B == true));

      Kind regards,
      -ChrisL​

      Comment


        #48
        Lots of seemingly convoluted stuff here. Example:

        if ((Position.MarketPosition == MarketPosition.Short)
        && (Close[0] < CurrentTriggerPrice))
        {
        CurrentTriggerPrice = (Close[0] - (TrailFrequency * TickSize)) ;
        CurrentStopPrice = (Close[0] - (TrailStopDistance * TickSize)) ;
        }​​

        Looks like you are testing for short position, and if current price is below a trail stop trigger. If so, calculate the next trigger and reset the new stop. Problem is, the new stop is still below the current price unless you are using a negative number in the variable TrailStopDistance. Otherwise shouldn't:
        CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
        ?

        Also, what kind of test is this?
        Close[0] > 0
        Close[0] > 0 will always be greater than zero, will always be true.
        Last edited by eDanny; 03-17-2023, 11:33 AM.
        eDanny
        NinjaTrader Ecosystem Vendor - Integrity Traders

        Comment


          #49
          eDanny

          Hi I am concered about this line for Short setup. Script I posted above.
          if ((UseTrailingStop) && (IsInShortTrade) && (LastClose > Close[0]) && (stopOrder != null) && (Close[0] < CurrentTriggerPrice))

          my properites are these and it does use negative number. I sometimes get this error that order cant be changed. I was wondering if my script is ok for shorts? I used Close[0] > 0 just a a filler so i can use ternary operator. I coulndt come up with more meaningfull condition

          Code:
          #region Trade management
                          UseTrailingStop    = false;                
                          TrailFrequency = 5;
                          TrailStopDistance = -5;
                          CurrentTriggerPrice    = 0;
                          CurrentStopPrice = 0;
                          #endregion​
          Click image for larger version  Name:	image.png Views:	3 Size:	307.6 KB ID:	1241265
          Last edited by tkaboris; 03-20-2023, 05:41 AM.

          Comment


            #50
            eDanny

            Update i have updated TrailStopDistance so it uses positive number

            Code:
            private double CurrentTriggerPrice;
                    private double CurrentStopPrice;​
            In onbarupdate
            CurrentStopPrice = 0;

            Code:
            #region Trade management
            UseTrailingStop = false;
            TrailFrequency = 5;
            TrailStopDistance = 5;
            CurrentTriggerPrice = 0;
            CurrentStopPrice = 0;
            #endregion​​
            My trail is not setup correctly but i am not sure where. Its sometimes working sometimes strategy is not starting if trailstop enabled
            I have this for Long example
            Code:
            #region Compound 34B Setup
                                else if (C34B
                                    && (C34B == true)
                                    && (!IsInLongTrade) && (!IsInShortTrade) && (stopOrder == null)    && (Position.Quantity == 0)
                                    && main logic
                                    && (BarsSinceExitExecution(0, "", 0) > 2) || (BarsSinceExitExecution(0, "", 0) == -1)
                                    && (Position.MarketPosition == MarketPosition.Flat)))
                                {
            
                                    return;
                                }
                               if ((UseTrailingStop) && (IsInLongTrade) && (LastClose < Close[0]) && (stopOrder != null) && (Close[0] > CurrentTriggerPrice))
                                {
                                    //double slL = Instrument.MasterInstrument.RoundToTickSize(Close[0] - StopLoss * TickSize);    
                                    CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
                                    CurrentStopPrice = (Close[0] - (TrailStopDistance * TickSize)) ;
                                    if(entryOrder != null)ChangeOrder(stopOrder, Position.Quantity, 0, CurrentStopPrice);                        
                                    LastClose = Close[0];
            
                                    if(SystemPrint && Trade34B == true)
                                    Print("Long 34B Trade Trailing Stop         slL = " + CurrentStopPrice + "        Position.Quantity = " + Position.Quantity + "        CurrentBar = " + CurrentBar);
                                    Print("CurrentriggerPrice = " + CurrentTriggerPrice + "    Current Stop Price = " + CurrentStopPrice + "    Current Close = " + Close[0]);
                                }
                                if ((Position.MarketPosition == MarketPosition.Long)
                                     && (Close[0] > CurrentTriggerPrice))
                                {
                                    CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
                                    CurrentStopPrice = (Close[0] - (TrailStopDistance * TickSize)) ;
                                }
                                ​                   ​
            ANd for Short
            Code:
            if (Trade34B
            
                                    && ((Trade34B == true)
                                    && (!IsInShortTrade) && (!IsInLongTrade) && (stopOrder == null)    && (Position.Quantity == 0)        
                                    && main logic
                                    && (BarsSinceExitExecution(0, "", 0) > 2) || (BarsSinceExitExecution(0, "", 0) == -1)
                                    && (Position.MarketPosition == MarketPosition.Flat)))
            
                                {
                                    entryOrder = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, TradeSize, 0, 0, "", "34B");
                                    IsInShortTrade = true;                        
                                    LastClose = Close[0];
                                    if (SystemPrint == true && Trade34B == true)
                                    {
                                    }
                                    return;
                                }
                                if ((UseTrailingStop) && (IsInShortTrade) && (LastClose > Close[0]) && (stopOrder != null) && (Close[0] < CurrentTriggerPrice))
                                {
                                    //double slL = Instrument.MasterInstrument.RoundToTickSize(Close[0] + (StopLoss * TickSize));                    
                                    CurrentTriggerPrice = (Close[0] - (TrailFrequency * TickSize)) ;
                                    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;                    
                                    if(entryOrder != null)ChangeOrder(stopOrder, Position.Quantity, 0, CurrentStopPrice);                        
                                    LastClose = Close[0];
            
                                    if(SystemPrint == true && Trade34B == true)
                                    Print("Short 34B Trade Trailing Stop         sIL = " + CurrentStopPrice + "        Position.Quantity = " + Position.Quantity + "        CurrentBar = " + CurrentBar);
                                    Print("CurrentriggerPrice = " + CurrentTriggerPrice + "    Current Stop Price = " + CurrentStopPrice + "    Current Close = " + Close[0]);
            
                                }    
                                if ((Position.MarketPosition == MarketPosition.Short)
                                     && (Close[0] < CurrentTriggerPrice))
                                {
                                    CurrentTriggerPrice = (Close[0] - (TrailFrequency * TickSize)) ;
                                    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
                                }
                                #endregion​
            My stoporder variable comes from unmanaged from here
            Code:
             if (StopLoss > 0 && sl.ApproxCompare(bid) < 0)
                                {
                                    stopOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, filled, 0, sl, oco, "SL");
                                    IsInLongTrade = true;                        
                                    if(SystemPrint)
                                    Print("Enter Long Trade StopLoss = " + sl);
                                }​
            Last edited by tkaboris; 03-20-2023, 05:53 AM.

            Comment


              #51
              Guys,

              Grateful for your help in advance.

              What should be the code for drawing a down arrow for an upper wick on a down bar candle: Condition is when the price from High to Open is greater than the price from Open + Close.

              Comment


                #52
                You need to check when you are submitting a stop order if it is across the market e.g. the stop price is >= the current bid for a sell (or use the last price if you don't have bid/ask in your strategy). If it is across the market and would immediately be marketable, you need to submit a market order instead of a stop market order because a stop market order would be rejected. If you are changing an existing order, and you can't change its type, you would need to cancel that and put in a market order to exit - you can't just changeorder a stop market's price across the market or it would be rejected.
                Last edited by QuantKey_Bruce; 04-27-2023, 05:39 PM.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                48 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                126 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                66 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X