public Alert ExitAlertCreate(Bar exitBar, Position position, double stopOrLimitPrice, string signalName, Direction direction, MarketLimitStop exitMarketLimitStop) { this.checkThrowEntryBarIsValid(exitBar); this.checkThrowPositionToCloseIsValid(position); double priceScriptOrStreaming = stopOrLimitPrice; OrderSpreadSide orderSpreadSide = OrderSpreadSide.Unknown; if (exitMarketLimitStop == MarketLimitStop.Market) { priceScriptOrStreaming = this.getStreamingPriceForMarketOrder(exitMarketLimitStop, direction, out orderSpreadSide); } PositionLongShort longShortFromDirection = MarketConverter.LongShortFromDirection(direction); double exitPriceScript = exitBar.ParentBars.SymbolInfo.RoundAlertPriceToPriceLevel( priceScriptOrStreaming, true, longShortFromDirection, exitMarketLimitStop); Alert alert = new Alert(exitBar, position.Shares, exitPriceScript, signalName, direction, exitMarketLimitStop, orderSpreadSide, //this.executor.Script, this.executor.Strategy); alert.AbsorbFromExecutor(executor); alert.PositionAffected = position; // moved to CallbackAlertFilled - we can exit by TP or SL - and position has no clue which Alert was filled!!! //position.ExitCopyFromAlert(alert); alert.PositionAffected.ExitAlertAttach(alert); return(alert); }
private double getStreamingPriceForMarketOrder(MarketLimitStop entryMarketLimitStop, Direction direction, out OrderSpreadSide priceSpreadSide) { double priceForMarketAlert = -1; priceSpreadSide = OrderSpreadSide.Unknown; switch (entryMarketLimitStop) { case MarketLimitStop.Market: priceForMarketAlert = this.executor.DataSource.StreamingProvider.StreamingDataSnapshot .GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming( this.executor.Bars.Symbol, direction, out priceSpreadSide, false); break; case MarketLimitStop.AtClose: string msg = "[" + direction + "]At[" + entryMarketLimitStop + "]" + " when LastBar[" + (this.executor.Bars.Count - 1) + "]; No way I can bring you a future price," + " even by executing your order right now" + "; can't do inequivalent repacement to LastBar.Close"; throw new Exception(msg); //break; case MarketLimitStop.Stop: case MarketLimitStop.Limit: case MarketLimitStop.StopLimit: string msg2 = "STREAMING_BID_ASK_IS_IRRELEVANT_FOR_STOP_OR_LIMIT_ALERT [" + direction + "]At[" + entryMarketLimitStop + "]" + " when LastBar[" + (this.executor.Bars.Count - 1) + "]"; throw new Exception(msg2); default: throw new Exception("no handler for MarketLimitStop.[" + entryMarketLimitStop + "]"); } return(priceForMarketAlert); }
public Alert EntryAlertCreate(Bar entryBar, double stopOrLimitPrice, string entrySignalName, Direction direction, MarketLimitStop entryMarketLimitStop) { this.checkThrowEntryBarIsValid(entryBar); double priceScriptOrStreaming = stopOrLimitPrice; OrderSpreadSide orderSpreadSide = OrderSpreadSide.Unknown; if (entryMarketLimitStop == MarketLimitStop.Market) { priceScriptOrStreaming = this.getStreamingPriceForMarketOrder(entryMarketLimitStop, direction, out orderSpreadSide); } PositionLongShort longShortFromDirection = MarketConverter.LongShortFromDirection(direction); // ALREADY_ALIGNED_AFTER GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming double entryPriceScript = entryBar.ParentBars.SymbolInfo.RoundAlertPriceToPriceLevel( priceScriptOrStreaming, true, longShortFromDirection, entryMarketLimitStop); double shares = this.executor.PositionSizeCalculate(entryBar, entryPriceScript); Alert alert = new Alert(entryBar, shares, entryPriceScript, entrySignalName, direction, entryMarketLimitStop, orderSpreadSide, //this.executor.Script, this.executor.Strategy); alert.AbsorbFromExecutor(executor); return(alert); }
public Alert(Bar bar, double qty, double priceScript, string signalName, Direction direction, MarketLimitStop marketLimitStop, OrderSpreadSide orderSpreadSide, Strategy strategy) : this() { if (direction == Direction.Unknown) { string msg = "ALERT_CTOR_DIRECTION_MUST_NOT_BE_UNKNOWN: when creating an Alert, direction parameter can't be null"; throw new Exception(msg); } if (bar == null) { string msg = "ALERT_CTOR_BAR_MUST_NOT_BE_NULL: when creating an Alert, bar parameter can't be null"; throw new Exception(msg); } if (bar.ParentBars == null) { string msg = "ALERT_CTOR_PARENT_BARS_MUST_NOT_BE_NULL: when creating an Alert, bar.ParentBars can't be null"; throw new Exception(msg); } this.Bars = bar.ParentBars; this.PlacedBar = bar; this.PlacedBarIndex = bar.ParentBarsIndex; this.Symbol = bar.Symbol; this.BarsScaleInterval = this.Bars.ScaleInterval; if (this.Bars.SymbolInfo != null) { SymbolInfo symbolInfo = this.Bars.SymbolInfo; this.SymbolClass = (string.IsNullOrEmpty(symbolInfo.SymbolClass) == false) ? symbolInfo.SymbolClass : "UNKNOWN_CLASS"; this.MarketOrderAs = symbolInfo.MarketOrderAs; } this.AccountNumber = "UNKNOWN_ACCOUNT"; if (this.DataSource.BrokerProvider != null && this.DataSource.BrokerProvider.AccountAutoPropagate != null && string.IsNullOrEmpty(this.Bars.DataSource.BrokerProvider.AccountAutoPropagate.AccountNumber) != false) { this.AccountNumber = this.Bars.DataSource.BrokerProvider.AccountAutoPropagate.AccountNumber; } this.Qty = qty; this.PriceScript = priceScript; this.SignalName = signalName; this.Direction = direction; this.MarketLimitStop = marketLimitStop; this.OrderSpreadSide = orderSpreadSide; this.Strategy = strategy; if (this.Strategy != null) { this.StrategyID = this.Strategy.Guid; this.StrategyName = this.Strategy.Name; } if (this.Strategy.Script != null) { string msg = "Looks like a manual Order submitted from the Chart"; return; } }
public Order(Alert alert, bool fromAutoTrading, bool forceOverwriteAlertOrderFollowedToNewlyCreatedOrder = false) : this() { if (alert == null) { string msg = "Order(): alert=null (serializer will get upset) for " + this.ToString(); throw new Exception(msg); } if (alert.OrderFollowed != null && forceOverwriteAlertOrderFollowedToNewlyCreatedOrder == false) { string msg = "I refuse to create one more order for an alert.OrderFollowed!=null; alert[" + alert + "] alert.OrderFollowed[" + alert.OrderFollowed + "]"; alert.OrderFollowed.AppendMessage(msg); throw new Exception(msg); } this.PriceRequested = alert.PriceScript; this.QtyRequested = alert.Qty; this.FromAutoTrading = fromAutoTrading; //this.TimeCreatedServer = alert.TimeCreatedLocal; // due to serverTime lagging, replacements orders are born before the original order... this.TimeCreatedBroker = alert.Bars.MarketInfo.ConvertLocalTimeToServer(DateTime.Now); //this.PositionFollowed = new Position() when order.State becomes OrderState.Filled; this.SpreadSide = alert.OrderSpreadSide; //this.ExtendedOrderType = alert.ExtendedOrderType; // Bid/Ask Dictionaries are synchronized => no exceptions if (alert.DataSource != null && alert.DataSource.StreamingProvider != null) { this.CurrentBid = alert.DataSource.StreamingProvider.StreamingDataSnapshot.BestBidGetForMarketOrder(alert.Symbol); this.CurrentAsk = alert.DataSource.StreamingProvider.StreamingDataSnapshot.BestAskGetForMarketOrder(alert.Symbol); } this.Alert = alert; alert.OrderFollowed = this; //MOVED_TO OrderProcessor::CreatePropagateOrderFromAlert() for {if (alert.IsExitAlert) alert.PositionAffected.EntryAlert.OrderFollowed.DerivedOrdersAdd(newborn);} // if (alert.PositionAffected != null && alert.PositionAffected.EntryAlert != null && alert.PositionAffected.EntryAlert.OrderFollowed != null) { // alert.PositionAffected.EntryAlert.OrderFollowed.DerivedOrdersAdd(this); // // TODO will also have to notify ExecutionForm on this Order, which will close a Position // } alert.MreOrderFollowedIsNotNull.Set(); // Order is fully constructed, all properties assigned, go read them }
public Order() // called by Json.Deserialize(); what if I'll make it protected? { this.GUID = newGUID(); this.messages = new ConcurrentQueue <OrderStateMessage>(); this.ExtendedOrderType = ""; this.PriceRequested = 0; this.PriceFill = 0; this.QtyRequested = 0; this.QtyFill = 0; //this.PositionFollowed = null; //this.AccountPositionFollowed = null; this.State = OrderState.Unknown; this.SernoSession = 0; //QUIK this.SernoExchange = 0; //QUIK this.IsReplacement = false; this.ReplacementForGUID = ""; this.ReplacedByGUID = ""; this.IsKiller = false; this.VictimGUID = ""; this.KillerGUID = ""; this.StateImageIndex = 0; this.StateUpdateLastTimeLocal = DateTime.MinValue; this.FromAutoTrading = false; this.SlippageFill = 0; this.SlippageIndex = 0; this.CurrentAsk = 0; this.CurrentBid = 0; this.SpreadSide = OrderSpreadSide.Unknown; this.MreActiveCanCome = new ManualResetEvent(false); this.DerivedOrders = new List <Order>(); this.DerivedOrdersGuids = new List <string>(); }
private double getStreamingPriceForMarketOrder(MarketLimitStop entryMarketLimitStop, Direction direction, out OrderSpreadSide priceSpreadSide) { double priceForMarketAlert = -1; priceSpreadSide = OrderSpreadSide.Unknown; switch (entryMarketLimitStop) { case MarketLimitStop.Market: priceForMarketAlert = this.executor.DataSource.StreamingProvider.StreamingDataSnapshot .GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming( this.executor.Bars.Symbol, direction, out priceSpreadSide, false); break; case MarketLimitStop.AtClose: string msg = "[" + direction + "]At[" + entryMarketLimitStop + "]" + " when LastBar[" + (this.executor.Bars.Count - 1) + "]; No way I can bring you a future price," + " even by executing your order right now" + "; can't do inequivalent repacement to LastBar.Close"; throw new Exception(msg); //break; case MarketLimitStop.Stop: case MarketLimitStop.Limit: case MarketLimitStop.StopLimit: string msg2 = "STREAMING_BID_ASK_IS_IRRELEVANT_FOR_STOP_OR_LIMIT_ALERT [" + direction + "]At[" + entryMarketLimitStop + "]" + " when LastBar[" + (this.executor.Bars.Count - 1) + "]"; throw new Exception(msg2); default: throw new Exception("no handler for MarketLimitStop.[" + entryMarketLimitStop + "]"); } return priceForMarketAlert; }
public virtual double GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming(string symbol, Direction direction , out OrderSpreadSide oss, bool forceCrossMarket) { double priceLastQuote = this.LastQuoteGetPriceForMarketOrder(symbol); if (priceLastQuote == 0) { string msg = "QuickCheck ZERO priceLastQuote=" + priceLastQuote + " for Symbol=[" + symbol + "]" + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot"; Assembler.PopupException(msg); //throw new Exception(msg); } double currentBid = this.BestBidGetForMarketOrder(symbol); double currentAsk = this.BestAskGetForMarketOrder(symbol); if (currentBid == 0) { string msg = "ZERO currentBid=" + currentBid + " for Symbol=[" + symbol + "]" + " while priceLastQuote=[" + priceLastQuote + "]" + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot"; ; Assembler.PopupException(msg); //throw new Exception(msg); } if (currentAsk == 0) { string msg = "ZERO currentAsk=" + currentAsk + " for Symbol=[" + symbol + "]" + " while priceLastQuote=[" + priceLastQuote + "]" + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot"; Assembler.PopupException(msg); //throw new Exception(msg); } double price = 0; oss = OrderSpreadSide.ERROR; SymbolInfo symbolInfo = Assembler.InstanceInitialized.RepositoryCustomSymbolInfo.FindSymbolInfo(symbol); MarketOrderAs spreadSide; if (forceCrossMarket) { spreadSide = MarketOrderAs.LimitCrossMarket; } else { spreadSide = (symbolInfo == null) ? MarketOrderAs.LimitCrossMarket : symbolInfo.MarketOrderAs; } if (spreadSide == MarketOrderAs.ERROR || spreadSide == MarketOrderAs.Unknown) { string msg = "Set Symbol[" + symbol + "].SymbolInfo.LimitCrossMarket; should not be spreadSide[" + spreadSide + "]"; Assembler.PopupException(msg); throw new Exception(msg); //return; } switch (direction) { case Direction.Buy: case Direction.Cover: switch (spreadSide) { case MarketOrderAs.LimitTidal: oss = OrderSpreadSide.AskTidal; price = currentAsk; break; case MarketOrderAs.LimitCrossMarket: oss = OrderSpreadSide.BidCrossed; price = currentBid; // Unknown (Order default) becomes CrossMarket break; case MarketOrderAs.MarketMinMaxSentToBroker: oss = OrderSpreadSide.MaxPrice; price = currentAsk; break; case MarketOrderAs.MarketZeroSentToBroker: oss = OrderSpreadSide.MarketPrice; price = currentAsk; // looks like default, must be crossmarket to fill it right now break; default: string msg2 = "no handler for spreadSide[" + spreadSide + "] direction[" + direction + "]"; throw new Exception(msg2); } break; case Direction.Short: case Direction.Sell: switch (spreadSide) { case MarketOrderAs.LimitTidal: oss = OrderSpreadSide.BidTidal; price = currentBid; break; case MarketOrderAs.LimitCrossMarket: oss = OrderSpreadSide.AskCrossed; price = currentAsk; // Unknown (Order default) becomes CrossMarket break; case MarketOrderAs.MarketMinMaxSentToBroker: oss = OrderSpreadSide.MinPrice; price = currentBid; // Unknown (Order default) becomes CrossMarket break; case MarketOrderAs.MarketZeroSentToBroker: oss = OrderSpreadSide.MarketPrice; price = currentBid; // looks like default, must be crossmarket to fill it right now break; default: string msg2 = "no handler for spreadSide[" + spreadSide + "] direction[" + direction + "]"; throw new Exception(msg2); } break; default: string msg = "no handler for direction[" + direction + "]"; throw new Exception(msg); } if (double.IsNaN(price)) { Debugger.Break(); } symbolInfo = Assembler.InstanceInitialized.RepositoryCustomSymbolInfo.FindSymbolInfoOrNew(symbol); price = symbolInfo.AlignOrderPriceToPriceLevel(price, direction, MarketLimitStop.Market); return(price); }
public virtual double GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming(string symbol, Direction direction , out OrderSpreadSide oss, bool forceCrossMarket) { double priceLastQuote = this.LastQuoteGetPriceForMarketOrder(symbol); if (priceLastQuote == 0) { string msg = "QuickCheck ZERO priceLastQuote=" + priceLastQuote + " for Symbol=[" + symbol + "]" + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot"; Assembler.PopupException(msg); //throw new Exception(msg); } double currentBid = this.BestBidGetForMarketOrder(symbol); double currentAsk = this.BestAskGetForMarketOrder(symbol); if (currentBid == 0) { string msg = "ZERO currentBid=" + currentBid + " for Symbol=[" + symbol + "]" + " while priceLastQuote=[" + priceLastQuote + "]" + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot"; ; Assembler.PopupException(msg); //throw new Exception(msg); } if (currentAsk == 0) { string msg = "ZERO currentAsk=" + currentAsk + " for Symbol=[" + symbol + "]" + " while priceLastQuote=[" + priceLastQuote + "]" + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot"; Assembler.PopupException(msg); //throw new Exception(msg); } double price = 0; oss = OrderSpreadSide.ERROR; SymbolInfo symbolInfo = Assembler.InstanceInitialized.RepositoryCustomSymbolInfo.FindSymbolInfo(symbol); MarketOrderAs spreadSide; if (forceCrossMarket) { spreadSide = MarketOrderAs.LimitCrossMarket; } else { spreadSide = (symbolInfo == null) ? MarketOrderAs.LimitCrossMarket : symbolInfo.MarketOrderAs; } if (spreadSide == MarketOrderAs.ERROR || spreadSide == MarketOrderAs.Unknown) { string msg = "Set Symbol[" + symbol + "].SymbolInfo.LimitCrossMarket; should not be spreadSide[" + spreadSide + "]"; Assembler.PopupException(msg); throw new Exception(msg); //return; } switch (direction) { case Direction.Buy: case Direction.Cover: switch (spreadSide) { case MarketOrderAs.LimitTidal: oss = OrderSpreadSide.AskTidal; price = currentAsk; break; case MarketOrderAs.LimitCrossMarket: oss = OrderSpreadSide.BidCrossed; price = currentBid; // Unknown (Order default) becomes CrossMarket break; case MarketOrderAs.MarketMinMaxSentToBroker: oss = OrderSpreadSide.MaxPrice; price = currentAsk; break; case MarketOrderAs.MarketZeroSentToBroker: oss = OrderSpreadSide.MarketPrice; price = currentAsk; // looks like default, must be crossmarket to fill it right now break; default: string msg2 = "no handler for spreadSide[" + spreadSide + "] direction[" + direction + "]"; throw new Exception(msg2); } break; case Direction.Short: case Direction.Sell: switch (spreadSide) { case MarketOrderAs.LimitTidal: oss = OrderSpreadSide.BidTidal; price = currentBid; break; case MarketOrderAs.LimitCrossMarket: oss = OrderSpreadSide.AskCrossed; price = currentAsk; // Unknown (Order default) becomes CrossMarket break; case MarketOrderAs.MarketMinMaxSentToBroker: oss = OrderSpreadSide.MinPrice; price = currentBid; // Unknown (Order default) becomes CrossMarket break; case MarketOrderAs.MarketZeroSentToBroker: oss = OrderSpreadSide.MarketPrice; price = currentBid; // looks like default, must be crossmarket to fill it right now break; default: string msg2 = "no handler for spreadSide[" + spreadSide + "] direction[" + direction + "]"; throw new Exception(msg2); } break; default: string msg = "no handler for direction[" + direction + "]"; throw new Exception(msg); } if (double.IsNaN(price)) { Debugger.Break(); } symbolInfo = Assembler.InstanceInitialized.RepositoryCustomSymbolInfo.FindSymbolInfoOrNew(symbol); price = symbolInfo.AlignOrderPriceToPriceLevel(price, direction, MarketLimitStop.Market); return price; }
public Order() { // called by Json.Deserialize(); what if I'll make it protected? this.GUID = newGUID(); this.messages = new ConcurrentQueue<OrderStateMessage>(); this.ExtendedOrderType = ""; this.PriceRequested = 0; this.PriceFill = 0; this.QtyRequested = 0; this.QtyFill = 0; //this.PositionFollowed = null; //this.AccountPositionFollowed = null; this.State = OrderState.Unknown; this.SernoSession = 0; //QUIK this.SernoExchange = 0; //QUIK this.IsReplacement = false; this.ReplacementForGUID = ""; this.ReplacedByGUID = ""; this.IsKiller = false; this.VictimGUID = ""; this.KillerGUID = ""; this.StateImageIndex = 0; this.StateUpdateLastTimeLocal = DateTime.MinValue; this.FromAutoTrading = false; this.SlippageFill = 0; this.SlippageIndex = 0; this.CurrentAsk = 0; this.CurrentBid = 0; this.SpreadSide = OrderSpreadSide.Unknown; this.MreActiveCanCome = new ManualResetEvent(false); this.DerivedOrders = new List<Order>(); this.DerivedOrdersGuids = new List<string>(); }