DE
DELETED_USER
Blocked user by Spotware at 17 Jan 2024, 13:24
0 follower(s) 0 following 0 subscription(s)
Topics
23 Sep 2016, 00:45
 1906
 2
12 Apr 2016, 17:34
 3013
 1
Replies

DELETED_USER
06 Dec 2016, 21:16

RE:

Yes thats it cyfer, set it for the just opened position. 

 

Thanks!

 

cyfer said:

I can't follow what's going on here 

why are you opening 2 pending orders after the initial order is filled ??

are you trying to set Stop Loss and Take profit for the "Just opened" Position ? .. in that case you should set those parameters in the call back function

private void OnPositionOpened(PositionOpenedEventArgs args)
{
    var position = args.Position;
    var stopLoss = The Stop Loss you want to set;
    var takeProfit= the Take Profit you want to set;
    ModifyPosition(position, stopLoss, takeProfit);

}

 

 

 


DELETED_USER
04 Dec 2016, 22:35

I am still struggeling to get this working since the stoploss and limit order actually open a new position rather than closing the actual position. 

For example: OnBarClose I am sending a buylimitorder to open a position. 

var buyLmtOrder = PlaceLimitOrder(TradeType.Buy, Symbol, size, buyLimit, "label");

OnPositionOpened I then send a stop and limit order based on the entryPrice of the position once the buyLmtOrder is filled. 

   private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            Print("Opened");
            var position = args.Position;
            Print("Position opened at {0}", position.EntryPrice);

            if (position.TradeType == TradeType.Buy)
            {
                PlaceLimitOrder(TradeType.Sell, Symbol, (long)position.Quantity, TPBuy, "label");
                PlaceStopOrder(TradeType.Sell, Symbol, (long)position.Quantity, sellSL, "label");
            }
        }

OnPositionClosed I then cancel the 2nd outstanding order so if stoploss is filled I cancel the limit and vice versa. 

  private void OnPositionClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.SymbolCode == Symbol.Code && position.NetProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }

            else if (position.SymbolCode == Symbol.Code && position.NetProfit < 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
        }

Any help would be much appreciated. 

 

 

 


DELETED_USER
26 Sep 2016, 12:50

Thanks for your reply lucian. 

1) Running on the same server. 

2) Running on the same CAlgo instance. 

3) CAlgo

4) Nothing in the logs. I cannot see the order being placed or rejected. Order doesnt exist. 

Thanks


DELETED_USER
13 Sep 2016, 23:33

RE: RE:

I am interested in the actual daily bar's close. 

Symbol.Bid > ma.Result.LastValue would be called everytime the bid is above the MA. I am interested when the DailyBar closes above (crosses above) the MA for the first time. 

Wouldn't daily.Close.Last(1) > ma.Result.LastValue be called everytime the last close is above the MA?

croucrou said:

Unless you are interested in the previous bar's close, then:

if (daily.Close.Last(1) > ma.Result.LastValue)
//buy
 
if (daily.Close.Last(1) < ma.Result.LastValue)
//sell

Assuming, that the "ma" is also based on the daily timeframe series.

 


DELETED_USER
30 Aug 2016, 18:51

RE:

Works fine thanks. 

 

a1112 said:

Hi, I think the guy that started this thread had the same problem, solution is in last post (works for me)

/forum/calgo-support/8502?page=1#4

hope it helps

 

 


DELETED_USER
23 Aug 2016, 22:46

RE:

I installed Visual Studio 2013 now, but when pressing "Edit in Visual Studio" I dont get any message or error and Visual studio does not open!

Spotware said:

Dear Trader, 

Please be advised that, unfortunately, cAlgo is not compatible with Visual Studio 2015 just yet. Although, you can use it with Visual Studio 2013

 


DELETED_USER
19 Aug 2016, 00:28

RE:

Thanks so much for your reply and suggestions. I will try tomorrow and let you know. 

 

lucian said:

Try to replace:

 

 var posnumb = Positions.Find("Test");
            Print(posnumb);

            if (posnumb != null)
            {
                Print("Close");
                Print(posnumb);
                ClosePosition(posnumb);
                Print("Close Positions");
                count = 0;
                Print("Position Closed EOD");

                Print(Positions.Count);
            }

