예제 #1
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();
     }
 }
예제 #2
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();
     }
 }
예제 #3
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;
            }
        }