protected override void OnBarUpdate() { if (CurrentBar < BarsRequiredToTrade) { return; } GuerillaStrategyItem activeItem = guerillaStrategy.GetActiveItem(Time[0]); bool runActiveItem = activeItem != null && activeItem.TickInterval == BarsArray[BarsInProgress].BarsPeriod.BaseBarsPeriodValue; bool runPositionItem = positionStrategyItem != null && positionStrategyItem.TickInterval == BarsArray[BarsInProgress].BarsPeriod.BaseBarsPeriodValue; //Active only -> BAU if (runActiveItem && positionStrategyItem == null) { FillSignals(activeItem); OrderStuff(activeItem, true); } //Active == position -> BAU else if (runActiveItem && runPositionItem && activeItem.ToString() == positionStrategyItem.ToString()) { FillSignals(activeItem); OrderStuff(activeItem, true); } //Active != position -> position to close else if (runActiveItem && runPositionItem && activeItem.ToString() != positionStrategyItem.ToString()) { FillSignals(positionStrategyItem); OrderStuff(positionStrategyItem, false); } //Position only -> position to close else if (runPositionItem) { FillSignals(positionStrategyItem); OrderStuff(positionStrategyItem, false); } }
private void RecordTradeProps(Execution execution) { if (execution.Order != null && execution.Order.OrderState == OrderState.Filled) { if ((execution.Order.Name == ENTER_LONG || execution.Order.Name == ENTER_SHORT) && this.Position.Quantity >= this.Quantity) { this.guerillaTraderBridge.Reset(); this.guerillaTraderBridge.ot_entryTimestamp = execution.Order.Time; this.guerillaTraderBridge.ot_tickRange = 0; this.guerillaTraderBridge.ot_entryPrice = execution.Order.AverageFillPrice; this.guerillaTraderBridge.ot_size = execution.Order.Quantity; this.guerillaTraderBridge.ot_tradeType = execution.Order.Name == ENTER_LONG ? (int)TradeTypes.LongFuture : (int)TradeTypes.ShortFuture; this.guerillaTraderBridge.ot_trigger = (int)TradeTriggers.Crossover; this.guerillaTraderBridge.ot_trend = execution.Order.Name == ENTER_LONG ? (int)TrendTypes.Bullish : (int)TrendTypes.Bearish; this.guerillaTraderBridge.ot_diff = 0; this.guerillaTraderBridge.ot_diffXX = 0; this.guerillaTraderBridge.ot_diffXDiff = 0; this.guerillaTraderBridge.ot_diffXSlope = 0; this.guerillaTraderBridge.ot_diffXChange = 0; this.guerillaTraderBridge.ot_adx = 0; this.guerillaTraderBridge.ot_adxSlope = 0; this.guerillaTraderBridge.ot_extraData1 = this.openingPositionStrategyItem.BearishSetupsString; this.guerillaTraderBridge.ot_extraData2 = this.openingPositionStrategyItem.BullishSetupsString; if (this.openingPositionStrategyItem.InitialStopLoss > 0) { if (execution.Order.Name == ENTER_LONG) { if (this.openingPositionStrategyItem.Contrarian) { this.openingPositionStrategyItem.InitialStopLoss = this.guerillaTraderBridge.ot_entryPrice - (this.openingPositionStrategyItem.InitialStopLoss - this.guerillaTraderBridge.ot_entryPrice); } double currentBid = GetCurrentBid(); if (this.openingPositionStrategyItem.InitialStopLoss >= currentBid) { this.openingPositionStrategyItem.InitialStopLoss = currentBid - TickSize; } ExitLongStopMarket(0, true, this.Position.Quantity, this.openingPositionStrategyItem.InitialStopLoss, EXIT, ENTER_LONG); } else { if (this.openingPositionStrategyItem.Contrarian) { this.openingPositionStrategyItem.InitialStopLoss = this.guerillaTraderBridge.ot_entryPrice + (this.guerillaTraderBridge.ot_entryPrice - this.openingPositionStrategyItem.InitialStopLoss); } double currentAsk = GetCurrentAsk(); if (this.openingPositionStrategyItem.InitialStopLoss <= currentAsk) { this.openingPositionStrategyItem.InitialStopLoss = currentAsk + TickSize; } ExitShortStopMarket(0, true, this.Position.Quantity, this.openingPositionStrategyItem.InitialStopLoss, EXIT, ENTER_SHORT); } this.openingPositionStrategyItem.InitialStopLoss = 0; } this.openingPosition = false; this.positionStrategyItem = openingPositionStrategyItem; this.openingPositionStrategyItem = null; } else if (this.Position.MarketPosition == MarketPosition.Flat) { this.guerillaTraderBridge.ot_exitTimestamp = execution.Order.Time; this.guerillaTraderBridge.ot_exitPrice = execution.Order.AverageFillPrice; if (execution.Order.Instrument.MasterInstrument.Name == "ES") { this.guerillaTraderBridge.ot_marketId = 1; this.guerillaTraderBridge.ot_commissions = 4.04 * Quantity; } else if (execution.Order.Instrument.MasterInstrument.Name == "NQ") { this.guerillaTraderBridge.ot_marketId = 4; this.guerillaTraderBridge.ot_commissions = 4.04 * Quantity; } else if (execution.Order.Instrument.MasterInstrument.Name == "6E") { this.guerillaTraderBridge.ot_marketId = 7; this.guerillaTraderBridge.ot_commissions = 5.32 * Quantity; } else if (execution.Order.Instrument.MasterInstrument.Name == "GC") { this.guerillaTraderBridge.ot_marketId = 33; this.guerillaTraderBridge.ot_commissions = 2.34 * Quantity; } else if (execution.Order.Instrument.MasterInstrument.Name == "UB") { this.guerillaTraderBridge.ot_marketId = 14; this.guerillaTraderBridge.ot_commissions = 1.69 * Quantity; } if (this.guerillaTraderBridge.ot_tradeType == (int)TradeTypes.LongFuture) { this.guerillaTraderBridge.ot_profitLoss = ((this.guerillaTraderBridge.ot_exitPrice - this.guerillaTraderBridge.ot_entryPrice) * execution.Order.Instrument.MasterInstrument.PointValue) * this.guerillaTraderBridge.ot_size; } else if (this.guerillaTraderBridge.ot_tradeType == (int)TradeTypes.ShortFuture) { this.guerillaTraderBridge.ot_profitLoss = ((this.guerillaTraderBridge.ot_entryPrice - this.guerillaTraderBridge.ot_exitPrice) * execution.Order.Instrument.MasterInstrument.PointValue) * this.guerillaTraderBridge.ot_size; } this.guerillaTraderBridge.ot_adjProfitLoss = this.guerillaTraderBridge.ot_profitLoss - this.guerillaTraderBridge.ot_commissions; this.guerillaTraderBridge.ot_profitLossPerContract = this.guerillaTraderBridge.ot_adjProfitLoss / this.guerillaTraderBridge.ot_size; if (State == State.Realtime && this.TradingAccountId > 0) { Bridge.SaveTradeFromNt(this.guerillaTraderBridge.ot_marketId, this.guerillaTraderBridge.ot_tradeType, this.guerillaTraderBridge.ot_trigger, this.guerillaTraderBridge.ot_trend, this.guerillaTraderBridge.ot_diff, this.guerillaTraderBridge.ot_diffXX, this.guerillaTraderBridge.ot_diffXDiff, this.guerillaTraderBridge.ot_diffXSlope, this.guerillaTraderBridge.ot_diffXChange, this.guerillaTraderBridge.ot_tickRange, this.guerillaTraderBridge.ot_entryTimestamp, this.guerillaTraderBridge.ot_entryPrice, this.guerillaTraderBridge.ot_exitTimestamp, this.guerillaTraderBridge.ot_exitPrice, this.guerillaTraderBridge.ot_commissions, this.guerillaTraderBridge.ot_profitLoss, this.guerillaTraderBridge.ot_adjProfitLoss, this.guerillaTraderBridge.ot_size, this.guerillaTraderBridge.ot_profitLossPerContract, this.TradingAccountId, this.guerillaTraderBridge.ot_adx, 0, false, true, false, this.positionStrategyItem.Contrarian, this.guerillaTraderBridge.ot_adxSlope, 0, this.guerillaTraderBridge.ot_extraData1, this.guerillaTraderBridge.ot_extraData2, this.positionStrategyItem.ToString()); } if (execution.Order.IsStopMarket) { if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ExitStop", positionStrategyItem); } } this.closingPosition = false; this.positionStrategyItem = null; } } }
private bool OrderStuff(GuerillaStrategyItem strategyItem, bool canOpenPosition) { if (strategyItem == null) { return(false); } bool orderSent = false; if (!strategyItem.Contrarian) { if (this.Position.MarketPosition == MarketPosition.Flat && !openingPosition && canOpenPosition && strategyItem.BearishEntrySignal) { openingPosition = true; openingPositionStrategyItem = strategyItem; EnterShort(0, Quantity, ENTER_SHORT); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "EnterShort", strategyItem); } } else if (this.Position.MarketPosition == MarketPosition.Flat && !openingPosition && canOpenPosition && strategyItem.BullishEntrySignal) { openingPosition = true; openingPositionStrategyItem = strategyItem; EnterLong(0, Quantity, ENTER_LONG); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "EnterLong", strategyItem); } } else if (this.Position.MarketPosition == MarketPosition.Long && strategyItem.BullishExitSignal) { if (strategyItem.BearishEntrySignal && canOpenPosition) { openingPosition = true; openingPositionStrategyItem = strategyItem; closingPosition = true; EnterShort(0, this.Position.Quantity, ENTER_SHORT); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ReverseToShort", strategyItem); } } else { closingPosition = true; ExitLong(0, this.Position.Quantity, EXIT, ENTER_LONG); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ExitLong", strategyItem); } } } else if (this.Position.MarketPosition == MarketPosition.Short && strategyItem.BearishExitSignal) { if (strategyItem.BullishEntrySignal && canOpenPosition) { openingPosition = true; openingPositionStrategyItem = strategyItem; closingPosition = true; EnterLong(0, this.Position.Quantity, ENTER_LONG); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ReverseToLong", strategyItem); } } else { closingPosition = true; ExitShort(0, this.Position.Quantity, EXIT, ENTER_SHORT); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ExitShort", strategyItem); } } } } else { if (this.Position.MarketPosition == MarketPosition.Flat && !openingPosition && canOpenPosition && strategyItem.BullishEntrySignal) { openingPosition = true; openingPositionStrategyItem = strategyItem; EnterShort(0, this.Quantity, ENTER_SHORT); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "EnterShort", strategyItem); } } else if (this.Position.MarketPosition == MarketPosition.Flat && !openingPosition && canOpenPosition && strategyItem.BearishEntrySignal) { openingPosition = true; openingPositionStrategyItem = strategyItem; EnterLong(0, this.Quantity, ENTER_LONG); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "EnterLong", strategyItem); } } else if (this.Position.MarketPosition == MarketPosition.Long && strategyItem.BearishExitSignal) { if (strategyItem.BullishEntrySignal && canOpenPosition) { openingPosition = true; openingPositionStrategyItem = strategyItem; closingPosition = true; EnterShort(0, this.Position.Quantity, ENTER_SHORT); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ReverseToShort", strategyItem); } } else { closingPosition = true; ExitLong(0, this.Position.Quantity, EXIT, ENTER_LONG); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ExitLong", strategyItem); } } } else if (this.Position.MarketPosition == MarketPosition.Short && strategyItem.BullishExitSignal) { if (strategyItem.BearishEntrySignal && canOpenPosition) { openingPosition = true; openingPositionStrategyItem = strategyItem; closingPosition = true; EnterLong(0, this.Position.Quantity, ENTER_LONG); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ReverseToLong", strategyItem); } } else { closingPosition = true; ExitShort(0, this.Position.Quantity, EXIT, ENTER_SHORT); if (this.LogTrades) { PrintValues(Time[0].ToString("g"), "ExitShort", strategyItem); } } } } return(orderSent); }
private void FillSignals(GuerillaStrategyItem strategyItem) { if (strategyItem == null) { return; } #region Misc int previousTrendLookback = 4; int previousTrendBarLookback = 4; int previousRequiredTrendBars = 2; int requiredTrendBars = 1; int jumpInTrendPeriod = 5; int indecisionPeriod = 5; bool previousIndecision = this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 2, indecisionPeriod, x => x == 0) && this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 2, indecisionPeriod, x => x == 0); #endregion #region Bearish Signals List <bool> bearishEntrySetups = new List <bool>(); bool overbought = strategyItem.StochasticsFilter ? this.GuerillaCountIf(stochasticsList[BarsInProgress], x => x >= 70, 0, 4, x => x > 0) : true; #region Shooting star reversal bearishEntrySetups.Add(this.GuerillaCountIf(greenBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) && this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) && shootingStarIndicatorList[BarsInProgress][1] == 1 && bearishTrendBarIndicatorList[BarsInProgress][0] == 1 && overbought); if (bearishEntrySetups.Last()) { strategyItem.InitialStopLoss = High[1] + TickSize; } #endregion #region Doji entries if (!bearishEntrySetups.Any(x => x)) { bearishEntrySetups.Add(this.GuerillaCountIf(dojiIndicatorList[BarsInProgress], x => x == 1, 2, 2, x => x >= 1) && this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x >= 1) && overbought); if (bearishEntrySetups.Last()) { int dojiIndex = dojiIndicatorList[BarsInProgress][2] == 1 ? 2 : 3; strategyItem.InitialStopLoss = High[dojiIndex] + TickSize; } } else { bearishEntrySetups.Add(false); } if (!bearishEntrySetups.Any(x => x)) { bearishEntrySetups.Add(dojiIndicatorList[BarsInProgress][1] == 1 && bearishTrendBarIndicatorList[BarsInProgress][0] == 1 && overbought); if (bearishEntrySetups.Last()) { strategyItem.InitialStopLoss = High[1] + TickSize; } } else { bearishEntrySetups.Add(false); } #endregion #region Trend bar reversal if (!bearishEntrySetups.Any(x => x)) { bearishEntrySetups.Add(this.GuerillaCountIf(greenBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) && this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) && this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x == requiredTrendBars) && overbought); if (bearishEntrySetups.Last()) { strategyItem.InitialStopLoss = High[2] + TickSize; } } else { bearishEntrySetups.Add(false); } #endregion #region Indecision breakout if (!bearishEntrySetups.Any(x => x)) { bearishEntrySetups.Add(previousIndecision && this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x == requiredTrendBars)); if (bearishEntrySetups.Last()) { double high = 0; for (int i = 0; i < indecisionPeriod; i++) { if (High[i + 2] > high) { high = High[i + 2]; } } strategyItem.InitialStopLoss = high + TickSize; } } else { bearishEntrySetups.Add(false); } #endregion #region Jump in trend if (!bearishEntrySetups.Any(x => x) && !strategyItem.Contrarian) { bearishEntrySetups.Add(this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, jumpInTrendPeriod, x => x == jumpInTrendPeriod)); if (bearishEntrySetups.Last()) { strategyItem.InitialStopLoss = MAX(High, jumpInTrendPeriod)[0]; } } else { bearishEntrySetups.Add(false); } #endregion strategyItem.BearishSetupsString = String.Join(",", bearishEntrySetups.Select(x => x.ToString().ToLower())); strategyItem.BearishEntrySignal = bearishEntrySetups.Any(x => x); #endregion #region Bullish Signals List <bool> bullishEntrySetups = new List <bool>(); bool oversold = strategyItem.StochasticsFilter ? this.GuerillaCountIf(stochasticsList[BarsInProgress], x => x <= 30, 0, 4, x => x > 0) : true; #region Shooting star reversal bullishEntrySetups.Add(this.GuerillaCountIf(redBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) && this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) && hammerIndicatorList[BarsInProgress][1] == 1 && bullishTrendBarIndicatorList[BarsInProgress][0] == 1 && oversold); if (bullishEntrySetups.Last()) { strategyItem.InitialStopLoss = Low[1] - TickSize; } #endregion #region Doji entries if (!bullishEntrySetups.Any(x => x)) { bullishEntrySetups.Add(this.GuerillaCountIf(dojiIndicatorList[BarsInProgress], x => x == 1, 2, 2, x => x >= 1) && this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x >= 1) && oversold); if (bullishEntrySetups.Last()) { int dojiIndex = dojiIndicatorList[BarsInProgress][2] == 1 ? 2 : 3; strategyItem.InitialStopLoss = Low[dojiIndex] - TickSize; } } else { bullishEntrySetups.Add(false); } if (!bullishEntrySetups.Any(x => x)) { bullishEntrySetups.Add(dojiIndicatorList[BarsInProgress][1] == 1 && bullishTrendBarIndicatorList[BarsInProgress][0] == 1 && oversold); if (bullishEntrySetups.Last()) { strategyItem.InitialStopLoss = Low[1] - TickSize; } } else { bullishEntrySetups.Add(false); } #endregion #region Trend bar reversal if (!bullishEntrySetups.Any(x => x)) { bullishEntrySetups.Add(this.GuerillaCountIf(redBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) && this.GuerillaCountIf(bearishTrendBarIndicatorList[BarsInProgress], x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) && this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x == requiredTrendBars) && oversold); if (bullishEntrySetups.Last()) { strategyItem.InitialStopLoss = Low[2] - TickSize; } } else { bullishEntrySetups.Add(false); } #endregion #region Indecision breakout if (!bullishEntrySetups.Any(x => x)) { bullishEntrySetups.Add(previousIndecision && this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x == requiredTrendBars)); if (bearishEntrySetups.Last()) { double low = 0; for (int i = 0; i < indecisionPeriod; i++) { if (High[i + 2] > low) { low = Low[i + 2]; } } strategyItem.InitialStopLoss = low - TickSize; } } else { bullishEntrySetups.Add(false); } #endregion #region Jump in trend if (!bullishEntrySetups.Any(x => x) && !strategyItem.Contrarian) { bullishEntrySetups.Add(this.GuerillaCountIf(bullishTrendBarIndicatorList[BarsInProgress], x => x == 1, 0, jumpInTrendPeriod, x => x == jumpInTrendPeriod)); if (bullishEntrySetups.Last()) { strategyItem.InitialStopLoss = MIN(Low, jumpInTrendPeriod)[0]; } } else { bullishEntrySetups.Add(false); } #endregion strategyItem.BullishSetupsString = String.Join(",", bullishEntrySetups.Select(x => x.ToString().ToLower())); strategyItem.BullishEntrySignal = bullishEntrySetups.Any(x => x); #endregion #region Exit Signals strategyItem.BullishExitSignal = this.GuerillaCountIf(indecisionBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x == 2) || dojiIndicatorList[BarsInProgress][0] == 1 || bearishTrendBarIndicatorList[BarsInProgress][0] == 1 || strategyItem.BearishEntrySignal; strategyItem.BearishExitSignal = this.GuerillaCountIf(indecisionBarIndicatorList[BarsInProgress], x => x == 1, 0, 2, x => x == 2) || dojiIndicatorList[BarsInProgress][0] == 1 || bullishTrendBarIndicatorList[BarsInProgress][0] == 1 || strategyItem.BullishEntrySignal; #endregion }