NexusFi: Find Your Edge


Home Menu

 





help with multiple position strategy


Discussion in NinjaTrader

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




 
Search this Thread

help with multiple position strategy

  #1 (permalink)
maggtrading
quintana roo, méxico
 
Posts: 84 since Mar 2013
Thanks Given: 222
Thanks Received: 40

hello everyone,



i have been trying to code some simple strategies in nt and so far have not had much problem if i use just one single position. if i try to have multiple positions in the same direction, i am having trouble with the code to manage those positions.


i have emailed nt's ninjascript support staff and for the most part they know their stuff and are quite helpful. some days ago one of these ninjascript support people helped me put an idea into code, but it is still incomplete. i would greatly appreciate if more advanced programmers could tell me how should i structure a strategy so that it did the following:



as long as uptrend condition is true do the following, as soon as uptrend condition is no longer true, exit all long positions. [how could i bracket or condition all this code so that all long positions and their respective stops are closed as soon as the uptrend condition is no longer true?]

as soon as uptrend condition becomes true, open one long position. [the entries would look like these and are ok, that's not where i'm having trouble]

 
Code
if  uptrend = true
{
EnterLong("first entry");
}

if uptrend = true and given number of bars later and no new lower lows
{
EnterLong("second entry");
}
if uptrend = true and given number of bars later and no new lower lows
{
EnterLong("third entry");
}

[the position management section the people at nt have helped me with is what is incomplete]



 
Code
.....
        private IOrder firstEntry = null;
        private IOrder secondEntry = null;
.....
 
    if(Close[0] > Open[0] && Position.MarketPosition == MarketPosition.Flat && firstEntry == null)
            {
                firstEntry = EnterLong("first entry");
                SetStopLoss("first entry", CalculationMode.Ticks, 10, false);
            }
            if(Close[0] > Open[0] && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1 && secondEntry == null)
            {
                secondEntry = EnterLong("second entry");//This is the entry order for our second position. Our Stop will be submitted in the OnExecution() method
                SetStopLoss("second entry", CalculationMode.Ticks, 10, false);
            }
            if(positionOnePrice != 0 && Close[0] - positionOnePrice > 10 * TickSize)//This checks that the first entry has been executed and then compares if the difference from the current Close and our Entry price for Position One is greater than 10 ticks
                SetStopLoss("first entry", CalculationMode.Ticks, 0, false); // This moves our stop to 0
            if(positionTwoPrice != 0 && Close[0] - positionTwoPrice > 10 * TickSize)//This checks that the first entry has been executed and then compares if the difference from the current Close and our Entry price for Position One is greater than 10 ticks
                SetStopLoss("second entry", CalculationMode.Ticks, 0, false); // This moves our stop to 0
            if(positionOnePrice != 0 && Close[0] - positionOnePrice > 20 *TickSize)
                SetStopLoss("first entry" , CalculationMode.Ticks, -10, false);// This will move our stop to be 10 ticks behind the current price which would be +10 above our initial entry
            if(positionTwoPrice != 0 && Close[0] - positionTwoPrice > 20 *TickSize)
                SetStopLoss("second entry" , CalculationMode.Ticks, -10, false);// This will move our stop to be 10 ticks behind the current price which would be +10 above our initial entry
            
            
        }
        protected override void OnExecution(IExecution execution)
        {
            if(firstEntry != null && firstEntry == execution.Order)
                positionOnePrice = execution.Price;
            
            if(secondEntry != null && secondEntry == execution.Order)
                positionTwoPrice = execution.Price;
                
        }
.....

the idea is for the strategy to set a stoploss order for each position and if price moves in my favor, move each stop first to break even and then to lock profit. it seems like it would be necessary to use iorder objects and onexecution() methods, so the strategy knows how many positions it has opened and at what price each. the guys at nt helped me with the above code, but if i try to test it on a chart, it will only take a long position once (my guess is that all variables need to be reset to their starting values after the positions are closed because the uptrend condition stopped being true or a stop was hit. i just don't know how to do that).


so, i have a position management system that causes the strategy to open positions only the first time the uptrend condition is true in a chart, but i would like the strategy to open those positions every time the condition is true, shut everything down if uptrend stops being true, otherwise manage the positions and then reset everything after the positions were closed.


i would greatly appreciate it if anyone can help me complete this code so that it is more functional. thanks in advance.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Deepmoney LLM
Elite Quantitative GenAI/LLM
ZombieSqueeze
Platforms and Indicators
Are there any eval firms that allow you to sink to your …
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 




Last Updated on June 23, 2016


© 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