Category Trend  Published on 03/08/2022

Simple EMA Crossover cBot

Description

// I need help here

            bool close_buy = standart_ema.Result.LastValue > fast_ema.Result.LastValue;
            bool close_sell = standart_ema.Result.LastValue < fast_ema.Result.LastValue;

            var buy_position = Positions.Find("order_buy", SymbolName, TradeType.Buy);
            var sell_position = Positions.Find("order_sell", SymbolName, TradeType.Sell);

            
            if (standart_ema.Result.LastValue > fast_ema.Result.LastValue)
                ClosePosition(buy_position);

            else if (standart_ema.Result.LastValue < fast_ema.Result.LastValue)
                ClosePosition(sell_position);
            
     

I am new one in C# programming and cAlgo. Please help me to close position correctly. 
When i start my cBot, it stops on next bar.

It is hard to work with ClosePosition() method correctly

Can you help me in comment?


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

        public ExponentialMovingAverage slow_ema;
        public ExponentialMovingAverage standart_ema;
        public ExponentialMovingAverage fast_ema;

        protected override void OnStart()
        {
            slow_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 200);
            standart_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 14);
            fast_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 5);

            

        }
        protected override void OnTick()
        {
            bool buy_case = fast_ema.Result.LastValue > slow_ema.Result.LastValue;
            bool sell_case = fast_ema.Result.LastValue < slow_ema.Result.LastValue;

            bool buy_order = fast_ema.Result.LastValue > standart_ema.Result.LastValue;
            bool sell_order = fast_ema.Result.LastValue < standart_ema.Result.LastValue;

            

            if (buy_case && buy_order)
            {
                if (!IsBuyPositionOpen(TradeType.Buy))
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "order_buy");
            }

            else if (sell_case && sell_order)
            {
                if (!IsSellPositionOpen(TradeType.Sell))
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "order_sell");
            }

            bool close_buy = standart_ema.Result.LastValue > fast_ema.Result.LastValue;
            bool close_sell = standart_ema.Result.LastValue < fast_ema.Result.LastValue;

            var buy_position = Positions.Find("order_buy", SymbolName, TradeType.Buy);
            var sell_position = Positions.Find("order_sell", SymbolName, TradeType.Sell);

            
            if (standart_ema.Result.LastValue > fast_ema.Result.LastValue)
                ClosePosition(buy_position);

            else if (standart_ema.Result.LastValue < fast_ema.Result.LastValue)
                ClosePosition(sell_position);
            
        }

        protected override void OnBar()
        {
           /*
            
           */
        }

        private bool IsBuyPositionOpen(TradeType type)
        {
             return Positions.FindAll("order_buy", SymbolName, type).Length > 0;
        }

        private bool IsSellPositionOpen(TradeType type)
        {
            return Positions.FindAll("order_sell", SymbolName, type).Length > 0;
        }
    }
}


oleh.mudryi's avatar
oleh.mudryi

Joined on 02.08.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: .lwm LT EMA Crossover m1.algo
  • Rating: 0
  • Installs: 1357
  • Modified: 02/08/2022 13:54
Comments
Log in to add a comment.
oleh.mudryi's avatar
oleh.mudryi · 2 years ago

Okay. I have found how to resolve it
 

var buy_position = Positions.FindAll("order_buy", SymbolName, TradeType.Buy);

var sell_position = Positions.FindAll("order_sell", SymbolName, TradeType.Sell);

 

foreach (var position in buy_position)

{

if (close_buy)

ClosePosition(position);

}

 

foreach (var position in sell_position)

{

if (close_sell)

ClosePosition(position);

}

oleh.mudryi's avatar
oleh.mudryi · 2 years ago

Okay. I used debugger and I see error on ClosePosition 

the NullReferenceException indicates that your code is trying to work with an object that has a null value as its reference

oleh.mudryi's avatar
oleh.mudryi · 2 years ago

Okay, thanks. 
I used your code and it continue to stop after one bar. 
I think something wrong with ClosePosition and bot can't repeat cycle 
OnTick or OnBar

I need bot that can work 24/7. And if it turn off on next bar, this is bullshit. 

I see what you have chenged:
1) FullAccess
2) protected override void OnBar()
3) if (close_buy)
                ClosePosition(buy_position);
 
But it didn't resolve main problem. 

Thanks for reccomendations. I will try to find problem in debugger

SE
Sendgate · 2 years ago

Hi,

Well it worked for me, opened and closed a position. Backtesting it would not do any good cause it only shows you how to open and close a position.

Use the code I gave you and try it.

If you want to see what it does you need to run it in Visual Studio.

You can read about the steps here:

https://ctrader.info/d/442-how-to-debug-a-cbot-using-visual-studio-2022

 

Cheers

 

 

 

oleh.mudryi's avatar
oleh.mudryi · 2 years ago

Hi, thanks for your reply. 

Try to backtest it. 

Bot stops on next bar - this is the problem. 

If i take away function ClosePosition - bot works

So I think the problem is with it

It stops my bot without any mistakes in code or build

It is hard to find mistake

SE
Sendgate · 2 years ago

This is a start:

Cheers,

--
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.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess)]
    public class NewcBot : Robot
    {
        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
 
        public ExponentialMovingAverage slow_ema;
        public ExponentialMovingAverage standart_ema;
        public ExponentialMovingAverage fast_ema;
 
        protected override void OnStart()
        {
            slow_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 200);
            standart_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 14);
            fast_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 5);             
        }
        
        protected override void OnBar()
        {
            bool buy_case = fast_ema.Result.LastValue > slow_ema.Result.LastValue;
            bool sell_case = fast_ema.Result.LastValue < slow_ema.Result.LastValue;
 
            bool buy_order = fast_ema.Result.LastValue > standart_ema.Result.LastValue;
            bool sell_order = fast_ema.Result.LastValue < standart_ema.Result.LastValue;
 
             
 
            if (buy_case && buy_order)
            {
                if (!IsBuyPositionOpen(TradeType.Buy))
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "order_buy");
            }
 
            else if (sell_case && sell_order)
            {
                if (!IsSellPositionOpen(TradeType.Sell))
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "order_sell");
            }
 
            bool close_buy = standart_ema.Result.LastValue > fast_ema.Result.LastValue;
            bool close_sell = standart_ema.Result.LastValue < fast_ema.Result.LastValue;
 
            var buy_position = Positions.Find("order_buy", SymbolName, TradeType.Buy);
            var sell_position = Positions.Find("order_sell", SymbolName, TradeType.Sell);
 
             
            if (close_buy)
                ClosePosition(buy_position);
 
            else if (close_sell)
                ClosePosition(sell_position);
             
        }
      
        private bool IsBuyPositionOpen(TradeType type)
        {
             return Positions.FindAll("order_buy", SymbolName, type).Length > 0;
        }
 
        private bool IsSellPositionOpen(TradeType type)
        {
            return Positions.FindAll("order_sell", SymbolName, type).Length > 0;
        }        
     
    }
}