Here is the code that buys on the open of the bar and subsequently buys every 10 points thereafter. This will involve tracking the last purchase price and ensuring that another purchase is made only when the price has moved 10 points from the last purchase price.
Code: Select all
// Declare variables to track the last purchase price and initial purchase
vars:
lastPurchasePrice(0),
initialPurchaseDone(false);
// Risk Management Inputs
inputs:
profitTarget(5), // Profit target in points
stopLoss(10); // Stop loss in points
// Check if initial purchase has not been done
If not initialPurchaseDone Then
Begin
// Buy on the open of the bar
buy next bar at open;
// Set the last purchase price to the open price of the bar
lastPurchasePrice = open;
// Set initial purchase status to done
initialPurchaseDone = true;
End;
// Check if the price has moved 10 points from the last purchase price
If initialPurchaseDone and c >= lastPurchasePrice + 10 Then
Begin
// Buy at the current price
buy next bar at market;
// Update the last purchase price to the current price
lastPurchasePrice = c;
End;
// Risk Management Commands
// Set a profit target to automatically secure profits when the price reaches the specified points in your favor
setprofittarget_pt(profitTarget);
// Set a stop loss to limit potential losses to the specified points when the price moves unfavorably
setstoploss_pt(stopLoss);
Explanation:
Variables:
lastPurchasePrice: Keeps track of the price at which the last purchase was made.
initialPurchaseDone: Boolean variable to track whether the initial purchase has been done.
Initial Purchase:
The system buys at the open of the bar if initialPurchaseDone is false.
After the initial purchase, lastPurchasePrice is set to the open price of the bar, and initialPurchaseDone is set to true.
Subsequent Purchases:
The system checks if the price has moved 10 points from the lastPurchasePrice.
If it has, it buys at the current market price and updates lastPurchasePrice to the current price.
Risk Management:
Sets a profit target and stop loss as per the input values profitTarget and stopLoss.
This code ensures that after an initial purchase on the open of the bar, subsequent purchases are made every 10 points thereafter.