Asynchronous verses Synchronous

Created at 07 Jun 2013, 14:08
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!
lec0456's avatar

lec0456

Joined 14.11.2012

Asynchronous verses Synchronous
07 Jun 2013, 14:08


I have some code in the OnTick event that calls the Trade.Close method. 

In the OnClosePosition I set a global Variable to get the pips from the last trade that closed.

 

My problem is that after I call Trade.Close I want to execute a trade usung the pips from the last closing trade.  It seems that this is all occuring Asychronously, so it is still using the last traded pips from the trade before,not the trade I just closed...

 

I hope somebody understands what I'm trying to say...


@lec0456
Replies

cAlgo_Fanatic
07 Jun 2013, 14:18

If you create a new trade in the OnPositionClosed event wouldn't that serve the purpose? 


@cAlgo_Fanatic

lec0456
07 Jun 2013, 14:25

Its that, I don't want to execute a trade everytime a trade closes.  I have certain criteria in the OnTick event that causes the trade to close and open another.

I tryed using a wait loop:  

CloseOpenPositions();
Wait: if(Trade.IsExecuting) goto Wait;
var SellMarketOrder = new MarketOrderRequest(TradeType.Sell, TradeVol)
{Label="SellOrder",	StopLossPips=75, TakeProfitPips=(int)Math.Abs(LastTradePips)+8};
Print(MarketSeries.OpenTime[t0]+" TP:"+Math.Abs(LastTradePips));
Trade.Send(SellMarketOrder);

but it hangs...

 


@lec0456

cAlgo_Fanatic
07 Jun 2013, 15:27

In the OnTick event use this instead:

if(Trade.IsExecuting) return;

So, if you set a bool field (global scope) to true on the OnPositionClosed event and in the OnTick event within your other conditional statements where you open a trade check this field as well? The field would also need to be reset to false somewhere depending on the logic.  
I'm not quite sure if I understand the full logic here to be able to say that this is indeed your solution.
If you do not want to share the logic on the forum please send an email to engage@spotware.com with more details or code.

 


@cAlgo_Fanatic

lec0456
07 Jun 2013, 15:46

Well,   the problem with the statement 

if(Trade.IsExecuting) return;

Is that it kicks you out of the OnTick event so that now the next tick would execute but the trade would have been be closed by the previous tick, so it would not know to execute this "special" type of trade.

 

But, I did what you suggested, I setup a global flag so that the OnClosePosition will execute the special trade if the flag is set and it resets the flag.

 

I guess I was curious if there was another way around the issue with code in the OnTick event, but I got it to work like you said

 


@lec0456

hichem
07 Jun 2013, 18:33

Interesting!

Actually I have made a c# library to make transform asynchronous cAlgo Trade Requests into synchronous ones. With this library I was able to :

- Handle lost connections by asserting that each request has been succesfully executed before sending the next request.

- Handle coordination between Trade requests. This is done by sequencing trade request execution which I think will allow you to overcome your problem.

 

The library is on BitBucket: https://bitbucket.org/mrhichem/scyware-calgo-framework

Give me your email so I can share it with you.

 

Cheers.

 


@hichem

lec0456
07 Jun 2013, 19:03

OK,

lecespedes@hotmail.com


@lec0456