OPEN Position when Close price did breakout the setup price.

Created at 22 Nov 2018, 03:11
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!
NG

nguyendan81985

Joined 24.10.2018

OPEN Position when Close price did breakout the setup price.
22 Nov 2018, 03:11


Dear Support,

 

I'm trying to write a code like: when close price above value which can be installed. ==> open Buy

Close price below value which can be installed ==> open Sell.

my code is below.  but i backtested and some situation is not working. could you help me to check. thanks

 

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 SampleBreakoutcBot : Robot
    {
        [Parameter("Top", DefaultValue = 10000)]
        public double Source1 { get; set; }

        [Parameter("Bottom", DefaultValue = 0)]
        public double Source2 { get; set; }

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


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

        [Parameter("TP_BUY", DefaultValue = 20)]
        public int TPBUY { get; set; }

        [Parameter("TP_SELL", DefaultValue = 20)]
        public int TPSELL { get; set; }

        [Parameter("SL_BUY", DefaultValue = 10)]
        public int SLBUY { get; set; }

        [Parameter("SL_SELL", DefaultValue = 10)]
        public int SLSELL { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnBar()
        {

            var price = MarketSeries.Close.Last(0);
            var longPos = Positions.Find("label", Symbol, TradeType.Buy);
            var shortPos = Positions.Find("label", Symbol, TradeType.Sell);


            if (MarketSeries.Close.Last(0) > MarketSeries.Open.Last(0) && MarketSeries.Close.Last(0) > Source1 && longPos == null)
            {
                Print("Open Buy");
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnitsBUY, "name", SLBUY, TPBUY);
                Notifications.PlaySound("C:\\Alert\\Ring08.wav");
            }
            if (MarketSeries.Close.Last(0) < MarketSeries.Open.Last(0) && MarketSeries.Close.Last(0) < Source2 && shortPos == null)
            {
                Print("Open Sell");
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnitsSELL, "name", SLSELL, TPSELL);
                Notifications.PlaySound("C:\\Alert\\Ring08.wav");
            }

        }


        private long VolumeInUnitsBUY
        {
            get { return Symbol.QuantityToVolume(QuantityBUY); }
        }

        private long VolumeInUnitsSELL
        {
            get { return Symbol.QuantityToVolume(QuantitySELL); }
        }
    }
}


@nguyendan81985
Replies

PanagiotisCharalampous
22 Nov 2018, 11:11

Hi nguyendan81985,

Last(0) will return the values for the bar just opened. Therefore Open and Close prices will be the same. If you are interested in the values of the bar just closed, use Last(1).

Best Regards,

Panagiotis


@PanagiotisCharalampous

nguyendan81985
22 Nov 2018, 12:03

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

Last(0) will return the values for the bar just opened. Therefore Open and Close prices will be the same. If you are interested in the values of the bar just closed, use Last(1).

Best Regards,

Panagiotis

dear Panagiotis

Thanks for your reply, i do again as below. and i think it is working. any advise from you for my code?

 

      protected override void OnBar()
        {

            var price = MarketSeries.Close.Last(1);
            var longPos = Positions.Find("label", Symbol, TradeType.Buy);
            var shortPos = Positions.Find("label", Symbol, TradeType.Sell);


            if (MarketSeries.Close.Last(1) > MarketSeries.Open.Last(1) && MarketSeries.Close.Last(1) > Source1 && longPos == null)
            {
                Print("Open Buy");
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnitsBUY, "name", SLBUY, TPBUY);
                Notifications.PlaySound("C:\\Alert\\Ring08.wav");
            }
            if (MarketSeries.Close.Last(1) < MarketSeries.Open.Last(1) && MarketSeries.Close.Last(1) < Source2 && shortPos == null)
            {
                Print("Open Sell");
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnitsSELL, "name", SLSELL, TPSELL);
                Notifications.PlaySound("C:\\Alert\\Ring08.wav");
            }

 


@nguyendan81985

PanagiotisCharalampous
22 Nov 2018, 12:05

Hi nguyendan81985,

As long as it does what you expect it to do then it looks fine to me.

Best Regards,

Panagiotis


@PanagiotisCharalampous

nguyendan81985
23 Nov 2018, 02:44 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

As long as it does what you expect it to do then it looks fine to me.

Best Regards,

Panagiotis

dear Panagiotis 

Thanks for your advise. by the way, im coding with Ichimoku Indicator, but i dont know exactly the currently of tenkan, kijun,chiko, span in the current, past, and future. could you pls advise me for this issue.

