PowerLanguage for Backtesting Multiple Signals

Questions about MultiCharts and user contributed studies.
jt404
Posts: 5
Joined: 27 Jul 2017
Has thanked: 2 times

PowerLanguage for Backtesting Multiple Signals

Postby jt404 » 27 Jul 2017

I am trying to write the code for a trading strategy using moving averages (as well as bollinger bands and stochastic lines) in order to perform backtesting and am struggling with one specific aspect of PowerLanguage. I have scoured the discussion forums for guidance in regards to my specific issue with no luck.

To make sure my questions are clear, I will focus on the 3 moving averages.

1. How do I buy a certain value of stock (say, if I wanted to treat "one unit" as $10,000)?
-I have only been able to find ways to buy a certain number of shares/contracts. Should I consider making a variable? What would that look like?

2. Perhaps more importantly, how do I make sure my buy and sell commands consider the overall position of the portfolio at the time of the trade? For example, how to make the overall position 3 units long, and not just say to buy 3 units?
-You will see that in my current code, the command in the example I just mentioned is to just buy 3 instead of making sure the portfolio shifts to being 3 long overall.


Here is my code for my moving average crossing signal (each bar represents a day):

Code: Select all

inputs: Price( Close ), FastLength( 51 ), MedLength( 105 ), SlowLength( 161 ) ;
variables: Value1 = 14000 dollars, Value3 = 42000 dollars ;
variables: var0(0), var1(0), var2(0) ;

var0 = AverageFC( Price, FastLength ) ;
var1 = AverageFC( Price, MedLength ) ;
var2 = AverageFC( Price, SlowLength ) ;

If var0 Crosses Over var1 and var1 > var2 Then Buy ("buy3") Value3 Next Bar at Open ;
If var0 Crosses Over var2 and var1 > var2 Then Buy ("buy") Value1 Next Bar at Open ;
If var0 Crosses Over var1 and var2 > var0 Then Sell Short ("sell") Value1 Next Bar at Open ;

Thank you in advance for your help!
Last edited by jt404 on 31 Jul 2017, edited 1 time in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby TJ » 27 Jul 2017

See post #1 & #2
viewtopic.php?t=11713

jt404
Posts: 5
Joined: 27 Jul 2017
Has thanked: 2 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby jt404 » 31 Jul 2017

Code is now in a code box.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby TJ » 31 Jul 2017


User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby TJ » 31 Jul 2017

...

1. How do I buy a certain value of stock (say, if I wanted to treat "one unit" as $10,000)?
-I have only been able to find ways to buy a certain number of shares/contracts. Should I consider making a variable? What would that look like?

...

Thank you in advance for your help!

You can try this:

Code: Select all


input:
Unit.value ( 10000 ) ;

var:
shares.to.trade ( 0 ) ;

shares.to.trade = unit.value / close ;
This is the framework. You can adjust the formula according to your instrument or trade size.
You also need to add a routine to round down to the nearest lot size.

jt404
Posts: 5
Joined: 27 Jul 2017
Has thanked: 2 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby jt404 » 01 Aug 2017

Code: Select all

inputs: Price( Close ), FastLength( 51 ), MedLength( 105 ), SlowLength( 161 ), Unit.value( 10000 ) ;
variables: var0(0), var1(0), var2(0), shares.to.trade( 0 ), shares.to.trade3( 0 ) ;

var0 = AverageFC( Price, FastLength ) ;
var1 = AverageFC( Price, MedLength ) ;
var2 = AverageFC( Price, SlowLength ) ;
shares.to.trade = round(Unit.value / close) ;
shares.to.trade3 = round(3*(Unit.value / close)) ;

If var0 Crosses Over var2 and var1 > var2 Then Buy ("buy") shares.to.trade Next Bar at Open ;
If var0 Crosses Over var1 and var1 > var2 Then Buy("buy3") shares.to.trade3 Next Bar at Open ;
If var0 Crosses Over var1 and var2 > var0 Then Sell Short ("sell") shares.to.trade Next Bar at Open ;
One of the errors I am getting is for the command to buy shares.to.trade (with the cursor selecting the "s" on "shares.to.trade"), which says, "'Next Bar' can only be applied to 'Open', 'Date', 'Time', 'DateTime', or 'Time_s'" Is it clear why this might be?

