Position Event Doesn't work

Created at 17 Aug 2018, 10:28
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!
fxwisdom1@gmail.com's avatar

fxwisdom1@gmail.com

Joined 18.05.2018

Position Event Doesn't work
17 Aug 2018, 10:28


Below is my code.

 

WHen I place, modify, or close order.

I don't see any of my print come out in the cBot Log. Please kindly help

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 SLhelper : Robot
    {
        [Parameter("SL Step Triggers", DefaultValue = 7.0)]
        public double sl { get; set; }

        protected override void OnStart()
        {

        }

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

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

        void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            Print("Hello World");
            var position = args.Position;
            Print(position.Label + " TIDAK ADA");
            ModifyPosition(position, position.VolumeInUnits, sl);
            Print("EROR");
            Print("123123");
        }

        void OnPositionsClosed(PositionModifiedEventArgs args)
        {
            Print("Modified");
        }
        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            Print("78678678678");
            if (position.NetProfit < 0)
            {
                var tradeType = position.TradeType;
                if (tradeType == TradeType.Buy)
                    tradeType = TradeType.Sell;
                else if (tradeType == TradeType.Sell)
                    tradeType = TradeType.Buy;
                var symbol = MarketData.GetSymbol(position.SymbolCode);
                var volume = position.VolumeInUnits;
                var label = position.Label;

                ExecuteMarketOrder(tradeType, symbol, volume * 2, label);
            }
        }
    }
}

 


@fxwisdom1@gmail.com
Replies

fxwisdom1@gmail.com
17 Aug 2018, 10:30

Sorry correction to the code above.

Btw problem still not solved. Kindly help

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 SLhelper : Robot
    {
        [Parameter("SL Step Triggers", DefaultValue = 7.0)]
        public double sl { get; set; }

        protected override void OnStart()
        {

        }

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

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

        void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            Print("Hello World");
            var position = args.Position;
            Print(position.Label + " TIDAK ADA");
            ModifyPosition(position, position.VolumeInUnits, sl);
            Print("EROR");
            Print("123123");
        }

        void OnPositionsModified(PositionModifiedEventArgs args)
        {
            Print("Modified");
        }
        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            Print("78678678678");
            if (position.NetProfit < 0)
            {
                var tradeType = position.TradeType;
                if (tradeType == TradeType.Buy)
                    tradeType = TradeType.Sell;
                else if (tradeType == TradeType.Sell)
                    tradeType = TradeType.Buy;
                var symbol = MarketData.GetSymbol(position.SymbolCode);
                var volume = position.VolumeInUnits;
                var label = position.Label;

                ExecuteMarketOrder(tradeType, symbol, volume * 2, label);
            }
        }
    }
}

 


@fxwisdom1@gmail.com

fxwisdom1@gmail.com
17 Aug 2018, 10:36

Please try the code aboev and place a market order, then modify its SL/TP.

I expect there should be a print out message on the cBot log.


@fxwisdom1@gmail.com

PanagiotisCharalampous
17 Aug 2018, 10:38

Hi fxwisdom1@gmail.com,

You have written the functions but you do not handle the events anywhere. Modify your OnStart as below

        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
            Positions.Modified += OnPositionsModified;
            Positions.Closed += OnPositionsClosed;
        }

Best Regards,

Panagiotis


@PanagiotisCharalampous

fxwisdom1@gmail.com
17 Aug 2018, 10:51

RE:

Panagiotis Charalampous said:

Hi fxwisdom1@gmail.com,

You have written the functions but you do not handle the events anywhere. Modify your OnStart as below

        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
            Positions.Modified += OnPositionsModified;
            Positions.Closed += OnPositionsClosed;
        }

Best Regards,

Panagiotis

Woot Wooott looks like it's working!!!

 

Thank you Panagiotis!


@fxwisdom1@gmail.com