Replies

driftingprogrammer
09 Mar 2020, 18:55 ( Updated at: 21 Dec 2023, 09:21 )

RE: RE:

noeyamn said:

PanagiotisCharalampous said:

Hi noeyamn,

You did not answer my question.  Why do you add references manually? I installed SQLite using Package Manager and I have no problems. See below

Everything seems fine and the cBot builds without a problem.

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis,

I manually load the assembly to avoid the exception "Unable to load DLL 'SQLite.Interop.dll'" when running the bot only (compilation was succesful) 

The exception happens to me when starting the bot.

Did you try running the bot you showed on the screenshot ?

___________________________

I have just restarted from scratch with a brand new bot and the error is different now :

=> Crashed in OnStart with TypeInitializationException: The type initializer for 'System.Data.SQLite.UnsafeNativeMethods' threw an exception.

The error happens when starting bot. Compilation is succesful

When placing a breakpoint, the error happens on

_SqliteConnection = new SQLiteConnection("Data Source=" + dataBasePath + "; Version=3; New=True;");

It is a security exception

 

 

This security exception typically happens when you have not given AccessRights.FileSystem to your cbot.


@driftingprogrammer

driftingprogrammer
08 Mar 2020, 23:36

More Details

Here is another post complaining that they could not connect to SQLite database - 

 

I have mentioned in this post - 

how i had to use old libraries for connection to postgresql but similar solution did not work for SQLite.

I am using Visual Studio 2017, are we suppose to use 2019 version?

 

 

Thanks.


@driftingprogrammer

driftingprogrammer
06 Mar 2020, 22:08

Such an important feature missing.

Optimization runs for days, it is ridiculous that i keep lossing that data. We need a way to export it to CSV.


@driftingprogrammer

driftingprogrammer
25 Feb 2020, 08:00

Making development easy

The first time I open metatrader and the first item it shows me under the tab Article is - SQLITE: NATIVE HANDLING OF SQL DATABASES IN MQL5

Where they have given step by step instructions on how to achieve what i have been trying to achieve in cTrader without any success.

And so far cTrader does not even seem to bother to acknowledge weather they support SQLite, or they intend to to sometime in the future.

Is there a reason why one would not conclude from this example that MT5 ecosystem is way more mature for people interested in algorithmic trading.


@driftingprogrammer

driftingprogrammer
19 Feb 2020, 22:28

cTrader Market Sentiment formula

Hi,

When ctrader is showing the market sentiment I am assuming it is using the same data that is being provided in the API through the object MarketDepth.

Is that right? That would mean cTrader is just applying some formula on it.

I am wondering if the below formula is something similar to how cTrader calculates the market sentiment

1. Add all the Volumes*Prices pairs and lets call it AskTotal and BidTotal

2. Multiply the Symbol Bid Price with the total volume of Bids and see the difference from BidTotal

3. Multiply the Symbol Ask Price with the total volume of Asks and see the difference from AskTotal

4. If the difference is higher incase of bids then it is a strong sell sentiment, else it is a buy sentiment

5. It could also keep a track of last x number of entries to get a sense of that timeframe.

 

Does this concept of market sentiment make any sense.

Please let me know.

Cheers,

Vipin. 


@driftingprogrammer

driftingprogrammer
19 Feb 2020, 10:32

Thanks a lot

It all makes sense now. Thanks.


@driftingprogrammer

driftingprogrammer
19 Feb 2020, 06:14

Made a guess

Hi,

Since there is no documentation I just made a guess reading a sentence each for the 4 types in the API.

My understanding is that when the stop loss is hit then this property will open a new trade in the opposite direction based on certain condition.

Is that not correct?

 

Thanks in advance,

Warm Regards,

Vipin


@driftingprogrammer

driftingprogrammer
18 Feb 2020, 17:20

Sure

itchamp84 said:

hi there,

can someone please help in coding a cbot which opens new order which is double of current order if it goes 2 pips against the trade please?

Let me try to explain, 

I put a buy order of 10k at $1.2222

market moves towards sell and it comes to $1.2220, which means my account is in loss of two pips right now. 

At this time I want to open another order of $20K in the same direction as original order 

Thanks

 

 

What help were you looking for


@driftingprogrammer

driftingprogrammer
16 Feb 2020, 14:29

Visual Studio Editor Warnings and errors

buccinator said:

admin said:

You may extend the class name of the robot you are trying to reference. 

 

//#reference: ..\Robots\test_Referenced.algo
// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot]
    public class test_ReferenceRobot : test_Referenced
    {
        protected override void OnStart()
        {
        	base.Volume = 100;
            base.OnStart();
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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


 

// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot]
    public class test_Referenced : Robot
    {
        [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }


        protected override void OnStart()
        {
            Print("test_Referenced Volume {0}", Volume);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

 


 

 

for anyone interested it doing this it seems you have to remove the [Robot] line from the base class.

https://ctrader.com/forum/calgo-support/3827?page=1

Following the steps mentioned here Visual Studio Code Editor continues to show compilation errors (although the build does not throw any errors and is successful) which makes writing code extremely annoying in Visual Studio.

Is there i recommended way to get rid of these compilation warnings inside Visual Studio 2017.

 

Thanks in advance,

Cheers,

Vipin.


@driftingprogrammer

driftingprogrammer
16 Feb 2020, 13:02

I am not sure of external libraries can be imported to cTrader

nordic said:

ClickAlgo said:

Hi Nordic,

Panagiotis is correct, if you compile with VS and the red asterisk on the cBot disappears then it is compiled, make sure you are looking at your errors tab in VS.

I compile using only VS on a regular basis for cBots, indicators and Add-Ons.

Paul.

Thanks guys, appreciate it (I was ignoring the VS errors tab).  I think I've found the culprit, which seems to be my insistence to use System.ValueTuple as a method return type.  Of course there are workarounds, but I've been trying to get this particular function to work as it simplifies the class library I'm building.

VS will happily build the below code with no errors in a project created from scratch, but not with a cBot launched out of cTrader (target framework set to 4.7.1):

private (double A, double B) TupleMethod()
        {
            return (2.1, 7.6);
        }

When I remove the implicit ValueTuple return type, VS builds the bot and it can be used as normal.  I think this may be a bug?

At any rate, thanks for the help and patience!

 

Hi,

This post would suggest that it is a breeze to just upgrade the project properties to .NetFramework 4.6.1 and download all the latest packages from nuget, compile the code in Visual Studio and expect it to run like a breeze inside cTrader.

My experience has suggested otherwise and it is elaborated on these 2 posts

 

 

I feel like i might be missing something.

 

Thanks,

Vipin.


@driftingprogrammer

driftingprogrammer
16 Feb 2020, 12:46

I think Robot is still stuck with .NetFramework 4 Client Profile

I am not sure i understand how to move my robot to higher versions of .Net Framework.

Here is a post which complains that i am unable to use SQLite from my cbot. - 

When i tried using Postgres i did what is recommended, take a fresh robot, open in Visual Studio 2017, change the .net framework in the project properties to 4.6.1 from 4.0 Client Profile.

This enables me to get the latest Postgres package from nuget.

But while running this as a console application would be fine when i run it as a robot i get the following error

 

'FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral

 

In ioder to fix that i need to create a new project and not increase its .Net Framework version but rather select a older version of the postgres package on nuget to be able to install it on my project.

After compilation now when i run my robot it is able to connect to the postgres database from inside cTrader.

(Although this methodology did not fix the SQLite connectivity issue) This shows to me that although one can compile the code using latest packages from nuget, if it needs to actually be able to run in cTrader it needs to run against the version of those tools that were built for the older version 4 Client Profile to be specific in this case.

jaredthirsk said:

I think this discussion as a prerequisite needs to understand in-place upgrades for .NET 4.  Here are some articles from 2012 that perhaps give an adequate overview:

 

https://www.hanselman.com/blog/NETVersioningAndMultiTargetingNET45IsAnInplaceUpgradeToNET40.aspx

https://weblog.west-wind.com/posts/2012/Mar/13/NET-45-is-an-inplace-replacement-for-NET-40

 

Suggestion for Spotware: for your new Robot/Indicator csproj project templates, change the default to something from this era, like 4.6.1 or 4.7.2 or 4.5 or something. 4.0 Client Profile may technically work (if I understand it right?) but it sounds like people have problems with nuget (?) so it might be better to at least go to 4.5.

 


@driftingprogrammer

driftingprogrammer
16 Feb 2020, 12:43

RE:

driftingprogrammer said:

Hi,

I am trying to embed a lightweight database in my robots so they can communicate through that database and log event in the database and also create a sort of audit log since at this point i have not found any way of saving the log outputs in cTrader Automate.

I have changed the .net Framework to 4.6.1 in Visual Studio and imported the System.Data.Sqllite package using Nuget Package.

Theoretically speaking this is all i need to do and now i can start invoking SQLite API's to create file based database and access them.

I can write a console application on Visual Studio and everything goes fine, i can see my dlls are being copied to the bin/Debug folder.

But when i run my robot (it has compiled fine since i have imported SQLite using Nuget Package in Visual Studio 2017)

I get the below error

"We have an exception Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

 

I have spent a lot of time but now i think i am going in circles none of the solutions mentioned here worked for me - 

 

Any guidance would be appreciated, or is there some other recommend database that can be used by the robot, please let me know.

Thanks in advance,

Warm Regards,

Vipin,
 

I have now tried doing the same thing as mentioned above but without changing the .NetFramework to version 4.6.1 . For .NetFramework 4 Client Profile itself i downloaded the System.Data.Sqlite package but still when i try to create a connection from within the robot it throws the below exception.

 

Crashed in OnStart with DllNotFoundException: Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
 

In my experience with Postgres , I was getting an error when my .NetFramework was 4.6.1 so i had to downgrade it back to 4 Client Profile and rather choose a much older version of the postgres package on nuget, the one that would install with .NetFramework 4 Client Profle, and then things worked fine for me, i was able to create connections to the database.

So in a way i was hoping that the reason for the above failure might have been me upgrading the .Net Framework. But no luck here, here the robot fails to connect to the database although I have used the packages from nuget which are compatible with .NetFrameowork 4 Client Profile.


@driftingprogrammer

driftingprogrammer
01 Feb 2020, 16:28

Is this not available in API already

Hi,

The documentation says 

public TradeResult ExecuteMarketOrder(TradeType tradeType, string symbolName, double volume, string label, double? stopLossPips, double? takeProfitPips, string comment, bool hasTrailingStop)

So it seems by setting the last boolean parameter to true cAlgo will implement the trailing stop loss so I am not able to understand in which situation will one need to use the code that is provided in this thread.

Please let me know.

Thanks in advance,

Warm Regards,

Vipin.

 

PanagiotisCharalampous said:

Hi Jan,

See the example below

// -------------------------------------------------------------------------------------------------
//
//    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
//
//    The "Trailing Stop Loss Sample" Robot places a Buy or Sell Market order according to user input.  
//    When the order is filled it implements trailing stop loss.
//
// -------------------------------------------------------------------------------------------------

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 TrailingStopLossSample : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }
        [Parameter("Buy")]
        public bool Buy { get; set; }
        [Parameter("Stop Loss", DefaultValue = 5)]
        public double StopLoss { get; set; }
        [Parameter("Trigger When Gaining", DefaultValue = 1)]
        public double TriggerWhenGaining { get; set; }
        [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
        public double TrailingStopLossDistance { get; set; }
        [Parameter("Step (pips)", DefaultValue = 5)]
        public double Step { get; set; }

        private double _highestGain;
        private bool _isTrailing;

        protected override void OnStart()
        {
            //Execute a market order based on the direction parameter
            ExecuteMarketOrder(Buy ? TradeType.Buy : TradeType.Sell, Symbol, Volume, "SampleTrailing", StopLoss, null);
            //Set the position's highest gain in pips
            _highestGain = Positions[0].Pips;
        }

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

            if (position == null)
            {
                Stop();
                return;
            }
            //If the trigger is reached, the robot starts trailing
            if (position.Pips >= TriggerWhenGaining)
            {         
                //Based on the position's direction, we calculate the new stop loss price and we modify the position
                if (position.TradeType == TradeType.Buy)
                {
                    var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance);
                    if (newSLprice > position.StopLoss)
                    {
                        ModifyPosition(position, newSLprice, null);
                    }
                }
                else
                {
                    var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
                    if (newSLprice < position.StopLoss)
                    {
                        ModifyPosition(position, newSLprice, null);
                    }
                }
                TriggerWhenGaining += Step;
            }
        }

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

Best Regards,

Panagiotis

 


@driftingprogrammer

driftingprogrammer
31 Jan 2020, 12:59

Thanks a lot

Thanks so much for this, this is such an integral need for a robot that it should be built into the API.

 

Jiri said:

Hi, there isn't built-in function called on each bar except from the market series of the chart that you can override. However, you can write your own function for each series.

using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OnBar : Robot
    {
        private MarketSeries seriesMinute1, seriesMinute15, seriesMinute30;
        private int lastIndexMinute1, lastIndexMinute15, lastIndexMinute30;

        protected override void OnStart()
        {
            seriesMinute1 = MarketData.GetSeries(TimeFrame.Minute);
            seriesMinute15 = MarketData.GetSeries(TimeFrame.Minute15);
            seriesMinute30 = MarketData.GetSeries(TimeFrame.Minute15);
        }

        protected override void OnTick()
        {
            int currentIndexMinute1 = seriesMinute1.Open.Count - 1;
            int currentIndexMinute15 = seriesMinute15.Open.Count - 1;
            int currentIndexMinute30 = seriesMinute30.Open.Count - 1;

            if (currentIndexMinute1 > lastIndexMinute1)
            {
                OnBarMinute1(currentIndexMinute1);
                lastIndexMinute1 = currentIndexMinute1;
            }

            if (currentIndexMinute15 > lastIndexMinute15)
            {
                OnBarMinute15(currentIndexMinute15);
                lastIndexMinute15 = currentIndexMinute15;
            }

            if (currentIndexMinute30 > lastIndexMinute30)
            {
                OnBarMinute30(currentIndexMinute30);
                lastIndexMinute30 = currentIndexMinute30;
            }
        }

        private void OnBarMinute1(int index)
        {
            // Called on each new bar of Minute1 dataseries
        }

        private void OnBarMinute15(int index)
        {
            // Called on each new bar of Minute15 dataseries
        }

        private void OnBarMinute30(int index)
        {
            // Called on each new bar of Minute30 dataseries
        }
    }
}

 

 


@driftingprogrammer

driftingprogrammer
20 Jan 2020, 14:37

RE:

I looked at the link you sent and still did not find any such code sample.

And thanks for letting me know it is not possible to trigger a cBot from an Indicator, in the absence of any best practices or architecture document one has to keep guessing on how to accomplish things.

 

Thanks.

 

PanagiotisCharalampous said:

Hi driftingprogrammer,

There is no way to trigger cBots from an indicator therefore you should not expect to find documentation for something that does not exist. If you want to reference an indicator you can find several examples in our API Guides as well as our cBot samples that come with every cTrader installation.

Regarding your complaint about not helping traders, a simple look around the forum will refute your claim. We try to help traders to use cTrader and we try to answer every possible question. However there is always a thin line between answering questions to help traders to move to the right direction and a "Please do a,b,c for me" request. As Spotware we cannot engage into custom request development. If I misunderstood Matteo's question then I apologize and I am here to help him further. As I said above there are a lot of samples on how to reference an indicator from a cBot in the sample cBots in cTrader Automate.

Best Regards,

Panagiotis 

Join us on Telegram

 

 


@driftingprogrammer

driftingprogrammer
20 Jan 2020, 14:02

Humiliating answer

The one response to the question here is humiliating. First cTrader has the worst documentation, in all the examples of cBots in their documentation there is absolutely not the slightest mention of how to have cBots triggered by an Indicator. And when someone has come to the forum to ask a legitimate question the answer being given is 'IF YOU DONT KNOW HOW TO CODE THEN RENT A CODER'.

Is that the purpose of this forum, Pimping consultants.


@driftingprogrammer

driftingprogrammer
19 Jan 2020, 14:45

Still no documentaion

Thanks for this thread, 7 years later people are still looking for these 2 lines since they have still not been added to the API.


@driftingprogrammer

driftingprogrammer
21 Dec 2019, 13:16

Hi,

I am facing the same problem in my Indicator.

Half the year the server runs on UTC+2 time zone and half the year it runs on UTC+3 time zone. The transition happens randomly during a certain weekend.

When my Indicator is invoked for MarketSeries.OpenTime.LastSeries being 0030hr on 4th november, i want to know if it is in UTC+2 timezone or UTC+3 timezone, or in other words i want to know if the daylight saving is on or off because only then i can know if the data is applicable to 3rd November or 4the November.

 

Any help is appreciated.

 

Thanks in advance,

Warm Regards,

Vipin.


@driftingprogrammer