Stochastic Levels

Created at 07 Jul 2020, 19: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!
LU

luciancastro89

Joined 07.07.2020

Stochastic Levels
07 Jul 2020, 19:41


Hello guys, hope everything is well.

 

Is it possible to automate a buy order when periodK% cross bottom level from stochastic oscilator and sell order when perdiodK% cross top level?

I got a stochastic bot but its only open order when K cross D.

Anyone have this kind of bot? I am trying to build a scalper bot but I am a newbie coder

 

Thanks in advance <3


@luciancastro89
Replies

PanagiotisCharalampous
08 Jul 2020, 08:16

Hi luciancastro89,

You can share your cBot's code and somebody might be able to guide you on what you should change.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

luciancastro89
08 Jul 2020, 18:08

Ok thanks for your reply!

 

Im trying to modify this bot, istead of buy/sell when period K cross D on the stochastic, the bot should buy when are oversold and sell when overbought on stochastic.

 

Also if it is possible to add a moving average to help, only buying when price closes above MA and sell when closes below.

 

 

This is the code:

 

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 StochasticBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Take Profit", Group = "Risk", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
        public int takeProfit { get; set; }

        [Parameter("Stop Loss", Group = "Risk", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
        public int stopLoss { get; set; }

        [Parameter("Percent K", Group = "Stochastic", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
        public int percentK { get; set; }

        [Parameter("Percent K Slow", Group = "Stochastic", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
        public int percentKSlow { get; set; }

        [Parameter("Percent D", Group = "Stochastic", DefaultValue = 150, MinValue = 4, Step = 1, MaxValue = 1000)]
        public int percentD { get; set; }


        private StochasticOscillator stochasticOscillator;
        private double volumeInUnits;



        protected override void OnStart()
        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            stochasticOscillator = Indicators.StochasticOscillator(percentK, percentKSlow, percentD, MovingAverageType.Exponential);
        }

        protected override void OnBar()
        {
            if (stochasticOscillator.PercentK.Last(2) < stochasticOscillator.PercentD.Last(2))
            {
                if (stochasticOscillator.PercentK.LastValue > stochasticOscillator.PercentD.LastValue)
                {
                    Print(stochasticOscillator.PercentK.LastValue);
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Stochastic", stopLoss, takeProfit);
                }
            }
            if (stochasticOscillator.PercentK.Last(2) > stochasticOscillator.PercentD.Last(2))
            {
                if (stochasticOscillator.PercentK.LastValue < stochasticOscillator.PercentD.LastValue)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "Stochastic", stopLoss, takeProfit);
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

 


@luciancastro89

PanagiotisCharalampous
09 Jul 2020, 08:18

Hi luciancastro89,

These are the lines you have to change 

            if (stochasticOscillator.PercentK.Last(2) < stochasticOscillator.PercentD.Last(2))
            {
                if (stochasticOscillator.PercentK.LastValue > stochasticOscillator.PercentD.LastValue)
                {
            if (stochasticOscillator.PercentK.Last(2) > stochasticOscillator.PercentD.Last(2))
            {
                if (stochasticOscillator.PercentK.LastValue < stochasticOscillator.PercentD.LastValue)
                {

Instead of making these checks which actually check if the two lines have crossed each other, you need to check if PercentK has crossed a specific value. e.g.

           if (stochasticOscillator.PercentK.Last(2) > 20)
            {
                if (stochasticOscillator.PercentK.LastValue < 20)
                {

I hope this helps

Best Regards

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luciancastro89
13 Jul 2020, 00:51

Thank you very much Panagiotis, it works !!

 

<3


@luciancastro89