Help me plz, sl of Artificial Intelligence ROBOT dosnt work!

Created at 07 Oct 2013, 15:53
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!
KH

khorshidi07

Joined 03.10.2013

Help me plz, sl of Artificial Intelligence ROBOT dosnt work!
07 Oct 2013, 15:53


Hello

StopLoss of Artificial Intelligence ROBOT (/algos/robots/show/46) dos not work.

it has error.

could anybody repair it?!

thanks alot.

khorshidi.


@khorshidi07
Replies

khorshidi07
07 Oct 2013, 17:28

Mr Spotware or Mr algotrader or a programmer person,(I dont know all!), could edit above robot?

it is NECESSARY for me

thank you


@khorshidi07

Spotware
08 Oct 2013, 11:30 ( Updated at: 21 Dec 2023, 09:20 )

RE:

hosseinkhorshidi said:

Mr Spotware or Mr algotrader or a programmer person,(I dont know all!), could edit above robot?

it is NECESSARY for me

thank you

Have you experienced the issue in backtesting?
What are the input parameters you have used
Can you send a screenshot such as the following showing that the stop loss is not added properly on position modified, for the backtest?


@Spotware

khorshidi07
08 Oct 2013, 12:44

thank you Mr spotware...

when a position opened, Sl failed like below image.

 


@khorshidi07

khorshidi07
08 Oct 2013, 12:45


@khorshidi07

Spotware
08 Oct 2013, 15:40

If you have modified the code, please post it so that we can investigate further. The value of the Stop Loss is probably too low.


@Spotware

khorshidi07
08 Oct 2013, 16:31

before modified, it has problem too.

i dont modified too much.

i modified a litle according to site of MQ4.

i send you last modified code.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot("ArtificialIntelligence")]
    public class ArtificialIntelligence : Robot
    {

        [Parameter(DefaultValue = "ArtificialIntelligence")]
        public string LabelName { get; set; }

        [Parameter("x1", DefaultValue = 120)]
        public int x1 { get; set; }

        [Parameter("x2", DefaultValue = 120)]
        public int x2 { get; set; }

        [Parameter("x3", DefaultValue = 19)]
        public int x3 { get; set; }

        [Parameter("x4", DefaultValue = 100)]
        public int x4 { get; set; }

        [Parameter("FastMA", DefaultValue = 12)]
        public int FastMA { get; set; }

        [Parameter("SlowMA", DefaultValue = 26)]
        public int SlowMA { get; set; }

        [Parameter("Step", DefaultValue = 9)]
        public int Step { get; set; }

        [Parameter("StopLoss", DefaultValue = 200)]
        public int StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 1)]
        public int Volume { get; set; }

        private MacdHistogram macd;
        private Position pos;
        private bool IsPosOpen = false;
        private double sl;
        protected override void OnStart()
        {
            macd = Indicators.MacdHistogram(SlowMA, FastMA, 9);
        }

        protected override void OnTick()
        {
            int last = MarketSeries.Close.Count - 1;
            if (!(MarketSeries.Open[last] == MarketSeries.High[last] && MarketSeries.Open[last] == MarketSeries.Low[last]))
                return;
            double per = percertron();
            if (!IsPosOpen)
            {
                if (per > 0)
                {
                    Trade.CreateBuyMarketOrder(Symbol, Volume);
                    IsPosOpen = true;
                }
                if (per < 0)
                {
                    Trade.CreateSellMarketOrder(Symbol, Volume);
                    IsPosOpen = true;
                }
            }
            else
            {
                if (pos.TradeType == TradeType.Buy && per < 0)
                {
                    Trade.Close(pos);
                    Trade.CreateSellMarketOrder(Symbol, Volume);
                    return;
                }
                else
                {
                    if (Symbol.Ask > sl + StopLoss * 2 * Symbol.PointSize)
                        Trade.ModifyPosition(pos, Symbol.Ask - StopLoss * Symbol.PointSize, 0);
                }
                if (pos.TradeType == TradeType.Sell && per > 0)
                {
                    Trade.Close(pos);
                    Trade.CreateBuyMarketOrder(Symbol, Volume);
                }
                else
                {
                    if (Symbol.Bid < sl - StopLoss * 2 * Symbol.PointSize)
                        Trade.ModifyPosition(pos, Symbol.Bid + StopLoss * Symbol.PointSize, 0);
                }
            }
        }

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

        protected override void OnPositionOpened(Position openedPosition)
        {
            pos = openedPosition;
            if (openedPosition.TradeType == TradeType.Buy)
            {
                sl = Symbol.Ask - StopLoss * Symbol.PointSize;
                Trade.ModifyPosition(openedPosition, sl, 0);
            }
            if (openedPosition.TradeType == TradeType.Sell)
            {
                sl = Symbol.Bid + StopLoss * Symbol.PointSize;
                Trade.ModifyPosition(openedPosition, sl, 0);
            }
        }

        private double percertron()
        {
            int last = MarketSeries.Close.Count - 1;
            double w1 = x1 - 100;
            double w2 = x2 - 100;
            double w3 = x3 - 100;
            double w4 = x4 - 100;
            double a1 = macd.Histogram[last - 1];
            double a2 = macd.Histogram[last - 1 - Step];
            double a3 = macd.Histogram[last - 1 - Step * 2];
            double a4 = macd.Histogram[last - 1 - Step * 3];
            return w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4;
        }
    }
}

 


