Topics
16 Feb 2016, 07:16
 7172
 7
15 Aug 2015, 14:47
 5214
 15
28 Apr 2015, 14:20
 8036
 18
26 Feb 2015, 07:19
 2964
 2
23 Oct 2014, 13:52
 2281
 2
05 Sep 2014, 00:08
 0
 3107
 3
19 Aug 2014, 13:23
 2658
 3
Replies

hiba7rain
08 Aug 2018, 10:01

it would be appreciated if someone can add the draw. Text code to this so that it presents the value of differences on the chart 


@hiba7rain

hiba7rain
25 Jul 2018, 07:35

RE: RE:

hiba7rain said:

Panagiotis Charalampous said:

Hi hiba7rain,

It is not. You did not put the brackets.

oops yes i forgot the brackets :)

 

thanks alot for the correction 

ill give it a try and see how it works 

its still with this code having issue of not sending emails and if i remove it the indicator keeps sending multiple times 

ill try to put the same indicator logic in Cbot and see how it works althought that the purpose should be indicator and not cBot 

maybe you cank share different code with us for indicators to send email only once other than the provided above ?

thanks 


@hiba7rain

hiba7rain
24 Jul 2018, 10:41

RE:

Panagiotis Charalampous said:

Hi hiba7rain,

It is not. You did not put the brackets.

oops yes i forgot the brackets :)

 

thanks alot for the correction 

ill give it a try and see how it works 


@hiba7rain

hiba7rain
24 Jul 2018, 10:36

RE:

Panagiotis Charalampous said:

This is because you set _emailSent to true even if no email was sent. Change it to the following

                if ( !_emailSent)
                 {
                     Notifications.SendEmail("XXX@XX.com", "XXX@XX.com", Symbol.Code + "Signal", "XXXXX");
                     _emailSent = true;
                 }

 

sorry i didnt get you, its the same code I did 

do you mean to set it to false?


@hiba7rain

hiba7rain
24 Jul 2018, 10:18

RE:

Panagiotis Charalampous said:

Hi hiba7rain,

What do you mean what you say it is not working? Is the email not sent? Is it sent multiple times? Alos note that I use a cBot and not an indicator,

Best Regards,

Panagiotis

Hi 

yes i noticed that you using Cbot coding 

my first issue was emails alerts are sent multiple times, so I added the code as i showed to you above (as to flag the emails sent out and stop sending multiple times) but it did not work at all no emails sent   


@hiba7rain

hiba7rain
24 Jul 2018, 10:10

RE:

Panagiotis Charalampous said:

Hi hiba7rain,

You could raise a flag as soon as you send a notification and check the flag in order not to send it again. See below an example

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 NewcBot : Robot
    {
        private bool _notificationSent;
        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            var sendNotification = false;
            //  Make all your checks here and update sendNotification variable accordingly
            //   .
            //   .
            //   .
            if (sendNotification && !_notificationSent)
            {
                Notifications.SendEmail("email", "email", "Subject", "Text");
                _notificationSent = true;
            }
        }
        protected override void OnBar()
        {
            _notificationSent = false;
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis

Thanks Panagiotis,

I will try to fix it as you mentioned but need your advise as I have tried to do the same logic as below but did not work with me so what was missing or mistake I made on below code 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SignalINdi : Indicator
    {


        private bool _emailSent;

protected override void Initialize()
        {
           

            //

         
        }

public override void Calculate(int index)
        {
            
        


            
            if ((close2 < JV4) && (close1 > JV3))
            {
                
                SSig[index - 1] = MarketSeries.Median[index] - Symbol.TickSize * 20;
                
                
                if ( !_emailSent)
                    Notifications.SendEmail("XXX@XX.com", "XXX@XX.com", Symbol.Code + "Signal", "XXXXX");
                
                _emailSent = true;
                

            }

 


@hiba7rain

hiba7rain
30 Aug 2017, 17:02

Thanks for your support I'll check it
@hiba7rain

hiba7rain
28 Aug 2017, 21:01

Thanks but still can't get it
@hiba7rain

hiba7rain
04 Jul 2017, 14:48

RE:

thanks G for your support 

I think I did it worng as below 

i have question what would be the difference  between label if set as

[Parameter(DefaultValue = "TcBot")]
public string cBotLabel { get; set; }

​Or to use this 

private const string label = "TcBot"

 

protected override void OnTick
        {
           var netProfit = 0.0;
            foreach (var openedPosition in Positions)
            {
                netProfit += openedPosition.NetProfit + openedPosition.Commissions;
            }        
            {
                if (Account.Equity - Account.Balance >= Aprofit)
                {

                    foreach (var openedPosition in Positions)
                    {
                      ClosePosition(openedPosition);
                        foreach (var pendingOrder in PendingOrders)
                        {
                            CancelPendingOrder(pendingOrder);
                        }
                    }
                }
            }

 

tmc. said:

Using LINQ queries.

var positions = Positions.Where(position => position.Label == cBotLabel).ToList();

if (positions.Sum(position => position.GrossProfit) >= Aprofit)
{
    positions.ForEach(position => ClosePositionAsync(position));
}

 

 


@hiba7rain

hiba7rain
20 Jun 2017, 15:34

RE:

Thanks payment 

it was nice of you 

now i will see if i can make it as drawn object on the chart 

payment said:

When you run this it gives you 3 values every tick

1. Distance between MA

2. Distance to SLOW MA

3. Distance to FAST MA

 

Just look under Log when you run it

 

--------------------

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 TrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        [Parameter("Slow Periods", DefaultValue = 50)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend Robot";

        public double distance = 0;
        public double distance1 = 0;
        public double distance2 = 0;


        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

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

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);


            distance = Math.Round(Math.Abs((currentSlowMa - currentFastMa) / Symbol.PipSize));
            distance1 = Math.Round(Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));
            distance2 = Math.Round(Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));


            Print("DISTANCE BETWEEN MA=" + distance + "\tSLOW=" + distance1 + "\tFAST=" + distance2);



            //Print(currentSlowMa);
            //Print(currentFastMa);

        }
    }
}

 


