Repeat a cycle in my bot

Created at 12 Aug 2022, 12:37
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!
CE

Cesare_Spani

Joined 12.08.2022

Repeat a cycle in my bot
12 Aug 2022, 12:37


Hi, I need a little help.

This is my bot code.

It executes a market order live depending on the cross of the two SMAs.
Once the TP or SL is reached, the bot should reopen a new live position by double-checking the cross of the two SMAs.

In practice, start from the beginning.

How can I place this order? Anyone help me by editing my code and entering the code to repeat the loop? Many thanks in advance.

CODE:

using System;
using System.Collections.Generic;
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 Cesare_bot : Robot
    {
        private TradeType Direction = TradeType.Buy;

        [Parameter("SMA1 Period" )]
        public int PeriodsSma1 { get; set; }
        [Parameter("SMA2 Period" )]
        public int PeriodsSma2 { get; set; }

        [Parameter("SMA Source")]
        public DataSeries Source { get; set; }

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }



        protected override void OnStart()
        {

            _sma1 = Indicators.SimpleMovingAverage(Source, PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(Source, PeriodsSma2);

            Print("Decimal ", Symbol.Digits);

            if (_sma1.Result.LastValue > _sma2.Result.LastValue)
            {
 Direction = TradeType.Buy;
 }
 else
{
Direction = TradeType.Sell;
}
ExecuteMarketOrder(Direction, SymbolName, Symbol.QuantityToVolumeInUnits(0.03), "CesareLive", 1.5, 1.5);
}

}

}
 


@Cesare_Spani
Replies

PanagiotisCharalampous
12 Aug 2022, 12:54

Hi DAX_scalper,

You don't need to create any loop. What you need to do is

  1. Move your code in OnBar() method
  2. Check for crosses instead of check just the position of the two MAs

This way your bot will place trades on every cross.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous