Derive buy/sell prices from a different stock then the indicator

I have written a code in which the buy/sell signals are calculated based on the prices of VXX.

When a buy/sell signal is given, I don’t want to buy VXX but buy another stock (SVXY or SPY).
I can get the prices of both stocks in EXPLORE but not in the BACKTEST …
I have tried to use the Foreign function, but it did not help.

SetTradeDelays (1, 1, 1, 1);

BuyPrice = O;
SellPrice = O;
ShortPrice = Foreign("SVXY", "O");
CoverPrice = Foreign("SVXY", "O");

//Trading Code
SMA = MA (C, 20);
BolliTop = BBandTop (C, 20, 2);
BolliBot = BBandBot (C, 20, 2);

Buy = Cross (C, BBandTop (C, 20, 2));
Sell = C > O;
Short = C < MA (C,20) AND C > Ref (C, -1);
Cover = Cross (C, MA (C, 20));
ApplyStop (stopTypeNBar, stopModeBars, Optimize( "max. days",9,3,9,1));

In your post you only talk about Buy/Sell but your code has Short/Cover also.

So I only post in regards to upper quoted sentences.

// code applied on SVXY or SPY
/// @link https://forum.amibroker.com/t/derive-buy-sell-prices-from-a-different-stock-then-the-indicator/8565/2
SetTradeDelays (1, 1, 1, 1);

SetForeign( "VXX" );
VXX_BuyCond = Cross (C, BBandTop (C, 20, 2));
VXX_SellCond = C > O;
RestorePriceArrays();

BuyPrice = O;
SellPrice = O;

// Buy/sell SVXY or SPY based on signals of VXX
Buy = VXX_BuyCond;
Sell = VXX_SellCond;

ApplyStop (stopTypeNBar, stopModeBars, Optimize( "max. days",9,3,9,1));

Or

// code applied on SVXY or SPY
/// @link https://forum.amibroker.com/t/derive-buy-sell-prices-from-a-different-stock-then-the-indicator/8565/2
SetTradeDelays (1, 1, 1, 1);

SetForeign( "VXX" );
VXX_BuyCond = Cross (C, BBandTop (C, 20, 2));
VXX_SellCond = C > O;
RestorePriceArrays();

BuyPrice = O;
SellPrice = O;

// Buy/sell SVXY or SPY based on signals of VXX
Buy = C < MA (C,20) AND C > Ref (C, -1) AND VXX_BuyCond;
Sell = Cross (C, MA (C, 20)) AND VXX_SellCond;

ApplyStop (stopTypeNBar, stopModeBars, Optimize( "max. days",9,3,9,1));

etc.

3 Likes

Hi fxshrat,

Thanks for your input, but I still have not reached the code I wanted originally.
The system works like this: All buy/sell/short/cover signals are derived from the price action of VXX.

When a buy signal is generated I want the system to buy VXX at the open.
When a sell signal is generated I want the system to sell VXX at the open.

When a short signal is generated I want the system to buy SVXY at the open
When a cover signal is generated I want the system to sell SVXY at the open.

Below you can find the system code as I have it now (only applicable on VXX):

//Initial Parameters
SetOption( "InitialEquity", 50000);
SetPositionSize(100, spsPercentOfEquity);
SetOption("CommissionMode", 1); //  percent 
SetOption("CommissionAmount", 0.1); //   
SetOption ("AllowSamebarExit", 0);
SetTradeDelays (1, 1, 1, 1);

BuyPrice = Open;
SellPrice = Open; 
ShortPrice = Open;
CoverPrice = Open;

//Trading Code
Buy = Cross (C, BBandTop (C, 20, 2));
Sell = C > O;
Short = C < MA (C,20) AND C > Ref (C, -1);
Cover = Cross (C, MA (C, 20));
ApplyStop (stopTypeNBar, stopModeBars, Optimize( "max. days",4,3,9,1));

MinHoldBars = 2;

Buy = Buy AND Status("barinrange");

BarsInTrade = 0;

for( i = 0; i < BarCount; i++ )
{

  // if in-trade, then increase bar counter

  if( BarsInTrade > 0 ) BarsInTrade ++;

  else

  if( Buy[ i ] ) BarsInTrade = 1; // on buy signal start counting bars-in-trade

 

  // if we are in trade - remove sells occurring too soon

  if( BarsInTrade < MinHoldBars ) Sell[ i ] = 0;

  else

  if( Sell[ i ] ) BarsInTrade = 0; // on sell reset barsintrade flag

}

printf("barsintrade:\n");

WriteVal(barsintrade);

Ok, I finally managed to find the solution on my own thanks to the references to the Foreign () and Name() functions. For those interested, in the code below you can find the solution to my original problem stated in this thread:

//Initial Parameters
SetOption( "InitialEquity", 10000);
SetPositionSize(100, spsPercentOfEquity);
SetOption("CommissionMode", 2); //  usd amount 
SetOption("CommissionAmount", 1); //   
SetOption ("AllowSamebarExit", 0);
SetTradeDelays (1, 1, 1, 1);


BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

if (Name() == "VXX")
{ 
Buy = Cross (C, BBandTop (C, 20, 2));
Sell = C > O;

ApplyStop (stopTypeNBar, stopModeBars, Optimize( "max. days",4,3,9,1));

MinHoldBars = 2;
}

if (Name() == "SVXY")
{ 
SetForeign ("VXX");

Buy1 = C < MA (C,20) AND C > Ref (C, -1);
Sell1 = Cross (C, MA (C, 20));
ApplyStop (stopTypeNBar, stopModeBars, Optimize( "max. days",4,3,9,1));

MinHoldBars = 2;

RestorePriceArrays();
Buy = Buy1;
Sell = Sell1;
}
1 Like

Good job by myself that I did not respond on this anymore.

Ask yourself why no one has responded to you anymore for weeks.
The reason is that your posts have been big time wasters.

Why?
Here is why...

You said this one in your first post (Please read loudly and clearly to yourself):

Pay very special attention to

don't

So since you didn't want to buy VXX I have made two examples which exactly do that!

Then all of the sudden you say completely contradicting words

If you are a comedian then that has been worst comedy ever so far in this forum.
(As aside your most recent code did not even have any short signals anymore)

Hopefully your next thread will be better (everyone deserves second chance)...

BTW, if the world would be a cartoon (and after reading your post of 12 days ago)...

1 Like

@ fxshrat: thanks for your input anyway.
I realize it might have been a stupid question for you, but for me as a beginner it took a while to get it done. I tried it with your proposed solution, but it did not get me to the result I needed. It was only after I started using the Name() functions I got to the solution.