Simple EMA Crossover cBot free

by oleh.mudryi in category Trend at 02/08/2022
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?

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
´╗┐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;
        }
    }
}
Comments

Sendgate - August 02, 2022 @ 19:09

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;
        }        
     
    }
}

oleh.mudryi - August 02, 2022 @ 19:24

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

Sendgate - August 02, 2022 @ 21:26

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 - August 03, 2022 @ 11:29

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

oleh.mudryi - August 03, 2022 @ 12:29

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 - August 03, 2022 @ 18:53

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);

}

emmapink246 - September 13, 2022 @ 11:46

too detailed, a quality blog coloring pages

otis5842 - September 27, 2022 @ 05:37

The EMA crossover is a bot that uses the EMA framed as a trading signal. The crossover bot is a simple trading strategy that combines two different signals (EMA and MACD) in one trade. The EMA crossover bot is an easy to use strategy that uses the EMA and MACD as trading signals. 

s.garry2255 - October 09, 2022 @ 12:19

It looks great as usual. Thank you so much for sharing. As a newbie here. I need your good guidelines. I would like to start forex trading. Is there any proven method that can help me to earn? Thank you!

0