3 registered members (Ayumi, Akow, AndrewAMD),
1,505
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Trading multiple assets
#460674
07/10/16 01:32
07/10/16 01:32
|
Joined: Jun 2016
Posts: 33 NC
Cstyle
OP
Newbie
|
OP
Newbie
Joined: Jun 2016
Posts: 33
NC
|
I'm getting some pretty bizarre behavior when I trade multiple assets. If I just trade UK100, here is what the first trade looks like: Type Asset ID Lots Open Close Entry Exit Profit Roll ExitType Long UK100 51201 1 3/19/2009 9:00 3/24/2009 5:01 3888 3948 7.77 -0.03 Stop
When I trade both UK100 and GER30, Here is what the trades look like: Name Type Asset ID Lots Open Close Entry Exit Profit Roll ExitType PennyPicker6 Long UK100 51201 1 3/19/2009 9:00 3/24/2009 2:50 3888 4234 45.52 0 Sold PennyPicker6 Long GER30 51302 1 3/19/2009 10:00 3/24/2009 2:50 4070 4235 16.55 0 Sold
It looks like Zorro is getting the exit price for UK100 from GER30's price data. UK100 did not trade anywhere near that price on that date, so that obviously can't be right. Any idea what could be causing behavior like that?
Last edited by Cstyle; 07/10/16 20:47.
|
|
|
Re: Trading multiple assets
[Re: Cstyle]
#460690
07/10/16 23:42
07/10/16 23:42
|
Joined: Jun 2016
Posts: 33 NC
Cstyle
OP
Newbie
|
OP
Newbie
Joined: Jun 2016
Posts: 33
NC
|
Here is what the trades look like on the chart: As you can see, the strategy is often exiting UK100 way outside the range that the asset normally trades in, for big wins and big losses. Any ideas how this could be happening? I'm convinced it has something to do with trading multiple assets.
|
|
|
Re: Trading multiple assets
[Re: jcl]
#460712
07/11/16 16:32
07/11/16 16:32
|
Joined: Jun 2016
Posts: 33 NC
Cstyle
OP
Newbie
|
OP
Newbie
Joined: Jun 2016
Posts: 33
NC
|
Here is the code for just a single asset, which works perfectly:
// Penny Picking strategy ///////////////////
#include <profile.c>
int TrailWhenProfitable()
{
static bool TrailInitiated;
var AverageOpen = 0;
int Count = 0;
if(NumOpenLong > 0 )
{
var AverageOpen = 0;
int Count = 0;
for(open_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
if (priceClose() > AverageOpen + (2 * ATR(100)) && TrailInitiated == false)
{
TradeStopLimit = priceClose() - 2 * ATR(100);
TradeTrailLimit = priceClose() - ATR(100);
TrailInitiated = true;
printf(" TradeStopLimit = %.3f, TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
printf(" ATR = %.3f, priceClose = %.3f", ATR(100), priceClose());
printf(" AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
}
if (TrailInitiated == true && ltod(ET) > 1230)
{
exitLong();
printf(" ltod = %f", (var) ltod(ET));
}
}
else
TrailInitiated = false;
return 0;
}
function run()
{
StartDate = 2009;
EndDate = 2016;
LookBack = 500;
set(LOGFILE); // log all trades
set(TICKS); // normally needed for TMF
vars Price = series(price());
vars Trend = series(LowPass(Price,500));
if(valley(Trend) && NumOpenLong < 4)
{
var AverageOpen = 0;
var AverageProfit = 0;
int Count = 0;
for(open_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
switch (Count)
{
case 0:
enterLong(TrailWhenProfitable);
break;
case 1:
if (AverageProfit < -0.10)
{
enterLong(TrailWhenProfitable);
printf(" enter 2nd trade ");
}
break;
case 2:
if (AverageProfit < -0.15)
{
enterLong(TrailWhenProfitable);
printf(" enter 3rd trade ");
}
break;
case 3:
if (AverageProfit < -0.20)
{
enterLong(TrailWhenProfitable);
printf(" enter 4th trade ");
}
break;
default:
printf("None of them! ");
}
} // if trending
}
Here is the code with multiple assets which is exiting at erroneous prices:
// Penny Picking strategy ///////////////////
#include <profile.c>
int TrailWhenProfitable()
{
bool TrailInitiated;
static bool TrailInitiatedA;
static bool TrailInitiatedB;
static bool TrailInitiatedC;
while(asset(loop("UK100","GER30", "NAS100")))
{
if(NumOpenLong > 0 )
{
var AverageOpen = 0;
int Count = 0;
for(open_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
switch (Asset)
{
case "UK100":
TrailInitiated = TrailInitiatedA;
break;
case "GER30":
TrailInitiated = TrailInitiatedB;
break;
case "NAS100":
TrailInitiated = TrailInitiatedC;
break;
}
if (priceClose() > AverageOpen + (2 * ATR(100)) && TrailInitiated == false)
{
TradeStopLimit = priceClose() - 2 * ATR(100);
TradeTrailLimit = priceClose() - ATR(100);
TrailInitiated = true;
printf(" TradeStopLimit = %.3f, TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
printf(" ATR = %.3f, priceClose = %.3f", ATR(100), priceClose());
printf(" AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
switch (Asset)
{
case "UK100":
printf("Asset UK100");
break;
case "GER30":
printf("Asset GER30");
break;
case "NAS100":
printf("Asset NAS100");
break;
default:
printf("None of them! ");
} // switch
}
if (TrailInitiated == true && ltod(ET) > 1230)
{
exitLong();
printf(" ltod = %f", (var) ltod(ET));
}
} // if NumOpenLong > 0
else
TrailInitiated = false;
switch (Asset)
{
case "UK100":
TrailInitiatedA = TrailInitiated;
break;
case "GER30":
TrailInitiatedB = TrailInitiated;
break;
case "NAS100":
TrailInitiatedC = TrailInitiated;
break;
} // switch
} // while asset loop
return 0;
}
function run()
{
StartDate = 2009;
EndDate = 2016;
LookBack = 500;
set(LOGFILE); // log all trades
set(TICKS); // normally needed for TMF
while(asset(loop("UK100","GER30","NAS100")))
{
vars Price = series(price());
vars Trend = series(LowPass(Price,500));
if(valley(Trend) && NumOpenLong < 4)
{
var AverageOpen = 0;
var AverageProfit = 0;
int Count = 0;
for(open_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
switch (Count)
{
case 0:
enterLong(TrailWhenProfitable);
break;
case 1:
if (AverageProfit < -0.10)
{
enterLong(TrailWhenProfitable);
printf(" enter 2nd trade ");
}
break;
case 2:
if (AverageProfit < -0.15)
{
enterLong(TrailWhenProfitable);
printf(" enter 3rd trade ");
}
break;
case 3:
if (AverageProfit < -0.20)
{
enterLong(TrailWhenProfitable);
printf(" enter 4th trade ");
}
break;
default:
printf("None of them! ");
} // switch count
} // if trending
} // while asset loop
} // run
|
|
|
Re: Trading multiple assets
[Re: jcl]
#460723
07/11/16 23:31
07/11/16 23:31
|
Joined: Jun 2016
Posts: 33 NC
Cstyle
OP
Newbie
|
OP
Newbie
Joined: Jun 2016
Posts: 33
NC
|
If the purpose was just setting a trailing stop, there are several examples in the manual. It's a little more complex than that. First of all, if the trade goes against me, I increase my position, lowering the average entry price. A trail is only set once priceClose moves above the average entry price. The code works perfectly for just one asset, as I showed in the previous post. I'm doing something wrong though when I add in multiple assets. In addition, it still doesn't make sense to me why Zorro would ever have me exit a trade at a price significantly higher than the high of that day, or significantly below the low of a day. Regardless how flawed my code is, that shouldn't happen. No matter what I did in live trading I could never sell my position way above the high of the day, so in that manner zorro is not accurately simulating live action. Just look at that image I posted earlier!
|
|
|
Re: Trading multiple assets
[Re: Cstyle]
#460733
07/12/16 06:57
07/12/16 06:57
|
Joined: Jul 2000
Posts: 27,986 Frankfurt
jcl
Chief Engineer
|
Chief Engineer
Joined: Jul 2000
Posts: 27,986
Frankfurt
|
You can absolutely produce such an image with your script when you overwrite trade parameters with data from wrong assets. Programming is not foolproof - you can do anything with any variable, no matter if it makes sense or not. Garbage in, garbage out.
Your single asset TMF makes some sense to me, but I do not understand the purpose of your multi asset version. Delete it and start over. Think about what parameters you want to change - then program just that, preferably without "while", "loop", "asset", or similar stuff, and without overwriting other trades. If you are not sure how to do it, just ask here. if you get strange results, you can normally easily find out why - use the single step mode and watch all variables in question.
|
|
|
Re: Trading multiple assets
[Re: jcl]
#460863
07/17/16 20:53
07/17/16 20:53
|
Joined: Jun 2016
Posts: 33 NC
Cstyle
OP
Newbie
|
OP
Newbie
Joined: Jun 2016
Posts: 33
NC
|
Your single asset TMF makes some sense to me, but I do not understand the purpose of your multi asset version. Delete it and start over. Think about what parameters you want to change - then program just that, preferably without "while", "loop", "asset", or similar stuff, and without overwriting other trades. My strategy is dependent on the average entry price of all of the trades for a single asset. How do I get that without a loop? Thanks!
|
|
|
Re: Trading multiple assets
[Re: Cstyle]
#460865
07/18/16 07:27
07/18/16 07:27
|
Joined: Apr 2014
Posts: 482 Sydney, Australia
boatman
Senior Member
|
Senior Member
Joined: Apr 2014
Posts: 482
Sydney, Australia
|
Rather than a while(asset(loop( ....))), try for(open_trades) or for(current_trades). There are examples of each in the manual, but post here if you get stuck!
Last edited by boatman; 07/18/16 07:30.
|
|
|
Re: Trading multiple assets
[Re: boatman]
#460895
07/20/16 00:35
07/20/16 00:35
|
Joined: Jun 2016
Posts: 33 NC
Cstyle
OP
Newbie
|
OP
Newbie
Joined: Jun 2016
Posts: 33
NC
|
I have it working perfectly now. Thanks everyone for your help!
// Penny Picking strategy ///////////////////
#include <profile.c>
int TrailWhenProfitable()
{
var AverageOpen = 0;
int Count = 0;
if(NumOpenLong > 0 )
{
var AverageOpen = 0;
int Count = 0;
for(current_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
if (priceClose() > AverageOpen + (2 * ATR(100)) && TradeStopLimit <= 0)
{
TradeStopLimit = priceClose() - 2 * ATR(100);
TradeTrailLimit = priceClose() - ATR(100);
printf(" TradeStopLimit = %.3f, TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
printf(" ATR = %.3f, priceClose = %.3f", ATR(100), priceClose());
printf(" AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
}
if (TradeStopLimit > 0 && ltod(ET) > 1230)
{
exitLong();
printf(" ltod = %f", (var) ltod(ET));
}
}
return 0;
}
function run()
{
StartDate = 2009;
EndDate = 2016;
LookBack = 500;
set(LOGFILE); // log all trades
set(TICKS); // normally needed for TMF
Trail = 0;
Stop = 0;
while(asset(loop("UK100","GER30","NAS100")))
{
vars Price = series(price());
vars Trend = series(LowPass(Price,500));
if(valley(Trend) && NumOpenLong < 4)
{
var AverageOpen = 0;
var AverageProfit = 0;
int Count = 0;
for(current_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
if(AverageOpen > 0)
AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
switch (Count)
{
case 0:
enterLong(TrailWhenProfitable);
break;
case 1:
if (AverageProfit < -0.10)
{
enterLong(TrailWhenProfitable);
printf(" enter 2nd trade ");
printf(" AverageOpen %.3f ", AverageOpen);
}
break;
case 2:
if (AverageProfit < -0.15)
{
enterLong(TrailWhenProfitable);
printf(" enter 3rd trade ");
}
break;
case 3:
if (AverageProfit < -0.20)
{
enterLong(TrailWhenProfitable);
printf(" enter 4th trade ");
}
break;
default:
printf("None of them! ");
} // switch count
} // if trending
} // while asset loop
} // run
|
|
|
|