Replies

nguyendan81985
28 Sep 2021, 04:05

RE:

ClickAlgo said:

You can also try this: 

 

it worked. thanks. But i want to add this bot to Telegram Channel. could you pls hep me how to code? thanks


@nguyendan81985

nguyendan81985
21 Jan 2021, 11:08

RE: RE: RE:

mgpublic said:

 

Thx for reply and help.

How do I code so that it finds and closes the position with SymbolName "USDJPY" either if its posBuy or posSell. In the example above its only posBuy.

hi

 

you can try as below:

 

foreach (var position in Positions)
                {
                    if (position.SymbolName == Symbol.Name)
                    {

                        ClosePosition(position);
                    }

                }


@nguyendan81985

nguyendan81985
11 Jan 2021, 15:55

RE:

Dear Mr. Panagiotis,

Thanks for your help. it is worked for me.

 


@nguyendan81985

nguyendan81985
09 Jan 2021, 02:51

RE:

PanagiotisCharalampous said:

Hi nguyendan81985,

Then you have done something wrong but I do not know what it is without the source code.

Best Regards,

Panagiotis 

Join us on Telegram 

Dear Mr. Panagiotis,

thanks for you advise, my code is working for open position 2 and position 3 also. however, i want to close all position if the position 3.pips is > 30 pip. my source code as below and there is some wrong: only position 3 is closed if position.pip>30pip. could u pls advise me?

 

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 test : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
        [Parameter("Slow Periods", DefaultValue = 21)]
        public int SlowPeriods { get; set; }

        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }

        private ExponentialMovingAverage slowMa;
        protected override void OnStart()
        {
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, 21);

        }


        protected override void OnTick()
        {
            var SellOrder = Positions.Find("Sell", SymbolName, TradeType.Sell);
            var SellOrder2 = Positions.Find("Sell2", SymbolName, TradeType.Sell);
            var SellOrder3 = Positions.Find("Sell3", SymbolName, TradeType.Sell);


            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", null, null);
                    }

                    if (SellOrder.Pips <= -40 && SellOrder3 == null)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell3", null, null);
                    }
                }
            }



            if (SellOrder != null && SellOrder2 != null && SellOrder3 == null)
            {

                foreach (var position in Positions)
                {
                    if (position.Pips > 20)
                    {
                        ClosePosition(position);
                    }
                }
            }

            else if (SellOrder3 != null)
            {

                foreach (var position in Positions)
                {
                    if (position.Pips > 30)
                    {
                        ClosePosition(position);
                    }
                }
            }

        }


        protected override void OnBar()
        {


            var SellOrder = Positions.Find("Sell", SymbolName, TradeType.Sell);
            var SellOrder2 = Positions.Find("Sell2", SymbolName, TradeType.Sell);

            var SellOrder3 = Positions.Find("Sell3", SymbolName, TradeType.Sell);

            var Open1 = MarketSeries.Open.Last(1);
            var Close1 = MarketSeries.Close.Last(1);
            var High1 = MarketSeries.High.Last(1);
            var Low1 = MarketSeries.Low.Last(1);

            var Open2 = MarketSeries.Open.Last(2);
            var Close2 = MarketSeries.Close.Last(2);
            var High2 = MarketSeries.High.Last(2);
            var Low2 = MarketSeries.Low.Last(2);

            var Close3 = Bars.ClosePrices.Last(3);
            var Open3 = Bars.OpenPrices.Last(3);
            var High3 = Bars.HighPrices.Last(3);
            var Low3 = MarketSeries.Low.Last(3);

            double sma21_1 = slowMa.Result.LastValue;
            double sma21_2 = slowMa.Result.Last(2);
            double sma21_3 = slowMa.Result.Last(3);

            if (SellOrder == null)
            {
                if (Close3 > sma21_3 && Close2 > sma21_2 && Close1 < sma21_1)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell", null, null);
                }
            }


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

    }
}

 


@nguyendan81985

nguyendan81985
08 Jan 2021, 11:01

RE:

PanagiotisCharalampous said:

Hi nguyendan81985,

You should put your code in OnTick() method rather than in OnBar().

Best Regards,

