Exemplo n.º 1
0
        public int check_ts_cs(GF.Api.Positions.PositionChangedEventArgs e)
        {
            //Called inside of loop of positions in Array  : )
            var opl = get_position_pnl(gfClient, e);
            var sym = e.ContractPosition.Contract.Symbol;

            var def  = false;
            var idef = 0.0;

            //If no value in dict, save them in -- (Avoid errors)
            if (hi_pnl.TryGetValue(sym, out idef))                              //Make sure this doesnt write over any current value?
            {
                hi_pnl[sym]      = 0;
                trigger_pnl[sym] = 0;
            }


            //If new high, write over past hi_pnl
            if (opl > hi_pnl[sym])                                          //MIGHT need to init this dictionary when position opens?
            {
                hi_pnl[sym] = opl;
            }

            //Update Trigger price...
            trigger_pnl[sym] = hi_pnl[sym] * (1 - trail_dec);

            //Begin Trailing with Trigger...
            if (opl >= trail_tgt)
            {
                if (!trail_on.TryGetValue(sym, out def))
                {
                    trail_on[sym] = true;
                }
            }

            if (trail_on[sym])
            {
                if (opl <= trigger_pnl[sym])
                {
                    Console.WriteLine($"Exitting {sym} at {opl}");
                    //PlaceOrder(gfClient, e.ContractPosition.Contract.ElectronicContract, OrderSide.BuyToCover);  -- Replace w MKT order (or STOP ordeR?)
                    return(1);                                                   //signals time to exit... and logs -- Cleaner.
                }
            }

            if (opl <= fixed_stop)
            {
                return(-1);
            }

            return(0);
        }
Exemplo n.º 2
0
        //Initial Version...
        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);
        }