@hiba7rain

hiba7rain
20 Jun 2017, 14:24

RE:

how would i get also the distance between current price ant the moving avarege say for example the slow MA

payment said:

Actually change the FAST to SLOW and SLOW TO FAST  then it's correct

 

Print("DISTANCE BETWEEN MA=" + distance + "\tSLOW=" + distance1 + "\tFAST=" + distance2);

 

 

Look under LOG when you run it and it will give you the distances every tick

 


@hiba7rain

hiba7rain
20 Jun 2017, 14:21

RE:

Thanks Payment 

ill test it 

payment said:

TRY THIS ONE I TESTED IT

 

----------------------------------


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 TrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        [Parameter("Slow Periods", DefaultValue = 50)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend Robot";

        public double distance = 0;
        public double distance1 = 0;
        public double distance2 = 0;


        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

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

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);


            distance = Math.Round(Math.Abs((currentSlowMa - currentFastMa) / Symbol.PipSize));
            distance1 = Math.Round(Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));
            distance2 = Math.Round(Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));


            Print("DISTANCE BETWEEN MA=" + distance + "\tFAST=" + distance1 + "\tSLOW=" + distance2);

            //Print(currentSlowMa);
            //Print(currentFastMa);

        }
    }
}

 


@hiba7rain

hiba7rain
20 Jun 2017, 13:40

RE:

Thanks Payment that was helpfull much appriciated 

if to use onTick what will be changed

and how to get the disstance between the current price and one of the moving averages  

payment said:

HERE IS THE SAME CODE BELOW WITH THE BITS I'VE ADDED UNDERLINED:

 

---------------------------

 

 

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("MA Type")]

        public MovingAverageType MAType { get; set; }

 

        [Parameter()]

        public DataSeries SourceSeries { get; set; }

 

        [Parameter("Slow Periods", DefaultValue = 10)]

        public int SlowPeriods { get; set; }

 

        [Parameter("Fast Periods", DefaultValue = 5)]

        public int FastPeriods { get; set; }

 

        [Parameter(DefaultValue = 10000, MinValue = 0)]

        public int Volume { get; set; }

 

        private MovingAverage slowMa;

        private MovingAverage fastMa;

        private const string label = "Sample Trend Robot";

 

public double distance = 0;

public double distance1 = 0;

public double distance2 = 0;

 

        protected override void OnStart()

        {

            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);

            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);

        }

 

        protected override void OnBar()

        {

            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);

            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

 

            var currentSlowMa = slowMa.Result.Last(0);

            var currentFastMa = fastMa.Result.Last(0);

            var previousSlowMa = slowMa.Result.Last(1);

            var previousFastMa = fastMa.Result.Last(1);

 

 

distance = Math.Abs((currentSlowMa - currentFastMa) / symbol.PipSize);

distance1 = Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / symbol.PipSize);

distance2 = Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / symbol.PipSize);

 

 

Print(distance + " D1=" + distance1 + " D2=" + distance2);

 

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)

            {

                if (shortPosition != null)

                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);

            }

            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)

            {

                if (longPosition != null)

                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);

            }

        }

    }

}

 


@hiba7rain

hiba7rain
19 Jun 2017, 12:31

any support is much appriciated 

Thanks 


@hiba7rain

hiba7rain
25 May 2016, 08:03

RE:

how to reference it to be used on cbot?

tmc. said:

