Newbee questions

Created at 06 Jul 2014, 09:00
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!
neverman's avatar

neverman

Joined 06.07.2014

Newbee questions
06 Jul 2014, 09:00


Hi guys,

I'm trying to transfer my code from MQL to C# for cAlgo and I got some questions about programming.

1. In MQL there is something called Magic Number that identify each order you place. Is there such thing in C#? If yes, any example of code for existing some placed order, or not. The idea is to avoid non stop ordering.

2. I need equivalent of iOpen(Symbol,1,0) in cAlgo OR the code that gives information for open, high, low, close for some symbol, in some minute (or minutes ago like in MQL), and in some period (for example M1).

3. How to collect tick quotes in memory. I suppose there is tick functions for that? In MQL i'm forced to collect every quote in array for future using?

3. I need come code to shows me the possible SL and TP for some placed order.

4. I need a code for placing changes in SL and TP for some order.

5. Of cource for observation needs, I need a code that places datas every tick in CSV file like ask(bid) quotes, server time (hour,minute, second, millisecond), connection speed to the server, current order (type (buy,sell),SL, TP, volume, magicnumber)

6. A code to shows me what quantity of some pair I can order for the money i got.

7. Is some code have compiled and source version in my installation? Where are they to have the ability to save it on other place? If compiled version exist, is it secure?

 

I hope soon to make good tutorial for programming under cAlgo.

P.S. Excuse me if some tutorial exist and didn't find it.

 

Best Regards,

Neverman


@neverman
Replies

Spotware
07 Jul 2014, 11:18

You can find Trading API guide by the following link: /api/guides/trading_api.

1. In MQL there is something called Magic Number that identify each order you place. Is there such thing in C#? If yes, any example of code for existing some placed order, or not. The idea is to avoid non stop ordering.

You can use Label parameter for such purposes. Example of usage label with ExecuteMarketOrder command:

            ExecuteMarketOrder(TradeType.Buy, Symbol, 100000, "My unique label");
            var position = Positions.Find("My unique label");

2. I need equivalent of iOpen(Symbol,1,0) in cAlgo OR the code that gives information for open, high, low, close for some symbol, in some minute (or minutes ago like in MQL), and in some period (for example M1).

You can use MarketData.GetSeries method:

            var dailyEurGbpSeries = MarketData.GetSeries("EURGBP", TimeFrame.Daily);
            var dailyHigh = dailyEurGbpSeries.High.Last(0);
            var dailyLow = dailyEurGbpSeries.Low.Last(0);

3. How to collect tick quotes in memory. I suppose there is tick functions for that? In MQL i'm forced to collect every quote in array for future using?

There is an example: /forum/cbot-support/3049?page=1#2

3. I need come code to shows me the possible SL and TP for some placed order.

Sorry, but we do not understand the question.

4. I need a code for placing changes in SL and TP for some order.

            var position = Positions.Find("my label");
            if (position != null)
            {
                var newStopLossValue = Symbol.Bid - 15 * Symbol.PipSize;
                var newTakeProfitValue = Symbol.Bid + 20 * Symbol.PipSize;
                ModifyPosition(position, newStopLossValue, newTakeProfitValue);
            }

5. Of cource for observation needs, I need a code that places datas every tick in CSV file like ask(bid) quotes, server time (hour,minute, second, millisecond), connection speed to the server, current order (type (buy,sell),SL, TP, volume, magicnumber)

You can find an example of writing to a CSV file there: /algos/cbots/show/495. You will need to put your to code to the OnTick handler. You can retrieve prices from Symbol.Ask and Symbol.Bid properties. Server.Time property has DateTime value which stores hour, minute, second and millisecond. Connection speed is not available in the API. There is no current order entity in our API. You can enumerate all Positions with foreach instruction. All position properties are presented there: /api/reference/position.

6. A code to shows me what quantity of some pair I can order for the money i got.

Sorry, but we do not understand the question.

7. Is some code have compiled and source version in my installation? Where are they to have the ability to save it on other place? If compiled version exist, is it secure?

When you compile source code .algo file is created. If you do not perform "Build with Source Code" command, no source code will be included to the .algo file.

.algo files are encrypted and can be used on other machines without source code.


@Spotware

neverman
07 Jul 2014, 20:47

RE:

3. I need come code to shows me the possible SL and TP for some placed order.

Sorry, but we do not understand the question.

 

6. A code to shows me what quantity of some pair I can order for the money i got.

Sorry, but we do not understand the question.

 

Excuse my english:

3. When I make an order, then its possible to change StopLoss and TakeProfit of this order. On MT4 there was a possible Stoploss and TakeProfit (pips away from current price). Is there something in cTrader and how to get this possible Stoploss and TakeProfit

 

6. How much money i have to possess in mu account to buy the minimum 10k eurusd, or 10k gold ... etc ... newbee question.


@neverman

Spotware
08 Jul 2014, 10:06

