Help with order cancel after x bars

Created at 26 Jan 2021, 01:48
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!
JA

jackhpfilerrowson

Joined 26.11.2020

Help with order cancel after x bars
26 Jan 2021, 01:48


Hi, I'm new to coding so was wondering if someone could help me get this code working. Thanks

 

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 NewcBot : Robot
    {
        private RelativeStrengthIndex Rsi;    
        private SimpleMovingAverage High;
        int GetBarsAgo(Position position);

        protected override void OnStart()
        {
      
            Rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);
            High = Indicators.SimpleMovingAverage(Bars.HighPrices, 1);
       
        }

        protected override void OnBar()
        {
     
        for (var i = MarketSeries.OpenTime.Count - 1; i >= 0; i--)
            {
                if (position.EntryTime > MarketSeries.OpenTime[i])
                    return MarketSeries.OpenTime.Count - 1 - i;
            }
            return -1;
                //Rsi
                if (Rsi.Result.LastValue < 60)
                    {
                        PlaceStopLimitOrder(TradeType.Buy, SymbolName, 10000, High.Result.Last(2), 0.5, "StopOrder", 5, 5);
                    }

            foreach (var order in PendingOrders)
             var barsAgo = GetBarsAgo(position);
            {
                if (order.Label == "StopOrder")
                if (barsAgo > 2)
                {
                    CancelPendingOrder(order);
                }

            }
       }
    }
}


@jackhpfilerrowson