crossing points

Created at 13 Oct 2012, 12:01
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!
PH

phamvanthanh

Joined 07.10.2012

crossing points
13 Oct 2012, 12:01


Hi,

I am developing my first robots from samples and having troubles that i can not explain.

The robot to create close the existing position and create a new position when slow period moving avarage crosses fast period moving avarage and when GrossProfit < anumber. But when I do the backtest and realtime testing it does not create marketorders or close positions at right positions ( crossing points). The GrossProfit condition works right. I have tried to find the error but I could not find it. Could someone please help me?. The codes as below

 

 protected override void OnTick()

        {

            if (Trade.IsExecuting) return;

            

            int lastIndex = slowMa.Result.Count - 1;

            int prevIndex = slowMa.Result.Count - 2;

 

            double currentSlowMa = slowMa.Result[lastIndex];

            double currentFastMa = fastMa.Result[lastIndex];

 

            double previousSlowMa = slowMa.Result[prevIndex];

            double previousFastMa = fastMa.Result[prevIndex];

 

            bool isLongPositionOpen = position != null && position.TradeType == TradeType.Buy;

            bool isShortPositionOpen = position != null && position.TradeType == TradeType.Sell;

           

 

            if ((previousSlowMa > previousFastMa) && (currentSlowMa <= currentFastMa) && !isLongPositionOpen) 

            {               

                ClosePosition();

                Buy();                

            }

 

            if ((previousSlowMa < previousFastMa) && (currentSlowMa >= currentFastMa) && !isShortPositionOpen)

            {               

                ClosePosition();

                Sell();               

            }

            

            if (position != null && position.GrossProfit < -StopLoss)

            {

               ClosePosition();

            }  

           

            

        }

 

        private void ClosePosition()

        {

            if (position != null)

            {

                Trade.Close(position);

                position = null;

            }

        }

 

        private void Buy()

        {

            Trade.CreateBuyMarketOrder(Symbol, Volume);

        }

 

        private void Sell()

        {

            Trade.CreateSellMarketOrder(Symbol, Volume);

        }

 

        protected override void OnPositionOpened(Position openedPosition)

        {

            position = openedPosition;

        }

    }

}

 

 

 

 


@phamvanthanh
Replies

admin
15 Oct 2012, 15:40

This code 

int lastIndex = slowMa.Result.Count - 1;

corresponds to the index of the last bar.  Therefore, at runtime, weather backtesting or real time, the last bar would not have completed and the values, high,low close, as well as indicator values that depend on those would not be final. The only final value is the open price and open time. But the rest will keep changing until the bar completes and a new bar starts which triggers a new OnBar event, which brings us to another subject. When comparing indicator values such as the code above, it is best to use the OnBar event rather than the OnTick event. Since using the OnTick event will trigger far more Trades than the OnBar resulting in probably more loss due to commissions taken.

So, you need to use the previous to the last candle compared to the one before the previous.

i.e. 

int lastIndex = slowMa.Result.Count - 2;

int prevIndex = slowMa.Result.Count - 3;

 


@admin

phamvanthanh
18 Oct 2012, 19:24

When I change the definition for the lastindex and the previndex the rossing points' problem solved but the above stop loss does not work right. can you help me explain this.

thanks

 


@phamvanthanh

admin
19 Oct 2012, 12:12

In the sample robots (e.g. SampleBuyTrailing, SampleBreakoutRobot)  StopLoss is defined as an int (integer)  and refers to the number of pips and is used then in an equation that translates the number of pips to the amount equivalent to those pips.

 

Please take a look at the sample robots that ship with cAlgo as well as sample examples and documentation for more help on setting StopLoss:
/forum/calgo-reference-samples/69
/docs/reference/calgo/api/internals/itrade/modifyposition

If the above does not help, please post the code where you set the StopLoss.

 

 


@admin

phamvanthanh
19 Oct 2012, 12:57

Yes, It workss. Thanks


@phamvanthanh