3. When I make an order, then its possible to change StopLoss and TakeProfit of this order. On MT4 there was a possible Stoploss and TakeProfit (pips away from current price). Is there something in cTrader and how to get this possible Stoploss and TakeProfit

There is no restriction for minimal pips away from spot prices.

6. How much money i have to possess in mu account to buy the minimum 10k eurusd, or 10k gold ... etc ... newbee question.

In order to calculate required margin you can use the following formula:

        private double GetRequiredMarginForLongPosition(long volume, Symbol symbol)
        {
            return volume / Account.Leverage * symbol.Ask * symbol.TickValue / symbol.TickSize;
        }

 


@Spotware

neverman
08 Jul 2014, 15:40

RE:

Spotware said:

3. When I make an order, then its possible to change StopLoss and TakeProfit of this order. On MT4 there was a possible Stoploss and TakeProfit (pips away from current price). Is there something in cTrader and how to get this possible Stoploss and TakeProfit

There is no restriction for minimal pips away from spot prices.

6. How much money i have to possess in mu account to buy the minimum 10k eurusd, or 10k gold ... etc ... newbee question.

In order to calculate required margin you can use the following formula:

        private double GetRequiredMarginForLongPosition(long volume, Symbol symbol)
        {
            return volume / Account.Leverage * symbol.Ask * symbol.TickValue / symbol.TickSize;
        }

 

thank you for the useful information ... it will help a lot for reprogramming my script. I suppose that I'll have more questions about the given information, but I'll ask later.

Great work !


@neverman

neverman
29 Jul 2014, 19:48

RE:

Spotware said:

5. Of cource for observation needs, I need a code that places datas every tick in CSV file like ask(bid) quotes, server time (hour,minute, second, millisecond), connection speed to the server, current order (type (buy,sell),SL, TP, volume, magicnumber)

You can find an example of writing to a CSV file there: /algos/cbots/show/495. You will need to put your to code to the OnTick handler. You can retrieve prices from Symbol.Ask and Symbol.Bid properties. Server.Time property has DateTime value which stores hour, minute, second and millisecond. Connection speed is not available in the API. There is no current order entity in our API. You can enumerate all Positions with foreach instruction. All position properties are presented there: /api/reference/position.

I can't handle this :(

I'm trying to save data every tick Ask and Bid together with Data and HH:MM:SS.mmm in CSV file. The problem is that the code saves me only last data. Please tell me where I'm wrong.

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class DumpToCSV: Robot
    {

        protected override void OnTick()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var folderPath = Path.Combine(desktopFolder, "cTraderData");
            Directory.CreateDirectory(folderPath);
            var filePath = Path.Combine(folderPath, Symbol.Code + "-" + Server.Time.Day + "-" + Server.Time.Month + "-" + Server.Time.Year + ".csv");

            using (var writer = File.CreateText(filePath))
            {

                writer.WriteLine(ConcatWithComma(Server.Time.Date, Server.Time.TimeOfDay, " ", Symbol.Ask, Symbol.Bid));

            }//using
        }//onTick

    }//DumpCSV
}//cAlgo

Aslo, I can't put on the first row label of colomns and can't add properly mileseconds to the HH:MM:SS.mmm ...

Also any ideas how to put working directory different than Desktop (maybe without granting full access rights ?). Thanks in advance

Help me please


@neverman

Invalid
30 Jul 2014, 09:34

RE: RE:
protected override void OnTick()
{
    using (var writer = File.CreateText(filePath))
    {
         //....
    }
}

File.CreateText doesn't append to existing file, it overrides existing one if it exists. Therefore on each tick you have only last record in the file. You can try File.AppendText in order to update file on each tick.

neverman said:

Spotware said:

5. Of cource for observation needs, I need a code that places datas every tick in CSV file like ask(bid) quotes, server time (hour,minute, second, millisecond), connection speed to the server, current order (type (buy,sell),SL, TP, volume, magicnumber)

You can find an example of writing to a CSV file there: /algos/cbots/show/495. You will need to put your to code to the OnTick handler. You can retrieve prices from Symbol.Ask and Symbol.Bid properties. Server.Time property has DateTime value which stores hour, minute, second and millisecond. Connection speed is not available in the API. There is no current order entity in our API. You can enumerate all Positions with foreach instruction. All position properties are presented there: /api/reference/position.

I can't handle this :(

I'm trying to save data every tick Ask and Bid together with Data and HH:MM:SS.mmm in CSV file. The problem is that the code saves me only last data. Please tell me where I'm wrong.

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class DumpToCSV: Robot
    {

        protected override void OnTick()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var folderPath = Path.Combine(desktopFolder, "cTraderData");
            Directory.CreateDirectory(folderPath);
            var filePath = Path.Combine(folderPath, Symbol.Code + "-" + Server.Time.Day + "-" + Server.Time.Month + "-" + Server.Time.Year + ".csv");

            using (var writer = File.CreateText(filePath))
            {

                writer.WriteLine(ConcatWithComma(Server.Time.Date, Server.Time.TimeOfDay, " ", Symbol.Ask, Symbol.Bid));

            }//using
        }//onTick

    }//DumpCSV
}//cAlgo

