Equity Curve Trading through Paper Account

Questions about MultiCharts and user contributed studies.
quantarb
Posts: 51
Joined: 14 Apr 2012
Has thanked: 9 times
Been thanked: 33 times

Equity Curve Trading through Paper Account

Postby quantarb » 20 Dec 2012

Is it is possible to trade your trading strategy’s equity curve by using multiple Interactive Broker accounts. I’m thinking trading the strategy in a simulated account and then trading the equity curve in the real account. Here is what I’m thinking so far.

Signal #1
Send orders to IB paper account which is essentially paper trading the strategy.

Signal #2
Sends orders to real account based on performance in simulated account

I’m not trying to do the traditional turn on your strategy when your equity curve is above some moving average and then turn off your strategy when your equity curve is below some moving average.

I want to copy the exact positions in my paper portfolio when I “buy” it. For example, my simulated portfolio might have
Long 1000 Shares of MSFT at 33.00
Long 1000 Shares of INTC at 28.00
Short 1000 Shares of QQ at 60.00

Right now the paper account has an unrealized loss of $20000. I want Signal #2 to buy 1000 Shares of MSFT and INTC and Short 1000 Shares of QQ in my real account.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Equity Curve Trading through Paper Account

Postby Andrew MultiCharts » 21 Dec 2012

Hello quantarb,

You need to understand 2 key points regarding this situation:
1. If you want to trade both on real and demo accounts, you will require at least 2 chart windows set up for trading and connected to each account.
2. If you want the strategy on the chart connected to real account to place orders based on some trading decisions made on the chart connected to demo account, the only way for you to pass any data between the scripts on different is to use global variables.

quantarb
Posts: 51
Joined: 14 Apr 2012
Has thanked: 9 times
Been thanked: 33 times

Re: Equity Curve Trading through Paper Account

Postby quantarb » 27 Dec 2012

Hi Andrew,

Thank you for your feedback. What syntax do I use to access the OpenPnl for an account? Here is the current pseudo code that I'm thinking about.



If Account#1OpenPnl > -3000 and Account#1MarketPosition = 1 then
begin
Buy Account1CurrentShares shares at AvgProc - 0.50 limit;
end;


If Account#1OpenPnl > - 3000 and Account#1MarketPosition = -1 then
begin
SellShort Account1CurrentShares shares at AvgProc + 0.50 limit;
end;

Use the same code for exit logic in your original trading signal

quantarb
Posts: 51
Joined: 14 Apr 2012
Has thanked: 9 times
Been thanked: 33 times

Re: Equity Curve Trading through Paper Account

Postby quantarb » 02 Jan 2013

Here is the code that I have come up with so far. I will need to get another IB account before I can test this code. If anyone with multiple IB accounts would like to test my code please let me know how it goes.

Code: Select all

Input: PaperAccountNum(""), NegativePnL(0);
Variables: PaperPnl(0), PaperQty(0),PaperAvgPrice(0), Qty(0);


PaperPnl = GetRTUnrealizedPL(PaperAccountNum);
PaperAvgPrice = GetPositionAveragePrice(GetSymbolName, PaperAccountNum);
PaperQty = GetPositionQuantity(GetSymbolName, PaperAccountNum);

Qty = absvalue(PaperQty - (CurrentShares * MarketPosition));

Condition1 = PaperPnl < NegativePnL and Qty > 0;

If PaperQty > 0 and Condition1 then
begin
Buy Qty shares next bar at PaperAvgPrice Limit;
end;

If PaperQty < 0 and Condition1 then
begin
SellShort Qty shares next bar at PaperAvgPrice Limit;
end;

//Enter same exit logic for the signal you're equity trading;





quantarb
Posts: 51
Joined: 14 Apr 2012
Has thanked: 9 times
Been thanked: 33 times

Re: Equity Curve Trading through Paper Account

Postby quantarb » 05 Jan 2013

I figured out that you can back test your equity trading curve strategy through portfolio backtester and global variables. You add the strategy you want to paper trade as Strategy #1 in the portfolio backtester and the equity trading curve trading strategy as Strategy #2.

For Strategy #1 you want to create an indicator to store the information you want to send to strategy 2. Below is some code to illustrate how to pass information between charts with the same symbol. I can pass the paper strategy's marketposition, avgentryprice, currentshares, and even a trading rule based on its open equity curve.

Code: Select all

Variables: SlowMA(0), FastMA(0), EntrySignal(0);
GVSetNamedInt(GetSymbolName+"MP", i_MarketPosition);
GVSetNamedInt(GetSymbolName+"AEP", i_AvgEntryPrice);
GVSetNamedInt(GetSymbolName+"CS", i_CurrentShares);
GVSetNamedDouble(GetSymbolName+"EntrySignal", EntrySignal);


FastMA = Average(i_OpenEquity, 20);
SlowMA = Average(i_OpenEquity, 100);

If FastMA > SlowMA Then EntrySignal = 1 Else EntrySignal = 0;
Strategy #2 is your equity curve trading system. I wrote a strategy that attempts to get a better entry price than the original strategy. The strategy below is general enough to work for strategies that scale in.

