Move stop to next candle's high (short) or low (long)

Created at 04 Nov 2015, 13:50
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!
CT

ctid205024

Joined 04.11.2015

Move stop to next candle's high (short) or low (long)
04 Nov 2015, 13:50


Could anyone give me a hand on how could I possibly move the stop to the next candle's high (when going short) or low (when going long)? 

Thank you


@ctid205024
Replies

mindbreaker
04 Nov 2015, 14:02

RE:
//Accessing historical O-H-L-C prices from Robots

// prev bar
int index = MarketSeries.Close.Count-2;


double high = MarketSeries.High[index];

double low = MarketSeries.Low[index];


with trailing

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 oodboo : Robot
    {
 
        [Parameter(DefaultValue = 50, MinValue = 0)]
        public int TrailingStop { get; set; }
 
        protected override void OnStart()
        {
            Print("Open position !!!");
        }
 
 
        protected override void OnTick()
        {
 
            //===================================================== Trailing
            if (TrailingStop > 0)
            {
                foreach (var openedPosition in Positions)
                {
                    Position Position = openedPosition;
                    if (Position.TradeType == TradeType.Buy)
                    {
                        //double distance = Symbol.Bid - Position.EntryPrice;
 
                        //if (distance >= TrailingStop * Symbol.PipSize)
                        //{
                        double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);
 
                        if (Position.StopLoss == null || newStopLossPrice > Position.StopLoss)
                        {
                            ModifyPosition(Position, newStopLossPrice, Position.TakeProfit);
                        }
                        //}
                    }
                    else
                    {
                        //double distance = Position.EntryPrice - Symbol.Ask;
 
                        //if (distance >= TrailingStop * Symbol.PipSize)
                        //{
 
                        double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);
 
                        if (Position.StopLoss == null || newStopLossPrice < Position.StopLoss)
                        {
                            ModifyPosition(Position, newStopLossPrice, Position.TakeProfit);
                        }
                        //}
                    }
                }
            }
        }
 
        protected override void OnStop()
        {
            Print("cBot was stoped.");
        }
    }
}

 


@mindbreaker

mindbreaker
04 Nov 2015, 14:07

RE: RE:

Get market series

/forum/cbot-support/7029


@mindbreaker

ctid205024
04 Nov 2015, 23:31

RE: RE:

Very good mate, thanks heaps!!

mindbreaker said:

//Accessing historical O-H-L-C prices from Robots

// prev bar
int index = MarketSeries.Close.Count-2;


double high = MarketSeries.High[index];

double low = MarketSeries.Low[index];


with trailing

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 oodboo : Robot
    {
 
        [Parameter(DefaultValue = 50, MinValue = 0)]
        public int TrailingStop { get; set; }
 
        protected override void OnStart()
        {
            Print("Open position !!!");
        }
 
 
        protected override void OnTick()
        {
 
            //===================================================== Trailing
            if (TrailingStop > 0)
            {
                foreach (var openedPosition in Positions)
                {
                    Position Position = openedPosition;
                    if (Position.TradeType == TradeType.Buy)
                    {
                        //double distance = Symbol.Bid - Position.EntryPrice;
 
                        //if (distance >= TrailingStop * Symbol.PipSize)
                        //{
                        double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);
 
                        if (Position.StopLoss == null || newStopLossPrice > Position.StopLoss)
                        {
                            ModifyPosition(Position, newStopLossPrice, Position.TakeProfit);
                        }
                        //}
                    }
                    else
                    {
                        //double distance = Position.EntryPrice - Symbol.Ask;
 
                        //if (distance >= TrailingStop * Symbol.PipSize)
                        //{
 
                        double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);
 
                        if (Position.StopLoss == null || newStopLossPrice < Position.StopLoss)
                        {
                            ModifyPosition(Position, newStopLossPrice, Position.TakeProfit);
                        }
                        //}
                    }
                }
            }
        }
 
        protected override void OnStop()
        {
            Print("cBot was stoped.");
        }
    }
}

 

 


@ctid205024