Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Array of trades

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Array of trades

    Hi.

    I know how to do it with doubles:

    Code:
    private double[] MyArray;
    protected override void Initialize()
    {
    	MyArray = new double[100];
    }
    
    //then I can access them using  MyArray[x]
    however, I want to store Orders, how to do that?

    Code:
    MyOrders["stop1"] = ExitLong(.....)
    MyOrders["stop2"] = ExitLong(.....)
    then I could access that later in Foreach, like:

    Code:
    foreach (string oName in MyOrders) {
      xyz= MyOrders[oName ].Quantity;  
      // etc..
    }
    Last edited by ttodua; 06-05-2017, 04:35 AM.

    #2
    Hello selnomeria,

    Thanks for opening the thread.

    You could use an array of Order objects, or you may wish to use a Dictionary if you would like to easily reference your collection of orders by name.

    You could reference the following syntax:

    Code:
    private Dictionary<string, Order>						myOrders;
    private Order[]											MyArrayOrders;
    Code:
    protected override void OnStateChange()
    {
    	...
    	else if (State == State.Configure)
    	{
    		myOrders = new Dictionary<string, Order>();
    		MyArrayOrders = new Order[10];
    	}
    Using a Dictionary:
    Code:
    protected override void OnBarUpdate()
    {
    	Order myTempOrder;
    	string myTempTag = "Tag";
    	myTempOrder = EnterLong(myTempTag);
    	
    	myOrders.Add(myTempTag, myTempOrder);
    	
    	foreach(KeyValuePair<string, Order> kvp in myOrders)
    	{
    		if( kvp.Key == "Tag" )
    			myTempOrder = kvp.Value;
    	}
    Using an Array:
    Code:
    protected override void OnBarUpdate()
    {
    			
    	MyArrayOrders[0] = EnterLong("MyTag");
    			
    	foreach( Order thisOrder in MyArrayOrders)
    	{
    		if( thisOrder.Name == "MyTag" )
    			Print(thisOrder.Name);
    	}
    Arrays and various types of Collections are extensively documented by Microsoft. You can reference their complete documentation for in depth explanations and examples below:

    Arrays: https://docs.microsoft.com/en-us/dot...e/arrays/index

    Dictionary's: https://docs.microsoft.com/en-us/dot...tframework-4.7

    Collections: https://docs.microsoft.com/en-us/dot...ts/collections

    Please let me know if I can be of further help.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by quantismo, 04-17-2024, 05:13 PM
    3 responses
    25 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by ScottWalsh, 04-16-2024, 04:29 PM
    7 responses
    34 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by cls71, Today, 04:45 AM
    0 responses
    5 views
    0 likes
    Last Post cls71
    by cls71
     
    Started by mjairg, 07-20-2023, 11:57 PM
    3 responses
    214 views
    1 like
    Last Post PaulMohn  
    Started by TheWhiteDragon, 01-21-2019, 12:44 PM
    4 responses
    547 views
    0 likes
    Last Post PaulMohn  
    Working...
    X