Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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):
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:
Can you help answer these questions from other members on NexusFi?
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.
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.
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.
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.
Does the following code adapt your rounding example correctly?
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.
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?