Help setting stoploss below/above previous candle low/high

Created at 17 May 2023, 21:21
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!
DA

davidmwabuka

Joined 07.03.2023

Help setting stoploss below/above previous candle low/high
17 May 2023, 21:21


I am trying to make the bot execute and set stoploss at the low of last bar like the way in the code below.

Suprisingly the bot places stoploss at  around 1.6pips from entry price on every position instead of below the low of previous bars. i dont even know where the 1.6pips valiue comes from.

here is my code please help. 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class EMAcBot : Robot
    {
        private ExponentialMovingAverage ema;
        private MarketSeries series;

        protected override void OnStart()
        {
            // Initialize the EMA from 1-minute timeframe
            series = MarketData.GetSeries(TimeFrame.Minute);
            ema = Indicators.ExponentialMovingAverage(series.Close, 8);
        }

        protected override void OnBar()
        {
            // Access the last closing price from 1-minute timeframe
            double lastPrice = series.Open.Last(1);

            // Use the EMA and lastPrice in any timeframe
            if (ema.Result.HasCrossedAbove(lastPrice, 1))
            {
                // EMA crossed above lastPrice


                ExecuteMarketOrder(TradeType.Sell SymbolName, 1000, "1min", Bars.HighPrices.Last(2), null);


                Print("EMA crossed below lastPrice!"); // Printing a message to the log
                // Do something
            }
            else if (ema.Result.HasCrossedBelow(lastPrice, 1))
            {
                // EMA crossed below lastPrice

                ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "1min", Bars.LowPrices.Last(2), null);


                Print("EMA crossed below lastPrice!"); // Printing a message to the log
                // Do something else
            }
            


        }
    }
}

 


@davidmwabuka
Replies

PanagiotisChar
06 Jun 2023, 10:11

Hi there,

Your problem is in the ExecuteMarketOrder method. You should be passing pips instead of absolute prices.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar