Back

Having trouble with instant liquidation. 10000 orders immediately

Here is my code (granted, this is a basic template of the code, which is why it wont make sense logically but don't worry about that). I'm looking to find a way to make the algorithm recognize to stop placing orders a billion times.

 

 

 

namespace QuantConnect
{   
    /*
    *   OANDA/QuantConnect Basic Template:
    *    Fundamentals to using a QuantConnect algorithm.
    *
    *    You can view the QCAlgorithm base class on Github:
    *    https://github.com/QuantConnect/Lean/tree/master/Algorithm
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            // Date range for your backtest
            // In live trading these are ignored.
            SetStartDate(2016, 1, 1);
            SetEndDate(2016, 1, 7);
            
            // Set cash allocation for backtest
            // In live trading this is ignored and your real account is used.
            SetCash(50000);
            
            // Specify the OANDA Brokerage: This gives lets us know the fee models & data.
            SetBrokerageModel(BrokerageName.OandaBrokerage);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Second, true);
              
            
            
        }

        // Event handler for the price events
        public override void OnData(Slice data)
        {
            int holdings = 0;
            if (!data.ContainsKey("EURUSD")) return;
        
        
            var spread2 = (data["EURUSD"].Ask.Close-data["EURUSD"].Bid.Close)*10000;
        
        
            
            var unitval2 = Portfolio.Cash*2;
        
            unitval2 = Decimal.ToInt32(unitval2);
        
            
            
            
            
            
            
            if (Portfolio.HoldStock == false && spread2<2 && holdings == 0)
            {
                holdings = 1;
                Random rand = new Random();
            int random = rand.Next(0,101);
                
                if(random<50)
                {
                    
                    Sell("EURUSD", unitval2);
                
                    
                }
                else if(random>=50)
                {
                
                    Buy("EURUSD", unitval2);
                
                }
                
                
               
            
            
        
            }
        while (holdings == 1)
        {
            
            if(Portfolio.TotalProfit>=Portfolio.Cash*(2/1000))
                {
                    
                    Liquidate("EURUSD");
                
                    holdings = 0;
                }
                
                else if(Portfolio.TotalProfit<=(44/10)*Portfolio.Cash*(2/1000))
                {
                
                    Liquidate("EURUSD");
                
                    holdings = 0;
                }
                else return;
        
        
        
        }
    }
}
}

Update Backtest







Hi Garret, you can use "if (Portfolio.Invested)" or "if (!Portfolio.Invested)" to decide if there are any assets invested in the portfolio instead of setting the variables like "holdings". 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thanks Jing Wu for the response, unfortunately, I was having the same problem when using that method, which is why I began experimenting with the variable "holdings". :(

0

The number of orders depends on the algorithm logic. In the algorithm above, you choose the second resolution, once the condition in "if else" statement is satisfied each second, Buy() and Sell() will place the market order immediately. "Liquidate()" will liquidate all holdings in your portfolio then "Portfolio.Invested" will be False after the liquidation.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thank you for the response again Jing Wu, however, I am confused. How am I supposed to make calculations using the second data and then hold the trade until a condition is true, before liquidation? So in the above code, I want to make calculations off of that second data, and then once the data meets the condition spread2<2, (which should happen very quickly), I want it to place the trade. I want it to then hold the trade until it is greater than or equal to my balance*2/1000 OR less than or equal to 4.4*myblance*2/1000. Is this not possible with the second data?

 

 

 

THanks

0

Happy to help but can you clarify your question Garret?

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This is just a pilot experiment to get the algorithm to place a trade and hold it until a condition is met. I want it to place the trade if the spread condition is met. I want it to hold that position until one of the two exit conditions is met. However, my code just executes a billion trades instead of holding the single trade until one of the exit conditions is met.

 

 

Thanks

0

Hi Garret, you got many orders executed because those liquidation conditions are met. In C#, "2/1000" is zero, you probably want to add a decimal keyword m like "2/1000m" to get 0.002. Other than that, the condition "if(Portfolio.TotalProfit<=(44/10m)*Portfolio.Cash*(2/1000m))," is met as Portfolio.TotalProfit is negative most of the time. You can add the Debug() message to log the conditions so you can check if it is reasonable to execute the liquidation order.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Look into the `SetHoldings` function. It has options to liquidate a portfolio before buying new holdings. It's also smart and minimizes transactions. 

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed