Problem writing Array items to the Console

Created at 23 Feb 2016, 22:26
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
Mikro's avatar

Mikro

Joined 20.06.2015

Problem writing Array items to the Console
23 Feb 2016, 22:26


I want to sort my Trades using Arrays.

For example with arrLongTrades[], arrShortTrades[], and so on.

My issues are

  1. I don't know how big the arrays are going to be when declaring them
  2. I can't figure out how to glue together a String with all the Trade.IDs in the arrays.

Concerning first issue, can anybody give me a hint how to manage Arrays with changing Dimension?

Concerning the second issue I am trying to fill the string array sTrades[] with the Order.Position.ID's from the int array Trades[]

foreach (var Trade in Trades)
{
        sTrades[sTrades.Length] = string.Format("{0}", Trade); //
}
Print(string.Join(";", sTrades));

Any idea how to get a string from this array??

Thanks!!


@Mikro
Replies

cyfer
24 Feb 2016, 15:14

Basically  Generic Lists

Reference 

Using System.Collections.Generics ;

Initiation 

Private List<HistroicalTrade> LongTrades = new List<HistoricaTrade>() ;

Addition

LongTrades.ADD(a long Trade) ;

Looping

for(int i = 0 ; i< LongTrades.count ; i++)
{



}

its something like that mainly , you can put any type or Custom Type in a generic list 


@cyfer

Mikro
29 Feb 2016, 01:01

RE:

cyfer said:

Addition
LongTrades.ADD(a long Trade) ;
its something like that mainly , you can put any type or Custom Type in a generic list 

Hi Cyfer,

tried it with List<int> and Trade IDs, which works, thank you.

Using 'HistoricalTrade' Types as a From of Lists instead could be very practical form my Bot though.

I couldn't figure out how to add items to <HistoricalTrade> Lists. If I open a new Trade, how do I add it to the List in your example an how do I query Information from the Trade, for example to remove it if it was closed?

I'm thinking like this

public List<HistoricalTrade> LongTrades = new List<HistoricalTrades>;

method1()
{
Trade=ExecuteMarketOder(TradeType.Buy, Symbol, 10000, "Long", 50,150);
if (Trade.IsSuccessful==true)
{
   LongTrades.add(???);
}
{

method2()   //check if StopLoss or TakeProfit has closed the Trade an remove it from List
{
foreach( var Trade in LongTrades)
{
if (Trade.???)   //check if Trade is closed
LongTrades.remove(Trade)
}
}

 


@Mikro

cyfer
29 Feb 2016, 12:52

Hello Mikro 

Adding a Trade to a historical Trade List is not hard 

 

       


        private List<HistoricalTrade> LongTrades = new List<HistoricalTrade>();
        private History hist;

         protected override void Initialize()
        {
            // Initialize and create nested indicators


            hist = History;
            for (int i = 0; i < hist.Count; i++)
            {
                LongTrades.Add(hist[i]);
                Print(hist[i].PositionId);
            }


        }

But , in your case it is 

AFAIK the Built-in Historical trade is for the Past only 

so if you are trying to make the List with new Trades that didn't even Finish .. i.e after you execute the trade , it should be added to the List 

then I think you should make you're own Type or Try to use Positions .

for instance ,  A historical trade includes the data about the Finished Date & closing Price .. which is obviously not suitable in your case 

in your example , you should make your own type which includes State of the Trade (Still Open/Closed) and only if it is closed you can add it to the Historical Trade List.

I think you should be really looking into Positions , it is already a Collection 


@cyfer