Replies

ChrisSpire
12 Feb 2024, 21:19 ( Updated at: 13 Feb 2024, 06:51 )

RE: Positions.Opened event and NullReferenceException

Thank you!

ctrader.guru said: 

Try this approach https://github.com/cTrader-Guru/CallBack-Example/blob/master/CallBack%20Example/CallBack%20Example.cs

 

/*  CTRADER GURU    Homepage    : https://ctrader.guru/    Telegram    : https://t.me/ctraderguru    Twitter     : https://twitter.com/cTraderGURU/    Facebook    : https://www.facebook.com/ctrader.guru/    YouTube     : https://www.youtube.com/cTraderGuru    GitHub      : https://github.com/ctrader-guru    LinkedIn    : https://www.linkedin.com/company/ctrader-guru/    Discord     : https://discord.gg/yE5XE3z5Nz*/using System;using cAlgo.API;using cTraderGuru.CallbackExample;namespace cAlgo.Robots{    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]    public class CallBackExample : Robot    {                protected override void OnStart()        {                    }        protected override void OnTick()        {            HelloWorld.Show(Print, Stop) ;        }        protected override void OnStop()        {        }    }}namespace cTraderGuru.CallbackExample {    public static class HelloWorld {        public static void Show(Action<string> MyPrint, Action MyStop) {            MyPrint( "Hello World cTrader Guru!" );            MyStop();                }        }}

 


@ChrisSpire

ChrisSpire
10 Feb 2024, 12:06

RE: RE: RE: Positions.Opened event and NullReferenceException

PanagiotisCharalampous said: 

Hi there,

You cannot use the Print method inside the ClassLibrary since it does not have access to the log. You should rewrite your code so that the message printing takes place inside the main cBot.

Best regards,

Panagiotis

Thanks! Works well!


@ChrisSpire

ChrisSpire
09 Feb 2024, 17:19

RE: Positions.Opened event and NullReferenceException

I have updated the original post (awaiting moderation).

PanagiotisCharalampous said: 

Hi there,

Please share with us the complete cBot code and steps how to reproduce this exception.

Best regards,

Panagiotis

 


@ChrisSpire

ChrisSpire
03 Feb 2024, 15:51

RE: Getting exact money value of stop loss

ctrader.guru said: 

You can also remove the class that is no longer needed now:

(...)

Thank you! Sorry for bugging but I still need some adjustment for indices. I get wrong results (they are 10x too big) for GER40 or US100. Methods name have changed but the code inside is the same as yours.

I added another variant for 2 digits:

        switch (cBot.Symbol.Digits)
        {
            case 2:
                multy = 1000;
                break;
            case 3:
                multy = 0.1;
                break;
            case 4:
            case 5:
                multy = 10;
                break;
        }

But now XAUUSD is broken (gives 10x to small result). Is there a way to do this properly or I should make adjustments for each symbol (sound quite insane)?

 


@ChrisSpire

ChrisSpire
03 Feb 2024, 15:04

RE: Daily profit

firemyst said: 

//Get the historical trade stuff.
HistoricalTrade ht = History.FindLast(yourposition.Label, yourposition.SymbolName, yourposition.TradeType);

If you are looking for a daily profit from all your closed trades, you can try:

var DailyProfit = History.Where(x => x.ClosingTime.Date == Server.Time.Date).Sum(x => x.NetProfit);

if you want to filer just the current symbol (where you run cBot):

var DailyProfit = History.Where(x => x.ClosingTime.Date == Server.Time.Date).Where(x => x.Symbol == Symbol).Sum(x => x.NetProfit);

if you want to sum open positions profit try (for all symbols):

var OpenPositionsProfit = Positions.Sum(x => x.NetProfit);

@ChrisSpire

ChrisSpire
07 Jan 2024, 11:05

RE: Getting exact currecny value of stop loss

That is exactly my goal, thank you. 

Nevertheless your example still gives me different values. See screenshot (backtesting) below based on copy/paste of your code to new cBot with just ExecuteMarketOrder added;

ctrader.guru said: 

I am not sure I understand what you mean but I will try to answer with a concrete example and maybe start from this example to better understand what you need:

using cAlgo.API;using cAlgo.API.Internals;using System;namespace cAlgo.Robots{    [Robot(AccessRights = AccessRights.None)]    public class TradesRisky : Robot    {        class Risky        {            public double Pips { get; set; }            public double Commissions { get; set; }        }        protected override void OnStart()        {            Positions.Modified += OnPositionModified;            UpdateData();        }        protected override void OnTick()        {            UpdateData();        }        public void OnPositionModified(PositionModifiedEventArgs args)        {            UpdateData();        }        private void UpdateData() {            Risky MyRisky = GetTotalPipsRisky();            double MoneyRisky = GetPipValueInMoney(MyRisky.Pips);            Chart.DrawStaticText("Risky",                                 string.Format("You are risking {0} pips for a total value of {1} {2} and {3} {2} commissions.", MyRisky.Pips, MoneyRisky, Account.Asset, MyRisky.Commissions),                                 VerticalAlignment.Top,                                 HorizontalAlignment.Left,                                 Color.Red                                 );        }        // This function can be improved        private double GetPipValueInMoney(double pips)        {            return Math.Round(pips * Symbol.PipValue / ( Symbol.PipSize * 100), 2);        }        private Risky GetTotalPipsRisky()        {            double pips = 0;            double commissions = 0;            Position[] MyPositions = Positions.FindAll(null, SymbolName);            foreach (Position position in Positions)            {                if (position.StopLoss != null && position.StopLoss > 0) pips += Math.Abs((double)position.StopLoss - position.EntryPrice);                commissions += position.Commissions;            }            return new Risky            {                Pips = DigitsToPips(pips),                Commissions = commissions            };        }        private double DigitsToPips(double pips)        {            return Math.Round(pips / Symbol.PipSize, 2);        }    }}

 


@ChrisSpire

ChrisSpire
01 Nov 2023, 11:58 ( Updated at: 02 Nov 2023, 06:49 )

RE: CRASHES: Positions.Where(x => x.Symbol == Symbol).Where(x => x.Label.Contains('_')).Count()

Thank you!

PanagiotisChar said: 

Hi there, 

You should check if Label is null before calling it. See below

            var CrazyPositionsExist = Positions.Where(x => x.Symbol == Symbol).Where(x => x.Label != null && x.Label.Contains('_')).Count() > 0 ? true : false;

 


@ChrisSpire