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

Custom FillType - how to get IOrder

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

    Custom FillType - how to get IOrder

    Hi,
    I have implemented custom FillType and implemented its main method:

    Code:
    public override void Fill([COLOR="Red"]Order[/COLOR] o)
    Everything is working, no problems. I just now need to add som advanced logic,
    where I need access to order by IOrder interface (used everywhere across whole API).
    What I have in parameter - is just Order class.


    My problem now is, that parameter of the method above is Order - instead of IOrder.
    I need IOrder, because I store various useful data related to each IOrder, during its lifecycle. For example I store all IExecutions associated with IOrder and many other similar metadata... I store this metadata in simple Map (Dictionary), where key is the IOrder and value is the object with Metadata..

    So this is main question:
    How can I get the IOrder object in custom FillType - when parameter of its method is only Order object?
    I need access to IOrder, to get access to Metadata stored in my Map (Dictionary).

    What I tried to resolve this problem - but it is not working:
    1. I tried to simply cast the parameter to the IOrder, but this fails with exception.
    2. I tried to store order metadata in map, where key is IOrder#OrderId - so I would not be dependent on the IOrder instance. But this can fail in live trading, because OrderId can change during lifetime of the IOrder - as mentioned in the NT7 documentation for IOrder.
    Last edited by misova; 01-11-2016, 08:48 AM.

    #2
    Hello,

    Thank you for the question.

    I would be unsure without some sample of what you are doing here.

    The default override void Fill(Order o) would be unable to be changed as it is being passed an Order object from internal logic.

    Are you manually calling this override to supply your own orders? if so I am unsure this would be accurate or work as that would not be the same as it being called internally.

    If you are manually calling this method, would it not be better to instead just create your own method with the correct overloads for what you are trying to do?

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi,
      what I am doing is this:

      In trading strategy:
      I store many useful contextual data to each IOrder. I store this data in simple map, defined by Dictionary<IOrder, MyOrderMetadata>. That''s all - pretty simple. To have a ratio behind it or some example - I am storing for example all IExecutions to each IOrder. I am also storing what's is the role of the order - it can be entry/profit/stoploss. And much more other useful things, related to the IOrder. That's all - pretty simple.

      What is my problem?
      I am have implemented custom FillType. This is easy and it works as expected.

      As we all know, each FillType has this main method:
      Code:
      public override void Fill(Order o)
      This method has parameter of class Order. But - all my metadata are stored and searched in map by IOrder. As I have no IOrder instance in the parameter, I cannot get access to all of my metadata previously stored.

      If it is needed to understand - what I am trying to achieve in the custom FillType, I can explain shortly:
      In case it happens, that both Profit and Stoploss order can be filled in next bar, then I want to make sure, that loss will be reported from such trade and not a profit. To achieve this, I need to influence the filling logic by implementing my custom FillType. In this case, I have to prevent filling Profit-order and forcing filling Stoploss-order. To achieve all this logic, I need acces to my custom metadata stored for each IOrder.

      It can be useful to understand, what I am trying to achieve - but I do not want to overwhelm you with unrelevant implementations details, when my problem is very very simple and straightforward. It is just 2 points:
      1. I am storing metadata to each IOrder - in structure like Dictionary<IOrder, MyOrderMetadata>. I have all important info there.
      2. But when implementing custom FillType, I cannot lookup that Dictionary by IOrder - because I have none IOrder in FillType. What I have instead, is some Order class.


      Question is also simple:
      So how can I get the IOrder from Order class, that is provided by method in FillType?
      (as I store all my metadata under key of type IOrder, I need the IOrder access the metadata)

      Or alternative question:
      If there is no way, how to get IOrder in FillType, then how should I store information related to IOrder, so I can access it later in custom FillType using information provided in class Order?
      Last edited by misova; 01-11-2016, 01:25 PM.

      Comment


        #4
        Hello,

        Thank you for the reply.

        I am not sure of a way this would be possible as these two types were not intended to be used in this way to start with.

        Generally a FillType would only be called for the Backtest on each fill and only for each order that is internally passed to the FillType. It is not a script that is concurrently running with the strategy so anything that needs to be done logically in a FillType would need to be done in one iteration. This means it can not really store its own collections of information and only would be able to accept the single type 'Order' for its purposes.

        Your strategy would interface with Strategy methods and call all of the internal logic, this translates internally into the call to the fill type which would be supplied the 'Order' to the fill type for further instruction on what to do in that case.

        Being that this logic is not happening in the fill type but you are storing this data in your strategy, I would really see no specific way to transfer that data to the fill type. What would likely need to happen would be the logic used in the strategy would in some way need to be replicated in the fill type so that it can do whatever is needed for each passed Order object.

        There are no examples or documented ways that I am aware of to pass extra data specifically to a FillType from a strategy. You may be able to workaround using writing to file or a static class but neither I would recommend using in general as in both cases you could run into other errors like locked files or old data being used.

        Please let me know if I may be of further assistnace.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Yes, I see, it's non-standard problem, so it is not so easy to find solution.

          I made it working right now - by replacing key from old IOrder, to new order-Token (simple string).
          This way I am storing metadata in structure Dictionary<string, MyOrderMetadata>.
          They key is Token of the order now.

          As Token property is accessible also from Order class, I can lookup my metadata correctly.

          I think it is a little hack and not so elegant. But it works as expected.

          Thank you Jesse for all your great effort to help.
          I really appreciate it.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by junkone, Today, 11:37 AM
          2 responses
          12 views
          0 likes
          Last Post junkone
          by junkone
           
          Started by frankthearm, Yesterday, 09:08 AM
          12 responses
          43 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by quantismo, 04-17-2024, 05:13 PM
          5 responses
          35 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by proptrade13, Today, 11:06 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          35 views
          0 likes
          Last Post love2code2trade  
          Working...
          X