fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. // ===== SIMPLE VERSION FOR QUICK TESTING ============================
  10. struct Candle {
  11. double open, high, low, close;
  12. };
  13.  
  14. struct Trade {
  15. bool isBuy;
  16. double entry, sl, tp;
  17. double profit;
  18. };
  19.  
  20. class SimpleTester {
  21. private:
  22. vector<Candle> generateTestData(int count) {
  23. vector<Candle> candles;
  24. double price = 100.0;
  25. srand(time(0));
  26.  
  27. for(int i = 0; i < count; i++) {
  28. Candle c;
  29. c.open = price;
  30.  
  31. // Random movement
  32. double change = ((rand() % 100) - 50) / 100.0;
  33. price += change;
  34.  
  35. // Create candle with random range
  36. double range = abs(change) * 2.0 + 0.1;
  37. c.high = price + range * 0.3;
  38. c.low = price - range * 0.2;
  39. c.close = price;
  40.  
  41. // Occasionally create engulfing patterns
  42. if(i > 0 && rand() % 20 == 0) {
  43. if(rand() % 2 == 0) {
  44. // Bullish engulfing
  45. c.open = candles.back().close - 0.2;
  46. c.close = candles.back().open + 0.3;
  47. } else {
  48. // Bearish engulfing
  49. c.open = candles.back().close + 0.2;
  50. c.close = candles.back().open - 0.3;
  51. }
  52. }
  53.  
  54. candles.push_back(c);
  55. price = c.close;
  56. }
  57. return candles;
  58. }
  59.  
  60. bool isBullEngulfing(const Candle& curr, const Candle& prev) {
  61. return (prev.close < prev.open) && // Previous bearish
  62. (curr.close > curr.open) && // Current bullish
  63. (curr.open <= prev.close) && // Engulfing condition
  64. (curr.close >= prev.open);
  65. }
  66.  
  67. bool isBearEngulfing(const Candle& curr, const Candle& prev) {
  68. return (prev.close > prev.open) && // Previous bullish
  69. (curr.close < curr.open) && // Current bearish
  70. (curr.open >= prev.close) && // Engulfing condition
  71. (curr.close <= prev.open);
  72. }
  73.  
  74. double calculateSMA(const vector<Candle>& candles, int period, int index) {
  75. if(index < period) return 0.0;
  76. double sum = 0.0;
  77. for(int i = 0; i < period; i++) {
  78. sum += candles[index - i].close;
  79. }
  80. return sum / period;
  81. }
  82.  
  83. public:
  84. void runTest() {
  85. cout << "=== VOLATILITY 100 ENGULFING EA TEST ===\n";
  86. cout << "Generating test data...\n";
  87.  
  88. vector<Candle> candles = generateTestData(200);
  89. vector<Trade> trades;
  90.  
  91. // Parameters
  92. double tpPoints = 30.0;
  93. double slPoints = 30.0;
  94. double pointValue = 0.01;
  95. int maxTrades = 3;
  96. int smaPeriod = 9;
  97.  
  98. int totalTrades = 0;
  99. int winningTrades = 0;
  100. double totalProfit = 0.0;
  101.  
  102. cout << "\nStarting simulation...\n";
  103.  
  104. for(int i = 2; i < candles.size(); i++) {
  105. Candle curr = candles[i];
  106. Candle prev = candles[i-1];
  107.  
  108. // Calculate SMA
  109. double sma = calculateSMA(candles, smaPeriod, i);
  110.  
  111. // Check for BUY signal
  112. if(trades.size() < maxTrades) {
  113. if(isBullEngulfing(curr, prev) && sma < curr.close) {
  114. Trade trade;
  115. trade.isBuy = true;
  116. trade.entry = curr.close;
  117. trade.sl = curr.low - (slPoints * pointValue);
  118. trade.tp = curr.close + (tpPoints * pointValue);
  119.  
  120. // Check if trade would hit TP/SL
  121. double profit = 0.0;
  122. for(int j = i + 1; j < candles.size() && j < i + 50; j++) {
  123. if(candles[j].high >= trade.tp) {
  124. profit = tpPoints; // TP hit
  125. break;
  126. } else if(candles[j].low <= trade.sl) {
  127. profit = -slPoints; // SL hit
  128. break;
  129. }
  130. }
  131.  
  132. trade.profit = profit;
  133. trades.push_back(trade);
  134.  
  135. cout << "BUY Signal at candle " << i
  136. << " Entry: " << trade.entry
  137. << " Profit: " << profit << " points\n";
  138.  
  139. totalTrades++;
  140. totalProfit += profit;
  141. if(profit > 0) winningTrades++;
  142. }
  143.  
  144. // Check for SELL signal
  145. else if(isBearEngulfing(curr, prev) && sma > curr.close) {
  146. Trade trade;
  147. trade.isBuy = false;
  148. trade.entry = curr.close;
  149. trade.sl = curr.high + (slPoints * pointValue);
  150. trade.tp = curr.close - (tpPoints * pointValue);
  151.  
  152. // Check if trade would hit TP/SL
  153. double profit = 0.0;
  154. for(int j = i + 1; j < candles.size() && j < i + 50; j++) {
  155. if(candles[j].low <= trade.tp) {
  156. profit = tpPoints; // TP hit
  157. break;
  158. } else if(candles[j].high >= trade.sl) {
  159. profit = -slPoints; // SL hit
  160. break;
  161. }
  162. }
  163.  
  164. trade.profit = profit;
  165. trades.push_back(trade);
  166.  
  167. cout << "SELL Signal at candle " << i
  168. << " Entry: " << trade.entry
  169. << " Profit: " << profit << " points\n";
  170.  
  171. totalTrades++;
  172. totalProfit += profit;
  173. if(profit > 0) winningTrades++;
  174. }
  175. }
  176. }
  177.  
  178. // Print results
  179. cout << "\n=== TEST RESULTS ===\n";
  180. cout << "Total Trades: " << totalTrades << endl;
  181. cout << "Winning Trades: " << winningTrades << endl;
  182. cout << "Losing Trades: " << (totalTrades - winningTrades) << endl;
  183.  
  184. if(totalTrades > 0) {
  185. double winRate = (winningTrades * 100.0) / totalTrades;
  186. cout << "Win Rate: " << winRate << "%\n";
  187. cout << "Total Profit: " << totalProfit << " points\n";
  188. cout << "Average Profit per Trade: " << (totalProfit / totalTrades) << " points\n";
  189.  
  190. // Calculate profit factor
  191. double grossProfit = 0.0;
  192. double grossLoss = 0.0;
  193. for(const auto& trade : trades) {
  194. if(trade.profit > 0) grossProfit += trade.profit;
  195. else grossLoss += abs(trade.profit);
  196. }
  197.  
  198. if(grossLoss > 0) {
  199. double profitFactor = grossProfit / grossLoss;
  200. cout << "Profit Factor: " << profitFactor << endl;
  201. }
  202. }
  203.  
  204. cout << "\n=== TRADE EXAMPLES ===\n";
  205. for(int i = 0; i < min(5, (int)trades.size()); i++) {
  206. cout << "Trade " << (i+1) << ": "
  207. << (trades[i].isBuy ? "BUY" : "SELL")
  208. << " Profit: " << trades[i].profit << " points\n";
  209. }
  210. }
  211. };
  212.  
  213. int main() {
  214. SimpleTester tester;
  215. tester.runTest();
  216. return 0;
  217. }
