Customer’s question:
Hi this is about Seetu. Is there a way to implement a Stop loss via code? Like 5% below the buy in price? Thanks.
Yes. Try this code:
// Set initial capital
SetOption("InitialEquity",50000);
// Positions sizes: use 25% of the current portfolio equity. (It means max. 4 open positions.)
SetPositionSize( 25, spsPercentOfEquity );
// Round a number of shares to 100.
SetOption("RoundLotSize",100);
// ------ Strategy logic --------
MA100 = CalcMA( Close, 100);
// Buy when Close crosses MA(100) up
Buy = Cross(Close,MA100);
// Sell when Close crosses MA(100) down
SellSignal = Cross(MA100,Close);
// StopLoss = 5% down from our buying price (BuyPrice = Close by default)
StopLossPrice = ValueWhen(Buy,Close) * 0.95;
// Sell when Close falls under StopLossPrice. ( StopLossPrice crosses Close up. )
// ("AND NOT SellSignal" added just to have cleaner Exploration data)
StopLossSignal = Cross(StopLossPrice,Close) AND NOT SellSignal;
// Add strategy logic and stop loss logic together to Sell
Sell = SellSignal OR StopLossSignal;
// Show me data in Exploration
Filter = Buy OR Sell;
AppendColumn(Close,"Close");
AppendColumn(MA100,"MA100");
AppendTextColumn(iif(Buy,"Buy",""),"Buy");
AppendTextColumn(iif(SellSignal,"Sell",""),"Sell");
AppendColumn(StopLossPrice,"StopLoss");
AppendTextColumn(iif(StopLossSignal,"Stop",""),"Stop");
AddColumn(ValueWhen(Buy,Close),"ValueWhen(Buy,Close)");
// Plot my data
DoPlot(Close,"Close",colorBlack);
DoPlot(MA100,"MA100",colorBlue);
DoPlot(StopLossPrice,"StopLossPrice",colorRed);