Click here to Login

                                                   How to define an alternate asset to buy when no positions are held?


Juan Parra
2014-02-24 22:43:56


I'm trying to do the following when backtesting a strategy:

- When no positions are held, buy an alternate asset (something like if positions=0 then buy asset b..., something to hold instead of cash).

Thanks a lot if you can help me with this!

2014-02-25 11:31:43



You can do that in the "OnEndPeriod" event of the money management script. Example:

double nbPositions = Portfolio.GetNumberOfPositions("long") + Portfolio.GetNumberOfPositions("short");
MMPosition pos = Portfolio.GetPosition("SPY");
if(nbPositions == 0)
if(pos == null)
Orders.AddLongPosition("SPY", 100, null);
if(pos != null)

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
How far are the close prices from the support line
Buy Falling Stocks Strategy
Sortino Ratio of an Asset
Asset Allocation: Equal Weight Rebalanced Monthly
Buy Indicator

How-to Lessons
How to dynamically change the number of positions in a portfolio?
How to speed up watchlist and screener plug-ins when working with...
How to download new quotes when selecting a chart
How to quickly add several positions to your portfolio
How to change volume bar colors when the price closes down

Related Forum Threads
how to add buy condition when the color change
How to force simulator to close open positions when data ends?
How are corporate action adjustments treated with Yahoo Finance
Backtest, if number of buy is more than open positions, how QS se...
How to create a watchlist of scrips that are in an intermediate u...

Blog Posts
How to get buy and sell orders for a portfolio based on a trading...
How to Backtest Each Stock or Asset Individually
Buy the best/top rated stocks or how to create powerful rank base...
How to Create Custom Metrics for Your Trading Positions
How to create buy and sell trading rules based on News Data

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2020
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.