Success #stdin #stdout 0s 5304KB
stdin
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

// ===== SIMPLE VERSION FOR QUICK TESTING ============================
struct Candle {
    double open, high, low, close;
};

struct Trade {
    bool isBuy;
    double entry, sl, tp;
    double profit;
};

class SimpleTester {
private:
    vector<Candle> generateTestData(int count) {
        vector<Candle> candles;
        double price = 100.0;
        srand(time(0));
        
        for(int i = 0; i < count; i++) {
            Candle c;
            c.open = price;
            
            // Random movement
            double change = ((rand() % 100) - 50) / 100.0;
            price += change;
            
            // Create candle with random range
            double range = abs(change) * 2.0 + 0.1;
            c.high = price + range * 0.3;
            c.low = price - range * 0.2;
            c.close = price;
            
            // Occasionally create engulfing patterns
            if(i > 0 && rand() % 20 == 0) {
                if(rand() % 2 == 0) {
                    // Bullish engulfing
                    c.open = candles.back().close - 0.2;
                    c.close = candles.back().open + 0.3;
                } else {
                    // Bearish engulfing
                    c.open = candles.back().close + 0.2;
                    c.close = candles.back().open - 0.3;
                }
            }
            
            candles.push_back(c);
            price = c.close;
        }
        return candles;
    }
    
    bool isBullEngulfing(const Candle& curr, const Candle& prev) {
        return (prev.close < prev.open) &&  // Previous bearish
               (curr.close > curr.open) &&  // Current bullish
               (curr.open <= prev.close) && // Engulfing condition
               (curr.close >= prev.open);
    }
    
    bool isBearEngulfing(const Candle& curr, const Candle& prev) {
        return (prev.close > prev.open) &&  // Previous bullish
               (curr.close < curr.open) &&  // Current bearish
               (curr.open >= prev.close) && // Engulfing condition
               (curr.close <= prev.open);
    }
    
    double calculateSMA(const vector<Candle>& candles, int period, int index) {
        if(index < period) return 0.0;
        double sum = 0.0;
        for(int i = 0; i < period; i++) {
            sum += candles[index - i].close;
        }
        return sum / period;
    }

public:
    void runTest() {
        cout << "=== VOLATILITY 100 ENGULFING EA TEST ===\n";
        cout << "Generating test data...\n";
        
        vector<Candle> candles = generateTestData(200);
        vector<Trade> trades;
        
        // Parameters
        double tpPoints = 30.0;
        double slPoints = 30.0;
        double pointValue = 0.01;
        int maxTrades = 3;
        int smaPeriod = 9;
        
        int totalTrades = 0;
        int winningTrades = 0;
        double totalProfit = 0.0;
        
        cout << "\nStarting simulation...\n";
        
        for(int i = 2; i < candles.size(); i++) {
            Candle curr = candles[i];
            Candle prev = candles[i-1];
            
            // Calculate SMA
            double sma = calculateSMA(candles, smaPeriod, i);
            
            // Check for BUY signal
            if(trades.size() < maxTrades) {
                if(isBullEngulfing(curr, prev) && sma < curr.close) {
                    Trade trade;
                    trade.isBuy = true;
                    trade.entry = curr.close;
                    trade.sl = curr.low - (slPoints * pointValue);
                    trade.tp = curr.close + (tpPoints * pointValue);
                    
                    // Check if trade would hit TP/SL
                    double profit = 0.0;
                    for(int j = i + 1; j < candles.size() && j < i + 50; j++) {
                        if(candles[j].high >= trade.tp) {
                            profit = tpPoints; // TP hit
                            break;
                        } else if(candles[j].low <= trade.sl) {
                            profit = -slPoints; // SL hit
                            break;
                        }
                    }
                    
                    trade.profit = profit;
                    trades.push_back(trade);
                    
                    cout << "BUY Signal at candle " << i 
                         << " Entry: " << trade.entry
                         << " Profit: " << profit << " points\n";
                    
                    totalTrades++;
                    totalProfit += profit;
                    if(profit > 0) winningTrades++;
                }
                
                // Check for SELL signal
                else if(isBearEngulfing(curr, prev) && sma > curr.close) {
                    Trade trade;
                    trade.isBuy = false;
                    trade.entry = curr.close;
                    trade.sl = curr.high + (slPoints * pointValue);
                    trade.tp = curr.close - (tpPoints * pointValue);
                    
                    // Check if trade would hit TP/SL
                    double profit = 0.0;
                    for(int j = i + 1; j < candles.size() && j < i + 50; j++) {
                        if(candles[j].low <= trade.tp) {
                            profit = tpPoints; // TP hit
                            break;
                        } else if(candles[j].high >= trade.sl) {
                            profit = -slPoints; // SL hit
                            break;
                        }
                    }
                    
                    trade.profit = profit;
                    trades.push_back(trade);
                    
                    cout << "SELL Signal at candle " << i 
                         << " Entry: " << trade.entry
                         << " Profit: " << profit << " points\n";
                    
                    totalTrades++;
                    totalProfit += profit;
                    if(profit > 0) winningTrades++;
                }
            }
        }
        
        // Print results
        cout << "\n=== TEST RESULTS ===\n";
        cout << "Total Trades: " << totalTrades << endl;
        cout << "Winning Trades: " << winningTrades << endl;
        cout << "Losing Trades: " << (totalTrades - winningTrades) << endl;
        
        if(totalTrades > 0) {
            double winRate = (winningTrades * 100.0) / totalTrades;
            cout << "Win Rate: " << winRate << "%\n";
            cout << "Total Profit: " << totalProfit << " points\n";
            cout << "Average Profit per Trade: " << (totalProfit / totalTrades) << " points\n";
            
            // Calculate profit factor
            double grossProfit = 0.0;
            double grossLoss = 0.0;
            for(const auto& trade : trades) {
                if(trade.profit > 0) grossProfit += trade.profit;
                else grossLoss += abs(trade.profit);
            }
            
            if(grossLoss > 0) {
                double profitFactor = grossProfit / grossLoss;
                cout << "Profit Factor: " << profitFactor << endl;
            }
        }
        
        cout << "\n=== TRADE EXAMPLES ===\n";
        for(int i = 0; i < min(5, (int)trades.size()); i++) {
            cout << "Trade " << (i+1) << ": " 
                 << (trades[i].isBuy ? "BUY" : "SELL")
                 << " Profit: " << trades[i].profit << " points\n";
        }
    }
};

int main() {
    SimpleTester tester;
    tester.runTest();
    return 0;
}
stdout
=== VOLATILITY 100 ENGULFING EA TEST ===
Generating test data...

Starting simulation...
SELL Signal at candle 11 Entry: 99.86 Profit: -30 points
BUY Signal at candle 12 Entry: 100.32 Profit: 30 points
SELL Signal at candle 23 Entry: 99.63 Profit: 30 points

=== TEST RESULTS ===
Total Trades: 3
Winning Trades: 2
Losing Trades: 1
Win Rate: 66.6667%
Total Profit: 30 points
Average Profit per Trade: 10 points
Profit Factor: 2

=== TRADE EXAMPLES ===
Trade 1: SELL Profit: -30 points
Trade 2: BUY Profit: 30 points
Trade 3: SELL Profit: 30 points