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

ATM Strategy problem with Strategy Id

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

    ATM Strategy problem with Strategy Id

    I've got a strategy that calls an ATM Template. For some reason it is having trouble finding or retaining? the StrategyId

    The salient long entry code reads:

    if (Historical) return;
    Print ("Line 485, Bar" + Bars.BarsSinceSession + " " + Time[0].TimeOfDay + " Pre anything Order Entry: Strategy Id: " + atmStrategyId + ", Order Id: " + orderId);
    if((atmStrategyId.Length > 0 ? GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat : 1==1)
    && realized_profit > loss_exit && realized_profit < profit_exit
    && PatternDesc != "")//wait til flat
    {
    Print("490");
    if (orderId.Length == 0 && atmStrategyId.Length == 0 && trade > 0 && trade < 10
    && (!string.IsNullOrEmpty(from_time_1) ? ((ToTime(Time[1]) >= start_time && ToTime(Time[1]) <= end_time) || (ToTime(Time[1]) >= start_time_1 && ToTime(Time[1]) <= end_time_1)) : (ToTime(Time[1]) >= start_time && ToTime(Time[1]) <= end_time))
    && btnLongEntry.Text == "Enter Long")
    {
    Print("496 Long Order Entry, Pre Set Ids " + atmStrategyId);
    entrybar = Bars.BarsSinceSession;
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    Print("498: " + Time[0] + "Pattern #: " + trade + ", " + entrybar + ", Bar: " + Bars.BarsSinceSession + ", Position: " + GetAtmStrategyMarketPosition(atmStrategyId) + ", StratId: " + atmStrategyId + ", Order Id: " + orderId);
    AtmStrategyCreate(OrderAction.Buy, TypeEntry, (TypeEntry != OrderType.Market ? Close[1]+LimitOffset*TickSize : 0),
    (TypeEntry != OrderType.StopLimit ? 0 : (Close[1]+StopOffset*TickSize)), TimeInForce.Day, orderId,
    (closeat == 1 ? PatternDesc : PatternDesc), atmStrategyId);

    and the Output window looks like:

    Line 485, Bar104 11:51:35 Pre anything Order Entry: Strategy Id: , Order Id:
    490
    496 Long Order Entry, Pre Set Ids
    **NT** GetAtmStrategyMarketPosition() method error: AtmStrategyId '8f881c91764d43998a46fa8a5ecb2bdd' does not exist
    498: 3/17/2011 11:51:35 AMPattern #: 5, 104, Bar: 104, Position: Flat, StratId: 8f881c91764d43998a46fa8a5ecb2bdd, Order Id: 01f5580fafa94ab8960874ec842b555f
    **NT** Submitting order with strategy 'TRO ATM w/ Buttons/a6d7ec29f4f247df967d037f94aa3495'
    548 3/17/2011 11:51:35 AM OrderID: 01f5580fafa94ab8960874ec842b555f, Entry Order Status: PendingSubmit, atmId: 8f881c91764d43998a46fa8a5ecb2bdd, Entry Bar: 104, Current Bar: 104
    328: 3/17/2011 11:51:35 AM atmStratId: 8f881c91764d43998a46fa8a5ecb2bdd, Position: Flat, Status: PendingSubmit
    548 3/17/2011 11:51:35 AM OrderID: 01f5580fafa94ab8960874ec842b555f, Entry Order Status: PendingSubmit, atmId: 8f881c91764d43998a46fa8a5ecb2bdd, Entry Bar: 104, Current Bar: 104


    After the entry, the order is placed and filled, the code checks for a completed trade with the following code:

    Print("328: " + Time[0] + " atmStratId: " + atmStrategyId + ", Position: " + GetAtmStrategyMarketPosition(atmStrategyId) + ", Status: " + status[2]);
    if((atmStrategyId.Length > 0 ? GetAtmStrategyMarketPosition(atmStrategyId)==Marke tPosition.Flat : 1==2) && (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected"))
    {
    Print("331 " + Time[0] + " StratId = " + atmStrategyId + " Position: " + GetAtmStrategyMarketPosition(atmStrategyId) + ", Order: " + status[2]);
    realized_profit += GetAtmStrategyRealizedProfitLoss(atmStrategyId);
    Print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
    Print("Your Realized Profit for today's trading is: " + realized_profit.ToString("C"));
    if(realized_profit < loss_exit || realized_profit > profit_exit)
    Print("Your trading is finished for today.");
    if(realized_profit > profit_exit) Print("Awesome day. Now go celebrate.");
    else if(realized_profit < loss_exit)
    Print("Tomorrow will be a new day and the sun will come up and the birds will sing. All is not lost.");
    Print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
    atmStrategyId = string.Empty;
    orderId = string.Empty;
    oldprojectedlow = 0;
    oldprojectedhigh = 0;
    Print("345: " + Time[0] + " reset the strategy id");

    But the Output window displays:

    328: 3/17/2011 11:51:36 AM atmStratId: 8f881c91764d43998a46fa8a5ecb2bdd, Position: Flat, Status: Filled
    331 3/17/2011 11:51:36 AM StratId = 8f881c91764d43998a46fa8a5ecb2bdd Position: Flat, Order: Filled
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    Your Realized Profit for today's trading is: $0.00
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    345: 3/17/2011 11:51:36 AM reset the strategy id

    At the time it Prints line 328: Market Status is LONG, not Flat.

    I hesitate to ask because I know your job is not my programming, but can you see any reason I am getting that NT error and why it shows the order Filled but the position is Flat. It seems the code is losing or misplacing the atmStrategyId.

    Thanks,
    Snap

    p.s., The really aggravating thing is that sometimes it does remember the Id and works properly.

    #2
    Hi snaphook,

    The provided sample has all the needed structure for this, including management of ATM strategy ID. I would work from this sample and only add complexity once you're verified functionality at each state. The sample is available at Tools > Edit NinjaScript > Strategy > SampleAtmStrategy

    You're missing, for example, this line from the sample:
    Code:
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    atmStrategyId = string.Empty;
    In the section below, you are missing curly brackets to block off the code you want to execute after the else if statement. If you don't have this it only executes the first line after this statement and all the other lines are then not attached to the conditional.

    Code:
    else if(realized_profit < loss_exit) 
    Print("Tomorrow will be a new day and the sun will come up and the birds will sing. All is not lost.");
    Print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
    atmStrategyId = string.Empty;
    For sharing this many lines of code, it will improve readability if you can post the file directly or mark it with the code tags in the forum.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Code is executing too fast

      I think I found the problem. The code executes so fast that the test for Market Position occurs after the order is filled but before the Market Position is updated.

      Note the Times, OrderStatus and Market Position. The first number is the line in NinjaScript. Note the bold and underlined sections. It goes from Flat & PendingSubmit to Flat and Filled to Flat and Filled (all @ 9:36:26) to Short and Filled @ 9:36:27.

      Is there a supported method for pausing code execution for a second or two?

      324: 3/18/2011 9:36:26 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Flat, Status: PendingSubmit
      539 3/18/2011 9:36:26 AM OrderID: 6e6b90b4465940a5b07515e417ab1465, Entry Order Status: PendingSubmit, atmId: 1a23414fb30f49b7b06f5008e648d382, Entry Bar: 173, Current Bar: 173
      324: 3/18/2011 9:36:26 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Flat, Status: Filled
      539 3/18/2011 9:36:26 AM OrderID: 6e6b90b4465940a5b07515e417ab1465, Entry Order Status: Filled, atmId: 1a23414fb30f49b7b06f5008e648d382, Entry Bar: 173, Current Bar: 173
      324: 3/18/2011 9:36:26 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Flat, Status: Filled
      539 3/18/2011 9:36:26 AM OrderID: 6e6b90b4465940a5b07515e417ab1465, Entry Order Status: Filled, atmId: 1a23414fb30f49b7b06f5008e648d382, Entry Bar: 173, Current Bar: 173
      324: 3/18/2011 9:36:27 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Short, Status: Filled
      539 3/18/2011 9:36:27 AM OrderID: 6e6b90b4465940a5b07515e417ab1465, Entry Order Status: Filled, atmId: 1a23414fb30f49b7b06f5008e648d382, Entry Bar: 173, Current Bar: 173
      324: 3/18/2011 9:36:28 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Short, Status: Filled
      539 3/18/2011 9:36:28 AM OrderID: 6e6b90b4465940a5b07515e417ab1465, Entry Order Status: Filled, atmId: 1a23414fb30f49b7b06f5008e648d382, Entry Bar: 173, Current Bar: 173
      324: 3/18/2011 9:36:30 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Short, Status: Filled
      539 3/18/2011 9:36:30 AM OrderID: 6e6b90b4465940a5b07515e417ab1465, Entry Order Status: Filled, atmId: 1a23414fb30f49b7b06f5008e648d382, Entry Bar: 173, Current Bar: 173
      324: 3/18/2011 9:36:31 AM atmStratId: 1a23414fb30f49b7b06f5008e648d382, Position: Short, Status: Filled

      Comment


        #4
        Hi snaphook, yes unfortunately the MarketPosition update is not guaranteed to be in until the next OnBarUpdate() is completed - I'm not really aware of a way to pause the execution, but perhaps a timer in the condition leading to the reverse you got?
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DustyContrer, Today, 04:02 AM
        0 responses
        3 views
        0 likes
        Last Post DustyContrer  
        Started by ETFVoyageur, Today, 02:15 AM
        0 responses
        11 views
        0 likes
        Last Post ETFVoyageur  
        Started by Board game geek, Today, 01:34 AM
        0 responses
        9 views
        0 likes
        Last Post Board game geek  
        Started by morrnel, 05-12-2024, 06:07 PM
        3 responses
        44 views
        0 likes
        Last Post wzgy0920  
        Started by FishTrade, Yesterday, 11:11 PM
        0 responses
        7 views
        0 likes
        Last Post FishTrade  
        Working...
        X