Trade management functions ( TMF )

Posted By: Neb

Trade management functions ( TMF ) - 12/22/20 14:55


Hi,

I have a few issue with using TMF to set/change my stop losses or trail my profit.

1)

When I am using TMF to setup and move my SL, it is not setting up SL at all ?

int myTMF()
{

for(open_trades) {
if(TradeIsLong && TradeProfit>0) TradeStopLimit = max(TradeStopLimit, priceClose()-4*ATR(40));

else if(TradeIsShort && TradeProfit>0) TradeStopLimit = min(TradeStopLimit, priceClose()+4*ATR(40));

if(TradeStopLimit != 0) plot("Stop",TradeStopLimit,DOT|MAIN,BLUE);

}
return 0;
}


Picture attached.


2) As I found out, I can't use distance in TMF to set it up ?

This won't work ->

TradeStopLimit = 50*PIP;

It set my stop basically to the price value of 50, as my test shows.

3)

TradeStopLimit = priceClose()-4*ATR(40));

I found this to work, but problem is that it is going up and down and in long trade I want it to go only up or not to change once it has been setup.

4)

How I can setup trail, as it said I can't setup with distance in PIP.
How actually I am setting it up, if I want to use different TradeTrailLock, according to the level of profit ?

if (TradeProfit >0)
{
TradeTrailLock = 0.2
}
if (TradeProfit >0.05*Balance)
{
TradeTrailLock = 0.8
}


I am not great in programming, just learning, sorry

Thanks in advance !!

Neb

Attached picture TradeStopLoss.JPG
Posted By: AndrewAMD

Re: Trade management functions ( TMF ) - 12/22/20 16:18

Never run trade loops inside a TMF, unless you have a very good reason to! (This is not one of them.)

Also, I assume you correctly assigned a TMF to a trade. Did you do that? This happens when you call enterLong(myTMF) or enterShort(myTMF).
Posted By: Neb

Re: Trade management functions ( TMF ) - 12/22/20 16:45

Hi Andrew,

Thanks for quick reply !!!

You meant about for {} loop ?
Ok !
Actually it is redundant I suppose, cause I've been calling for all open trades and then again checking if trade is long or short.

I did call enterLong(myTMF) and enterShort(myTMF) inside run{}, as it was in one of the examples. Is that ok or ... ?

Thanks !
Posted By: AndrewAMD

Re: Trade management functions ( TMF ) - 12/22/20 17:09

Yes and yes.

The TMF answers the question: "I have this trade open. What do I do with it?"

A trade loop such as for(open_trades) explicitly iterates through all open trades, which is a different way to handle trades or get information about them. Normally, you'd execute this from run() or tick().
Posted By: Neb

Re: Trade management functions ( TMF ) - 12/22/20 17:12

Thanks , got it !


And instead of using Trail, what actually not work inside TMF, i can use something like this:

if(TradeProfit>0.03*Balance && TradeIsLong)
{
TradeStopLimit = max(0.5*(TradePriceClose-TradePriceOpen), TradePriceClose-3*ATR(25));
plot("Stop",TradeStopLimit-10*PIP,MINV,BLUE);
}
if(TradeProfit>0.03*Balance && TradeIsShort)
{
TradeStopLimit = min(0.5*(TradePriceOpen-TradePriceClose), TradePriceClose+3*ATR(25));
plot("Stop",TradeStopLimit+10*PIP,MAXV,RED);
}

Posted By: AndrewAMD

Re: Trade management functions ( TMF ) - 12/23/20 01:58

I don't think it works to call plot() from a TMF.
Quote
If plot is called several times with the same Name in the same run cycle, only the last data value is plotted.
https://manual.zorro-project.com/plot.htm

More importantly, why are you using the value TradePriceClose when it is impossible to know what TradePriceClose is because your trade is still open? Use priceClose() instead.
Posted By: Neb

Re: Trade management functions ( TMF ) - 12/23/20 10:37

Hi Andrew,

It works, at least I was using it only to see how my SL was moving (debugging steps)
But generally, I can delete it.

Re TradePriceClose manual says:

https://zorro-project.com/manual/en/trade.htm

"If the trade is still open, it's the current price of the asset or contract".

So, this is why I was using it laugh
Posted By: Neb

Re: Trade management functions ( TMF ) - 12/23/20 12:09



Can I use optimize() and how inside TMF function ?
I would like to see where is the most optimal to move my SL ?
Or you maybe suggest to plot MAE and price profile ?

Thanks,

Neb
Posted By: AndrewAMD

Re: Trade management functions ( TMF ) - 12/23/20 13:48

Originally Posted by Neb
"If the trade is still open, it's the current price of the asset or contract".

So, this is why I was using it laugh
I think you're right!
Originally Posted by Neb
Can I use optimize() and how inside TMF function ?
I would like to see where is the most optimal to move my SL ?
Or you maybe suggest to plot MAE and price profile ?

Thanks,

Neb
Call optimize() in run(). If you need the optimized values in your TMF, share the return value from optimize() with your TMF, such as with a global value, AssetVar, or TMF parameter., i.e. enterLong(myTMF, parameter1, parameter2, ...).
Posted By: Neb

Re: Trade management functions ( TMF ) - 01/03/21 16:55

Thanks Andrew !

Much appreciated !

Neb
Posted By: Neb

Re: Trade management functions ( TMF ) - 01/03/21 17:10

Hi,

One more, for more experienced, because it is not quite clear to me.
How I am entering more than 1 lots if I am using TMF ?
Will it be 2nd parameter in entryLong/Short() and first in TMF() ?

Something like entryLong(myTMF, numLots) ?
And then myTMF(numLots) {}

Or different ?

Thanks,

Neb
Posted By: AndrewAMD

Re: Trade management functions ( TMF ) - 01/03/21 19:45

Simply set the global value Lots before calling enterLong() or enterShort(). Or any of these values:
https://zorro-project.com/manual/en/lots.htm
© 2024 lite-C Forums