TMF with adapting lots

Posted By: byakuren81

TMF with adapting lots - 06/12/18 07:11

With the trailing stop script below I would like to adapt the position size to the TradeStopLimit, however if I define Lots inside the function TrailingStop it is ignored and set to 1, also I do not have access to TradeStopLimit in the run even if it has been set in TrailingStop, it will output 0.
Could you please tell me how I could some this issue please?

int TrailingStop()
{
if(TradeIsLong and TradeProfit > 0)
TradeStopLimit = max(TradeStopLimit, priceLow() - 3 * ATR(20));

if(TradeIsShort and TradeProfit > 0)
TradeStopLimit = min(TradeStopLimit, priceHigh() + 3 * ATR(20));

return 0;
}

static TRADE* MyTrade = NULL;

function run()
{
set(TICKS);

BarPeriod = 1440;
LookBack = 61;
StartDate = 2017;
EndDate = 2018;

Slippage = 0;
Spread = 0;

if(!TradeIsOpen)
{
if(random() < 0)
MyTrade = enterShort(TrailingStop);
else
MyTrade = enterLong(TrailingStop);
}

if (TradeIsOpen)
{
ThisTrade = MyTrade;

printf("nStop = %.5f", TradeStopLimit);
}

set(LOGFILE);
}
Posted By: AndrewAMD

Re: TMF with adapting lots - 06/12/18 13:55

A TMF is intended for handling already-open trades. That means lot size was already determined by the time Zorro calls any TMF function.

Ideally, you should size your trade in run() before you call enterShort(TrailingStop) or enterLong(TrailingStop).

Depending on what you are trying to do, maybe you can create a global variable for the next lot size. You can set it from your TMF and get it from run().

Also, this is vague:
Code:
if(TradeIsLong and TradeProfit > 0)

Be explicit and use more parentheses.
Posted By: byakuren81

Re: TMF with adapting lots - 06/12/18 17:36

Ouch yep indeed thx for that I should be more careful about the parentheses!
© 2024 lite-C Forums