Let me know if it returns same values as original indicator.

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SlopeDirectionalLine : Indicator
    {
        [Parameter("Period", DefaultValue = 80)]
        public int Period { get; set; }
        [Parameter("Type", DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType MAType { get; set; }

        [Output("Result", Color = Colors.White)]
        public IndicatorDataSeries Result { get; set; }
        [Output("UpTrend", PlotType = PlotType.DiscontinuousLine, Color = Colors.Green)]
        public IndicatorDataSeries UpTrend { get; set; }
        [Output("DnTrend", PlotType = PlotType.DiscontinuousLine, Color = Colors.Red)]
        public IndicatorDataSeries DnTrend { get; set; }

        private MovingAverage ma1, ma2;

        protected override void Initialize()
        {
            ma1 = Indicators.MovingAverage(MarketSeries.Close, Period, MAType);
            ma2 = Indicators.MovingAverage(MarketSeries.Close, Period / 2, MAType);
        }

        public override void Calculate(int index)
        {
            Result[index] = 2 * ma2.Result[index] - ma1.Result[index];

            bool isUpTrend = Result[index] > Result[index - 1];
            if (isUpTrend)
            {
                UpTrend[index] = Result[index];
            }
            else
            {
                DnTrend[index] = Result[index];
            }
        }
    }
}

 

 


@hiba7rain

hiba7rain
18 Feb 2016, 08:57

how to compare and create indicator data series for a customized indicator?


@hiba7rain

hiba7rain
16 Feb 2016, 19:33

RE:

Thank you tmc

indicatorDaraSeries could you give simple example of it for compression between values

Thanks  

tmc. said:

You need to write the method finding highs and lows first, then you just compare them. However you don't need an array for it, it's better to use IndicatorDataSeries instead. I recommend to look at indicators that are published by others. Or hire someone to code the indicator you want. I'm also freelancer so feel free to contact me via email if you want my service.

 


@hiba7rain

hiba7rain
16 Feb 2016, 16:28

RE: RE: RE: RE: RE: RE: RE: RE: RE:

Thanks Gainer, as always you are very helpful 

i should test the codes 

by the way are experience with array, as you can see am trying to have array code to compare the values of two points say last two high or low points

thanks

 

gainer said:

hiba7rain said:

Thanks Gainer

i will use return after stop and check it again

 

You're the welcome!

If the "Stop()" in your case doesn't work well either, you can call directly the c# command:

  1. add the "using System.Diagnostics" reference
  2. call the c# Process.GetCurrentProcess().Close();
using System;
using System.Diagnostics; // <---- ADD THIS
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 NewcBot : Robot
    {
        int ticks = 0;

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            Print(ticks++);

            if (ticks == 10)
            {
                Process.GetCurrentProcess().Close();
            }
        }

        protected override void OnStop()
        {

        }
    }
}

 

 


@hiba7rain

hiba7rain
16 Feb 2016, 14:51 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Thanks TMC

that's relay helpful

but how if i would like to use the array to compare between two values say for example the last two high points or the last low points 

 

Thanks

tmc. said:

Hi, I hope this sample will help.

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ArraySample : Indicator
    {
        private double[] array;

        protected override void Initialize()
        {
            array = new double[4];

            array[0] = 23.6;
            array[1] = 38.2;
            array[2] = 50;
            array[3] = 61.8;

            foreach (var a in array)
                Print(a);
        }

        public override void Calculate(int index)
        {

        }
    }
}

 


@hiba7rain

hiba7rain
15 Feb 2016, 14:10

RE: RE: RE: RE: RE: RE: RE:

Thanks Gainer

i will use return after stop and check it again

gainer said:

gainer said:

mindbreaker said:

Pips count and stop:

            // profit pips
            double pips = 0;
            foreach (var trade in Positions)
            {
                pips = pips + trade.Pips;
            }
            if (pips > 100)
            {
                Stop();
            }

I used the Stop(); in some of my cBots and I noticed that the better way to obtain an immediate break of the execution is to add also a return; after the stop();

in my case a check to avoid to use live accounts my mistake:

remember to add a reference to "System.Windows.Forms"
-----

protected override void OnStart()
{
    if (Account.IsLive && System.Windows.Forms.MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT !!!\r\n\r\nDo you want to continue anyway ?", label, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No)
    {
        Stop();
        return;
    }
}

 

by the way... I had to use the full declaration "System.Windows.Forms" in the code, to avoid a conflict with another DLL, but the best way to use it is of course this

1) add the reference to "System.Windows.Forms" to your cBot so you can declare the "using"

-----

...

using System.Windows.Forms;

...


if (Account.IsLive && MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT!!!\r\n\r\nDo you want to continue anyway?", label, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
{
    Stop();
    return;
}

...

 

 


@hiba7rain