@khorshidi07

khorshidi07
08 Oct 2013, 16:36

code of EA MQ4:

 

http://www.earnforex.com/mt4-expert-advisors/ArtificialIntelligence.mq4

 

i think this EA is one of the best EA!


@khorshidi07

Spotware
09 Oct 2013, 09:34

The problem is actually the take profit is set to 0. It should be null if you don't want to set the take profit, i.e.:

Trade.ModifyPosition(posistion, stopLossValue, null);

 


@Spotware

khorshidi07
09 Oct 2013, 11:12

UNFORTUNATELY my programing is very bad and i couldnt edit robot!

if i want, my position have takprofir and stoploss in above robot, i write :

[Parameter("TakeProfit", DefaultValue = 200)]
        public int TakeProfit { get; set; }

above the :

[Parameter("StopLoss", DefaultValue = 200)]
        public int StopLoss { get; set; }

and i must what do i do else?!

if it is possible, you edit all of robot and insert here, plz!

thanks alot Mr spotware


@khorshidi07

virtuesoft
09 Oct 2013, 17:34

RE:

Replace all of the code in OnPositionOpened with the code below...

        protected override void OnPositionOpened(Position openedPosition)
        {
            pos = openedPosition;
            IsPosOpen = true;

            if (openedPosition.TradeType == TradeType.Buy)
            {
                sl = Symbol.Ask - StopLoss * Symbol.PointSize;
                Trade.ModifyPosition(openedPosition, sl, null);
            }
            if (openedPosition.TradeType == TradeType.Sell)
            {
                sl = Symbol.Bid + StopLoss * Symbol.PointSize;
                Trade.ModifyPosition(openedPosition, sl, null);
            }
        }

 

hosseinkhorshidi said:

UNFORTUNATELY my programing is very bad and i couldnt edit robot!

if i want, my position have takprofir and stoploss in above robot, i write :

[Parameter("TakeProfit", DefaultValue = 200)]
        public int TakeProfit { get; set; }

above the :

[Parameter("StopLoss", DefaultValue = 200)]
        public int StopLoss { get; set; }

and i must what do i do else?!

if it is possible, you edit all of robot and insert here, plz!

thanks alot Mr spotware

 


@virtuesoft

Spotware
09 Oct 2013, 17:53

If you need to set the take profit at the OnPositionOpened event: 

        protected override void OnPositionOpened(Position openedPosition)
        {
            pos = openedPosition;
            if (openedPosition.TradeType == TradeType.Buy)
            {
                sl = Symbol.Ask - StopLoss*Symbol.PointSize;
                var tp = Symbol.Ask + TakeProfit * Symbol.PointSize;

                Trade.ModifyPosition(openedPosition, sl, tp);
            }
            else
            {
                sl = Symbol.Bid + StopLoss*Symbol.PointSize;
                var tp = Symbol.Bid - TakeProfit * Symbol.PointSize;
                Trade.ModifyPosition(openedPosition, sl, tp);
            }
        }

If you also need to set the take profit in the OnTick event when the position is modified, please provide the logic.


@Spotware

khorshidi07
09 Oct 2013, 22:01

Thank you very very much.

the problem solved.


@khorshidi07

jeex
19 Oct 2013, 18:51

Null Reference

The code shows a crash on Null Reference in OnTick while testing. Testing shows its the pos in line 7'1.

It's supposed to be assigned in the  OnPositionOpened, but apperently doesn't.


@jeex

tradermatrix
20 Oct 2013, 12:50

erreur de volume ;1

 volume :10000


@tradermatrix