In your last post, by "lot size", do you mean the closest value to the value amount of the order for each trade (in my case, $10,000)?

I am still not sure how to resolve my second question about how to make sure my buy and sell commands consider the overall position of the portfolio at the time of each trade.

Thanks again for all your help so far.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby TJ » 04 Aug 2017


One of the errors I am getting is for the command to buy shares.to.trade (with the cursor selecting the "s" on "shares.to.trade"), which says, "'Next Bar' can only be applied to 'Open', 'Date', 'Time', 'DateTime', or 'Time_s'" Is it clear why this might be?
...

Thanks again for all your help so far.

You should change

Next Bar at Open

to

Next Bar at Market

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby TJ » 04 Aug 2017

...

In your last post, by "lot size", do you mean the closest value to the value amount of the order for each trade (in my case, $10,000)?

...
Thanks again for all your help so far.

If you trade stocks, most of the lots are 100 shares.

if you base your criteria on capital, $10,000 can buy you stocks in odd lot sizes.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby TJ » 04 Aug 2017

...

2. Perhaps more importantly, how do I make sure my buy and sell commands consider the overall position of the portfolio at the time of the trade? For example, how to make the overall position 3 units long, and not just say to buy 3 units?
-You will see that in my current code, the command in the example I just mentioned is to just buy 3 instead of making sure the portfolio shifts to being 3 long overall.
...
Thank you in advance for your help!
You can check your position at the broker.

Go to the Wiki,
Look under PowerLanguage Keyword Reference
get yourself acquainted with the following sections:
► Strategy Events‎ (8 P)
► Strategy Orders‎ (34 P)
► Strategy Performance‎ (33 P)
► Strategy Position‎ (47 P)
► Strategy Position Synchronization‎ (2 P)
► Strategy Position Trades‎ (29 P)
► Strategy Properties‎ (5 P)

jt404
Posts: 5
Joined: 27 Jul 2017
Has thanked: 2 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby jt404 » 17 Aug 2017

Okay, so it seems like the closest thing to what I'm looking for would be the following:
ChangeMarketPosition(#, price, name)

or I could combine an indicator with the other conditions I have already laid out.
Possible indicators:
"i_MarketPosition_at_Broker_for_The_Strategy"
"i_MarketPosition"
"-_MarketPosition_at_Broker"

Code: Select all

i_MarketPosition_at_Broker_for_The_Strategy(#, price, name)
//returns a positive value if position is long, negative if short
If var0 Crosses Over var2 and var1 > var2 and i_MarketPosition_at_Broker = 1 Then Buy ("buy") shares.to.trade Next Bar at Market ;
^This would mean if the first 2 conditions are met, and a 3rd condition is met that considers what the position is at the broker at the time of the trade, then by 1 unit.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: PowerLanguage for Backtesting Multiple Signals

Postby sptrader » 17 Aug 2017

Just a reminder :
"
i_MarketPosition
Notes
This function can only be used in studies (indicators). Use the regular MarketPosition in signals."

jt404
Posts: 5
Joined: 27 Jul 2017
Has thanked: 2 times

Re: PowerLanguage for Backtesting Multiple Signals

Postby jt404 » 18 Aug 2017

Thanks, sptrader. Here's the corrected version of my last post:


Okay, so it seems like the closest thing to what I'm looking for would be the following:
ChangeMarketPosition(#, price, name)

or I could combine a signal with the other conditions I have already laid out, such as:
"MarketPosition"
//This would provide me with information on the most recently closed position, therefore considering (to a degree) how many units long I am with the broker.

I also think I like that one better than "MarketPosition_at_Broker_for_The_Strategy" because I do not want to return the number of contracts; I want something showing whether I am currently long or short.

Code: Select all

MarketPosition
//returns a positive value if position is long, negative if short
If var0 Crosses Over var2 and var1 > var2 and MarketPosition = 1 Then Buy ("buy") shares.to.trade Next Bar at Market ;


Return to “MultiCharts”