// -------------- Work in Progress : TEST ------------------------// protected virtual bool FindPeaksAndOrder150() { int numOpenPositions = Math.Abs(S.TotalBuyTrades - S.TotalSellTrades); bool isFreshOrder = numOpenPositions == 0; //double percChange = OrderPlaceAlgoCode orderPlaceCode = OrderPlaceAlgoCode.ALGOLIMITREJECT; // 1. CHECK NEW TOP (Always occur for square off trade only) if (S.LastPeakType == PeakType.BOTTOM) { if (S.Ticks.PercChangeFromMax < 0 && Math.Abs(S.Ticks.PercChangeFromMax) >= S.PercChangeThreshold) { // Record tick abberations if (Math.Abs(S.Ticks.PercChangeFromMax) - S.PercChangeThreshold > 1) { Logger.LogWarningMessage( string.Format("{0}: At {1} : PriceTick non-continuous {4} times. PercThreshold = {2} S.Ticks.PercChangeFromMax = {3}.", AlgoParams.I.Symbol, S.Ticks.CurrTick.Di.QuoteTime, S.PercChangeThreshold, S.Ticks.PercChangeFromMax, S.Ticks.PercChangeFromMax / S.PercChangeThreshold)); } orderPlaceCode = AlgoTryPlaceOrder(false, S.Ticks.CurrTick); // Sell if (orderPlaceCode == OrderPlaceAlgoCode.SUCCESS) { // New TOP found S.LastPeakType = PeakType.TOP; S.Ticks.ResetMinMaxAndGetLastPeak(false, out _lastPeak); } } } // 2. CHECK NEW BOTTOM (Always occur for square off trade only) else if (S.LastPeakType == PeakType.TOP) { // Peak - New trades or pending squareoffs if (S.Ticks.PercChangeFromMin > 0 && Math.Abs(S.Ticks.PercChangeFromMin) >= S.PercChangeThreshold) { // Record tick abberations if (Math.Abs(S.Ticks.PercChangeFromMin) - S.PercChangeThreshold > 1) { Logger.LogWarningMessage( string.Format("{0}: At {1} : PriceTick non-continuous {4} times. PercThreshold = {2} S.Ticks.PercChangeFromMin = {3}.", AlgoParams.I.Symbol, S.Ticks.CurrTick.Di.QuoteTime, S.PercChangeThreshold, S.Ticks.PercChangeFromMin, S.Ticks.PercChangeFromMin / S.PercChangeThreshold)); } orderPlaceCode = AlgoTryPlaceOrder(true, S.Ticks.CurrTick); // Buy if (orderPlaceCode == OrderPlaceAlgoCode.SUCCESS) { // New BOTTTOM found S.LastPeakType = PeakType.BOTTOM; S.Ticks.ResetMinMaxAndGetLastPeak(true, out _lastPeak); } } } // 3. 1st or FRESH position PEAK (TOP/BOTTOM) (Never occurs for square off trade and always occur for fresh trade) else if (S.LastPeakType == PeakType.NONE) { bool isBuy = S.Ticks.PercChangeFromMin > 0 && Math.Abs(S.Ticks.PercChangeFromMin) >= S.PercChangeThreshold; bool isSell = S.Ticks.PercChangeFromMax < 0 && Math.Abs(S.Ticks.PercChangeFromMax) >= S.PercChangeThreshold; if (isBuy && isSell) { // May occur due to order not getting executed. But in offline analysis should not occur ideally var msg = string.Format("Error: NONE peak says both buy and sell." + "Some bug. Overcome is assume buy. max={0}, min={1}, th={2}", S.Ticks.PercChangeFromMax, S.Ticks.PercChangeFromMin, S.PercChangeThreshold); FileTracing.TraceOut(msg); isBuy = Math.Abs(S.Ticks.PercChangeFromMin) > Math.Abs(S.Ticks.PercChangeFromMax); isSell = !isBuy; } if (isBuy || isSell) { orderPlaceCode = AlgoTryPlaceOrder(isBuy, S.Ticks.CurrTick); // buy or sell if (orderPlaceCode == OrderPlaceAlgoCode.SUCCESS) { // Set last peak as reverse of current tick direction S.LastPeakType = isBuy ? PeakType.BOTTOM : PeakType.TOP; if (S.LastPeakType == PeakType.BOTTOM) { // 1st peak is BOTTOM _lastPeak = S.Ticks.MinTick; } else { // 1st peak is TOP _lastPeak = S.Ticks.MaxTick; } } } } if (orderPlaceCode == OrderPlaceAlgoCode.SUCCESS) { var tickNum = S.TotalTickCount; while (_allPeaks.ContainsKey(tickNum)) { tickNum += 1; } _allPeaks.Add(tickNum, _lastPeak); } else if (orderPlaceCode == OrderPlaceAlgoCode.RETRY) { S.IsOrderExecutionPending = true; } return(orderPlaceCode == OrderPlaceAlgoCode.SUCCESS); }
public override void AddTick(SymbolTick si) { bool marketClosing = false; if (!AlgoUpdateTick(si)) { PostProcAddTick(); return; } UpdateMinMax(); OrderPlaceAlgoCode isOrderPlaceSuccess = OrderPlaceAlgoCode.ALGORISKREJECT; // ************** 1. CHECK TOP ********************** // // detect peaks and square off trigger points if (S.LastPeakType == PeakType.BOTTOM) { bool canOrderThisAnalysis = true; // whether can palce new order on same tick where squareoff is tried // Sell Squareoff check if (AlgoParams.IsSquareOffTrigger && S.PercChangeFromMax < 0 && Math.Abs(S.PercChangeFromMax) >= AlgoParams.PercSquareOffThreshold) { int numOpenPositions = Math.Abs(S.TotalBuyTrades - S.TotalSellTrades); if (numOpenPositions != 0) { // May be add quantity also to all these calculations double profitPerc = GetSquareOffProfitPerc(); if (!AlgoParams.IsMinProfitMust || profitPerc > AlgoParams.PercMinProfit) { // SquareOff point reached canOrderThisAnalysis = AlgoTryPlaceOrder(false, S.Ticks.CurrTick) != OrderPlaceAlgoCode.SUCCESS; } } } // New sell order if (S.PercChangeFromMax < 0 && Math.Abs(S.PercChangeFromMax) >= S.PercChangeThreshold) { // Record tick abberations if (Math.Abs(S.PercChangeFromMax) - S.PercChangeThreshold > 1) { Logger.LogWarningMessage( string.Format("{3}: {0} : PriceTick non-continuous {1}: PercThreshold = {4} S.PercChangeFromMax = {2} times.", S.Ticks.CurrTick.Di.QuoteTime, _discontinuousMaxTickNum++, S.PercChangeFromMax / S.PercChangeThreshold, AlgoParams.I.Symbol, S.PercChangeThreshold)); } // New top found S.LastPeakType = PeakType.TOP; _lastPeak = S.Ticks.MaxTick; S.Ticks.MinTick = S.Ticks.CurrTick; _allPeaks.Add(S.TotalTickCount, _lastPeak); if (canOrderThisAnalysis) { isOrderPlaceSuccess = AlgoTryPlaceOrder(false, S.Ticks.CurrTick); } } } // 2. CHECK BOTTOM else if (S.LastPeakType == PeakType.TOP) { bool canOrderThisAnalysis = true; // whether can palce new order on same tick where squareoff is tried // Look for squareoff if (AlgoParams.IsSquareOffTrigger && S.PercChangeFromMin > 0 && Math.Abs(S.PercChangeFromMin) >= AlgoParams.PercSquareOffThreshold) { int numOpenPositions = Math.Abs(S.TotalBuyTrades - S.TotalSellTrades); if (numOpenPositions != 0) { double profitPerc = GetSquareOffProfitPerc(); if (!AlgoParams.IsMinProfitMust || profitPerc > AlgoParams.PercMinProfit) { // SquareOff point reached canOrderThisAnalysis = AlgoTryPlaceOrder(true, S.Ticks.CurrTick) != OrderPlaceAlgoCode.SUCCESS; } } } // New buy trade if (S.PercChangeFromMin > 0 && Math.Abs(S.PercChangeFromMin) >= S.PercChangeThreshold) { // Record tick abberations if (Math.Abs(S.PercChangeFromMin) - S.PercChangeThreshold > 1) { Logger.LogWarningMessage( string.Format("{3}: {0} : PriceTick non-continuous {1}: PercThreshold = {4} S.PercChangeFromMin = {2} times.", S.Ticks.CurrTick.Di.QuoteTime, _discontinuousMaxTickNum++, S.PercChangeFromMax / S.PercChangeThreshold, AlgoParams.I.Symbol, S.PercChangeThreshold)); } // New bottom found S.LastPeakType = PeakType.BOTTOM; _lastPeak = S.Ticks.MinTick; S.Ticks.MaxTick = S.Ticks.CurrTick; // range of maxtick set at this point _allPeaks.Add(S.TotalTickCount, _lastPeak); if (canOrderThisAnalysis) { isOrderPlaceSuccess = AlgoTryPlaceOrder(true, S.Ticks.CurrTick); } } } // 3. 1st PEAK (TOP/BOTTOM) else if (S.LastPeakType == PeakType.NONE) { // 1st peak double percDiffMinMax = 100 * ((GetPriceForAnalysis(S.Ticks.MaxTick.Di) - GetPriceForAnalysis(S.Ticks.MinTick.Di)) / GetPriceForAnalysis(S.Ticks.MinTick.Di)); // should be > than mPercChangeThreshold bool isBuy = S.PercChangeFromMin > 0 && Math.Abs(S.PercChangeFromMin) >= S.PercChangeThreshold; bool isSell = S.PercChangeFromMax < 0 && Math.Abs(S.PercChangeFromMax) >= S.PercChangeThreshold; if (isBuy && isSell) { FileTracing.TraceOut("Error , 1st peak says both buy and sell. Bug in order placing, order not getting executed. Still recover from it.."); isBuy = Math.Abs(S.PercChangeFromMin) > Math.Abs(S.PercChangeFromMax); isSell = !isBuy; } //if (Math.Abs(percDiffMinMax) >= mPercChangeThreshold) if (isBuy || isSell) { isOrderPlaceSuccess = AlgoTryPlaceOrder(isBuy, S.Ticks.CurrTick); if (isOrderPlaceSuccess == OrderPlaceAlgoCode.SUCCESS) { // Set last peak as reverse of current tick direction S.LastPeakType = isBuy ? PeakType.BOTTOM : PeakType.TOP; if (S.LastPeakType == PeakType.BOTTOM) { // 1st peak is BOTTOM _lastPeak = S.Ticks.MinTick; } else { // 1st peak is TOP _lastPeak = S.Ticks.MaxTick; } _allPeaks.Add(S.TotalTickCount, _lastPeak); } } } PostProcAddTick(); }