to:

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 NewcBot : Robot
    {

        public int count;

        protected override void OnBar()
        {
            foreach (var posnumb in Positions.FindAll("Test"))
            {
                Print(posnumb);
                {
                    Print("Close");
                    Print(posnumb);
                    ClosePosition(posnumb);
                    Print("Close Positions");
                    count = 0;
                    Print("Position Closed EOD");

                    Print(Positions.Count);
                }
            }
        }

    }
}

 

 


DELETED_USER
18 Aug 2016, 22:31

I am using this code with actual order execution. this line of code always gives null! 

       var posnumb = Positions.Find("Test");

 

thats why closed positions throws an error null reference exception object not set to an instance of an object

It never recognises when a new position is opened. 

 

 

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(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        public int count = 0;

        protected override void OnStart()
        {

            count = 1;
            Print(count = 1);
            Print(Positions.Count);
        }

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

            ExecuteMarketOrder(TradeType.Buy, Symbol, 100000);

            var posnumb = Positions.Find("Test");
            Print(posnumb);

            if (posnumb != null)
            {
                Print("Close");
                Print(posnumb);
                ClosePosition(posnumb);
                Print("Close Positions");
                count = 0;
                Print("Position Closed EOD");

                Print(Positions.Count);
            }
        }
        /*
            if (Positions.Count < 2)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 100000);
                count++;
                Print(Positions.Count, count);
            }
            */

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


DELETED_USER
18 Aug 2016, 21:51

I am getting a NullExceptionError at this line, do you get the same?

 ClosePosition(Positions.Find("WRB"));


DELETED_USER
18 Aug 2016, 20:57

RE: RE: RE:
I am getting a Null Exeception error for Positions.Count != 0. Any thoughts?

Old Account said:

MRSV said:

marc.sischka said:

Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

Thanks

 

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

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("pos"));
            }
            
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "pos");
        }

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

Set timeframe to daily. You didn't say if you wantet to open a buy or sell position, so I set it to open a buy position.

 

 


DELETED_USER
18 Aug 2016, 20:56

RE: RE:

I am getting a NullException error for 

(Positions.Count != 0)

 

MRSV said:

marc.sischka said:

Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

Thanks

 

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

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("pos"));
            }
            
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "pos");
        }

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

Set timeframe to daily. You didn't say if you wantet to open a buy or sell position, so I set it to open a buy position.

 


DELETED_USER
18 Aug 2016, 20:55

Any thoughts?


DELETED_USER
11 Aug 2016, 11:36

I experienced the same issue again yesterday. 

Request to close position PID52853703 is sent to server
2016.08.10 20:55:02.962 | → Request to close position PID52853703 is ACCEPTED, order OID79474546 created (10/08/2016 20:55:02.926 UTC+0)
2016.08.10 20:55:02.993 | Request to close position PID52853703 is sent to server
2016.08.10 20:55:03.071 | Request to close position PID52853703 is sent to server
2016.08.10 20:55:03.118 | Request to close position PID52853703 is sent to server
2016.08.10 20:55:03.180 | Request to close position PID52853703 is sent to server
2016.08.10 20:55:03.368 | → Order OID79474546 is FILLED at 1.11756, position PID52853703 closed (10/08/2016 20:55:02.999 UTC+0)
2016.08.10 20:55:03.462 | → Failed to close position PID52853703 with error "TRADING_BAD_VOLUME"
2016.08.10 20:55:03.462 | → Failed to close position PID52853703 with error "TRADING_BAD_VOLUME"
2016.08.10 20:55:03.477 | → Failed to close position PID52853703 with error "TRADING_BAD_VOLUME"
2016.08.10 20:55:03.587 | → Failed to close position PID52853703 with error "TRADING_BAD_VOLUME"

 

A close position request is sent to the server which is accepted. Why is the request sent another 4 times? The strategy runs the logic onBar() (1minute data). 

After the position has been closed. The strategy again tries to close the position, even though the position already has been closed. Trading_bad_volume error is due to size being 0 since the position already has been closed. 

Any thoughts would be much appreciated. 

Thanks


DELETED_USER
20 Jul 2016, 14:28

