Entry trade

Created at 19 Feb 2019, 11:14
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!
JE

jelle2500

Joined 25.11.2017

Entry trade
19 Feb 2019, 11:14


hello,

I would like to take a buy trade when current price breaks the high of the last closed candle ( MarketSeries.High.Last(1)  )

And i would like to take a sell trade when the current price breaks the low of the last closed candle ( MarketSeries.Low.Last(1)  )

 

So how to code this?

 

 

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

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

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



        private const string label = "....";

        protected override void OnStart()
        {
          
        }

        protected override void OnBar()
        {
            var stopLossBuy =
            var SLBuy = 

            var stopLossSell =
            var SLSell =
            {
                if (...............)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SLBuy, null);
                }
                else if (................)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SLSell, null);
                }

                var longPos = Positions.Find(label, Symbol, TradeType.Buy);
                var shortPos = Positions.Find(label, Symbol, TradeType.Sell);

                Position[] positions = Positions.FindAll(label, Symbol);

                foreach (Position position in positions)
                {
                    if 
                        ClosePosition(longPos);

                    else if 
                        ClosePosition(shortPos);

                    else if 

                  
                }
            }
        }


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


@jelle2500
Replies

erisoftdevelop
19 Feb 2019, 20:51

Well if you will use the OnBar event the evaluation of the condition to buy or Sell will be in the creation of the third candle.

  var lastcandlehigh= MarketSeries.High.Last(1);
  var beforelastcandlehigh= MarketSeries.High.Last(2);

 

// Buy Condition as you propose

// Example of Buy: Lastcandlehigh= 50 , beforelastcandlehigh=48 clearly a buy.

// Example of Sell: Lastcandlehigh: 50, beforelastcandlehigh=60 clearly a sell (ELSE condition)

if(lastcandlehigh>beforelastcandlehigh){

 ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SLBuy, null);

}else{

 ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SLSell, null);

}

 

As a observation this code could produce some effect in higher timeframe in short time frame as 15m and 5min or 1min not recommended, Why? Due to the Spread (The difference of the BID and ASK price) provided by the different brokers. Take in consideration requester.


@erisoftdevelop

jelle2500
20 Feb 2019, 01:03

RE:

erisoftdevelop said:

Well if you will use the OnBar event the evaluation of the condition to buy or Sell will be in the creation of the third candle.

  var lastcandlehigh= MarketSeries.High.Last(1);
  var beforelastcandlehigh= MarketSeries.High.Last(2);

 

// Buy Condition as you propose

// Example of Buy: Lastcandlehigh= 50 , beforelastcandlehigh=48 clearly a buy.

// Example of Sell: Lastcandlehigh: 50, beforelastcandlehigh=60 clearly a sell (ELSE condition)

if(lastcandlehigh>beforelastcandlehigh){

 ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SLBuy, null);

}else{

 ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SLSell, null);

}

 

As a observation this code could produce some effect in higher timeframe in short time frame as 15m and 5min or 1min not recommended, Why? Due to the Spread (The difference of the BID and ASK price) provided by the different brokers. Take in consideration requester.

Thanks fot your help! i've got it.

 

Jelle


@jelle2500