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

Multiple Position when using OnEachTick Strategy

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

    Multiple Position when using OnEachTick Strategy

    Hi,
    I am using a simple strategy and would like it to run on each tick
    the problem is that when there is a position enter signal and the position filled, the strategy continue sending position enter signal although i check :
    if (Position.MarketPosition != MarketPosition.Flat || _pendingPosition == true)

    Here is the simplified code, can you see any problems in the design/code ?
    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Test : Strategy
    {
    private const string EnterShortName = "Open Short";
    private const string ProfitShort = "Profit Short";
    private const string StopShort = "Stop Short";
    
    private bool _pendingPosition;
    private bool _enterShort;
    private double _stopPrice;
    private double _profitPrice;
    
    
    protected override void OnBarUpdate()
    {
    _enterShort = false;
    
    if (Position.MarketPosition != MarketPosition.Flat || _pendingPosition == true)
    return;
    
    _enterShort = SomeCondition...
    
    if (_enterShort)
    {
    _stopPrice = Close[0] + (8 * TickSize);
    EnterShortLimit(1, Close[0] - TickSize, EnterShortName);
    }
    }
    
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
    _pendingPosition = orderState == OrderState.Accepted || orderState == OrderState.Working || orderState == OrderState.Submitted;
    }
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order.OrderState == OrderState.Filled)
    {
    if (exec.Order.Name == EnterShortName)
    SetStopProfit(execution);
    }
    }
    
    private void SetStopProfit(Execution execution)
    {
    double entryPrice = execution.Order.AverageFillPrice;
    
    if (Position.MarketPosition == MarketPosition.Short)
    {
    _profitPrice = entryPrice - (TickSize * 6);
    
    ExitShortStopMarket(0, true, 1, _stopPrice, StopShort, EnterShortName);
    ExitShortLimit(0, true, 1, _profitPrice, ProfitShort, EnterShortName);
    }
    }
    
    }
    }
    Click image for larger version

Name:	Capture.JPG
Views:	127
Size:	100.8 KB
ID:	1163027
    Thanks

    #2
    Hello yaniv,

    The position takes time to change. When it does it will trigger OnPositionUpdate.

    You can use logic to control when orders are submitted. You could assign the order to a variable and only allow new orders when the variable is null.

    Below is a link to an example.


    You could also use a bool that is changed to true after the order is placed and then changed back to false when IsFirstTickOfBar is true.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Stanfillirenfro, Today, 07:23 AM
    1 response
    4 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by cmtjoancolmenero, Yesterday, 03:58 PM
    2 responses
    22 views
    0 likes
    Last Post cmtjoancolmenero  
    Started by olisav57, Yesterday, 07:39 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by cocoescala, 10-12-2018, 11:02 PM
    7 responses
    944 views
    0 likes
    Last Post Jquiroz1975  
    Started by oviejo, Today, 12:28 AM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X