Trigger Buy After Moving Above MA and Meets the 150 pips Requirements

Created at 25 Aug 2021, 05:41
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!
Fron's avatar

Fron

Joined 08.01.2020

Trigger Buy After Moving Above MA and Meets the 150 pips Requirements
25 Aug 2021, 05:41


I need Help with this Idea, I want to have a bot be triggered by crossing the MA, As well Trigger  A BUY 150 After. Thank you! 


@Fron
Replies

amusleh
25 Aug 2021, 15:46

Hi,

Please check the simple moving average example cBot: cAlgo API Reference - SimpleMovingAverage Interface (ctrader.com)

The example cBot opens a buy position if faster MA cross upward the slower MA and a sell position if faster M<A cross downward the slower MA.

If you need something more complicated you can post a job request or ask one of our consultants to develop your cBot.


@amusleh

Fron
25 Aug 2021, 16:46

RE: Yo

amusleh said:

Hi,  

Please check the simple moving average example cBot: cAlgo API Reference - SimpleMovingAverage Interface (ctrader.com)

The example cBot opens a buy position if faster MA cross upward the slower MA and a sell position if faster M<A cross downward the slower MA.

If you need something more complicated you can post a job request or ask one of our consultants to develop your cBot.

I have already build serveral bot with crossing MA's I was asking, my question is, what API do i  use to trigger a buy in a  cbot after its  price crosses 150 pips above that MA. 


@Fron

amusleh
25 Aug 2021, 18:17

Hi,

You just have to subtract the current price from MA value:

using cAlgo.API;
using cAlgo.API.Indicators;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SimpleMovingAverageSample : Robot
    {
        private double _volumeInUnits;
        private SimpleMovingAverage _simpleMovingAverage;

        [Parameter("Source", Group = "MA")]
        public DataSeries MaSource { get; set; }

        [Parameter("Period", DefaultValue = 9, Group = "MA")]
        public int MaPeriod { get; set; }

        [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 10, Group = "Trade")]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 10, Group = "Trade")]
        public double TakeProfitInPips { get; set; }

        [Parameter("Label", DefaultValue = "Sample", Group = "Trade")]
        public string Label { get; set; }

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _simpleMovingAverage = Indicators.SimpleMovingAverage(MaSource, MaPeriod);
        }

        protected override void OnTick()
        {
            if (BotPositions.Length > 0) return;

            var priceDistanceFromMa = Bars.ClosePrices.LastValue - _simpleMovingAverage.Result.LastValue;
            var priceDistanceFromMaInPips = priceDistanceFromMa * (Symbol.TickSize / Symbol.PipSize * Math.Pow(10, Symbol.Digits));

            if (priceDistanceFromMaInPips >= 150)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }
    }
}

 


@amusleh

Fron
26 Aug 2021, 03:06

RE: Brotha

amusleh said:

Hi,

You just have to subtract the current price from MA value:

using cAlgo.API;
using cAlgo.API.Indicators;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SimpleMovingAverageSample : Robot
    {
        private double _volumeInUnits;
        private SimpleMovingAverage _simpleMovingAverage;

        [Parameter("Source", Group = "MA")]
        public DataSeries MaSource { get; set; }

        [Parameter("Period", DefaultValue = 9, Group = "MA")]
        public int MaPeriod { get; set; }

        [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 10, Group = "Trade")]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 10, Group = "Trade")]
        public double TakeProfitInPips { get; set; }

        [Parameter("Label", DefaultValue = "Sample", Group = "Trade")]
        public string Label { get; set; }

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _simpleMovingAverage = Indicators.SimpleMovingAverage(MaSource, MaPeriod);
        }

        protected override void OnTick()
        {
            if (BotPositions.Length > 0) return;

            var priceDistanceFromMa = Bars.ClosePrices.LastValue - _simpleMovingAverage.Result.LastValue;
            var priceDistanceFromMaInPips = priceDistanceFromMa * (Symbol.TickSize / Symbol.PipSize * Math.Pow(10, Symbol.Digits));

            if (priceDistanceFromMaInPips >= 150)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }
    }
}

 

You are Amazing brotha thank you! Not a pro at Csharp but working on it ! Big Step for me! Realized I need to learn more Math.pow and its works ! 


@Fron