Problem with this logic ?

Created at 20 Nov 2015, 15:47
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!
SI

sim_trader

Joined 03.08.2013

Problem with this logic ?
20 Nov 2015, 15:47


Hi Everyone,

Thanks for all your help in the past. I am trying to copy my trades in from MT4 to Ctrader, they are all stop orders. They are written to a csv with the comment as the unique MT4 ID.

 Everything works perfectly with one exception, that occasionally the same trade is copied twice. It is not initially (there are never duplicate orders in the Calgo order list) but later I see 2 trades with the same comment (MT4 ID). I think it happens when the order opens/closes. Calgo does not recognize the order and reads in the stop order again so it gets executed twice.

This leads me to believe I have done something wrong with the following logic.

foreach (var exorder in PendingOrders)
                {
                    if (exorder.Comment == order.comment)
                    {
                        allowtrade = 0;

                    }
                }


                foreach (HistoricalTrade trade in History)
                {
                    if (trade.Comment == order.comment)
                    {
                        allowtrade = 0;

                    }
                }

                foreach (var position in Positions)
                {
                    if (position.Comment == order.comment)
                    {
                        allowtrade = 0;

                    }
                }

What I am trying to say is “if the trade is not already in the orders”, “if the trades is not already in the open positions” or “if the trade is not already in the history” then it must be new so copy it in.

allowtrade=0 prevent the trade from being copied in.

Can anyone see what I am doing wrong is there a better to do this. In simple terms I need to check in C-trader to ensure the position does not already exist.

Please be gentle, I have only been programming C for 3 weeks.

Thanks so much, Cheers

Andre


@sim_trader
Replies

ClickAlgo
21 Nov 2015, 07:14

Hi,

Use a Boolean operator and name it to something meaningful like positionExists, than before you do your for-each loops set it to false, replace allowtrade with positionExists = true

This will assume no position exists from the start and anyone of your conditions will set it to true.

Investigate LINQ and LAMDA expressions for cleaner and more powerful coding, all the for-each loops could be replaced with 3 lines of code:-

positionsExist = Positions.Where(x => (x.Comment == order.comment)).Count() > 0 ? true : false;
positionsExist = PendingOrders.Where(x => (x.Comment == order.comment)).Count() > 0 ? true : false;
positionsExist = History.Where(x => (x.Comment == order.comment)).Count() > 0 ? true : false;

Paul.

 

 


@ClickAlgo

ClickAlgo
21 Nov 2015, 07:18

RE:
// sorry i put too many parenthesis in there, this is a better example

positionExists = Positions.Where(x => x.Comment == order.comment).Count() > 0 ? true : false;
positionExists = PendingOrders.Where(x => x.Comment == order.comment).Count() > 0 ? true : false;
positionExists = History.Where(x => x.Comment == order.comment).Count() > 0 ? true : false;

 


@ClickAlgo

sim_trader
21 Nov 2015, 19:49

Thanks

Hi Paul,

Thanks again, I really appreciate your time.

I cannot believe however efficient that is. One line to find, count, and check the condition.

It compiles perfectly.

Wonderful, thanks so much. I am certainly going to do more reading. I had never heard of LINQ and LAMDA.

Thanks again for your time !

Cheers, Andre


@sim_trader