public ShortProfitTargetReachedLookingToAdjustStopLoss(ATRTrade aContext, MqlApi mql4) : base(mql4)
        {

            currentLL = 99999;
            this.timeWhenProfitTargetWasReached = mql4.TimeCurrent();
            this.context = aContext;
        }
 public WaitForBreakToPlaceLimitOrder(ATRTrade _trade, int _limitOrderType, double _rangeLow, double _rangeHigh, double _entryPrice, double _cancelPrice, double _positionSize, MqlApi mql4) : base(mql4)
 {
     trade = _trade;
     limitOrderType = _limitOrderType;
     rangeHigh = _rangeHigh;
     rangeLow = _rangeLow;
     cancelPrice = _cancelPrice;
     positionSize = _positionSize;
     entryPrice = _entryPrice;
 }
        public LowestLowReceivedEstablishingEligibilityRange(ATRTrade aContext, MqlApi mql4) : base(mql4)
        {
            this.context = aContext;
            this.entryTime = mql4.Time[0];
            this.rangeHigh = mql4.High[0];
            this.rangeLow = mql4.Low[0];
            this.barCounter = 0;
            this.buffer = aContext.getRangeBufferInMicroPips() / OrderManager.getPipConversionFactor(mql4);
            context.setTradeType(TradeType.LONG);

            //context.addLogEntry("Lowest Low found - establishing eligibility range. Lowest low: " + mql4.DoubleToString(mql4.Close[0], mql4.Digits), true);
        }
        public HighestHighReceivedEstablishingEligibilityRange(ATRTrade aContext, MqlApi mql4) : base(mql4)
        {
            this.context = aContext;
            this.entryTime = mql4.Time[0];
            this.rangeHigh = mql4.High[0];
            this.rangeLow = mql4.Low[0];
            this.barCounter = 0;
            this.buffer = aContext.getRangeBufferInMicroPips() / OrderManager.getPipConversionFactor(mql4); ///Works for 5 Digts pairs. Verify that calculation is valid for 3 Digits pairs

            context.setTradeType(TradeType.SHORT);

            //context.addLogEntry("Highest high found - establishing eligibility range. Highest high: " + mql4.DoubleToString(mql4.Close[0], mql4.Digits), true);
        }
 private ATRTrade context; //hides conext in Trade
 public BuyOrderFilledProfitTargetNotReached(ATRTrade aContext, MqlApi mql4) : base(mql4)
 {
     this.context = aContext;
     context.setOrderFilledDate(mql4.TimeCurrent());
     context.Order.OrderType = OrderType.BUY;
 }
