Bot opens positions; stop bot; restart bot; does it pick up previously open positions?

Created at 08 Apr 2019, 10:26
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!
FI

firemyst

Joined 26.03.2019

Bot opens positions; stop bot; restart bot; does it pick up previously open positions?
08 Apr 2019, 10:26


Hi everyone:

With automating trades, here's the sequence of events:

1) Start bot (one of many)

2) bot opens a position

3) I stop the bot for whatever reason; trade is still open on server because TP or SL isn't hit yet.

4) I restart bot

 

Question:

should the bot recognize the position it opened previously? Or is a bot only aware of positions it opened while the bot itself is running?

eg, will Positions.Count == 1 or should Positions.Count == 0?

 

Thank you.


@firemyst
Replies

firemyst
09 Apr 2019, 03:56

The answer to this question is Positions.Count == 1.

I ran the following test bot code to see what happens. I opened two FOREX positions with two instances of the same bot - one EURUSD and another AUDUSD. 

The first instance opened EURUSD. 

I stopped the bot and restarted with the ExecuteMarketOrder line commented out. It found 1 position.

Then I created a second instance of the bot running against AUDUSD. It opened a position.

I stopped both bots, exited out of cTrader, and then logged back in.

I started both bot instances and both recognized 2 opened positions.

Then I closed all the positions. 

Code below.

Thank you.

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

        protected override void OnStart()
        {
            // Put your initialization logic here
            //Uncomment this line to place an order with different symbols when bot starts
            //ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "My " + Symbol.Code + " test", null, 10);

            Print("Positions.Count: \"{0}\"", Positions.Count);
            foreach (Position p in Positions)
            {
                Print("Symbol: \"{0}\"; Entry time: \"{1}\"", p.SymbolCode, p.EntryTime);
            }
        }

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

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

            //Uncomment these lines to close the positions after a few have been opened.
            //foreach (Position p in Positions)
            //{
            //    ClosePosition(p);
            //}
        }
    }
}

 


@firemyst