These scripts achieve these goals:
- Normally a stoploss order gets trigerred based on the last trade price. These scripts will create a stoposs order in DAS Trader such that it gets trigerred based on the Ask or the Bid (although it can be modified to use the last trade price for the trigger).
- The scripts will break a big order into multiple smaller orders of the desired size. For example, if we have 30k shares and we want to exit the full position once the stop is trigerred and we want to execute the order in the chunks of 4k shares, then the script will automatically do this. Some brokers some times take a long time to process a large order, so breaking into smaller chunks solves this problem.
- When you add to an existing position, then the stoploss order does not need to be updated as it reads the position size when the stop is trigered and not when it is created.
The stoploss order is created using the alert feature of the DAS Trader. These scripts do some verbose logging to the DAS Event Logger so that we can look into the Event Log to find some runtime information.
Make sure you understand all the scripts completely and test them using the paper trading account for different use cases before using them. Modify them based on your own unique situation and requirements. Use these scripts at your own risk.
Here are the scripts:
Set these global variables either during the DAS initializations or using a hotkey or hotbutton:
$SUBPENNY_TRAIL=0.0010;
$PENNY_TRAIL=0.01;
$EXIT_SHARE=9000;
Update the values of the above global variables based on your own risk tolerance and specific requirement of the strategy. You can also modify these global variables and the scripts below to have these parameters for each stock individually.
MsgLog("SMART-FLATTEN called. $flatten_sym = " + $flatten_sym);
$flatten_mon = GetVar($flatten_sym + "_MON");
$flatten_mon.Symbol = $flatten_sym;
$flatten_local=NewUserObj();
$flatten_local.saved_share = $flatten_mon.Share;
if ($EXIT_SHARE < 99) {
MsgLog("$EXIT_SHARE not defined. Returning...");
return;
}
if ($flatten_mon.Pos < 1) {
MsgLog("There is no position. Returning...");
return;
}
$flatten_local.premsg1 = "Bid = " + $flatten_mon.Bid + ", Ask = " + $flatten_mon.Ask;
$flatten_local.full_exit_orders_num = $flatten_mon.Pos / $EXIT_SHARE;
$flatten_local.full_exit_orders_num = $flatten_local.full_exit_orders_num - ($flatten_local.full_exit_orders_num % 1);
$flatten_local.last_exit_shares = $flatten_mon.Pos % $EXIT_SHARE;
$flatten_local.premsg2 = "Symbol=" + $flatten_mon.Symbol + ", Pos=" + $flatten_mon.Pos + ", full_exit_orders_num=" + $flatten_local.full_exit_orders_num + ", last_exit_shares=" + $flatten_local.last_exit_shares;
$flatten_local.I = 0;
while ($flatten_local.I < $flatten_local.full_exit_orders_num) {
$flatten_mon.Route=MARKET;$flatten_mon.Share=$EXIT_SHARE;$flatten_mon.Tif=DAY;$flatten_mon.Send=REVERSE;
$flatten_local.I = $flatten_local.I + 1;
}
$flatten_mon.Route=MARKET;$flatten_mon.Share=$flatten_local.last_exit_shares;$flatten_mon.Tif=DAY;$flatten_mon.Send=REVERSE;
$flatten_mon.Share = $flatten_local.saved_share;
MsgLog("SMART-FLATTEN PRE1: " + $flatten_local.premsg1);
MsgLog("SMART-FLATTEN PRE2: " + $flatten_local.premsg2);
MsgLog("SMART-FLATTEN done. Bid = " + $flatten_mon.Bid + ", Ask = " + $flatten_mon.Ask);
MsgLog("FLATTEN called. Symbol = " + Symbol + ", Bid = " + Bid + ", Ask = " + Ask);
CXL ALLSYMB;
$_alert_to_del = GetAlertObj(SYMBOL + "_Auto_SL");
if (IsObj($_alert_to_del)) {
$_alert_to_del.delete();
}
$_mon_flatten = GetWindowObj();
SetVar(SYMBOL + "_MON", $_mon_flatten);
$flatten_sym=SYMBOL;
ExecHotkey("#K9-SMART-FLATTEN");
MsgLog("FLATTEN done. Symbol = " + Symbol + ", Bid = " + Bid + ", Ask = " + Ask);
MsgLog("SET-SL-ALERT-LONG called. Symbol = " + Symbol + ", Bid = " + Bid + ", Ask = " + Ask);
CXL ALLSYMB;
$_alert_to_del = GetAlertObj(SYMBOL + "_Auto_SL");
if (IsObj($_alert_to_del)) {
$_alert_to_del.delete();
}
$_mon_alert = GetWindowObj();
SetVar($_mon_alert.Symbol + "_MON", $_mon_alert);
// Get SL Price
if ($_mon_alert.Bid > 1) {
$_sl_price = $_mon_alert.Bid - $PENNY_TRAIL;
} else {
$_sl_price = $_mon_alert.Bid - $SUBPENNY_TRAIL;
}
// Create Alert
$_alert_new_sl_long = NewAlertObj();
$_alert_new_sl_long.Name = $_mon_alert.Symbol + "_Auto_SL";
$_alert_new_sl_long.Symb = $_mon_alert.Symbol;
$_alert_new_sl_long.AddItem("L1 Ask", "<=", $_sl_price);
$_alert_new_sl_long.PopupWindow = 0;
$_alert_new_sl_long.Speak=0;
$_alert_new_sl_long.PlaySound=0;
$_alert_new_sl_long.Autodelete=1;
$_alert_new_sl_long.Loop=0;
// Create exit orders on the alert trigger
$_alert_new_sl_long.Script = "MsgLog(\"SET-SL-ALERT-LONG trigerred\"); $flatten_sym=\"" + $_mon_alert.Symbol + "\";ExecHotkey(\"#K9-SMART-FLATTEN\"); MsgLog(\"SET-SL-ALERT-LONG trigger done. Bid = \" + $_mon_alert.Bid + \", Ask = \" + $_mon_alert.Ask);";
$_alert_new_sl_long.Save();
MsgLog("SET-SL-ALERT-LONG created: symbol = " + $_mon_alert.Symbol + ", Condition: L1 Ask <= " + $_sl_price);
MsgLog("SET-SL-ALERT-SHORT called. Symbol = " + Symbol + ", Bid = " + Bid + ", Ask = " + Ask);
CXL ALLSYMB;
$_alert_to_del = GetAlertObj(SYMBOL + "_Auto_SL");
if (IsObj($_alert_to_del)) {
$_alert_to_del.delete();
}
$_mon_alert = GetWindowObj();
SetVar($_mon_alert.Symbol + "_MON", $_mon_alert);
// Get SL Price
if ($_mon_alert.Ask > 1) {
$_sl_price = $_mon_alert.Ask + $PENNY_TRAIL;
} else {
$_sl_price = $_mon_alert.Ask + $SUBPENNY_TRAIL;
}
// Create Alert
$_alert_new_sl_short = NewAlertObj();
$_alert_new_sl_short.Name = $_mon_alert.Symbol + "_Auto_SL";
$_alert_new_sl_short.Symb = $_mon_alert.Symbol;
$_alert_new_sl_short.AddItem("L1 Bid", ">=", $_sl_price);
$_alert_new_sl_short.PopupWindow = 0;
$_alert_new_sl_short.Speak=0;
$_alert_new_sl_short.PlaySound=0;
$_alert_new_sl_short.Autodelete=1;
$_alert_new_sl_short.Loop=0;
// Create exit orders on the alert trigger
$_alert_new_sl_short.Script = "MsgLog(\"SET-SL-ALERT-SHORT trigerred\"); $flatten_sym=\"" + $_mon_alert.Symbol + "\";ExecHotkey(\"#K9-SMART-FLATTEN\"); MsgLog(\"SET-SL-ALERT-SHORT trigger done. Bid = \" + $_mon_alert.Bid + \", Ask = \" + $_mon_alert.Ask);";
$_alert_new_sl_short.Save();
MsgLog("SET-SL-ALERT-SHORT created: symbol = " + $_mon_alert.Symbol + ", Condition: L1 Bid >= " + $_sl_price);