for example: i want to open Buy when the close price is cross above kumo (span A or Span B) and Span A (in future) > Span B (future)

i attached the picture which i did set value of tenkan,kijun, span A, B... this is correct or not? pls help to advise me. many thank

 

var tenkan = ichimokuKinkoHyo.TenkanSen.Last(1);
            var kijun = ichimokuKinkoHyo.KijunSen.Last(1);
            var spanA26 = ichimokuKinkoHyo.SenkouSpanA.Last(27);
            var spanA = ichimokuKinkoHyo.SenkouSpanA.Last(1);
            var spanB26 = ichimokuKinkoHyo.SenkouSpanB.Last(27);
            var spanB = ichimokuKinkoHyo.SenkouSpanB.Last(1);
            var chikou = ichimokuKinkoHyo.ChikouSpan.Last(1);
            var spanA52 = ichimokuKinkoHyo.SenkouSpanA.Last(53);
            var spanB52 = ichimokuKinkoHyo.SenkouSpanB.Last(53);

            var price26 = MarketSeries.Close.Last(26);
            var cur_price = MarketSeries.Close.Last(1);


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

            if (cur_price > tenkan && tenkan > kijun && chikou > price26 && (MarketSeries.Close.HasCrossedAbove(spanA26, 1) && spanA26 > spanB26) || (MarketSeries.Close.HasCrossedAbove(spanB26, 1) && spanB26 > spanA26) && spanA > spanB)
            {
                Print("BUY");
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, "label");
            }

            if (cur_price < tenkan && tenkan < kijun && chikou < price26 && (MarketSeries.Close.HasCrossedBelow(spanA26, 1) && spanA26 < spanB26) || (MarketSeries.Close.HasCrossedBelow(spanB26, 1) && spanB26 < spanA26) && spanA < spanB)
            {
                Print("SELL");
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, "label");
            }


@nguyendan81985

PanagiotisCharalampous
23 Nov 2018, 11:28

Hi nguyendan81985,

From a first look, your logic seems correct. Did you test it? Do you get correct entries? If not. then share the full cBot code and I can have a another look on backtesting.

Best Regards,

Panagiotis


@PanagiotisCharalampous

nguyendan81985
23 Nov 2018, 16:13 ( Updated at: 21 Dec 2023, 09:21 )

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

From a first look, your logic seems correct. Did you test it? Do you get correct entries? If not. then share the full cBot code and I can have a another look on backtesting.

Best Regards,

Panagiotis

Hi Panagiotis,

I just tested again and it correct as my wish. but i don't know how to set up Stoploss value below or above Kumo (Span A or Span B) and plus 5pip. could you help me to advise this code.

the picture attached for your easy understanding., the Take Profit shall be open or input.

 


@nguyendan81985

PanagiotisCharalampous
23 Nov 2018, 17:17

Hi nguyendan81985,

See below how to get the pips separating the currect SpanA value from the current Bid price, above and below

           //Above
           var pipsFormBidAbove = (ichimokuKinkoHyo.SenkouSpanA.Last(27) - Symbol.Bid) / Symbol.PipSize;
           //Below
           var pipsFormBidBelow = (Symbol.Bid - ichimokuKinkoHyo.SenkouSpanA.Last(27)) / Symbol.PipSize;

Best Regards,

Panagiotis


@PanagiotisCharalampous

nguyendan81985
26 Nov 2018, 14:47 ( Updated at: 21 Dec 2023, 09:21 )

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

See below how to get the pips separating the currect SpanA value from the current Bid price, above and below

           //Above
           var pipsFormBidAbove = (ichimokuKinkoHyo.SenkouSpanA.Last(27) - Symbol.Bid) / Symbol.PipSize;
           //Below
           var pipsFormBidBelow = (Symbol.Bid - ichimokuKinkoHyo.SenkouSpanA.Last(27)) / Symbol.PipSize;

Best Regards,

Panagiotis

Hi Panagiotis,

Many thanks, I got it. but i still have one trouble when i want to take profit when i using ichimoku system. could you advise me that how can I can take profit when Chiko crossing previous price. I attached picture for sample.

hope to see your advise soon. thanks so much

 

 


@nguyendan81985

PanagiotisCharalampous
26 Nov 2018, 15:48

Hi nguyendan81985,

In this case you should compare the last Chikou value with MarketSeries.Open.Last(x) and MarkerSeries.Close.Last(x). Note that the x value is always depended on the Kijun Sen periods defined for the indicator. This applies for SpanA and SpanB examples above as well. 

Best Regards,

Panagiotis


@PanagiotisCharalampous