8. 4. 2022

5 0

Set Stop Loss Except One

As we open several market orders, we may have to close all orders except one position   ("Magic Number Excluded")

For example,  5 long positions are open, we want to close all of them except one long position ("Magic Number Excluded")

Here is the code :

 

package SQ.Blocks.Order.Modify;

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(name="(SLO) Set Stop Loss Except One", display="Set Stop Loss Except One", returnType = ReturnTypes.Action)
@Help("Sets Stop Loss to a specified level except for one order : 'Magic Number Excluded' . If SL already exists it will be moved.")
@SortOrder(300)																						// 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. 
@CategoryOrder(200)
@IgnoreInBuilder
@NotSupportedFor(engines="EL")
public class SetStopLossExceptOne 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="", 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;

    @Parameter(defaultValue="MagicNumber", category="Order identification", showIfDefault=false)
    @Help("MagicNumber of the Excluded order, this order will not be taken into account")
    @Editor(type=Editors.SelectionVariables)
    public int MagicNumberExcluded;
    
    @Parameter(category="Stop Loss")
    @Editor(type=Editors.Formula, formulaName="RangeLevel")
    public IFormula StopLoss;
    
    
    //------------------------------------------------------------------------
    //------------------------------------------------------------------------
    //------------------------------------------------------------------------

    @Override
    public void OnAction() throws TradingException 
    {
        for(int i=0; i<Strategy.Trader.getOpenOrdersCount(false); i++) 
        {
            ILiveOrder order = Strategy.Trader.getOpenOrder(i, false);

            double openPrice = order.isNettingMode() ? order.getLastOpenPrice() : order.getOpenPrice();
            
            if(OrderFunctions.identify(order, Strategy, Symbol, Direction, MagicNumber, Comment)) 
            {
                if (order.getMagicNumber() != MagicNumberExcluded)	
                {
                    double sl = StopLoss.evaluateFormula(Strategy, Symbol, openPrice, Direction);
                    order.setSL(sl).Send();
                }
            }
        }
    }
}

 


 

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Related posts