Ejemplo n.º 1
0
 /// <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();
     }
 }
Ejemplo n.º 2
0
 /// <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();
     }
 }
Ejemplo n.º 3
0
        /// <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));
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <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));
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
 /// <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);
 }
Ejemplo n.º 7
0
 /// <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);
 }
Ejemplo n.º 8
0
 /// <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);
 }
Ejemplo n.º 9
0
 /// <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));
 }
Ejemplo n.º 10
0
 /// <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));
 }
Ejemplo n.º 11
0
 /// <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);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Remove all objects from the chart
 /// </summary>
 /// <param name="bot"></param>
 public static void ClearAllLines(this NewBot bot)
 {
     bot.Chart.RemoveAllObjects();
 }
Ejemplo n.º 13
0
        /// <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;
        }
Ejemplo n.º 14
0
        /// <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);
        }
Ejemplo n.º 15
0
        /// <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);
        }
Ejemplo n.º 16
0
        /// <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;
            }
        }