Ejemplo n.º 6
0
        public override int start()
        {

            //new bar?

            //the second condition is to skip the first couple of minutes of when the market opens on Sunday
            if (!bartime.Equals(Time[0]) && Time[0]-Time[2] < TimeSpan.FromHours(1))
            {
                //TODO verify that identity makes it still work
                bartime = Time[0];

                Session newSession = SessionFactory.getCurrentSession(sundayLengthInSeconds, HHLL_Threshold, lookBackSessions, atrType, this);
                if (currSession != newSession)
                {
                    currSession = newSession;

                    currSession.writeToCSV("session_atr.csv");
                    if (!currSession.tradingAllowed())
                    {
                        currSession.addLogEntry(true, "ATTENTION: Session could not be established", "\n", "Trading is disabled. Check log for details");
                    }
                    else
                    {

                        /*Print(TimeCurrent()," Start session: ", currSession.getName()," Start: ",currSession.getSessionStartTime()," End: ",currSession.getSessionEndTime()," ATR: ",currSession.getATR(), " (", (int) (currSession.getATR() * 100000), ") micro pips");
                        Print (" Ref: ",currSession.getHHLL_ReferenceDateTime(), " HH: ", currSession.getHighestHigh(), "@ ", currSession.getHighestHighTime(),
                               " LL: ", currSession.getLowestLow(), "@ ", currSession.getLowestLowTime());

                        */
                        double atr = currSession.getATR();
                        double tenDayHigh = currSession.getTenDayHigh();
                        double tenDayLow = currSession.getTenDayLow();
                        double ATR_OR = atr / (tenDayHigh - tenDayLow);
                        string sessionStatus = "";

                        if ((currSession.getATR() * OrderManager.getPipConversionFactor(this) < minATR) || (currSession.getATR() * OrderManager.getPipConversionFactor(this) > maxATR))
                        {
                            sessionStatus = ("ATR is not in range of: " + minATR + " - " + maxATR + ". No trades will be taken in this Session)");
                            currSession.tradingAllowed(false);
                        }
                        else if ((ATR_OR > maxATROR) || (ATR_OR < minATROR))
                        {
                            sessionStatus = ("ATR/OR is not in range of: " + minATROR.ToString("F2") + " - " + maxATROR.ToString("F2") + ". No trades will be taken in this Session)");
                            currSession.tradingAllowed(false);
                        }
                        else
                        {
                            sessionStatus = "ATR and ATR/OR are within range. Trades may be triggered in this sessions";
                        }

                        currSession.addLogEntry(true, "New Trading Session Established",
                                                      "Session name: ", currSession.getName(), "\n",
                                                      "Session start time: ", currSession.getSessionStartTime().ToString(), "\n",
                                                      "Reference date: ", currSession.getHHLL_ReferenceDateTime().ToString(), "\n",
                                                      "ATR: ", NormalizeDouble(atr, Digits), " (", (int)(currSession.getATR() * OrderManager.getPipConversionFactor(this)), " micro pips)", "\n",
                                                      "HH: ", currSession.getHighestHigh().ToString("F5"), "(", currSession.getHighestHighTime(), ") ", "LL: ", currSession.getLowestLow().ToString("F5"), "(", currSession.getLowestLowTime(), ") ", "\n",
                                                      "10 Day High is: ", tenDayHigh.ToString("F5"), " 10 Day Low is: ", tenDayLow.ToString("F5"), "\n",
                                                      "ATR / OR is: ", ATR_OR.ToString("F5"), "\n",
                                                      sessionStatus
                              );


                    }
                }
            }

            foreach (var trade in trades)
            {
                if (trade != null) trade.update();
            }



            if (currSession.tradingAllowed())
            {
                int updateResult = currSession.update((Bid + Ask) / 2);
                double atr = currSession.getATR();
                double curDailyRange = iHigh(null, MqlApi.PERIOD_D1, 0) - iLow(null, MqlApi.PERIOD_D1, 0);
                double DR_ATR = curDailyRange / atr;

                //if ((((ATR_OR < maxATROR) && (ATR_OR > minATROR)) || cutLossesBeforeATRFilter))


                if ((updateResult == 1) && (minATR < currSession.getATR() * OrderManager.getPipConversionFactor(this)) && (maxATR > currSession.getATR() * OrderManager.getPipConversionFactor(this)))
                {

                    string status = "";

                    bool go = false;
                    if ((DR_ATR < maxDRATR) && (DR_ATR > minDRATR))
                    {
                        go = true;
                        status = "DR/ATR is within range. Starting 10bar clock";
                    }
                    else
                    {
                        go = false;
                        status = "DR/ATR is too big. Trade is rejected";
                    }

                    currSession.addLogEntry(true, "Tradeable Highest High found",
                                                  "Highest high is: ", currSession.getHighestHigh().ToString("F5"), "\n",
                                                  "Time of highest high: ", currSession.getHighestHighTime().ToString(), "\n",
                                                  "Session high: ", iHigh(null, MqlApi.PERIOD_D1, 0).ToString("F5"), " Session low: ", iLow(null, MqlApi.PERIOD_D1, 0).ToString("F5"), "\n",
                                                  "DR / ATR is: ", DR_ATR.ToString("F5"), "\n",
                                                  status
                                                  );
                    if (go)
                    {
                        ATRTrade trade = new ATRTrade(false, lotDigits, logFileName, currSession.getHighestHigh(), currSession.getATR(), lengthOfGracePeriod, maxRisk, maxVolatility, minProfitTarget, rangeBuffer, rangeRestriction, currSession.getTenDayHigh() - currSession.getTenDayLow(), currSession, maxBalanceRisk, this);
                        trade.setState(new HighestHighReceivedEstablishingEligibilityRange(trade, this));
                        trades.Add(trade);
                    }

                }

                if ((updateResult == -1) && (minATR < currSession.getATR() * OrderManager.getPipConversionFactor(this)) && (maxATR > currSession.getATR() * OrderManager.getPipConversionFactor(this)))
                {

                    string status = "";

                    bool go = false;
                    if ((DR_ATR < maxDRATR) && (DR_ATR > minDRATR))
                    {
                        go = true;
                        status = "DR/ATR is within range. Starting 10bar clock";
                    }
                    else
                    {
                        go = false;
                        status = "DR/ATR is too big. Trade is rejected";
                    }

                    currSession.addLogEntry(true, "Tradeable Lowest Low found",
                                                  "Lowest low is: ", currSession.getLowestLow().ToString("F5"), "\n",
                                                  "Time of lowest low: ", currSession.getLowestLowTime().ToString(), "\n",
                                                  "Session high: ", iHigh(null, MqlApi.PERIOD_D1, 0).ToString("F5"), " Session low: ", iLow(null, MqlApi.PERIOD_D1, 0).ToString("F5"), "\n",
                                                  "DR / ATR is: ", DR_ATR.ToString("F5"), "\n",
                                                  status
                                                  );

                    if (go)
                    {
                        ATRTrade trade = new ATRTrade(false, lotDigits, logFileName, currSession.getLowestLow(), currSession.getATR(), lengthOfGracePeriod, maxRisk, maxVolatility, minProfitTarget, rangeBuffer, rangeRestriction, currSession.getTenDayHigh() - currSession.getTenDayLow(), currSession, maxBalanceRisk, this);
                        trade.setState(new LowestLowReceivedEstablishingEligibilityRange(trade, this));
                        trades.Add(trade);
                    }
                }
            }

            return base.start();
        }
        private ATRTrade context; //hides context in Trade

        public StopSellOrderOpened(ATRTrade aContext, MqlApi mql4) : base(mql4)
        {
            this.context = aContext;
        }
        private ATRTrade context; //hides context in Trade

        public BuyLimitOrderOpened(ATRTrade aContext, MqlApi mql4) : base(mql4)
        {
            this.context = aContext;
        }