Simple maxima/minima limit bot that won't make trades

Created at 27 Dec 2022, 08:52
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
DE

deraera909

Joined 27.12.2022

Simple maxima/minima limit bot that won't make trades
27 Dec 2022, 08:52


Hey,

I understand this may be the wrong place to post this so if it is, please let me know so I can remove this post and post in the correct area.

 

As the title suggests, I've coded up a bot (with limited C# knowledge) and whenever running any tests on this, backtrading or normal. I don't seem to have any luck with the bot making any trades at all. It's still in the middle of refinement as I was slowly deconstructing my code to try and fix it but have ran into a brick wall.

Please let me know if you see anything to be changed or if you'd like to know more about it in an attempt to help me fix it.

Thanks,

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class KMS : Robot
    {
        [Parameter("Kill My Self", DefaultValue = "Hello world!")]
        public string Message { get; set; }
        [Parameter("Buy Open Price", DefaultValue = "0", Group = "Buy", Step = 0.01)]
        public double BuyOpenPrice { get; set; }
        [Parameter("Buy Profit Price", DefaultValue = "0", Group = "Buy", Step = 0.01)]
        public double BuyProfitPrice { get; set; }
        [Parameter("Buy Loss Price", DefaultValue = "0", Group = "Buy", Step = 0.01)]
        public double BuyLossPrice { get; set; }
        [Parameter("Sell Open Price", DefaultValue = "0", Group = "Sell", Step = 0.01)]
        public double SellOpenPrice { get; set; }
        [Parameter("Sell Profit Price", DefaultValue = "0", Group = "Sell", Step = 0.01)]
        public double SellProfitPrice { get; set; }
        [Parameter("Sell Loss Price", DefaultValue = "0", Group = "Sell", Step = 0.01)]
        public double SellLossPrice { get; set; }
        

        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
            
            double buyOpen = Symbol.Bid*((BuyOpenPrice+100)/100);
            double buyProfit = buyOpen*((BuyProfitPrice+100)/100);
            double buyLoss = buyOpen*((BuyLossPrice+100)/100);
            
            double sellOpen = Symbol.Bid*((BuyOpenPrice+100)/100);
            double sellProfit = sellOpen*((SellProfitPrice+100)/100);
            double sellLoss = sellOpen*((SellLossPrice+100)/100);
            
            foreach (var order in PendingOrders)
            {
                // 2 orders
                if ((order.Label == "myBuyOrder") & (order.Label == "mySellOrder"))
                {
                    Print("Testing Testing, 1, 2, 3, Testing.");
                }
                
                // 1 order
                if ((order.Label == "myBuyOrder") ^ (order.Label == "mySellOrder"))
                {
                    CancelPendingOrder(order);
                }
                
                // 0 orders
                if (order.Label == null)
                {
                    var checkBuyPosition = Positions.Find("myBuyOrder", SymbolName, TradeType.Buy);
                    var checkSellPosition = Positions.Find("mySellOrder", SymbolName, TradeType.Sell);
                    
                    // 0 Positions
                    if (checkBuyPosition == null & checkSellPosition == null)
                    {
                        PlaceLimitOrder(TradeType.Buy, SymbolName, 1, Symbol.Ask-2*Symbol.PipSize, "myBuyOrder");
                        PlaceLimitOrder(TradeType.Sell, SymbolName, 1, Symbol.Bid-2*Symbol.PipSize, "mySellOrder");
                        var buyPosition = Positions.Find("myBuyOrder", SymbolName, TradeType.Buy);
                        var sellPosition = Positions.Find("mySellOrder", SymbolName, TradeType.Sell);
                        
                        if (buyPosition != null)
                        {
                            ModifyPosition(buyPosition, buyLoss, buyProfit);
                        }
                        if (sellPosition != null)
                        {
                            ModifyPosition(sellPosition, sellLoss, sellProfit);
                        }
                    }
                }
            }
        }

        protected override void OnBar()
        {
            // Handle my DIIICK here
            double buyOpen = Symbol.Bid*((BuyOpenPrice+100)/100);
            double buyProfit = buyOpen*((BuyProfitPrice+100)/100);
            double buyLoss = buyOpen*((BuyLossPrice+100)/100);
            
            double sellOpen = Symbol.Bid*((BuyOpenPrice+100)/100);
            double sellProfit = sellOpen*((SellProfitPrice+100)/100);
            double sellLoss = sellOpen*((SellLossPrice+100)/100);
            
            foreach (var order in PendingOrders)
            {
                // 2 orders
                if ((order.Label == "myBuyOrder") & (order.Label == "mySellOrder"))
                {
                    Print("Testing Testing, 1, 2, 3, Testing.");
                }
                
                // 1 order
                if ((order.Label == "myBuyOrder") ^ (order.Label == "mySellOrder"))
                {
                    CancelPendingOrder(order);
                }
                
                // 0 orders
                if (order.Label == null)
                {
                    var checkBuyPosition = Positions.Find("myBuyOrder", SymbolName, TradeType.Buy);
                    var checkSellPosition = Positions.Find("mySellOrder", SymbolName, TradeType.Sell);
                    
                    // 0 Positions
                    if ((checkBuyPosition == null) & (checkSellPosition == null))
                    {
                        PlaceLimitOrder(TradeType.Buy, SymbolName, 1, Symbol.Bid, "myBuyOrder");
                        PlaceLimitOrder(TradeType.Sell, SymbolName, 1, Symbol.Bid-2*Symbol.PipSize, "mySellOrder");
                        var buyPosition = Positions.Find("myBuyOrder", SymbolName, TradeType.Buy);
                        var sellPosition = Positions.Find("mySellOrder", SymbolName, TradeType.Sell);
                        
                        if (buyPosition != null)
                        {
                            ModifyPosition(buyPosition, buyLoss, buyProfit);
                        }
                        if (sellPosition != null)
                        {
                            ModifyPosition(sellPosition, sellLoss, sellProfit);
                        }
                    }
                }
            }
        }
    }
}
 


