Custom Class - Why not ExecuteMarketOrder he says...

Created at 26 Sep 2018, 14:18
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!
JO

Jonkey

Joined 09.11.2015

Custom Class - Why not ExecuteMarketOrder he says...
26 Sep 2018, 14:18


Hey team, 

This code will get a null pointer exeption on "ExecuteMarketOrder".

Can ExecuteMarketOrder() be called from a custom class like this?

Thank you in advance.

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 NewcBot : Robot
    {
        [Parameter("Buy", DefaultValue = true)]
        public bool tradeTypeBool { get; set; }

        [Parameter("Vol Qty", DefaultValue = 100000, MinValue = 1000, Step = 1000)]
        public int volQty { get; set; }

        MycBot thisIsMine;

        protected override void OnStart()
        {
            // Put your initialization logic here

            thisIsMine = new MycBot(tradeTypeBool, volQty);
        }

        protected override void OnTick()
        {
            // Put your core logic here
            if (!thisIsMine.running)
            {
                thisIsMine.AmIRunning(this);
            }
        }

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

    public class MycBot : NewcBot
    {
        public bool running;
        private double volume;
        private TradeType tt;

        public MycBot(bool tradeType, int vol)
        {
            running = false;
            volume = (double)vol;
            tt = tradeType == true ? TradeType.Buy : TradeType.Sell;
        }

        public void AmIRunning(Algo algo)
        {
            if (!running)
            {
                algo.Print("I am not running");
                try
                {
                    ExecuteMarketOrder(tt, algo.Symbol, volume);
                } catch (Exception e)
                {
                    algo.Print("{0}", e.StackTrace);
                }
                running = true;
                algo.Print("I am running");
            }
        }
    }
}

 


@Jonkey
Replies

PanagiotisCharalampous
26 Sep 2018, 14:29

Hi Jonkey,

The method needs to be called inside the cBot attached on the chart. Could you please explain us what are you trying to do? Maybe we can suggest something different.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Jonkey
26 Sep 2018, 14:33

RE:

Panagiotis Charalampous said:

Hi Jonkey,

The method needs to be called inside the cBot attached on the chart. Could you please explain us what are you trying to do? Maybe we can suggest something different.

Best Regards,

Panagiotis

I just needed a better way to manage the context of many sets of compound trades? I was thin ing maybe a struct, that then fed data like average position.EntryPrice back to main algo?

Thank you Panagiotis.


@Jonkey