global breakeven of a portfolio inside tmf function

Posted By: nanotir

global breakeven of a portfolio inside tmf function - 08/04/17 02:25

Hi

I am trying to get a breakeven (or close all trades when all_open_trades_profit>x) of a multiasset strategy inside the tmf function but I cannot make it to work.
Any ideas?
Posted By: jcl

Re: global breakeven of a portfolio inside tmf function - 08/04/17 11:52

Inside a TMF you have only information about the current trade, but not about the other trades. For summing up all profits I'd use a simple for() loop, no TMF.
Posted By: nanotir

Re: global breakeven of a portfolio inside tmf function - 08/04/17 14:28


Thanks for the clarification. I thought I could sum all the profits inside the tmf function.

If I need to use a for loop in the run function, then I will lost the global breakeven if it happens inside the barperiod, thats why I'd like to catch it on a tick level, or should I then use 1 minute candles so that the for loop in the run function does the work?
Posted By: jcl

Re: global breakeven of a portfolio inside tmf function - 08/05/17 10:12

No, use a loop in the tick function, not the run function. The tick function runs at every price quote and has access to all trades.
Posted By: Ger1

Re: global breakeven of a portfolio inside tmf function - 08/08/17 09:34

I got a similar question to this.

I got a portfolio that trades on a daily timeframe.

Lets say I got 5 algos trading on EUR/USD and all 5 will enter. Is it possible to get the number of trades that are to be opened?

This would help to adjust money to be invested on each trade based on the number of trades at the same time
Posted By: jcl

Re: global breakeven of a portfolio inside tmf function - 08/08/17 14:50

When you get a trade signal, do not trade immediately, but store the signal somewhere, f.i. in an AlgoVar or AssetVar. When all signals are processed, count them and then calculate the invested amounts and enter the trades.
Posted By: Ger1

Re: global breakeven of a portfolio inside tmf function - 08/11/17 20:57

thanks for your advice jcl.
I tried to set up very high entry conditions and then enterLong controlled by a tmf.

Within the tmf I ran two loops - the first loop counts all the pending trades for each algo (int n used) and after being finished with counting I enter the trade in the second loop, however, I divided TradeLots by n. I used the AlgoVar count to find out whether it is the first loop(to count pending trades) or the second to finally enter the trades adjusted by TradeLots.

However, something seems to be wrong as in certain instances the tmf crashes --> that's when: TradeLots=TradeLots/n; (n is equal 0)
I don't really understand why that happens.

Could you advise please?

Below my code:

#define count as_int(AlgoVar[0])

int n;
int tmf(var sys)
{
if(TradeIsNewBar)
{
n=0;
return 0;
}
if(sys==0)
{
algo("systemA");
}
else if(sys==1)
{
algo("systemB");
}

else if(sys==2)
{
algo("systemC");
}

if(count==1)
{
count=0;
// if(n==0) printf("n %i",date()); --> should never happen
// printf("n %i %s",n,Algo);
TradeLots=TradeLots/n;
// printf("nl %i ",TradeLots);
return 2;
}

if(TradeIsPending)
{
n=n+1;
// printf("n %i ",NumPendingTotal);
count=1;
return 4;
}
return 0;
}

function systemA()
{
...
if(entry_condition == true)
{
Entry = 99999;
enterLong(tmf,0)
}
}

function systemB()
{ ...
}

function systemC()
{ ...
}


function run()
{
BarPeriod = 1440;
...
while(asset(loop("SPX500","NAS100","US30")))
while(algo(loop("sytemA","sytemB","sytemC")))
....
}
Posted By: Ger1

Re: global breakeven of a portfolio inside tmf function - 08/19/17 05:11

Finally decided to to write 2 for-loops to loop through all assets and algos before actually entering trades and increasing a counter in case of an enter signal in any one of them.

After that I looped through again and set Lots = lot_number/count.

This seems to work as intended.

Below a simple strategy to illustrate:




int count;
int lot;

function tradeB(bool trade)
{

Stop = 2*ATR(30);

vars Price = series(priceClose());

LifeTime=1;
if(Price[0]>Price[1]) //simple entry condition
{
if(trade==false) count++; //increase the counter in case of an entry signal

else if(trade==true) //actual entry
{
printf("n %i %i %s %s",lot,count,Algo,Asset);
Lots=lot/count;
enterLong();
}
}
}


function tradeA(bool trade)
{

Stop = 2*ATR(30);

vars Price = series(priceClose());

LifeTime=1;
if(Price[0]>Price[1])
{
if(trade==false) count++;

else if(trade==true)
{
printf("n %i %i %s %s",lot,count,Algo,Asset);
Lots=lot/count;
enterLong();
}
}

}


function run()
{
StartDate = 20150110;
EndDate = 20150120;
BarPeriod = 1440;

set(TICKS);

lot = 100;



count=0;

string assets[2];
assets[0] = "EUR/USD";
assets[1] = "USD/JPY";

int i;
for(i=0;i!=2;i++) //loop to count entry signals
{
asset(assets[i]);
tradeA(false);
tradeB(false);
}

//actual trade entry loop
while(algo(loop("A","B")))
while(asset(loop("EUR/USD","USD/JPY")))
{
// printf("n %i",count);
if(Algo == "A")
{
tradeA(true);
}

if(Algo == "B")
{
tradeB(true);
}

}
}
Posted By: kmerlo

Re: global breakeven of a portfolio inside tmf function - 08/19/17 10:21

Thanks, very nice solution :-)
© 2024 lite-C Forums