Closed Trades Info

Created at 30 Sep 2020, 20:54
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!
PA

patzoot@gmail.com

Joined 30.09.2020

Closed Trades Info
30 Sep 2020, 20:54


Hi all,

 

I need some help with coding a piece of a condition for opening a trade; the condition should check if the last 2 or 3 trades closed with a loss, and if so, not place any further trades. My programming skills are very limited so any examples would be greatly appreciated. Regards,

#region Order Processing Conditions
        protected override void OnTick()
        {
            if (Symbol.Spread / Symbol.PipSize <= MaxSpread)
            {              
                if (Symbol.Bid > _ma1.LastValue)
                    if (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == "PatTestBot") < MaxOpenPos)
                        ProcessBuy();

                if (Symbol.Ask < _ma1.LastValue)
                    if (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == "PatTestBot") < MaxOpenPos)
                        ProcessSell();
            }            
        }
        #endregion

 


@patzoot@gmail.com
Replies

PanagiotisCharalampous
02 Oct 2020, 08:24

Hi there,

You can try something like this to count the number of consecutive losses.

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


        int _consecutiveLosses;
        protected override void OnStart()
        {
            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.NetProfit < 0)
                _consecutiveLosses++;
            else
                _consecutiveLosses = 0;
        }

        protected override void OnBar()
        {

        }

        protected override void OnTick()
        {
    
        }


    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous