NexusFi: Find Your Edge


Home Menu

 





Overall Position in EasyLanguage/PowerLanguage


Discussion in EasyLanguage Programming

Updated
    1. trending_up 5,092 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 6 posts
    2. attach_file 0 attachments




 
Search this Thread

Overall Position in EasyLanguage/PowerLanguage

  #1 (permalink)
jtc40
Illinois
 
Posts: 3 since Aug 2017
Thanks Given: 2
Thanks Received: 0

I am new to PowerLanguage and am trying to write the code for a trading strategy using simple moving averages in order to perform backtesting. I am struggling with one specific aspect of coding and have scoured the discussion forums for guidance in regards to the issue to no avail.

1. How do I buy a certain value of stock (say, if I wanted to treat "one unit" as $10,000)?
Right now, I'm using what's in bold below-

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

 
Code
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 Market ;
If var0 Crosses Over var1 and var1 > var2 Then Buy ("buy3") shares.to.trade3 Next Bar at Market ; 
If var0 Crosses Over var1 and var2 > var0 Then Sell Short ("sell") shares.to.trade Next Bar at Market ;

2. 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? I never want to be longer than 3 units and never shorter than 1 unit.

I have found a MarketPosition_at_Broker command, which would give me something like:

 
Code
MarketPosition_at_Broker
//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 ;

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
MC PL editor upgrade
MultiCharts
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
21 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,434 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627


Hi jtc40,

1. Your approach should work, however the "round" reserved words requires two inputs, you need to add the number
of decimal places you want to round to.
 
Code
Round( 1000.51, 1 ) ;
Apart from using round you can also take a look at the reserved word "Floor" as this will always give you the greatest integer value lower than the input value - which from a risk perspective might make more sense in trading as this way you can never exceed your unit.value in number of shares (which can happen when using round).

2. I am not sure I understand your question correctly, are you working on a chart or in the Portfolio Trader? Do you intend to have the strategy take up to three long entries in the same direction on this chart or on the symbol or over the entire portfolio, but not more?

MarketPosition_at_Broker will only give you the current market position at the broker, which would be the same during a backtest.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #4 (permalink)
jtc40
Illinois
 
Posts: 3 since Aug 2017
Thanks Given: 2
Thanks Received: 0

Thanks, ABCTG.

1. Will rounding make me buy a value of $10,000? Or can I somehow make it buy the closest number of shares to $10,000?

My issue with that is that I don't want to buy 10,000 shares (contracts), I want to buy the closest value to $10,000, which can be a different number of shares depending on the stock price.

 
Code
shares.to.trade = round(Unit.value / close, 1 ) ;
shares.to.trade3 = round(3*(Unit.value / close, 1 )) ;
//^is that the same as 3 units of $10,000 each?


2. I am working on Multicharts in order to backtest on symbols.

I never want to be more than 3 long on any given symbol (sorry, I should have made that clearer).

So MarketPosition_at_Broker will not work for backtesting as it will be the same every time.

Reply With Quote
  #5 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,434 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

jtc40,

1. Your code approach already converts the currency amount into a size and this is what you have to do in my opinion, too as you can't simply use the currency amount directly. You will need to adapt my rounding example, though, as this rounds to one decimal place.

2. I am afraid this could still mean chart or Portfolio Trader. If we are talking about a chart, you could only check that particular chart and not several charts using the same symbol (at least not that easily). You can take a look at the Position Trades related reserved words in the MC wiki to count your open positions on a chart: https://www.multicharts.com/trading-software/index.php/Category:Strategy_Position_Trades. CurrentEntries might also give you the result you are looking for.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #6 (permalink)
jtc40
Illinois
 
Posts: 3 since Aug 2017
Thanks Given: 2
Thanks Received: 0

Thanks, ABCTG.

Does the following code adapt your rounding example correctly?

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

If var0 Crosses Over var2 and var1 > var2 Then Buy ("buy") shares.to.trade Next Bar at Market ;
If var0 Crosses Over var1 and var1 > var2 Then Buy ("buy3") shares.to.trade3 Next Bar at Market ; 
If var0 Crosses Over var1 and var2 > var0 Then Sell Short ("sell") shares.to.trade Next Bar at Market ;

Still getting an error when I try to compile the code in the PowerLanguage Editor.

The error appears at the beginning of the word "shares.to.trade3" and says, "'Next Bar' can only be applied to 'Open', 'Date', 'Time', 'DateTime' or 'Time_s'."

I have read the PowerLanguage Keyword Reference and tried to move the "shares.to.trade" to different spots within the code, but I still get the same error.

Reply With Quote
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,434 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

jtc40,

in my opinion it does not, but I am not sure what your intentions are. To how many decimal spaces are you rounding the size and why are you using this number?

Your error message doesn't point to the exact error unfortunately, but you can take a look at this link to find out what your order command is missing: https://www.multicharts.com/trading-software/index.php/Buy

Regards,

ABCTG


jtc40 View Post
Thanks, ABCTG.

Does the following code adapt your rounding example correctly?

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

If var0 Crosses Over var2 and var1 > var2 Then Buy ("buy") shares.to.trade Next Bar at Market ;
If var0 Crosses Over var1 and var1 > var2 Then Buy ("buy3") shares.to.trade3 Next Bar at Market ; 
If var0 Crosses Over var1 and var2 > var0 Then Sell Short ("sell") shares.to.trade Next Bar at Market ;

Still getting an error when I try to compile the code in the PowerLanguage Editor.

The error appears at the beginning of the word "shares.to.trade3" and says, "'Next Bar' can only be applied to 'Open', 'Date', 'Time', 'DateTime' or 'Time_s'."

I have read the PowerLanguage Keyword Reference and tried to move the "shares.to.trade" to different spots within the code, but I still get the same error.


Follow me on Twitter Reply With Quote




Last Updated on September 1, 2017


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts