Catching closing trades

Posted By: DanielX

Catching closing trades - 04/17/20 08:18

Hi,

I have a rather simple strategy which I am trying to backtest to understand the Zorro platform.
It basically has a entry-condition and when met it enters a trade using the enterLong-function with a Stop and Profit set. (done in the run() function of course)

So far working as expected and enters new trades when it should, but my question is how I can capture when the Stop or Profit is hit in a bar-cycle.
I know I can use the ThisTrade to get access to the recently entered trade, but how can I find trades that was closed by hitting the Stop or Profit in the same bar-cycle?

Do I have to write my own TradeManagementFunction, do I have to loop through all closed trades, or is there another way?

/Daniel
Posted By: jcl

Re: Catching closing trades - 04/17/20 11:09

Loop through the closed trades, and check if TradeBarClose is identical to the current bar.
Posted By: DanielX

Re: Catching closing trades - 04/20/20 09:32

Hi,
thanks, that was kind of easy laugh

However, when trying the following:

for(closed_trades)
if(TradeBarClose == Bar)
printf("\n --- TradePriceOpen: %.2f TradePriceClose: %.2f TradeStopLimit %.2f TradeStopDiff %.2f TradeProfitLimit: %.2f TradeProfit: %.2f",
(var) TradePriceOpen, (var) TradePriceClose, (var) TradeStopLimit, (var) TradeStopDiff, (var) TradeProfitLimit, (var) TradeProfit );

I get the following result:

--- TradePriceOpen: 33.58 TradePriceClose: 31.21 TradeStopLimit 31.22 TradeStopDiff -2.36 TradeProfitLimit: 36.96 TradeProfit: 5.14
--- TradePriceOpen: 30.83 TradePriceClose: 28.52 TradeStopLimit 28.54 TradeStopDiff -2.31 TradeProfitLimit: 33.93 TradeProfit: 2.49
--- TradePriceOpen: 30.62 TradePriceClose: 28.39 TradeStopLimit 28.38 TradeStopDiff -2.25 TradeProfitLimit: 33.67 TradeProfit: 8.00

The TradeProfit looks really strange.
The first trade for example is a loss, but TradeProfit = 5.14
According to the documentation TradeProfit = (TradePriceClose-TradePriceOpen) which isn't true for any of the trades...


/Daniel
Posted By: DanielX

Re: Catching closing trades - 04/20/20 14:00

Found the problem...
I had incorrect data for the asset in AccountsFix.csv.


So now it looks a bit better when testing.
© 2024 lite-C Forums