Click here to Login





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

  1

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


Hi,

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!



QuantShare
2014-02-25 11:31:43

  0

Hi,

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);
}
}
else
{
if(pos != null)
{
pos.ClosePosition();
}
}




No more messages
0




Reply:

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"










QuantShare

Trading Items
How far are the close prices from the support line
Sortino Ratio of an Asset
Asset Allocation: Equal Weight Rebalanced Monthly
Buy Falling Stocks Strategy
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









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright 2019 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
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.