private void placePrototypeOncePositionClosed(Bar bar) { bool isBacktesting = this.Executor.Backtester.IsBacktestingNow; //WHATS_THE_DIFFERENCE? if (isBacktesting) return; if (bar.ParentBarsIndex == 138) { //Debugger.Break(); } if (base.HasPositionsOpenNow) { return; } if (base.HasAlertsPending) { // only kill pending entries, but leave activated SL & TP for an open position UNTOUCHED !!!! ExecutionDataSnapshot snap = this.Executor.ExecutionDataSnapshot; List <Alert> pendings = snap.AlertsPendingSafeCopy; if (pendings.Count > 0) { string msg = pendings.Count + " last AlertsPending[" + snap.AlertsPending[pendings.Count - 1] + "]"; //PrintDebug(msg); foreach (Alert alert in pendings) { int wasntFilledDuringPastNbars = bar.ParentBarsIndex - alert.PlacedBarIndex; if (wasntFilledDuringPastNbars >= 30) { //if (alert.PositionAffected.Prototype != null) {} //base.Executor.CallbackAlertKilledInvokeScript(alert); base.AlertKillPending(alert); } } } return; } double protoPlacementOffsetPct = 1; double TPpct = 2; double SLpct = -1; double SLApct = -0.8; double protoPlacement = bar.Close + bar.Close * protoPlacementOffsetPct / 100; double TP = bar.Close * TPpct / 100; double SL = bar.Close * SLpct / 100; double SLactivation = bar.Close * SLApct / 100; SLactivation = 0; // when SLactivation == 0 Prototype generates Stop alert instead of StopLoss PositionPrototype protoLong = new PositionPrototype(this.Bars.Symbol, PositionLongShort.Long, protoPlacement, TP, SL, SLactivation); PositionPrototype protoShort = new PositionPrototype(this.Bars.Symbol, PositionLongShort.Short, -protoPlacement, TP, SL, SLactivation); //PositionPrototype protoFixed = new PositionPrototype(this.Bars.Symbol, PositionLongShort.Long, 158000, +150.0, -50.0, -40.0); //PositionPrototype proto = barNewStaticArrived.Close < 158000 ? protoLong : protoShort; PositionPrototype proto = protoLong; base.Executor.PositionPrototypeActivator.PlaceOnce(proto); }
public void ConsumeQuoteOfStreamingBar(Quote quote) { //Bar barLastFormed = quoteToReach.ParentStreamingBar; ExecutionDataSnapshot snap = this.backtester.Executor.ExecutionDataSnapshot; foreach (Indicator indicator in snap.Indicators.Values) { try { indicator.OnNewStreamingQuote(quote); } catch (Exception ex) { Debugger.Break(); } } if (snap.AlertsPending.Count > 0) { var dumped = snap.DumpPendingAlertsIntoPendingHistoryByBar(); if (dumped > 0) { //string msg = "here is at least one reason why dumping on fresh quoteToReach makes sense" // + " if we never reach this breakpoint the remove dump() from here" // + " but I don't see a need to invoke it since we dumped pendings already after OnNewBarCallback"; string msg = "DUMPED_BEFORE_SCRIPT_EXECUTION_ON_NEW_BAR_OR_QUOTE"; } int pendingCountPre = this.backtester.Executor.ExecutionDataSnapshot.AlertsPending.Count; int pendingFilled = this.backtester.Executor.MarketSimStreaming.SimulatePendingFill(quote); int pendingCountNow = this.backtester.Executor.ExecutionDataSnapshot.AlertsPending.Count; if (pendingCountNow != pendingCountPre - pendingFilled) { string msg = "NOT_ONLY it looks like AnnihilateCounterparty worked out!"; } if (pendingCountNow > 0) { string msg = "pending=[" + pendingCountNow + "], it must be prototype-induced 2 closing TP & SL"; } } //this.backtester.Executor.Script.OnNewQuoteCallback(quoteToReach); ReporterPokeUnit pokeUnit = this.backtester.Executor.ExecuteOnNewBarOrNewQuote(quote); }
public void ConsumeBarLastStraticJustFormedWhileStreamingBarWithOneQuoteAlreadyAppended(Bar barLastFormed) { if (barLastFormed == null) { string msg = "Backtester starts generating quotes => first StreamingBar is added; for first four Quotes there's no static barsFormed yet!! Isi"; return; } //INVOCATION_WONT_DO_ANY_JOB this.simulatePendingFillPreExecuteEveryTick(null); ExecutionDataSnapshot snap = this.backtester.Executor.ExecutionDataSnapshot; foreach (Indicator indicator in snap.Indicators.Values) { if (indicator.NotOnChartBarsKey == null) { indicator.OnNewStaticBarFormed(barLastFormed); } else { string msg = "Generate quotes for the Non-Chart-Bars and feed them into your indicators!"; } } this.backtester.Executor.Strategy.Script.OnBarStaticLastFormedWhileStreamingBarWithOneQuoteAlreadyAppendedCallback(barLastFormed); }
public override void OnBarStaticLastFormedWhileStreamingBarWithOneQuoteAlreadyAppendedCallback(Bar barStaticFormed) { Bar barStreaming = base.Bars.BarStreaming; if (this.Executor.Backtester.IsBacktestingNow == false) { Debugger.Break(); } if (barStaticFormed.ParentBarsIndex <= 2) { return; } if (barStaticFormed.IsBarStreaming) { string msg = "SHOULD_NEVER_HAPPEN [email protected][" + barStaticFormed + "] while Streaming[" + barStreaming + "]"; Debugger.Break(); } Position lastPos = base.LastPosition; bool isLastPositionNotClosedYet = base.IsLastPositionNotClosedYet; if (isLastPositionNotClosedYet) { if (lastPos.EntryFilledBarIndex > barStaticFormed.ParentBarsIndex) { string msg1 = "prev bar you placed on streaming, now that streaming is static and positionEntry is still in the future but you have it filled?..."; Debugger.Break(); } if (lastPos.ExitAlert != null) { string msg1 = "you want to avoid POSITION_ALREADY_HAS_AN_EXIT_ALERT_REPLACE_INSTEAD_OF_ADDING_SECOND" + " ExitAtMarket by throwing [can't have two closing alerts for one positionExit] Strategy[" + this.Strategy.ToString() + "]"; Debugger.Break(); return; } if (barStaticFormed.ParentBarsIndex == 163) { Debugger.Break(); StreamingDataSnapshot streaming = this.Executor.DataSource.StreamingProvider.StreamingDataSnapshot; Quote lastQuote = streaming.LastQuoteGetForSymbol(barStaticFormed.Symbol); double priceForMarketOrder = streaming.LastQuoteGetPriceForMarketOrder(barStaticFormed.Symbol); } string msg = "ExitAtMarket@" + barStaticFormed.ParentBarsIdent; Alert exitPlaced = ExitAtMarket(barStreaming, lastPos, msg); log("Execute(): " + msg); } ExecutionDataSnapshot snap = base.Executor.ExecutionDataSnapshot; int alertsPendingCount = snap.AlertsPending.Count; int positionsOpenNowCount = snap.PositionsOpenNow.Count; bool hasAlertsPendingOrPositionsOpenNow = base.HasAlertsPendingOrPositionsOpenNow; if (hasAlertsPendingOrPositionsOpenNow) { if (alertsPendingCount > 0) { if (snap.AlertsPending[0] == lastPos.EntryAlert) { string msg = "EXPECTED: I don't have open positions but I have an unfilled alert from lastPosition.EntryAlert=alertsPending[0]"; } else if (snap.AlertsPending[0] == lastPos.ExitAlert) { string msg = "EXPECTED: I have and open lastPosition with .ExitAlert=alertsPending[0]"; } else { string msg = "UNEXPECTED: pending alert doesn't relate to lastPosition; who is here?"; } } if (positionsOpenNowCount > 1) { string msg = "EXPECTED: I got multiple positions[" + positionsOpenNowCount + "]"; if (snap.PositionsOpenNow[0] == lastPos) { msg += "50/50: positionsMaster.Last = positionsOpenNow.First"; } } return; } if (barStaticFormed.ParentBarsIndex == 30) { Debugger.Break(); StreamingDataSnapshot streaming = this.Executor.DataSource.StreamingProvider.StreamingDataSnapshot; Quote lastQuote = streaming.LastQuoteGetForSymbol(barStaticFormed.Symbol); double priceForMarketOrder = streaming.LastQuoteGetPriceForMarketOrder(barStaticFormed.Symbol); } if (barStaticFormed.Close > barStaticFormed.Open) { string msg = "BuyAtMarket@" + barStaticFormed.ParentBarsIdent; Position buyPlaced = BuyAtMarket(barStreaming, msg); //Debugger.Break(); } else { string msg = "ShortAtMarket@" + barStaticFormed.ParentBarsIdent; Position shortPlaced = ShortAtMarket(barStreaming, msg); //Debugger.Break(); } //base.Executor.ChartShadow.LineDrawModify(...); }