Another piece of code which I had trouble with in the past, which is to calculate the position size. Sometimes the logic works fine however other times I get Bad_Volume error since the size is rounded down to 0. 

 

  private void CalculateSize(double StopLoss)
        {
            double totalBalance = Account.Balance;

            // Calculate the total risk allowed per trade.
            double riskPerTrade = (totalBalance * RiskPercent) / 100;

            // Add the stop loss, commission pips and spread to get the total pips used for the volume calculation.
            double totalPips = StopLoss;

            // Calculate the exact volume to be traded. Then round the volume to the nearest 10,000 and convert to an int so that it can be returned to the caller.
            double exactVolume = Math.Round(riskPerTrade / (Symbol.PipValue * totalPips), 0);
            Print("Volume", exactVolume);
            size = (int)Symbol.NormalizeVolume(exactVolume, RoundingMode.Up);
            //size = (((int)exactVolume / 1000) * 1000);
            //size = (int)Math.Ceiling(exactVolume / 1000) * 1000;
            Print("Size", size);

            // Finally, check that the calculated volume is not greater than the MaxVolume parameter. If it is greater, reduce the volume to the MaxVolume value.
            if (size > MaxVolume)
                size = MaxVolume;
        }


DELETED_USER
20 Jul 2016, 14:11

I am getting the same error again but now for this bit of code, any help would be much appreciated. I can't seem to find the nullexception, the algo prints "Execution time" so the error must be somewhere after. 

Thanks!

  if (Time.Hour == 20 && Time.Minute >= 55 && Time.Minute < 56)
            {
                Print("Execution Time");
            }
            else
            {
                //Print("We wait for the proper time");
                return;
            }

            ///Close Open Positions on Close
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("algo"));
                Print("Position Closed EOD");
            }

            if (HasPosition(Symbol))
                return;

            if (buySignal)
            {
                Print("Ready to Calculate Size");
                CalculateSize(sellSL);
                Print("Size Calculated");
                double buyLimit = (Symbol.Ask + Symbol.Bid) / 2;
                //var buyOrder = ExecuteMarketOrder(TradeType.Buy, Symbol, size, "algo", sellSL, TP);
                Print("LimitPrice:{0}, Bid: {1}, Offer: {2}", buyLimit, Symbol.Bid, Symbol.Ask);
                var buyLmtOrder = PlaceLimitOrder(TradeType.Buy, Symbol, size, buyLimit, "algo", sellSL, TP);
                //ExecuteMarketOrder(TradeType.Buy, Symbol, size, "algo", sellSL, TP);
                Print("TP: {0}, SellSL {1}", TP, sellSL);

                //Print(LastResult.Position.Commissions);
            }

            else if (sellSignal)
            {
                Print("Ready to Calculate Size");
                CalculateSize(buySL);
                Print("Size Calculated");
                double sellLimit = (Symbol.Ask + Symbol.Bid) / 2;
                //var sellOrder = ExecuteMarketOrder(TradeType.Sell, Symbol, size, "algo", buySL, TP);
                Print("LimitPrice:{0}, Bid: {1}, Offer: {2}", sellLimit, Symbol.Bid, Symbol.Ask);
                var sellLmtOrder = PlaceLimitOrder(TradeType.Sell, Symbol, size, sellLimit, "algo", buySL, TP);
                Print("TP: {0}, BuySL {1}", TP, buySL);
                //Print(LastResult.Position.Commissions);
            }


DELETED_USER
20 Jul 2016, 13:50

Hi, 

If I have multiple algos running in 1 account does if (Positions.Count != 0) take into consideration the positions of other strategies or just of the Algo where the code is executed?

Best,

Marc

 


DELETED_USER
29 Jun 2016, 00:25

RE:

Hi Cyfer, 

Does Time.Hour use the CurrentTime in Calgo and Server.Time.Hour the Windows time of the server?

Thanks

 

cyfer said:

You can go for something like this 

  protected override void OnBar()
        {
            if (Time.Hour == 21 && Time.Minute >= 55)
            {
                Print("Execution Time");
            }
            else
            {
                Print("We wait for the proper time");
            }
        }

You can also test Server.Time.Hour instead of Time.Hour and see what suites you 

If your strategy is based on a specific time of the day , I strongly advise against using OnTick() , its an overhead for nothing

 

 


DELETED_USER
23 Jun 2016, 15:57

Thanks for your reply, I understand how to sendemail within the code. I wanted to check if it is possible to send an email if the Broker Connection disconnects from CAlgo?


DELETED_USER
23 Jun 2016, 11:48

Thanks for your reply. OnBar() is set to Daily, so would your code actually execute?

The strategy is based on daily bars however I want to generate the trading signal before 10pm close due to tighter spread than wait for the day open at 22:05 and just execute the order OnBar() Open. 


DELETED_USER
17 Jun 2016, 14:53

Yes I understand, but I want to use this logic in live trading. I want to calculate the commission of the trade before the order is actually placed. 

Thanks,

Marc