Example #1
0
        public int run_cat_trail(GF.Api.Positions.PositionChangedEventArgs e)
        {
            //var account = gfClient.Accounts.Get().FirstOrDefault();
            //Should be inside loop ? Called inside loop?
            //var positions = GetOpenPositions(account, e);                     //Just in case we need more detail / Safety...

            int ret = 0;

            foreach (var pos in e.AsArray())
            {
                ret = check_ts_cs(pos);
                switch (ret)
                {
                case 0:
                    break;

                case -1:
                    Console.WriteLine($"CatStop Triggered -- Exiting position in {e.ContractPosition.Contract.Symbol}");
                    break;

                case 1:
                    Console.WriteLine($"TrailStop Triggered -- Exiting position in {e.ContractPosition.Contract.Symbol}");
                    break;

                default:
                    Console.WriteLine($"Error -- Please check position in {e.ContractPosition.Contract.Symbol}");
                    break;
                }

                if (ret != 0)
                {
                    var qty = Math.Abs(e.ContractPosition.Net.Volume);
                    if (e.ContractPosition.Net.Volume < 0)
                    {
                        PlaceOrder(gfClient, e.ContractPosition.Contract.ElectronicContract, OrderSide.BuyToCover, qty);
                        Console.WriteLine("SX order sent.");
                    }
                    if (e.ContractPosition.Net.Volume > 0)
                    {
                        PlaceOrder(gfClient, e.ContractPosition.Contract.ElectronicContract, OrderSide.Sell, qty);
                        Console.WriteLine("LX order sent.");
                    }
                }
            }
            return(ret);
        }
Example #2
0
        public IEnumerable <GF.Api.Positions.IPosition> GetOpenPositions(GF.Api.Accounts.IAccount account, GF.Api.Positions.PositionChangedEventArgs e)
        {
            //Made active an ATTRIBUTE / PROPERTY -- now can be called whenever.
            //GF.Api.Positions.IPosition[] active = { };                          //Should I make this a fixed memory level ? Will Dynamic create overflow?
            int idx = 0;

            foreach (var pos in e.AsArray())
            {
                //If Matched Account, Has fills, and has Open Position PNL... Add to active.
                if (e.ContractPosition.Account.ID == account.ID & e.ContractPosition.Fills != null & e.ContractPosition.OTE != 0)
                {
                    //Console.WriteLine(pos);
                    active[idx] = pos.ContractPosition;
                }
                idx += 1;
            }
            //Real purpose -- return changed positions -- > To feed into TrailStop.

            return(active);
        }