Ferme plusieurs positions ouvertes identifiées par un MagicNumber de départ et un nombre d'ordres. Simplifie la gestion de groupes de positions associées dans les stratégies de trading automatisé.
Après l'ouverture de plusieurs positions, ce snippet ferme N ordres (« Nb Order »), à partir d'un MagicNumber
Par exemple, vous avez 5 positions avec le Magic Number : 1004, 1003, 1002, 1001, 1000.
Vous saisissez le Magic number : 1004
Vous saisissez Nb Order : 5
Il fermera les 5 positions.
Voici le code
package SQ.Blocks.Order.Close;
import SQ.Functions.OrderFunctions;
import SQ.Internal.ActionBlock;
import com.strategyquant.lib.*;
import com.strategyquant.datalib.*;
import com.strategyquant.tradinglib.*;
// Modified in France by Emmanuel Evrard for the StrategyQuantX Community :)
@BuildingBlock(returnType = ReturnTypes.Action)
@Help("Close N positions that fit the criteria")
@SortOrder(500) // In AlgoWizard, this is a dropbox position (400 or 500 or 600 etc.) of this function in the menu of "Add New Condition", you may have to adjust it depending on the function already installed on the same dropbox.
@IgnoreInBuilder
@NotSupportedFor(engines="EL")
public class CloseNPositions extends ActionBlock
{
@Parameter(defaultValue="Current", category="Order identification", showIfDefault=false, allowAny=true)
public String Symbol;
@Parameter(defaultValue="0", category="Order identification", showIfDefault=false)
@Editor(type=Editors.Selection, values="Long=1,Short=-1,Any=0")
public int Direction;
@Parameter(defaultValue="MagicNumber", category="Order identification", showIfDefault=false)
@Help("Magic number that can identify the order.")
@Editor(type=Editors.SelectionVariablesWithAny)
public int MagicNumber;
@Parameter(defaultValue="Number Of Order", category="Order identification", showIfDefault=false)
@Help("Number Of Order is used to set the quantity of order, Their MagicNumbers are successives to the above MagicNumber")
@Editor(type=Editors.SelectionVariables)
public int NbOrder;
@Parameter(defaultValue="", category="Order identification", showIfDefault=false)
@Help("Comment can be also used to identify the order. In case of Comment, order matches if the order comments contains the text specified here.")
public String Comment;
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
public CloseNPositions()
{
}
//------------------------------------------------------------------------
@Override
public void OnAction() throws TradingException
{
int total = Strategy.Trader.getOpenOrdersCount(false);
if (NbOrder >= 1)
{
int MagicNumber1 = MagicNumber;
for(int i1 = 0; i1 < NbOrder; i1++)
{
MagicNumber = MagicNumber1 - i1 ;
for(int i=total-1; i>=0; i--)
{
ILiveOrder order = Strategy.Trader.getOpenOrder(i, false);
if(OrderFunctions.identify(order, Strategy, Symbol, Direction, MagicNumber, Comment))
{
order.setExitIndex((byte) -1);
order.Close(OrderCloseTypes.ExitSignal);
}
}
}
}
}
}
Gratuit
Fichiers
Snippet-ClosingNMarketOrder.sqm4 KBVerrouillé
Le téléchargement se débloque après l’achat ou avec un niveau actif.