Code: Select all

[IntrabarOrderGeneration = true];
Inputs: PricePct(0);
Variables: BuyPrice(0), ShortPrice(0), Qty(0), pMarketPosition(0), pAvgEntryPrice(0), pCurrentShares(0);


pMarketPosition = GVGetNamedInt(GetSymbolName+"MP", MarketPosition); // gets the MarketPosition from the paper strategy
pAvgEntryPrice = GVGetNamedInt(GetSymbolName+"AEP", AvgEntryPrice); // gets the AvgEntryPrice from the paper strategy
pCurrentShares = GVGetNamedInt(GetSymbolName+"CS", CurrentShares); // gets the CurrentShares from the paper strategy
Qty = pCurrentShares - CurrentShares; // the shares difference between paper and real strategy

BuyPrice = pAvgEntryPrice * (100 - PricePct)/100;
ShortPrice = pAvgEntryPrice * (100 + PricePct)/100;

Condition1 = Qty >0 and pAvgEntryPrice > 0;

if pMarketPosition =1 and Condition1 then //if the paper strategy is long
begin
Buy ("Better LE")Qty shares next bar at BuyPrice limit;
end;

if pMarketPosition = -1 and Condition1 then // if the paper strategy is short
begin
SellShort ("Better SE") Qty shares next bar at ShortPrice limit;
end;

You still need to include the exit logic from the original strategy. I would recommend you code the exit logic from your original strategy as a separate signal and then apply it to strategy 2. This will allow both strategies to get out at the same time vs the equity trading curve strategy waiting for the paper strategy to be flat first and then getting out.

The downside is the portfolio performance report will be an aggregate of the paper strategy and equity curve trading system. You easily compare the two by looking at the individual symbol performance.

quantarb
Posts: 51
Joined: 14 Apr 2012
Has thanked: 9 times
Been thanked: 33 times

Re: Equity Curve Trading through Paper Account

Postby quantarb » 07 Jan 2013

Now I want to see if I can create an equity curve trading based on a strategy's portfolio equity curve. Often times my mean reverting strategies will be all long or all a particular basket of stocks that are highly correlated. I found the turning point is usually when the portfolio PnL of this basket reaches a negative threshold.

Unfortunately, Multicharts does not break down the performance metric of each individual strategy. You will have to aggregate the performance of all the symbols in your paper strategy manually.
You can request the information you want from each symbol by typing out their symbol names. However, I don’t find this solution very elegant or practical if you have a large instrument list or are changing your instrument list frequently. It will be very easy to make a mistake.

GVSetNamedInt(Symbol1+"MP", i_MarketPosition);
GVSetNamedInt(Symbol2+"MP", i_MarketPosition);
GVSetNamedInt(Symbol3+"MP", i_MarketPosition);

quantarb
Posts: 51
Joined: 14 Apr 2012
Has thanked: 9 times
Been thanked: 33 times

Re: Equity Curve Trading through Paper Account

Postby quantarb » 01 May 2013

I just wanted to share with you guys the progress I have made with equity curve trading. Opening two charts to equity curve trading is very computational inefficient and impractical if you are trading many symbols and strategies. My goal is to create an equity curve trading algorithm using only one chart. The code below is designed to work strictly for live auto trading. You still need two separate charts in the portfolio backtester to backtest your equity curve trading strategy.

You will need to set your auto trading settings to Asynchronous [AA] and Assume the Initial Market Position at the Broker is the SAME AS on the CHART when automation is enabled. Otherwise Multicharts will delete the previous and entry signals on your chart and assume you're flat.

The code below run your normal trading strategy for back testing. This allows you to monitor the strategy's performance and then create equity trading curve rules on the performance. The equity curve trading logic is only used when you turn on autotrading and it only applies to the current bar meaning your next live trade. You need to use MarketPosition_At_Broker when live auto trading because your strategy might be long or short the security while you're flat.

The bad news is Multicharts is still sending out trades even though Condition1's output is false. Multicharts engineers are currently looking into this issue.

Code: Select all

If GetAppInfo(aiStrategyAuto) = 1 and LastBarOnChart_s = true then
begin
//Condition1 = Equity Curve Trading Rule And Normal Entry Logic
//Use MarketPosition_At_Broker in Exit Logic

end else
begin
//Condition1 = Normal Entry Logic
//Use MarketPosition in Exit Logic
end;

houss75
Posts: 50
Joined: 09 Feb 2014
Been thanked: 1 time

Re: Equity Curve Trading through Paper Account

Postby houss75 » 22 Feb 2014

Hello Quantarb

I just want to find a code to backtest the equity curve of my system.
the goal is to trade the system when his EC is above the 20 moving average.

Do you have a code that can help me

Rgds

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Equity Curve Trading through Paper Account

Postby MAtricks » 28 Feb 2015

Quantarb,

I'm curious how this project was concluded.. any updates?


Return to “MultiCharts”