Add positions to trade

I am trying to create a formula that adds a position each time an instrument goes in my favor up to a max of 4 positions to the following code. how would I do that?

// Parameters
period = 50; // 50-period for the current timeframe
stopLossPct = 0.5; // Stop Loss Percentage

// Moving Average Calculation for the current timeframe
myMA = MA(Close, period);  // Calculate the 50-period Moving Average

// Reference Daily Timeframe Moving Averages
dailyMA20 = TimeFrameGetPrice("C", inDaily); // Daily close to calculate MA on daily bars
dailyMA50 = TimeFrameGetPrice("C", inDaily);
dailyMA200 = TimeFrameGetPrice("C", inDaily);

ma20Daily = MA(dailyMA20, 20);   // 20-period MA on daily chart
ma50Daily = MA(dailyMA50, 50);   // 50-period MA on daily chart
ma200Daily = MA(dailyMA200, 200); // 200-period MA on daily chart

// Long Condition: Price must be either above all three MAs or below all three MAs (daily chart)
longCondition = (Low < myMA) AND (Close > myMA) ;//AND 
                //((Close > ma20Daily AND Close > ma50Daily AND Close > ma200Daily) OR 
                // (Close < ma20Daily AND Close < ma50Daily AND Close < ma200Daily));

// Exit Long Condition
exitLongCondition = (Close < myMA);

// Short Condition: Only enter short if the price is above all three MAs (daily chart)
shortCondition = (High > myMA) AND (Close < myMA) AND 
                 (Close > ma20Daily AND Close > ma50Daily AND Close > ma200Daily);

// Exit Short Condition
coverCondition = (Close > myMA);

// Generate signals
Buy = longCondition;
Sell = exitLongCondition;
Short = shortCondition;
Cover = coverCondition;

// Apply Stop Loss
ApplyStop(stopTypeLoss, stopModePercent, stopLossPct, True);

// Limit Position Size to 1 Contract Per Trade SetPositionSize(1, spsShares); // Restrict to 1 contract/lot per trade
SetPositionSize(1, spsShares);
// Capture Entry Time for Buy and Short signals
entryTime = IIf(Buy OR Short, DateTime(), Null);

// Add Column for Entry Time
AddColumn(entryTime, "Entry Time", formatDateTime);

Hint: when adding the code you correctly used those three back ticks ``` , but they must be in a NEW line for code block to work (I corrected it in the post above).

1 Like

Pyramiding scaling in / out is covered in the guide

https://www.amibroker.com/guide/h_pyramid.html

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.