@deraera909
Replies

PanagiotisChar
27 Dec 2022, 12:19 ( Updated at: 21 Dec 2023, 09:23 )

Hi there,

You are trying to place an order only when another order exists. This will never happen

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

deraera909
27 Dec 2022, 12:51 ( Updated at: 27 Dec 2022, 15:53 )

Hey,

Thanks for the help so far. I've updated the code and it's still not placing any trades. Am I looking at this wrong or should this be working?

foreach (var order in PendingOrders)
            {
                //int totalBuyPositions = Positions.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Buy);
                var buyPositions = Positions.Find("myBuyOrder", "AUDUSD");
                var sellPositions = Positions.Find("mySellOrder", "AUDUSD");
                int totalBuyOrders = PendingOrders.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Buy);
                int totalSellOrders = PendingOrders.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Sell);
                // 2 orders
                if ((totalBuyOrders == 1) && (totalSellOrders == 1))
                {
                    Print("Testing Testing, 1, 2, 3, Testing.");
                }
                
                // 1 order
                if (totalBuyOrders == 1 || totalSellOrders == 1)
                {
                    CancelPendingOrder(order);
                }
                
                // 0 orders
                //if ((PendingOrders.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Buy) == 0) && (PendingOrders.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Sell) == 0))
                if ((totalBuyOrders == 0) && (totalSellOrders == 0))
                {
                    //var checkBuyPosition = Positions.Find("myBuyOrder", SymbolName, TradeType.Buy);
                    //var checkSellPosition = Positions.Find("mySellOrder", SymbolName, TradeType.Sell);
                    
                    // 0 Positions
                    if ((buyPositions == null) && (sellPositions == null))
                    {
                        PlaceLimitOrder(TradeType.Buy, SymbolName, 1, Symbol.Ask-2*Symbol.PipSize, "myBuyOrder");
                        PlaceLimitOrder(TradeType.Sell, SymbolName, 1, Symbol.Bid-2*Symbol.PipSize, "mySellOrder");
                        var buyPosition = Positions.Find("myBuyOrder", SymbolName, TradeType.Buy);
                        var sellPosition = Positions.Find("mySellOrder", SymbolName, TradeType.Sell);
                        
                        if (buyPosition != null)
                        {
                            ModifyPosition(buyPosition, buyLoss, buyProfit);
                        }
                        if (sellPosition != null)
                        {
                            ModifyPosition(sellPosition, sellLoss, sellProfit);
                        }
                    }
                }
            }


@deraera909