Panagiotis 

Join us on Telegram 

Hi Panagiotis,

i put code in Ontick, but there are many position 2 which is opened when sellorder.pip <=20. i need only ONE position 2 is opened.

Best Regards,

Panagiotis 

 


@nguyendan81985

nguyendan81985
08 Jan 2021, 03:09

RE:

PanagiotisCharalampous said:

Hi nguyendan81985,

If you want to use the previous position's SL, try the following

            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", SL, 20).Position;
                        position.ModifyStopLossPrice(SellOrder.StopLoss);
                    }

                }
            }

Best Regards,

Panagiotis 

Join us on Telegram 

 

Dear Mr.Panagiotis 

thanks for your feedback. 

now i have problem with below coding. when selloder.pips <-20pip, the sell position 2 will be opened, but it only open when the bar is closed. How can it open position 2 when selloder.pips <-20, no need to waiting bar closed?

            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", SL, 20).Position;
                        position.ModifyStopLossPrice(SellOrder.StopLoss);
                    }

                }
            }

 


@nguyendan81985

nguyendan81985
26 Dec 2020, 15:34

RE:

admin said:

UPDATED

This robot is intended to be used as a sample and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.

// -------------------------------------------------------------------------------------------------
//  In "Sample Sell Trailing" Robot the user can set the variable "Trigger (pips)" 
//  which defines the point where the Stop Loss will start trailing the order. 
//  When the profit in pips is above or equal to "Trigger (pips)" the stop loss will start trailing the spot price.
//  Until this condition is met the user can set a normal Stop Loss using the "Stop Loss (pips)" variable. 
//  Variable "Trailing Stop (pips)" defines the number of pips the Stop Loss trails the spot price by. 
//  The user can select to also set a take profit in his order with parameter "Take Profit (pips)". 
//
// -------------------------------------------------------------------------------------------------

using cAlgo.API;

namespace cAlgo.Robots.Samples
{
    [Robot("Sample Sell Trailing Robot", TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSellTrailing : Robot
    {
        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }

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

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

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SampleSellTrailing", StopLoss, TakeProfit);
        }

        protected override void OnTick()
        {
            var position = Positions.Find("SampleSellTrailing");

            if (position == null)
            {
                Stop();
                return;
            }

            var distance = Symbol.Ask - position.EntryPrice;
            if (distance >= Trigger * Symbol.PipSize)
            {
                double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }
    }
}

 

how can i use this code in my robot? can you help me?, i just newer. thanks


@nguyendan81985

nguyendan81985
11 Apr 2020, 04:18

RE:

Spotware said:

Dear Trader,

If want your vertical lines to show whenever the red line crosses the blue line, you could try the following code instead

