How to have one Selling and one Buying Limit order and Stop order.

Created at 10 Feb 2020, 10:15
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!
YE

yeril123@hotmail.com

Joined 06.10.2016

How to have one Selling and one Buying Limit order and Stop order.
10 Feb 2020, 10:15


Hi, i have a strategy that i would like to by placing one long limit order and one short limit order (i also want to try the same with Stop orders instead of limit). Right now i just know how to keep one open buy position and one open sell position but when i change the "" ExecuteMarketOrder"" for  " PlaceLimitOrder" or " PlaceStopOrder" the bot starts opening an insane amount of pending orders and once 1 is triggered the bot crashes because so many orders. How can i keep only one buy and one sell for Limit and Stop orders??? Please help i willl really apreaciate it. D:

 

i was trying to do it and this was what was happening to me.


Rather than giving a bot with a lot of errors i put it back to the normal working bot with just market order.

This is my bot i just want to learn how to keep one buy and one sell Limit and Stop order to try.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class YeriBottesting : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Bot Name", DefaultValue = "EMA & RSI")]
        public string label { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        // 4 EMA

        [Parameter("EMA 1", DefaultValue = 8)]
        public int EMA1_Periods { get; set; }

        [Parameter("EMA 2", DefaultValue = 13)]
        public int EMA2_Periods { get; set; }


        //TP , ST , Trailing Stop

        [Parameter("Stop Loss", DefaultValue = 50)]
        public double SL { get; set; }
        [Parameter("Take Profit", DefaultValue = 150)]
        public double TP { get; set; }

        [Parameter("Include Trailing Stop", DefaultValue = false)]
        public bool IncludeTrailingStop { get; set; }

        [Parameter("Trailing Stop Trigger (pips)", DefaultValue = 20)]
        public int TrailingStopTrigger { get; set; }

        [Parameter("Trailing Stop Step (pips)", DefaultValue = 10)]
        public int TrailingStopStep { get; set; }



        private ExponentialMovingAverage EMA1;
        private ExponentialMovingAverage EMA2;

        private Position longPosition;
        private Position shortPosition;

        protected override void OnStart()
        {

            EMA1 = Indicators.ExponentialMovingAverage(SourceSeries, EMA1_Periods);
            EMA2 = Indicators.ExponentialMovingAverage(SourceSeries, EMA2_Periods);


        }
        protected override void OnTick()
        {
            longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            if (EMA1.Result.Last(1) > EMA2.Result.Last(1) && longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label, SL, TP);
            }
            else if (EMA1.Result.Last(1) < EMA2.Result.Last(1) && shortPosition == null)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label, SL, TP);
            }


            if (IncludeTrailingStop)
            {
                SetTrailingStop();
            }

        }
        protected override void OnBar()
        {


        }

        // Managing orders section

        private void SetTrailingStop()
        {
            var sellPositions = Positions.FindAll(label, SymbolName, TradeType.Sell);

            foreach (Position position in sellPositions)
            {
                double distance = position.EntryPrice - Symbol.Ask;

                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;

                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }

            var buyPositions = Positions.FindAll(label, SymbolName, TradeType.Buy);

            foreach (Position position in buyPositions)
            {
                double distance = Symbol.Bid - position.EntryPrice;

                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}


@yeril123@hotmail.com
Replies

PanagiotisCharalampous
10 Feb 2020, 10:45

Hi yeril123,

You can have a condition to place orders only when there are 0 pending orders. You can check the number of pending orders using PendingOrders.Count property.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

yeril123@hotmail.com
10 Feb 2020, 21:21

RE:

PanagiotisCharalampous said:

Hi yeril123,

You can have a condition to place orders only when there are 0 pending orders. You can check the number of pending orders using PendingOrders.Count property.

Best Regards,

Panagiotis 

Join us on Telegram

 

Hello, that helps a lot controlling all those pending orders, but by any chance do you know how to keep one buying and one selling? using PendingOrders.Count can limit the total numbers of pending order but if i put that limit to 2 then it will have 2 selling and i would like to have one buying and one selling. if you know how to i really appreciate your help.


@yeril123@hotmail.com