Dissecting the Nest Plus Auto Trading API

yusi

Well-Known Member
#82
8. After the break

This was left off at a bridge to cross -- there did not seem to be a direct way get an order status in AmiBroker. And without getting an order status, how was stop-loss, or add, or any additional functionality, to be achieved.

There are two options:

□ As far as AFLs or scripting is concerned, stay till automating just the placing of orders. The order management remains manual.

□ Switch to another language that can handle BSTR* parameters, and lose AmiBroker features.

An alternate way out is to jugar a harness; that is in progress...

But, perhaps, it would be illustrative to at least automate what can be done now with just placing of orders. Post this experiment, the discussion could get back to the dissection, which is a dry subject anyway.
 

yusi

Well-Known Member
#83
9. Basic autotrade

To illustrate a basic autotrade, what is needed is
  1. a SAR AFL,
  2. some automation related code,
  3. and, of course, a subscription to the Nest Plus trading API.

For the AFL, the requirement is an AFL that uses Stop & Reverse. The one that comes to mind is KBrain by Karthikmarar. This can be found at wisestocktrader.

This would certainly be far better than a
Buy = Cross(MACD(), Signal());
Sell = Cross(Signal(), MACD());

While it would be wiser to identify the appropriate time-frame / stock combination with backtesting, this experiment could stay with M5.

Here is today's post-noon chart for Hindalco captured post-market:

 

yusi

Well-Known Member
#84
9. Basic autotrade ... [2]

There has been a sample automation code that has been posted by many persons. Since there are no additional comments or writeup (and use the misspelled AplliedQuantity), it can be assumed that the original came from kaka (in another forum).

I will use it as-is, with a few modifications marked in blue.

Code:
_SECTION_BEGIN("Plus AutoTrading");

ClientID = ParamStr("ClientId", "000000");
Symbol = ParamStr("Symbol", "NIFTY13APRFUT");
Price = NumToStr(C, 1.2, True);
Quantity = Param("Quantity", 1, 1, 100, 1);
AT = ParamToggle("AutoTrade", "No,Yes");

AplliedQuantity = IIf(LastValue(Buy) AND LastValue(Cover) OR LastValue(Short) AND LastValue(Sell), Quantity * 2, Quantity);
RefNumber = Nz(StaticVarGet("RefNumber"));
Checkdt = Nz(StaticVarGet("lastdt"));
dt = LastValue(DateTime());
Cond = LastValue(Buy) OR LastValue(Short) OR LastValue(Sell) OR LastValue(Cover);

if(AT) {
    plus = CreateStaticObject([COLOR="Blue"]"X_Nest.PlusApi"[/COLOR]);

    if(plus) {
        plus.[COLOR="Blue"]X_Initialize[/COLOR]("TestX1");

        if(Cond AND Checkdt != dt ) {
            if(LastValue(Buy) OR LastValue(Cover)) {
                plus.[COLOR="Blue"]X_PlaceOrde[/COLOR]r("BUY", RefNumber, "NSE", Symbol, "DAY", "LIMIT", AplliedQuantity, Price, 0, 1, "CNC", ClientID);
            }
            if(LastValue(Short) OR LastValue(Sell)) {
                plus.[COLOR="Blue"]X_PlaceOrder[/COLOR]("SELL", RefNumber, "NSE", Symbol, "DAY", "LIMIT", AplliedQuantity, Price, 0, 1, "CNC", ClientID);
            }

            StaticVarSet("RefNumber",RefNumber + 1);
            StaticVarSet("lastdt", dt);
        }
    }
}

_SECTION_END();
The key changes for the harness are:

□ The Plus functions are prepended with 'X_". For example, instead of PlaceOrder, we use X_PlaceOrder.
□ Instead of calling plus.SetObjectName("TestX1"), a replacement X_Initialize() is called.
□ Some minor changes such as "NSE" / "CNC" as the test symbol is not a future.​
 

yusi

Well-Known Member
#85
9. Basic autotrade ... [3]

Putting the code to test for a short run gives this result:



□ Auto-trading was enabled at 12:55:17. This can be seen in title bar of the API Generated Orders screen.

□ The first buy order came in at 13:14. It was confirmed. The quantity was 2 -- this was coz the AFL was already in sell mode, and SAR doubled the quantity. The buy/sell code is independent of the auto-trade section; something to keep in mind.

□ There were two additional buy orders at 13:15 and 13:22 that came as a surprise. Obviously, they were not user confirmed. The signals were repainting ?! Need to investigate if they are due to an M1 data feed or other reason.

□ Chop, chop. A sell signal at 13:59, with another a few seconds later. One was confirmed.

□ The subsequent upward breakout buy came in at 14:40. Not taken. The price of this signal, 123.70, is higher than expected; realized that NOW had frozen for some time and data feed had stopped / delayed.
 

yusi

Well-Known Member
#86
9. Basic autotrade ... [4]

The take-home from the experiment (or give-aways):

□ Zerodha, Total amount due to us: 1.28 Dr

□ The auto-trade AFL needs work. A hidden inefficiency is that when auto-trade is enabled, CreateObject and Initialize are executed repeatedly. This would occur with every AFL execution, either by data feed or refresh interval, and these are COM calls. For example, the log says:
Code:
11/19/13 13:32:19: Initialize -- with name : TestX1
11/19/13 13:32:19:    2 : Already initialized
11/19/13 13:32:20: Initialize -- with name : TestX1
11/19/13 13:32:20:    2 : Already initialized
11/19/13 13:32:21: Initialize -- with name : TestX1
11/19/13 13:32:21:    2 : Already initialized
11/19/13 13:32:22: Initialize -- with name : TestX1
11/19/13 13:32:22:    2 : Already initialized
□ The Disclosed Qty (value 1) was wrongly set. Also, for this code, LIMIT should have been market orders else the risk on a non-filled trade comes in.

□ Terminal freezing for a few seconds can be problematic. Terminal stopping would be catastrophic -- there is no way to resume with the same object or trades.

□ There should be an Alert() or Say() when a buy/sell is triggered; the order confirmations had a slippage of 10 seconds or more.

□ Need a multi-monitor setup for this.

□ Need a Portable Toilet.
 

yusi

Well-Known Member
#88
Hi can we place order in next plus using excel vba. Please help
Hardly use excel vba, except when in dire need.

There are so many Excel experts here, maybe someone will help. Ask Josh1, he makes COM calls to AmiBroker; and this should be no different.

Edit: if you are not aware, Josh has a long-running thread Free RealTime Data...
 
Last edited:
#89
Hello Yusi

Nice work, and thanks for sharing, it will be very useful when anyone wants to automate their systems.

Thanks

:) Happy
 

yusi

Well-Known Member
#90
Hello Yusi

Nice work, and thanks for sharing, it will be very useful when anyone wants to automate their systems.

Thanks

:) Happy
Happy, coincidentally I was reading your entire thread (Simple Coding Help...) just last evening. You (and others) were providing great help. But what struck me was that you added that extra touch and flourish and insight with each response, which is a rare and admirable quality (as in the story of Michelangelo chiseling out the tooth in his apprenticeship days).
 

Similar threads