            if (TenkanSen.HasCrossedAbove(KijunSen, 0) || TenkanSen.HasCrossedBelow(KijunSen, 0))
            {
                ChartObjects.DrawVerticalLine(index.ToString(), MarketSeries.OpenTime[index], Colors.Red, 1, LineStyle.Solid);
            }

Best Regards,

cTrader Team

hi. can you help me to make a Text (Excample: "Cross") at position when Tenkan cross Kijun?


@nguyendan81985

nguyendan81985
14 Feb 2019, 18:38

i need this tool also, pls help to update in next revision ctrader. thanks


@nguyendan81985

nguyendan81985
21 Jan 2019, 14:37 ( Updated at: 21 Dec 2023, 09:21 )

i have same problem. pls help.


@nguyendan81985

nguyendan81985
03 Dec 2018, 12:36

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

You get this error because haOpen is not declared. I am a bit confused about what you are trying to do. Why don't you just call the indicator from within the cBot?

Best Regards,

Panagiotis

Hi Panagiotis Charalampous,

as my first post, i want to wite Cbot as example:

If haopen(1) > ema34(1)

Buy Open ...

and i want to declare haopen, haclose ... value. but there is error and i dont know to fix it. 


@nguyendan81985

nguyendan81985
03 Dec 2018, 11:32

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

Heikin Ashi are currently not available in cTrader Desktop and cTrader Automate API. If you are using the Heikin Ashi indicator, then you need to develop an Output parameter first before using in in your cBot.

Best Regards,

Panagiotis

Hi Panagiotis Charalampous,

 

i am using HA indicator as link: https://ctrader.com/algos/indicators/show/60

and i convert to Cbot as code below, and ther is Error when Cbot running: "Error CS0103: The name 'haOpen' does not exist in the current context"

Could you help me to fix it?

protected override void OnBar()
        {
            var open2 = MarketSeries.Open.Last(2);
            var high2 = MarketSeries.High.Last(2);
            var low2 = MarketSeries.Low.Last(2);
            var close2 = MarketSeries.Close.Last(2);

            var open1 = MarketSeries.Open.Last(1);
            var high1 = MarketSeries.High.Last(1);
            var low1 = MarketSeries.Low.Last(1);
            var close1 = MarketSeries.Close.Last(1);

            var haClose1 = (open2 + high2 + low2 + close2) / 4;
           var haClose = (open1 + high1 + low1 + close1) / 4;

            if (haOpen > 0)
            {
                haOpen = (haOpen + haClose1) / 2;
            }
            if (haOpen == 0)
            {
                haOpen = (open1 + close1) / 2;
            }

            var haHigh = Math.Max(Math.Max(high1, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low1, haOpen), haClose);
            
        }

 


@nguyendan81985

nguyendan81985
02 Dec 2018, 08:28

RE:

irmscher9 said:

Ok, I maanged to put it together myself through "onbar":

 

protected override void OnBar()
        {
            var open2 = MarketSeries.Open.Last(2);
            var high2 = MarketSeries.High.Last(2);
            var low2 = MarketSeries.Low.Last(2);
            var close2 = MarketSeries.Close.Last(2);

            var open1 = MarketSeries.Open.Last(1);
            var high1 = MarketSeries.High.Last(1);
            var low1 = MarketSeries.Low.Last(1);
            var close1 = MarketSeries.Close.Last(1);

            haClose1 = (open2 + high2 + low2 + close2) / 4;
            haClose = (open1 + high1 + low1 + close1) / 4;


            if (haOpen > 0)
            {
                haOpen = (haOpen + haClose1) / 2;
            }
            if (haOpen == 0)
            {
                haOpen = (open1 + close1) / 2;
            }

            haHigh = Math.Max(Math.Max(high1, haOpen), haClose);
            haLow = Math.Min(Math.Min(low1, haOpen), haClose);
            
        }

hi, 

i copied your code, but there is error: "Error CS0103: The name 'haOpen' does not exist in the current context"

did you fix it? 

 


@nguyendan81985

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

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

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

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

nguyendan81985
09 Nov 2018, 04:08

RE:

thanks, I'm trying, but in visual backtesting, There is not function to open BUY/sell position, Right?

 

Panagiotis Charalampous said:

Hi nguyendan81985,

You can check Visual Backtesting which included in Spotware Beta.

Best Regards,

Panagiotis

 


@nguyendan81985

nguyendan81985
28 Oct 2018, 06:20 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi nguyendan81985,

See below

using System;
using System.Collections.Generic;
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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        ExponentialMovingAverage _ema;
        protected override void OnStart()
        {
            _ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, 34);
        }

        protected override void OnTick()
        {
            if (MarketSeries.Close.HasCrossedAbove(_ema.Result, 1))
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000);
            if (MarketSeries.Close.HasCrossedBelow(_ema.Result, 1))
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis

Thank Panagiotis,

i used your method to use with CCI, but the code is not working as I want:

could you give me an example: CCI crossed above or below 100 value will do some thing... My coding as below. and i also attached the picture the result of my coding. could you help me to review and advise if something is wrong

private CommodityChannelIndex cci;

protected override void OnStart()
        {
            cci = Indicators.CommodityChannelIndex(14);
            

        }

        protected override void OnBar()
        {


            if (cci.Result.HasCrossedBelow(100.0, 1))
                Print("CCI cross below 100");

            if (cci.Result.HasCrossedBelow(200.0, 1))
                Print("CCI cross below 200");

}

 


@nguyendan81985

nguyendan81985
27 Oct 2018, 06:57

hi guy,

 

any update for this issue?


@nguyendan81985