Sharing arrays between two symbols

Hello,

I have a requirement to implement system level stop loss based on P/L of two symbols combined. i.e. if (P1 +P2) < SL (Profit/Loss of 1rst & 2nd Symbols repectively) , I need to place a sell order for both symbols. P1 & P2 will be calculated based on Buyprice & CurrentPrice . StaticVarSet will be set from both symbols for their P1/P2 variables & StaticVarGet will be getting the other variable P2/P1 to calculate the overall system's Profit/Loss & then generate a sell signal accordingly. However running two symbols sequentially on same thread (or maybe even in parallel on multiple threads) won't solve the purpose.

Amibroker is such a powerful piece of software, I am sure there must be some solution/wayout to this problem as well that I am unaware of.

Thanks in advance !!

Regards,
Rahul Jain

Why do you need to share Arrays ?

Can't you just reference the "Other" symbol using Foreign() functions and compute on it ?
This way whichever part runs in any sequence will access Latest state info of the other symbol.

Kindly clarify:

However running two symbols sequentially on same thread (or maybe even in parallel on multiple threads) won't solve the purpose.

Extrapolate the Problem in more detail.

You've just used 3-4 sentences, windows itself is not a RTOS and there will always be some code from another Thread that maybe locked out of the Critical section.
Two threads can't both update the same variable and exchange two different values.

and then just to add, that AB developer has come across this before, there are implementations like
https://www.amibroker.com/guide/afl/threadsleep.html
and the caveat too:
Please do NOT abuse this function. Using it may negatively impact performance. The function is provided for advanced users to implement inter-thread synchronization.

and some more links for multithread stuff
https://www.amibroker.com/guide/afl/staticvarcompareexchange.html

https://www.amibroker.com/guide/h_multithreading.html

This seems like a problem best solved by writing your own CBT. On each bar, simply retrieve the P/L for each symbol, and if your stop threshold is exceeded then exit the positions.

Hi @travick

I need the PnL of current ongoing trade (at that same bar) of "Other" symbol. Foreign would only help if static data (OHLC) of "Other" symbol needs to be retrieved. Maybe I just muddled up a bit by introducing "multi thread" term. The problem is same for multi thread & single thread.

Hi @mradtke,

Yes back-testing can be achieved via CBT using a 2 step approach

  1. dumping out the trade list without applying Stop loss.
  2. Applying the stop loss on the trade list by taking prices into consideration & calculating combined PnL of 2 symbols.

But I was wondering , how to implement it for live trading as I am using Amibroker to push Buy/Sell Signals to the Broker terminal.

Thanks for all your help @mradtke , @travick.

No, you don't need a two-step approach to implement a portfolio stop in the CBT. A mid-level or low-level CBT includes a bar-by-bar loop that can be used to evaluate the current state of your portfolio and enter or exit trades as needed. If you can get it working for a back test, then you can probably figure out how to apply it to your live trading as well.

Whoa, didn't read anywhere that is what a BT. Seemed to me as if it were a live system.