Example #1
0
    public void PositionSize(PendingOrder pendingorder, AgentState state)
    {
        //Check if this is for entry
        if (state != AgentState.EntryLong && state != AgentState.EntryShort)
        {
            return;
        }

        //Get the size of the position
        var positionsize = (pendingorder.Order.Quantity * FixedSize);

        //Get current stop order
        var currentstop = Agent.PendingOrders
                          .Where(x => !x.IsCancelled)
                          .FirstOrDefault(x => x.Order.Symbol == pendingorder.Order.Symbol &&
                                          (x.Order.Type == OrderType.Stop || x.Order.Type == OrderType.StopLimit));

        //See if we need to update our stop order
        if (currentstop != null)
        {
            currentstop.Update(x => x.Quantity = positionsize);
        }

        //Set our pending order on our size (check if we need to reverse the current order)
        var currentpos = Agent.Positions[pendingorder.Order.Security];

        if (!currentpos.IsFlat)
        {
            positionsize += Math.Abs(currentpos.Quantity);
        }

        //Update pending order
        pendingorder.Update(x => x.Quantity = positionsize);
    }
Example #2
0
    public void PositionSize(PendingOrder pendingorder, AgentState state)
    {
        //Check if this is for entry
        if (state != AgentState.EntryLong && state != AgentState.EntryShort)
        {
            return;
        }

        //Get current stop order
        var currentstop = Agent.PendingOrders
                          .Where(x => !x.IsCancelled)
                          .FirstOrDefault(x => x.Order.Symbol == pendingorder.Order.Symbol &&
                                          (x.Order.Type == OrderType.Stop || x.Order.Type == OrderType.StopLimit));

        //Check if we have a stop order, cannot determine the risk without it
        if (currentstop == null || !ATR.IsReady)
        {
            return;
        }

        //Get the current close
        var currentprice = CurrentBar[pendingorder.Order.Symbol].Close;

        //Get the current risk (delta price)/Leverage
        var risk = (Math.Abs(currentprice - currentstop.Order.StopPrice) / pendingorder.Order.Security.PipSize) * (pendingorder.Order.Security.PipValue / 100);

        //Calculate the amount of lots ((CE * %PE) / SV)
        int     microlots = Convert.ToInt32(((pendingorder.Account.Equity * (FixedPercentage * .0001M)) / (ATR.Result[0] / pendingorder.Order.Security.PipSize)));
        decimal quantity  = microlots * .01M;

        //Set the stop order on our size
        currentstop.Update(x => x.Quantity = quantity);

        //Set our pending order on our size (check if we need to reverse the current order)
        var currentpos = Agent.Positions[pendingorder.Order.Security];

        if (!currentpos.IsFlat)
        {
            quantity += Math.Abs(currentpos.Quantity);
        }

        //Update pending order
        pendingorder.Update(x => x.Quantity = quantity);
    }