/// <summary> /// Close all Open Positions for this bot /// </summary> /// <param name="bot"></param> public static void CloseAllPositions(this NewBot bot) { bot.LogInformation("Closing all Positions for " + bot.BotName + " bot with id " + bot.BotID); foreach (var order in bot.Positions.Where(p => p.Label == bot.BotID)) { order.Close(); } }
/// <summary> /// Clear all Pending Orders opened by this bot /// </summary> /// <param name="bot"></param> public static void ClearAllPendingOrders(this NewBot bot) { bot.LogInformation("Clearing all pending orders for " + bot.BotName + " bot with id " + bot.BotID); foreach (var order in bot.PendingOrders.Where(p => p.Label == bot.BotID)) { order.Cancel(); } }
/// <summary> /// Break even procedure for positions in profit /// </summary> /// <param name="bot"></param> public static void BreakEven(this NewBot bot) { // attempt break-event only if a position is open if (bot.CanOpenPosition) { return; } // retrieve all open positions for this bot var openPositions = bot.Positions.Where(p => p.Label == bot.BotID); foreach (var position in openPositions) { // check if position is in profit if (position.NetProfit <= 0) { continue; } // handle buy positions if (position.TradeType == TradeType.Buy) { // check if position has already broken even if (position.StopLoss >= position.EntryPrice) { continue; } // break-even if price and entry-price difference is >= bot.TrailBy if (bot.Bid - position.EntryPrice >= bot.ConvertPipsToDecimal(bot.TrailBy)) { position.ModifyStopLossPrice(position.EntryPrice + bot.ConvertPipsToDecimal(2)); } } // handle sell positions if (position.TradeType == TradeType.Sell) { // check if position has already broken even if (position.StopLoss <= position.EntryPrice) { continue; } // break-even if price and entry-price difference is >= bot.TrailBy if (position.EntryPrice - bot.Ask >= bot.ConvertPipsToDecimal(bot.TrailBy)) { position.ModifyStopLossPrice(position.EntryPrice - bot.ConvertPipsToDecimal(2)); } } } }
/// <summary> /// Price Trailing procedure for positions in profit /// </summary> /// <param name="bot"></param> public static void TrailStop(this NewBot bot) { // attempt trail-stop only if a position is open if (bot.CanOpenPosition) { return; } // retrieve all open positions for this bot var openPositions = bot.Positions.Where(p => p.Label == bot.BotID).ToList(); foreach (var position in openPositions) { // handle buy positions trailing if (position.TradeType == TradeType.Buy) { // check if position has broken even if (position.StopLoss < position.EntryPrice) { continue; } // trail price by bot.TrailBy if (bot.Bid - position.StopLoss >= bot.ConvertPipsToDecimal(bot.TrailBy)) { if (bot.Bid - bot.ConvertPipsToDecimal(bot.TrailBy) > position.EntryPrice) { position.ModifyStopLossPrice(bot.Bid - bot.ConvertPipsToDecimal(bot.TrailBy)); } } } else if (position.TradeType == TradeType.Sell) { // check if position has broken even if (position.StopLoss > position.EntryPrice) { continue; } // trail price by bot.TrailBy if (position.StopLoss - bot.Ask >= bot.ConvertPipsToDecimal(bot.TrailBy)) { if (bot.Ask + bot.ConvertPipsToDecimal(bot.TrailBy) < position.EntryPrice) { position.ModifyStopLossPrice(bot.Ask + bot.ConvertPipsToDecimal(bot.TrailBy)); } } } } }
/// <summary> /// returns a string indicating if both the Ask and Bid price levels are on any one side of this.Zero_Line /// </summary> /// <param name="bot"></param> /// <returns></returns> public static string PriceRelationToZeroLine(this NewBot bot) { // return above if both ask and bid prices are above the zero_line if (bot.Ask > bot.Zero_Line && bot.Bid > bot.Zero_Line) { return("above"); } // return below if both ask and bid prices are below the zero_line if (bot.Ask < bot.Zero_Line && bot.Bid < bot.Zero_Line) { return("below"); } // return nothing otherwise return(string.Empty); }
/// <summary> /// return the number of open pending orders for this bot /// </summary> /// <param name="bot"></param> /// <returns></returns> public static int CountPedingOrders(this NewBot bot) { return(bot.PendingOrders.Count); }
/// <summary> /// Return the number of open positions for this bot /// </summary> /// <param name="bot"></param> /// <returns></returns> public static int CountPositions(this NewBot bot) { return(bot.Positions.Count); }
/// <summary> /// Convert pips in integer form to decimal form for price calculations /// </summary> /// <param name="bot"></param> /// <param name="pips"></param> /// <returns></returns> public static double ConvertPipsToDecimal(this NewBot bot, int pips) { return(bot.Symbol.PipSize * pips); }
/// <summary> /// Convert volume units to lots /// </summary> /// <param name="bot"></param> /// <param name="volume"></param> /// <returns></returns> public static double ConvertVolumeToLots(this NewBot bot, double volume) { return(bot.Symbol.VolumeInUnitsToQuantity(volume)); }
/// <summary> /// Convert Lots to volume units /// </summary> /// <param name="bot"></param> /// <param name="lots"></param> /// <returns></returns> public static double ConvertLotsToVolume(this NewBot bot, double lots) { return(bot.Symbol.QuantityToVolumeInUnits(lots)); }
/// <summary> /// Remove a Specific ChartObject /// </summary> /// <param name="bot"></param> /// <param name="name"></param> public static void RemoveChartObject(this NewBot bot, string name) { bot.Chart.RemoveObject(objectName: name); }
/// <summary> /// Remove all objects from the chart /// </summary> /// <param name="bot"></param> public static void ClearAllLines(this NewBot bot) { bot.Chart.RemoveAllObjects(); }
/// <summary> /// Draw a Non-Interactive Horizontal Line on the chart /// </summary> /// <param name="bot"></param> /// <param name="name"></param> /// <param name="price"></param> /// <param name="color"></param> public static void DrawHorizontalLine(this NewBot bot, string name, double price, Color color) { var line = bot.Chart.DrawHorizontalLine(name: name, y: price, color: color, thickness: 1, lineStyle: LineStyle.Solid); line.IsInteractive = false; }
/// <summary> /// Log messages tagged with error /// </summary> /// <param name="bot"></param> /// <param name="errorMessage"></param> public static void LogError(this NewBot bot, string errorMessage) { var timeStamp = DateTime.Now.ToString(); bot.Print(bot.BotName + "[error] :: " + errorMessage); }
/// <summary> /// Log messages tagged with information /// </summary> /// <param name="bot"></param> /// <param name="message"></param> public static void LogInformation(this NewBot bot, string message) { var timeStamp = DateTime.Now.ToString(); bot.Print(bot.BotName + "[info] :: " + message); }
/// <summary> /// Open Positions /// </summary> /// <param name="bot"></param> public static void OpenPosition(this NewBot bot) { // verify bot.CanOpenPosition if (!bot.CanOpenPosition) { return; } // skip if zero_line is set to zero if (bot.Zero_Line == 0) { return; } // check price relation to zero_line var currentPriceRelationToZero_Line = bot.PriceRelationToZeroLine(); // handle if price is above zero_line if (currentPriceRelationToZero_Line == "above") { // skip if Ask price is below bot.Buy_Line if (bot.Ask < bot.Buy_Line) { return; } // open buy position if price has reached bot.Buy_Line var buyPositionResult = bot.ExecuteMarketOrder(tradeType: TradeType.Buy, symbolName: bot.SymbolName, volume: bot.ConvertLotsToVolume(bot.LotSize), label: bot.BotID); // skip if market order failed to be executed if (!buyPositionResult.IsSuccessful) { bot.LogError("Failed to Open Buy Position for " + bot.BotName + " bot with id " + bot.BotID); return; } // modify position stop loss buyPositionResult.Position.ModifyStopLossPips(bot.StopLoss); bot.LogInformation("Buy Position opened for " + bot.BotName + " bot with id " + bot.BotID); // stop bot from opening another position bot.CanOpenPosition = false; } else if (currentPriceRelationToZero_Line == "below") { // skip if Bid price is Above bot.Sell_Line if (bot.Bid > bot.Sell_Line) { return; } // open sell position if price has reached bot.Sell_Line var sellPositionResult = bot.ExecuteMarketOrder(tradeType: TradeType.Sell, symbolName: bot.SymbolName, volume: bot.ConvertLotsToVolume(bot.LotSize), label: bot.BotID); // skip if market order failed to be executed if (!sellPositionResult.IsSuccessful) { bot.LogError("Failed to Open sell Position for " + bot.BotName + " bot with id " + bot.BotID); return; } // modify position stop loss sellPositionResult.Position.ModifyStopLossPips(bot.StopLoss); bot.LogInformation("Sell Position opened for " + bot.BotName + " bot with id " + bot.BotID); // stop bot from opening another position bot.CanOpenPosition = false; } }