Aslo, I can't put on the first row label of colomns and can't add properly mileseconds to the HH:MM:SS.mmm ...

Also any ideas how to put working directory different than Desktop (maybe without granting full access rights ?). Thanks in advance

Help me please

 


@Invalid

neverman
07 Aug 2014, 22:32

Hello,

I worked on the following code and its not working very well:

var dailyEurGbpSeries = MarketData.GetSeries("EURGBP", TimeFrame.Minute);
var dailyClose = dailyEurGbpSeries.Close.Last(1);

 

I'm starting this code on tick graph with the idea to get the last M1 close (not current). I added Print(dailyClose) to the code and the result was strange.

07/08/2014 22:15:59.909 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:00.143 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:00.564 | GBP, EURGBP, t1 | Previous Close is0.79377

...

07/08/2014 22:16:06.211 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:06.243 | GBP, EURGBP, t1 | Previous Close is0.79381

As you see it took 6 seconds to show me the true Close M1. Where is the mistake ?

 


@neverman

neverman
11 Aug 2014, 20:01

RE:

Anyone available to help ?


@neverman

neverman
14 Aug 2014, 22:07

When my broker agent told me about cTrader, he assured me that there is a great tutorial and very useful forum when I can receive information about the coding ? Any idea why he lied me ? One WEEK I can't receive an answer of simple question ?! I suppose its better to rethink the idea of using non supporting software for trading.


@neverman

Spotware
15 Aug 2014, 09:41

RE:

neverman said:

Hello,

I worked on the following code and its not working very well:

var dailyEurGbpSeries = MarketData.GetSeries("EURGBP", TimeFrame.Minute);
var dailyClose = dailyEurGbpSeries.Close.Last(1);

 

I'm starting this code on tick graph with the idea to get the last M1 close (not current). I added Print(dailyClose) to the code and the result was strange.

07/08/2014 22:15:59.909 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:00.143 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:00.564 | GBP, EURGBP, t1 | Previous Close is0.79377

...

07/08/2014 22:16:06.211 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:06.243 | GBP, EURGBP, t1 | Previous Close is0.79381

As you see it took 6 seconds to show me the true Close M1. Where is the mistake ?

 

First of all, please rename dailyEurGbpSeries variable, because it is minute, not daily series. It is not clear why do you think that GetSeries took 6 seconds. Please try to clarify your question.


@Spotware

neverman
15 Aug 2014, 11:00

RE: RE:

Spotware said:

neverman said:

Hello,

I worked on the following code and its not working very well:

var dailyEurGbpSeries = MarketData.GetSeries("EURGBP", TimeFrame.Minute);
var dailyClose = dailyEurGbpSeries.Close.Last(1);

 

I'm starting this code on tick graph with the idea to get the last M1 close (not current). I added Print(dailyClose) to the code and the result was strange.

07/08/2014 22:15:59.909 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:00.143 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:00.564 | GBP, EURGBP, t1 | Previous Close is0.79377

...

07/08/2014 22:16:06.211 | GBP, EURGBP, t1 | Previous Close is0.79377

07/08/2014 22:16:06.243 | GBP, EURGBP, t1 | Previous Close is0.79381

As you see it took 6 seconds to show me the true Close M1. Where is the mistake ?

 

First of all, please rename dailyEurGbpSeries variable, because it is minute, not daily series. It is not clear why do you think that GetSeries took 6 seconds. Please try to clarify your question.

The code is ran on 1 tick graph. It is made every tick to show me Previous 1 Minute Close using above example.

From the above raport, there are 5 given only tick raport from a minute 22:16 ... until 22:16:06.211 the script shows Close Quote from minute 22:14 ... from 22:16:06.243, the script shows close quote from minute 22:15.

Normally, the Last Minute close have to be given after 22:15:59.909 ... am I right ?

or Is it possible the fiven time to be client time, not server time ?


@neverman

Spotware
15 Aug 2014, 11:40

You are right, time in the left column is your local time. You can add Server.Time to the Print statement.


@Spotware

neverman
15 Aug 2014, 18:35

RE:

Spotware said:

You are right, time in the left column is your local time. You can add Server.Time to the Print statement.

 

Problem solved ... the system time was wrong ... I have to find a way the system timer to stay always same with the server time ?!?


@neverman

Spotware
18 Aug 2014, 09:49

RE: RE:

neverman said:

Spotware said:

You are right, time in the left column is your local time. You can add Server.Time to the Print statement.

 

Problem solved ... the system time was wrong ... I have to find a way the system timer to stay always same with the server time ?!?

You can use Server.Time property in order to obtain current server time.


@Spotware